# # Gramps - a GTK+/GNOME based genealogy program # # Copyright (C) 2000-2004 Donald N. Allingham # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: #------------------------------------------------------------------------- # # Standard Python Modules # #------------------------------------------------------------------------- import os import tempfile import zipfile from gettext import gettext as _ #------------------------------------------------------------------------- # # Gramps modules # #------------------------------------------------------------------------- from TabbedDoc import * import const import Errors #------------------------------------------------------------------------- # # OpenSpreadSheet # #------------------------------------------------------------------------- class ODSTab(TabbedDoc): def __init__(self, columns): TabbedDoc.__init__(self, columns) self.f = None self.filename = None self.level = 0 self.time = "0000-00-00T00:00:00" def open(self,filename): import time t = time.localtime(time.time()) self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % \ (t[0],t[1],t[2],t[3],t[4],t[5]) if filename[-4:] != ".ods": self.filename = filename + ".ods" else: self.filename = filename try: self.content_xml = tempfile.mktemp() self.f = open(self.content_xml,"wb") except IOError,msg: raise Errors.ReportError(_("Could not create %s") % self.content_xml, msg) except: raise Errors.ReportError(_("Could not create %s") % self.content_xml) self.f = open(self.content_xml,"w") self.f.write('\n') self.f.write(' ') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') def close(self): self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.close() self._write_styles_file() self._write_manifest() self._write_meta_file() self._write_mimetype_file() self._write_zip() def start_row(self): self.f.write('\n') def end_row(self): self.f.write('\n') def write_cell(self, text): self.f.write('') self.f.write('>\n') self.f.write('') text = text.replace('&','&') # Must be first text = text.replace('<','<') text = text.replace('>','>') text = text.replace('\t','') text = text.replace('\n','') self.f.write(unicode(text)) self.f.write('\n') self.f.write('\n') # for col in range(1,self.span): # self.f.write('\n') def _write_zip(self): try: file = zipfile.ZipFile(self.filename,"w",zipfile.ZIP_DEFLATED) except IOError,msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg) raise Errors.ReportError(errmsg) except: raise Errors.ReportError(_("Could not create %s") % self.filename) file.write(self.manifest_xml,str("META-INF/manifest.xml")) file.write(self.content_xml,str("content.xml")) file.write(self.meta_xml,str("meta.xml")) file.write(self.styles_xml,str("styles.xml")) file.write(self.mimetype,str("mimetype")) file.close() os.unlink(self.manifest_xml) os.unlink(self.content_xml) os.unlink(self.meta_xml) os.unlink(self.styles_xml) def _write_styles_file(self): self.styles_xml = tempfile.mktemp() try: self.f = open(self.styles_xml,"wb") except IOError,msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.styles_xml, msg) raise Errors.ReportError(errmsg) except: pass raise Errors.ReportError(_("Could not create %s") % self.styles_xml) self.f = open(self.styles_xml,"w") self.f.write('\n') self.f.write('') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('???\n') self.f.write('\n') self.f.write('\n') self.f.write('Page 1\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('??? ') self.f.write('(???)\n') self.f.write('\n') self.f.write('\n') self.f.write('05/16/2001, ') self.f.write('10:53:17\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('Page 1 / ') self.f.write('99\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.close() def start_page(self): self.f.write('') for col in range(0,self.columns): self.f.write('\n') def end_page(self): self.f.write('\n') def _write_manifest(self): self.manifest_xml = tempfile.mktemp() try: self.f = open(self.manifest_xml,"wb") except IOError,msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.manifest_xml, msg) raise Errors.ReportError(errmsg) except: pass raise Errors.ReportError(_("Could not create %s") % self.manifest_xml) self.f = open(self.manifest_xml,"w") self.f.write('\n') self.f.write('') self.f.write('') self.f.write('') self.f.write('') self.f.write('') self.f.write('') #self.f.write('') self.f.write('\n') self.f.close() def _write_meta_file(self): self.meta_xml = tempfile.mktemp() try: self.f = open(self.meta_xml,"wb") except IOError,msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.meta_xml, msg) raise Errors.ReportError(errmsg) except: pass raise Errors.ReportError(_("Could not create %s") % self.meta_xml) self.f = open(self.meta_xml,"w") self.f.write('\n') self.f.write('\n'); self.f.write('\n') self.f.write('') self.f.write(const.program_name + ' ' + const.version) self.f.write('\n') self.f.write('') self.f.write(self.name) self.f.write('\n') self.f.write('') self.f.write(self.time) self.f.write('\n') self.f.write('') self.f.write(self.name) self.f.write('\n') self.f.write('') self.f.write(self.time) self.f.write('\n') self.f.write('0-00-00T00:00:00\n') self.f.write('en-US\n') self.f.write('1\n') self.f.write('PT0S\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.write('\n') self.f.close() def _write_mimetype_file(self): self.mimetype = tempfile.mktemp() try: self.f = open(self.mimetype,"wb") except IOError,msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.mimetype, msg) raise Errors.ReportError(errmsg) except: pass raise Errors.ReportError(_("Could not create %s") % self.mimetype) self.f = open(self.mimetype,"w") self.f.write('application/vnd.oasis.opendocument.spreadsheet') self.f.close() if __name__ == "__main__": file = ODSTab(3) file.open("test") file.start_page() for i in [ ('one', 'two', 'three'), ('fo"ur', 'fi,ve', 'six') ]: file.start_row() for j in i: file.write_cell(j) file.end_row() file.end_page() file.close()