From da1abaa31dde7780a9ef86b88da13a5f7335f054 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Fri, 11 Jun 2010 14:29:44 +0000 Subject: [PATCH] Optimize by replacing multiple calls to .write methods with one call where possible svn: r15558 --- src/docgen/ODSTab.py | 553 +++++++++++++++++------------- src/gen/plug/docgen/stylesheet.py | 58 ++-- 2 files changed, 341 insertions(+), 270 deletions(-) diff --git a/src/docgen/ODSTab.py b/src/docgen/ODSTab.py index 64bcbe652..057f11faa 100644 --- a/src/docgen/ODSTab.py +++ b/src/docgen/ODSTab.py @@ -41,6 +41,277 @@ import const import Errors +#------------------------------------------------------------------------- +# +# Constants +# +#------------------------------------------------------------------------- + +_XMLNS = '''\ +xmlns:office="%(urn)soffice:1.0" +xmlns:style="%(urn)sstyle:1.0" +xmlns:text="%(urn)stext:1.0" +xmlns:table="%(urn)stable:1.0" +xmlns:draw="%(urn)sdrawing:1.0" +xmlns:fo="%(urn)sxsl-fo-compatible:1.0" +xmlns:xlink="http://www.w3.org/1999/xlink" +xmlns:dc="http://purl.org/dc/elements/1.1/" +xmlns:meta="%(urn)smeta:1.0" +xmlns:number="%(urn)sdatastyle:1.0" +xmlns:svg="%(urn)ssvg-compatible:1.0" +xmlns:chart="%(urn)schart:1.0" +xmlns:dr3d="%(urn)sdr3d:1.0" +xmlns:math="http://www.w3.org/1998/Math/MathML" +xmlns:form="%(urn)sform:1.0" +xmlns:script="%(urn)sscript:1.0" +xmlns:dom="http://www.w3.org/2001/xml-events" +xmlns:xforms="http://www.w3.org/2002/xforms" +xmlns:xsd="http://www.w3.org/2001/XMLSchema" +xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +''' % {'urn': 'urn:oasis:names:tc:opendocument:xmlns:'} + +_DOC_FONTS = '''\ + + + + +''' + +_DOC_STYLES = '''\ + + + + + + + + + + + +''' + +_STYLES_FONTS = '''\ + + + + +''' + +_STYLES_STYLES = '''\ + + + + + + + + + + + + + +''' +_STYLES_AUTOMATIC = '''\ + + + + + + + + + + + + + + + + + + +''' + +_STYLES_MASTER = '''\ + + + + + ??? + + + + Page + 1 + + + + + + + + ??? + (???) + + + + + 05/16/2001 + , + 10:53:17 + + + + + Page + 1 / + 99 + + + + +''' + +_MANIFEST = '''\ + + + + + + + + +''' + +_META = '''\ + + + + + %(program)s %(version)s + + + %(name)s + + + %(time)s + + + %(name)s + + + %(time)s + + + 0-00-00T00:00:00 + + en-US + 1 + PT0S + + + + + + +''' #------------------------------------------------------------------------- # # ODSTab @@ -61,10 +332,9 @@ class ODSTab(TabbedDoc): t = time.localtime(time.time()) self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % t[:6] - if filename[-4:] != ".ods": - self.filename = filename + ".ods" - else: - self.filename = filename + self.filename = filename + if not filename.endswith('.ods'): + self.filename += '.ods' try: self.content_xml = tempfile.mktemp() @@ -75,59 +345,26 @@ class ODSTab(TabbedDoc): 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' + + ' ' + + '\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(_DOC_FONTS) + self.f.write(_DOC_STYLES) + self.f.write( + '\n' + '\n') def close(self): - self.f.write('\n') - self.f.write('\n') - self.f.write('\n') + self.f.write( + '\n' + '\n' + '\n' + ) self.f.close() self._write_styles_file() self._write_manifest() @@ -195,146 +432,15 @@ class ODSTab(TabbedDoc): 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( + ' ' + ) + self.f.write(_STYLES_FONTS) + self.f.write(_STYLES_STYLES) + self.f.write(_STYLES_AUTOMATIC) + self.f.write(_STYLES_MASTER) self.f.write('\n') self.f.close() @@ -360,23 +466,7 @@ class ODSTab(TabbedDoc): 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.write(_MANIFEST) self.f.close() def _write_meta_file(self): @@ -393,39 +483,13 @@ class ODSTab(TabbedDoc): 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.write(_META % + {'program': const.PROGRAM_NAME, + 'version': const.VERSION, + 'name' : self.name, + 'time' : self.time, + } + ) self.f.close() def _write_mimetype_file(self): @@ -446,7 +510,6 @@ class ODSTab(TabbedDoc): if __name__ == "__main__": - file = ODSTab(3) file.open("test") file.start_page() diff --git a/src/gen/plug/docgen/stylesheet.py b/src/gen/plug/docgen/stylesheet.py index 7b102cb18..99afb86aa 100644 --- a/src/gen/plug/docgen/stylesheet.py +++ b/src/gen/plug/docgen/stylesheet.py @@ -148,7 +148,7 @@ class StyleSheetList(object): """ Saves the current StyleSheet definitions to the associated file. """ - xml_file = open(self.__file,"w") + xml_file = open(self.__file, "w") xml_file.write("\n") xml_file.write('\n') @@ -157,39 +157,47 @@ class StyleSheetList(object): continue xml_file.write('\n' % escxml(name)) for p_name in sheet.get_paragraph_style_names(): + # Get variables for substitutions para = sheet.get_paragraph_style(p_name) - xml_file.write('\n') + + # Write out style definition + xml_file.write( + '\n' + ) xml_file.write('\n') xml_file.write('\n') xml_file.close()