From 04e249f9027af7576e22a096a500b21d9c2711d2 Mon Sep 17 00:00:00 2001 From: Don Allingham Date: Wed, 12 Sep 2007 15:02:03 +0000 Subject: [PATCH] 2007-09-12 Don Allingham * src/docgen/ODFDoc.py (ODFDoc): Fix ratio assignment svn: r8965 --- ChangeLog | 3 + src/docgen/ODFDoc.py | 133 +++++++++++++++++++++-------------------- src/plugins/WriteCD.py | 89 +++++++++++---------------- 3 files changed, 106 insertions(+), 119 deletions(-) diff --git a/ChangeLog b/ChangeLog index a4ea4ebef..77181e3e2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2007-09-12 Don Allingham + * src/docgen/ODFDoc.py (ODFDoc): Fix ratio assignment + 2007-09-12 Zsolt Foldvari * src/plugins/IndivComplete.py (write_person): * src/plugins/IndivSummary.py (write_report): diff --git a/src/docgen/ODFDoc.py b/src/docgen/ODFDoc.py index ec5de8f1c..c4fff471c 100644 --- a/src/docgen/ODFDoc.py +++ b/src/docgen/ODFDoc.py @@ -10,7 +10,7 @@ # 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, +# 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. @@ -61,12 +61,12 @@ from xml.sax.saxutils import escape _apptype = 'application/vnd.oasis.opendocument.text' _esc_map = { - '\x1a' : '', - '\x0c' : '', - '\n' : '', - '\t' : '', - '<super>' : '', - '</super>' : '', + '\x1a' : '', + '\x0c' : '', + '\n' : '', + '\t' : '', + '<super>' : '', + '</super>' : '', } #------------------------------------------------------------------------- @@ -74,10 +74,10 @@ _esc_map = { # ODFDoc # #------------------------------------------------------------------------- -class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): +class ODFDoc(BaseDoc.BaseDoc, BaseDoc.TextDoc, BaseDoc.DrawDoc): - def __init__(self,styles,type,template): - BaseDoc.BaseDoc.__init__(self,styles,type,template) + def __init__(self, styles, type, template): + BaseDoc.BaseDoc.__init__(self, styles, type, template) self.media_list = [] self.cntnt = None self.filename = None @@ -88,10 +88,10 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.page = 0 self.first_page = 1 - def open(self,filename): + def open(self, filename): 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]) + (t[0], t[1], t[2], t[3], t[4], t[5]) if filename[-4:] != ".odt": self.filename = filename + ".odt" @@ -109,7 +109,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): current_locale = locale.getlocale() self.lang = current_locale[0] if self.lang: - self.lang = self.lang.replace('_','-') + self.lang = self.lang.replace('_', '-') else: self.lang = "en-US" @@ -318,7 +318,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.cntnt.write('\n') self.cntnt.write('\n') - for col in range(0,style.get_columns()): + for col in range(0, style.get_columns()): self.cntnt.write('') @@ -433,16 +433,17 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self._write_zip() if self.print_req: app = Mime.get_application(_apptype) - Utils.launch(app[0],self.filename) + Utils.launch(app[0], self.filename) - def add_media_object(self,name,pos,x_cm,y_cm): + def add_media_object(self, name, pos, x_cm, y_cm): # try to open the image. If the open fails, it probably wasn't # a valid image (could be a PDF, or a non-image) - (x,y) = ImgManip.image_size(name) - if (x,y) == (0,0): + (x, y) = ImgManip.image_size(name) + if (x, y) == (0, 0): return + ratio = float(x_cm)*float(y)/(float(y_cm)*float(x)) if ratio < 1: act_width = x_cm act_height = y_cm*ratio @@ -450,12 +451,12 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): act_height = y_cm act_width = x_cm/ratio - media_list_item = (name,act_width,act_height) + media_list_item = (name, act_width, act_height) if not media_list_item in self.media_list: self.media_list.append(media_list_item) base = os.path.basename(name) - tag = base.replace('.','_') + tag = base.replace('.', '_') if self.new_cell: self.cntnt.write('') @@ -481,12 +482,12 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): if self.new_cell: self.cntnt.write('\n') - def start_table(self,name,style_name): + def start_table(self, name, style_name): self.cntnt.write('\n' % style_name) styles = self.get_style_sheet() table = styles.get_table_style(style_name) - for col in range(0,table.get_columns()): + for col in range(0, table.get_columns()): self.cntnt.write('\n') @@ -499,7 +500,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): def end_row(self): self.cntnt.write('\n') - def start_cell(self,style_name,span=1): + def start_cell(self, style_name, span=1): self.span = span self.cntnt.write('\n') - #for col in range(1,self.span): + #for col in range(1, self.span): # self.cntnt.write('\n') self.new_cell = 0 @@ -527,16 +528,16 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): def end_superscript(self): self.cntnt.write('') - def _add_zip(self,zfile,name,data,t): + def _add_zip(self, zfile, name, data, t): zipinfo = zipfile.ZipInfo(name.encode('latin-1')) zipinfo.date_time = t zipinfo.compress_type = zipfile.ZIP_DEFLATED - zfile.writestr(zipinfo,data) + zfile.writestr(zipinfo, data) def _write_zip(self): try: - zfile = zipfile.ZipFile(self.filename,"w",zipfile.ZIP_DEFLATED) - except IOError,msg: + zfile = 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: @@ -544,11 +545,11 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): t = time.localtime(time.time())[:6] - self._add_zip(zfile,"META-INF/manifest.xml",self.mfile.getvalue(),t) - self._add_zip(zfile,"content.xml",self.cntnt.getvalue(),t) - self._add_zip(zfile,"meta.xml",self.meta.getvalue(),t) - self._add_zip(zfile,"styles.xml",self.sfile.getvalue(),t) - self._add_zip(zfile,"mimetype",self.mimetype.getvalue(),t) + self._add_zip(zfile, "META-INF/manifest.xml", self.mfile.getvalue(), t) + self._add_zip(zfile, "content.xml", self.cntnt.getvalue(), t) + self._add_zip(zfile, "meta.xml", self.meta.getvalue(), t) + self._add_zip(zfile, "styles.xml", self.sfile.getvalue(), t) + self._add_zip(zfile, "mimetype", self.mimetype.getvalue(), t) self.mfile.close() self.cntnt.close() @@ -558,9 +559,9 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): for image in self.media_list: try: - ifile = open(image[0],mode='rb') + ifile = open(image[0], mode='rb') base = os.path.basename(image[0]) - self._add_zip(zfile,"Pictures/%s" % base, ifile.read(),t) + self._add_zip(zfile, "Pictures/%s" % base, ifile.read(), t) ifile.close() except: print "Could not open %s" % image[0] @@ -829,7 +830,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): def end_page(self): self.cntnt.write('\n') - def start_paragraph(self,style_name,leader=None): + def start_paragraph(self, style_name, leader=None): style_sheet = self.get_style_sheet() style = style_sheet.get_paragraph_style(style_name) self.level = style.get_header_level() @@ -856,11 +857,11 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.cntnt.write('\n') self.new_cell = 1 - def write_note(self,text,format,style_name): + def write_note(self, text, format, style_name): if format == 1: - text = escape(text,_esc_map) + text = escape(text, _esc_map) # Replace multiple spaces: have to go from the largest number down - for n in range(text.count(' '),1,-1): + for n in range(text.count(' '), 1, -1): text = text.replace(' '*n, ' ' % (n-1) ) self.start_paragraph(style_name) self.cntnt.write('') @@ -870,20 +871,20 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): elif format == 0: for line in text.split('\n\n'): self.start_paragraph(style_name) - line = line.replace('\n',' ') + line = line.replace('\n', ' ') line = ' '.join(line.split()) self.write_text(line) self.end_paragraph() - def write_text(self,text,mark=None): + def write_text(self, text, mark=None): """ Uses the xml.sax.saxutils.escape function to convert XML entities. The _esc_map dictionary allows us to add our own mappings. """ if mark: - key = escape(mark.key,_esc_map) - key = key.replace('"','"') + key = escape(mark.key, _esc_map) + key = key.replace('"', '"') if mark.type == BaseDoc.INDEX_TYPE_ALP: self.cntnt.write('' % key) @@ -891,7 +892,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.cntnt.write('' % mark.level) - self.cntnt.write(escape(text,_esc_map)) + self.cntnt.write(escape(text, _esc_map)) def _write_manifest(self): self.mfile = StringIO() @@ -973,7 +974,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.meta.write('\n') self.meta.write('\n') - def rotate_text(self,style,text,x,y,angle): + def rotate_text(self, style, text, x, y, angle): style_sheet = self.get_style_sheet() stype = style_sheet.get_draw_style(style) pname = stype.get_paragraph_style() @@ -984,7 +985,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): height = size*(len(text)) width = 0 for line in text: - width = max(width,FontScale.string_width(font,line)) + width = max(width, FontScale.string_width(font, line)) wcm = ReportUtils.pt2cm(width) hcm = ReportUtils.pt2cm(height) @@ -999,48 +1000,48 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): self.cntnt.write('rotate (%.8f) ' % -rangle) xloc = x-((wcm/2.0)*cos(rangle))+((hcm/2.0)*sin(rangle)) yloc = y-((hcm/2.0)*cos(rangle))-((wcm/2.0)*sin(rangle)) - self.cntnt.write('translate (%.3fcm %.3fcm)">\n' % (xloc,yloc)) + self.cntnt.write('translate (%.3fcm %.3fcm)">\n' % (xloc, yloc)) self.cntnt.write('\n') self.cntnt.write('' % pname) self.cntnt.write('' % pname) - self.cntnt.write(escape('\n'.join(text),_esc_map)) + self.cntnt.write(escape('\n'.join(text), _esc_map)) self.cntnt.write('\n\n') self.cntnt.write('\n') - def draw_path(self,style,path): + def draw_path(self, style, path): minx = 9e12 miny = 9e12 maxx = 0 maxy = 0 for point in path: - minx = min(point[0],minx) - miny = min(point[1],miny) - maxx = max(point[0],maxx) - maxy = max(point[1],maxy) + minx = min(point[0], minx) + miny = min(point[1], miny) + maxx = max(point[0], maxx) + maxy = max(point[1], maxy) self.cntnt.write('\n') - def draw_line(self,style,x1,y1,x2,y2): + def draw_line(self, style, x1, y1, x2, y2): self.cntnt.write('\n') self.cntnt.write('\n') - def draw_text(self,style,text,x,y): + def draw_text(self, style, text, x, y): style_sheet = self.get_style_sheet() box_style = style_sheet.get_draw_style(style) para_name = box_style.get_paragraph_style() pstyle = style_sheet.get_paragraph_style(para_name) font = pstyle.get_font() - sw = ReportUtils.pt2cm(FontScale.string_width(font,text))*1.3 + sw = ReportUtils.pt2cm(FontScale.string_width(font, text))*1.3 self.cntnt.write('' % para_name) self.cntnt.write('' % font.get_size() ) - self.cntnt.write(escape(text,_esc_map)) + self.cntnt.write(escape(text, _esc_map)) self.cntnt.write('') self.cntnt.write('\n') self.cntnt.write('\n') - def draw_box(self,style,text,x,y, w, h): + def draw_box(self, style, text, x, y, w, h): style_sheet = self.get_style_sheet() box_style = style_sheet.get_draw_style(style) para_name = box_style.get_paragraph_style() @@ -1105,19 +1106,19 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): if text != "": self.cntnt.write('' % para_name) self.cntnt.write('' % para_name) - self.cntnt.write(escape(text,_esc_map)) + self.cntnt.write(escape(text, _esc_map)) self.cntnt.write('') self.cntnt.write('\n') self.cntnt.write('\n') - def center_text(self,style,text,x,y): + def center_text(self, style, text, x, y): style_sheet = self.get_style_sheet() box_style = style_sheet.get_draw_style(style) para_name = box_style.get_paragraph_style() pstyle = style_sheet.get_paragraph_style(para_name) font = pstyle.get_font() - size = (FontScale.string_width(font,text)/72.0) * 2.54 + size = (FontScale.string_width(font, text)/72.0) * 2.54 self.cntnt.write('') self.cntnt.write('' % para_name) self.cntnt.write('' % para_name) - self.cntnt.write(escape(text,_esc_map)) + self.cntnt.write(escape(text, _esc_map)) self.cntnt.write('\n') self.cntnt.write('\n') self.cntnt.write('') diff --git a/src/plugins/WriteCD.py b/src/plugins/WriteCD.py index 7c03d682a..877b93360 100644 --- a/src/plugins/WriteCD.py +++ b/src/plugins/WriteCD.py @@ -8,7 +8,7 @@ # 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, +# 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. @@ -79,9 +79,9 @@ _title_string = _("Export to CD") # writeData # #------------------------------------------------------------------------- -def writeData(database,filename,person,option_box=None,callback=None): +def writeData(database, filename, person, option_box=None, callback=None): ret = 0 - writer = PackageWriter(database,filename,callback) + writer = PackageWriter(database, filename, callback) ret = writer.export() return ret @@ -92,7 +92,7 @@ def writeData(database,filename,person,option_box=None,callback=None): #------------------------------------------------------------------------- class PackageWriter: - def __init__(self,database,filename="",cl=0,callback=None): + def __init__(self, database, filename="", cl=0, callback=None): self.db = database self.cl = cl self.filename = filename @@ -109,41 +109,30 @@ class PackageWriter: try: uri = URI('burn:///%s' % base) - make_directory(uri,OPEN_WRITE) + make_directory(uri, OPEN_WRITE) except FileExistsError, msg: - QuestionDialog.ErrorDialog(_("CD export preparation failed"), + QuestionDialog.ErrorDialog(_("CD export preparation failed"), "1 %s " % str(msg)) return except: uri_name = "burn:///" + base - QuestionDialog.ErrorDialog("CD export preparation failed", + QuestionDialog.ErrorDialog("CD export preparation failed", 'Could not create %s' % uri_name) return - try: - uri = URI('burn:///%s/.thumb' % base) - make_directory(uri,OPEN_WRITE) - except FileExistsError, msg: - QuestionDialog.ErrorDialog("CD export preparation failed", - "2 %s " % str(msg)) - return - for obj_id in self.db.get_media_object_handles(): obj = self.db.get_object_from_handle(obj_id) oldfile = obj.get_path() root = os.path.basename(oldfile) if os.path.isfile(oldfile): - self.copy_file(oldfile,'burn:///%s/%s' % (base,root)) - mime_type = obj.get_mime_type() - if mime_type and mime_type.startswith("image"): - self.make_thumbnail(base,root,obj.get_path()) + self.copy_file(oldfile, 'burn:///%s/%s' % (base, root)) else: - print "Warning: media file %s was not found," % root,\ + print "Warning: media file %s was not found, " % root, \ "so it was ignored." # Write XML now - g = create('burn:///%s/data.gramps' % base,OPEN_WRITE ) - gfile = XmlWriter(self.db,None,2) + g = create('burn:///%s/data.gramps' % base, OPEN_WRITE ) + gfile = XmlWriter(self.db, None, 2) gfile.write_handle(g) g.close() @@ -154,27 +143,27 @@ class PackageWriter: try: uri = URI('burn:///%s' % base) - make_directory(uri,OPEN_WRITE) + make_directory(uri, OPEN_WRITE) except FileExistsError: - QuestionDialog.ErrorDialog(_("CD export preparation failed"), + QuestionDialog.ErrorDialog(_("CD export preparation failed"), "File already exists") return except: uri_name = "burn:///" + base - QuestionDialog.ErrorDialog(_("CD export preparation failed"), + QuestionDialog.ErrorDialog(_("CD export preparation failed"), _('Could not create %s') % uri_name) return try: uri = URI('burn:///%s/.thumb' % base) - make_directory(uri,OPEN_WRITE) + make_directory(uri, OPEN_WRITE) except FileExistsError, msg: - QuestionDialog.ErrorDialog("CD export preparation failed", + QuestionDialog.ErrorDialog("CD export preparation failed", "4 %s " % str(msg)) return except: uri_name = "burn:///" + base + "/.thumb" - QuestionDialog.ErrorDialog(_("CD export preparation failed"), + QuestionDialog.ErrorDialog(_("CD export preparation failed"), _('Could not create %s') % uri_name) return @@ -188,7 +177,7 @@ class PackageWriter: if o.get_reference_handle() == self.object_handle: nl.remove(o) p.set_media_list(nl) - self.db.commit_family(p,None) + self.db.commit_family(p, None) for key in self.db.get_person_handles(sort_handles=False): p = self.db.get_person_from_handle(key) @@ -197,7 +186,7 @@ class PackageWriter: if o.get_reference_handle() == self.object_handle: nl.remove(o) p.set_media_list(nl) - self.db.commit_person(p,None) + self.db.commit_person(p, None) for key in self.db.get_source_handles(): p = self.db.get_source_from_handle(key) nl = p.get_media_list() @@ -205,7 +194,7 @@ class PackageWriter: if o.get_reference_handle() == self.object_handle: nl.remove(o) p.set_media_list(nl) - self.db.commit_source(p,None) + self.db.commit_source(p, None) for key in self.db.get_place_handles(): p = self.db.get_place_from_handle(key) nl = p.get_media_list() @@ -213,7 +202,7 @@ class PackageWriter: if o.get_reference_handle() == self.object_handle: nl.remove(o) p.set_media_list(nl) - self.db.commit_place(p,None) + self.db.commit_place(p, None) for key in self.db.get_event_handles(): p = self.db.get_event_from_handle(key) nl = p.get_media_list() @@ -221,8 +210,8 @@ class PackageWriter: if o.get_reference_handle() == self.object_handle: nl.remove(o) p.set_media_list(nl) - self.db.commit_event(p,None) - self.db.remove_object(self.object_handle,None) + self.db.commit_event(p, None) + self.db.remove_object(self.object_handle, None) def leave_clicked(): # File is lost => do nothing, leave as is @@ -234,18 +223,15 @@ class PackageWriter: pass def fs_ok_clicked(obj): - newfile = unicode(fs_top.get_filename(), + newfile = unicode(fs_top.get_filename(), sys.getfilesystemencoding()) if os.path.isfile(newfile): - self.copy_file(newfile,'burn:///%s/%s' % (base,obase)) - ntype = Mime.get_type(newfile) - if ntype and ntype.startswith("image"): - self.make_thumbnail(base,obase,newfile) + self.copy_file(newfile, 'burn:///%s/%s' % (base, obase)) fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file")) fs_top.hide_fileop_buttons() - fs_top.ok_button.connect('clicked',fs_ok_clicked) - fs_top.cancel_button.connect('clicked',fs_close_window) + fs_top.ok_button.connect('clicked', fs_ok_clicked) + fs_top.cancel_button.connect('clicked', fs_close_window) fs_top.run() fs_top.destroy() @@ -259,20 +245,17 @@ class PackageWriter: oldfile = obj.get_path() root = os.path.basename(oldfile) if os.path.isfile(oldfile): - self.copy_file(oldfile,'burn:///%s/%s' % (base,root)) - mime_type = obj.get_mime_type() - if mime_type and mime_type.startswith("image"): - self.make_thumbnail(base,root,obj.get_path()) + self.copy_file(oldfile, 'burn:///%s/%s' % (base, root)) else: # File is lost => ask what to do self.object_handle = obj.get_handle() if missmedia_action == 0: - mmd = QuestionDialog.MissingMediaDialog(_("Media object could not be found"), + mmd = QuestionDialog.MissingMediaDialog(_("Media object could not be found"), _("%(file_name)s is referenced in the database, but no longer exists. " "The file may have been deleted or moved to a different location. " "You may choose to either remove the reference from the database, " "keep the reference to the missing file, or select a new file." - ) % { 'file_name' : oldfile }, + ) % { 'file_name' : oldfile }, remove_clicked, leave_clicked, select_clicked) missmedia_action = mmd.default_action elif missmedia_action == 1: @@ -285,17 +268,17 @@ class PackageWriter: # Write XML now uri = 'burn:///%s/data.gramps' % base uri = uri.encode('utf8') - g = create(uri,OPEN_WRITE) - gfile = XmlWriter(self.db,self.callback,2) + g = create(uri, OPEN_WRITE) + gfile = XmlWriter(self.db, self.callback, 2) gfile.write_handle(g) g.close() os.system("nautilus --no-desktop burn:///") return 1 - def copy_file(self,src,dest): - original = open(src,"r") + def copy_file(self, src, dest): + original = open(src, "r") destobj = URI(dest) - target = create(destobj,OPEN_WRITE) + target = create(destobj, OPEN_WRITE) done = 0 while 1: buf = original.read(2048) @@ -319,4 +302,4 @@ _description = _('Exporting to CD copies all your data and media ' _config = None _filename = 'burn' -register_export(writeData,_title,_description,_config,_filename) +register_export(writeData, _title, _description, _config, _filename)