From 8366df863740fabb1ce972e10c86ad574e8e705d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 20 May 2019 16:52:16 +0530 Subject: [PATCH] Run code quality checks on the odf package --- setup/check.py | 25 +++++---- setup/test.py | 38 +++++++------ src/odf/attrconverters.py | 63 +++++++++++---------- src/odf/dr3d.py | 3 +- src/odf/easyliststyle.py | 11 ++-- src/odf/element.py | 15 ++--- src/odf/elementtypes.py | 5 +- src/odf/grammar.py | 114 ++++++++++++++++++++------------------ src/odf/load.py | 6 +- src/odf/namespaces.py | 2 +- src/odf/odf2moinmoin.py | 13 +---- src/odf/odf2xhtml.py | 2 +- src/odf/odfmanifest.py | 1 - src/odf/opendocument.py | 32 +++++++---- src/odf/style.py | 2 +- src/odf/svg.py | 2 +- src/odf/teletype.py | 5 +- src/odf/thumbnail.py | 2 +- 18 files changed, 174 insertions(+), 167 deletions(-) diff --git a/setup/check.py b/setup/check.py index 96a0be5ae1..4afd87c9e1 100644 --- a/setup/check.py +++ b/setup/check.py @@ -26,18 +26,19 @@ class Check(Command): CACHE = 'check.json' def get_files(self): - for x in os.walk(self.j(self.SRC, 'calibre')): - for f in x[-1]: - y = self.j(x[0], f) - if x[0].endswith('calibre/ebooks/markdown'): - continue - if (f.endswith('.py') and f not in ( - 'feedparser.py', 'markdown.py', 'BeautifulSoup.py', 'dict_data.py', - 'unicodepoints.py', 'krcodepoints.py', 'jacodepoints.py', 'vncodepoints.py', 'zhcodepoints.py') and - 'prs500/driver.py' not in y) and not f.endswith('_ui.py'): - yield y - if f.endswith('.coffee'): - yield y + for dname in ('odf', 'calibre'): + for x in os.walk(self.j(self.SRC, dname)): + for f in x[-1]: + y = self.j(x[0], f) + if x[0].endswith('calibre/ebooks/markdown'): + continue + if (f.endswith('.py') and f not in ( + 'feedparser.py', 'markdown.py', 'BeautifulSoup.py', 'dict_data.py', + 'unicodepoints.py', 'krcodepoints.py', 'jacodepoints.py', 'vncodepoints.py', 'zhcodepoints.py') and + 'prs500/driver.py' not in y) and not f.endswith('_ui.py'): + yield y + if f.endswith('.coffee'): + yield y for x in os.walk(self.j(self.d(self.SRC), 'recipes')): for f in x[-1]: diff --git a/setup/test.py b/setup/test.py index da508f712d..e3a8ab4142 100644 --- a/setup/test.py +++ b/setup/test.py @@ -14,23 +14,8 @@ TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants class TestImports(unittest.TestCase): - def test_import_of_all_python_modules(self): + def base_check(self, base, exclude_packages, exclude_modules): import importlib - exclude_modules = {'calibre.gui2.dbus_export.demo', 'calibre.gui2.dbus_export.gtk'} - exclude_packages = {'calibre.devices.mtp.unix.upstream'} - if not iswindows: - exclude_modules |= {'calibre.utils.iphlpapi', 'calibre.utils.open_with.windows', 'calibre.devices.winusb'} - exclude_packages |= {'calibre.utils.winreg'} - if not isosx: - exclude_modules.add('calibre.utils.open_with.osx') - if not islinux: - exclude_modules |= { - 'calibre.utils.dbus_service', 'calibre.linux', - 'calibre.utils.linux_trash', 'calibre.utils.open_with.linux', - 'calibre.gui2.linux_file_dialogs' - } - exclude_packages.add('calibre.gui2.dbus_export') - base = os.path.join(SRC, 'calibre') import_base = os.path.dirname(base) count = 0 for root, dirs, files in os.walk(base): @@ -50,7 +35,26 @@ class TestImports(unittest.TestCase): continue importlib.import_module(full_module_name) count += 1 - self.assertGreater(count, 1000) + return count + + def test_import_of_all_python_modules(self): + exclude_modules = {'calibre.gui2.dbus_export.demo', 'calibre.gui2.dbus_export.gtk'} + exclude_packages = {'calibre.devices.mtp.unix.upstream'} + if not iswindows: + exclude_modules |= {'calibre.utils.iphlpapi', 'calibre.utils.open_with.windows', 'calibre.devices.winusb'} + exclude_packages |= {'calibre.utils.winreg'} + if not isosx: + exclude_modules.add('calibre.utils.open_with.osx') + if not islinux: + exclude_modules |= { + 'calibre.utils.dbus_service', 'calibre.linux', + 'calibre.utils.linux_trash', 'calibre.utils.open_with.linux', + 'calibre.gui2.linux_file_dialogs' + } + exclude_packages.add('calibre.gui2.dbus_export') + self.assertGreater(self.base_check(os.path.join(SRC, 'odf'), exclude_packages, exclude_modules), 10) + base = os.path.join(SRC, 'calibre') + self.assertGreater(self.base_check(base, exclude_packages, exclude_modules), 1000) def find_tests(which_tests=None): diff --git a/src/odf/attrconverters.py b/src/odf/attrconverters.py index 090409fdb4..78d309897c 100644 --- a/src/odf/attrconverters.py +++ b/src/odf/attrconverters.py @@ -17,8 +17,14 @@ # # Contributor(s): # -from .namespaces import * -import re, types +import re +import types + +from .namespaces import ( + ANIMNS, CHARTNS, CONFIGNS, DR3DNS, DRAWNS, FONS, FORMNS, MANIFESTNS, METANS, + NUMBERNS, OFFICENS, PRESENTATIONNS, SCRIPTNS, SMILNS, STYLENS, SVGNS, TABLENS, + TEXTNS, XFORMSNS, XLINKNS +) pattern_color = re.compile(r'#[0-9a-fA-F]{6}') pattern_vector3D = re.compile(r'\([ ]*-?([0-9]+(\.[0-9]*)?|\.[0-9]+)([ ]+-?([0-9]+(\.[0-9]*)?|\.[0-9]+)){2}[ ]*\)') @@ -102,9 +108,8 @@ def __save_prefix(attribute, arg, element): return type(u'')(arg) namespace = element.get_knownns(prefix) if namespace is None: - #raise ValueError, "'%s' is an unknown prefix" % str(prefix) + # raise ValueError, "'%s' is an unknown prefix" % str(prefix) return type(u'')(arg) - p = element.get_nsprefix(namespace) return type(u'')(arg) @@ -241,7 +246,7 @@ def cnv_percent(attribute, arg, element): # Real one doesn't allow floating point values pattern_points = re.compile(r'-?[0-9]+,-?[0-9]+([ ]+-?[0-9]+,-?[0-9]+)*') -#pattern_points = re.compile(r'-?[0-9.]+,-?[0-9.]+([ ]+-?[0-9.]+,-?[0-9.]+)*') +# pattern_points = re.compile(r'-?[0-9.]+,-?[0-9.]+([ ]+-?[0-9.]+,-?[0-9.]+)*') def cnv_points(attribute, arg, element): @@ -310,7 +315,7 @@ attrconverters = { ((ANIMNS,u'name'), None): cnv_string, ((ANIMNS,u'sub-item'), None): cnv_string, ((ANIMNS,u'value'), None): cnv_string, -# ((DBNS,u'type'), None): cnv_namespacedToken, + # ((DBNS,u'type'), None): cnv_namespacedToken, ((CHARTNS,u'attached-axis'), None): cnv_string, ((CHARTNS,u'class'), (CHARTNS,u'grid')): cnv_major_minor, ((CHARTNS,u'class'), None): cnv_namespacedToken, @@ -532,8 +537,8 @@ attrconverters = { ((DRAWNS,u'handle-range-y-maximum'), None): cnv_string, ((DRAWNS,u'handle-range-y-minimum'), None): cnv_string, ((DRAWNS,u'handle-switched'), None): cnv_boolean, -# ((DRAWNS,u'id'), None): cnv_ID, -# ((DRAWNS,u'id'), None): cnv_nonNegativeInteger, # ?? line 6581 in RNG + # ((DRAWNS,u'id'), None): cnv_ID, + # ((DRAWNS,u'id'), None): cnv_nonNegativeInteger, # ?? line 6581 in RNG ((DRAWNS,u'id'), None): cnv_string, ((DRAWNS,u'image-opacity'), None): cnv_string, ((DRAWNS,u'kind'), None): cnv_string, @@ -556,7 +561,7 @@ attrconverters = { ((DRAWNS,u'mirror-vertical'), None): cnv_boolean, ((DRAWNS,u'modifiers'), None): cnv_string, ((DRAWNS,u'name'), None): cnv_NCName, -# ((DRAWNS,u'name'), None): cnv_string, + # ((DRAWNS,u'name'), None): cnv_string, ((DRAWNS,u'nav-order'), None): cnv_IDREF, ((DRAWNS,u'nohref'), None): cnv_string, ((DRAWNS,u'notify-on-update-of-ranges'), None): cnv_string, @@ -693,10 +698,10 @@ attrconverters = { ((FORMNS,u'convert-empty-to-null'), None): cnv_boolean, ((FORMNS,u'current-selected'), None): cnv_boolean, ((FORMNS,u'current-state'), None): cnv_string, -# ((FORMNS,u'current-value'), None): cnv_date, -# ((FORMNS,u'current-value'), None): cnv_double, + # ((FORMNS,u'current-value'), None): cnv_date, + # ((FORMNS,u'current-value'), None): cnv_double, ((FORMNS,u'current-value'), None): cnv_string, -# ((FORMNS,u'current-value'), None): cnv_time, + # ((FORMNS,u'current-value'), None): cnv_time, ((FORMNS,u'data-field'), None): cnv_string, ((FORMNS,u'datasource'), None): cnv_string, ((FORMNS,u'default-button'), None): cnv_boolean, @@ -721,15 +726,15 @@ attrconverters = { ((FORMNS,u'list-source-type'), None): cnv_string, ((FORMNS,u'master-fields'), None): cnv_string, ((FORMNS,u'max-length'), None): cnv_nonNegativeInteger, -# ((FORMNS,u'max-value'), None): cnv_date, -# ((FORMNS,u'max-value'), None): cnv_double, + # ((FORMNS,u'max-value'), None): cnv_date, + # ((FORMNS,u'max-value'), None): cnv_double, ((FORMNS,u'max-value'), None): cnv_string, -# ((FORMNS,u'max-value'), None): cnv_time, + # ((FORMNS,u'max-value'), None): cnv_time, ((FORMNS,u'method'), None): cnv_string, -# ((FORMNS,u'min-value'), None): cnv_date, -# ((FORMNS,u'min-value'), None): cnv_double, + # ((FORMNS,u'min-value'), None): cnv_date, + # ((FORMNS,u'min-value'), None): cnv_double, ((FORMNS,u'min-value'), None): cnv_string, -# ((FORMNS,u'min-value'), None): cnv_time, + # ((FORMNS,u'min-value'), None): cnv_time, ((FORMNS,u'multi-line'), None): cnv_boolean, ((FORMNS,u'multiple'), None): cnv_boolean, ((FORMNS,u'name'), None): cnv_string, @@ -751,10 +756,10 @@ attrconverters = { ((FORMNS,u'title'), None): cnv_string, ((FORMNS,u'toggle'), None): cnv_boolean, ((FORMNS,u'validation'), None): cnv_boolean, -# ((FORMNS,u'value'), None): cnv_date, -# ((FORMNS,u'value'), None): cnv_double, + # ((FORMNS,u'value'), None): cnv_date, + # ((FORMNS,u'value'), None): cnv_double, ((FORMNS,u'value'), None): cnv_string, -# ((FORMNS,u'value'), None): cnv_time, + # ((FORMNS,u'value'), None): cnv_time, ((FORMNS,u'visual-effect'), None): cnv_string, ((FORMNS,u'xforms-list-source'), None): cnv_string, ((FORMNS,u'xforms-submission'), None): cnv_string, @@ -1183,7 +1188,7 @@ attrconverters = { ((TABLENS,u'border-model'), None): cnv_string, ((TABLENS,u'buttons'), None): cnv_string, ((TABLENS,u'buttons'), None): cnv_string, - ((TABLENS,u'case-sensitive'), None): cnv_boolean, + # ((TABLENS,u'case-sensitive'), None): cnv_boolean, ((TABLENS,u'case-sensitive'), None): cnv_string, ((TABLENS,u'cell-address'), None): cnv_string, ((TABLENS,u'cell-range-address'), None): cnv_string, @@ -1231,7 +1236,7 @@ attrconverters = { ((TABLENS,u'execute'), None): cnv_boolean, ((TABLENS,u'expression'), None): cnv_formula, ((TABLENS,u'field-name'), None): cnv_string, - ((TABLENS,u'field-number'), None): cnv_nonNegativeInteger, + # ((TABLENS,u'field-number'), None): cnv_nonNegativeInteger, ((TABLENS,u'field-number'), None): cnv_string, ((TABLENS,u'filter-name'), None): cnv_string, ((TABLENS,u'filter-options'), None): cnv_string, @@ -1295,7 +1300,7 @@ attrconverters = { ((TABLENS,u'protection-key'), None): cnv_string, ((TABLENS,u'query-name'), None): cnv_string, ((TABLENS,u'range-usable-as'), None): cnv_string, - ((TABLENS,u'refresh-delay'), None): cnv_boolean, + # ((TABLENS,u'refresh-delay'), None): cnv_boolean, ((TABLENS,u'refresh-delay'), None): cnv_duration, ((TABLENS,u'rejecting-change-id'), None): cnv_string, ((TABLENS,u'row'), None): cnv_integer, @@ -1303,7 +1308,7 @@ attrconverters = { ((TABLENS,u'search-criteria-must-apply-to-whole-cell'), None): cnv_boolean, ((TABLENS,u'selected-page'), None): cnv_string, ((TABLENS,u'show-details'), None): cnv_boolean, - ((TABLENS,u'show-empty'), None): cnv_boolean, + # ((TABLENS,u'show-empty'), None): cnv_boolean, ((TABLENS,u'show-empty'), None): cnv_string, ((TABLENS,u'show-filter-button'), None): cnv_boolean, ((TABLENS,u'sort-mode'), None): cnv_string, @@ -1391,7 +1396,7 @@ attrconverters = { ((TEXTNS,u'database-name'), None): cnv_string, ((TEXTNS,u'date-adjust'), None): cnv_duration, ((TEXTNS,u'date-value'), None): cnv_date, -# ((TEXTNS,u'date-value'), None): cnv_dateTime, + # ((TEXTNS,u'date-value'), None): cnv_dateTime, ((TEXTNS,u'default-style-name'), None): cnv_StyleNameRef, ((TEXTNS,u'description'), None): cnv_string, ((TEXTNS,u'display'), None): cnv_string, @@ -1410,7 +1415,7 @@ attrconverters = { ((TEXTNS,u'global'), None): cnv_boolean, ((TEXTNS,u'howpublished'), None): cnv_string, ((TEXTNS,u'id'), None): cnv_ID, -# ((TEXTNS,u'id'), None): cnv_string, + # ((TEXTNS,u'id'), None): cnv_string, ((TEXTNS,u'identifier'), None): cnv_string, ((TEXTNS,u'ignore-case'), None): cnv_boolean, ((TEXTNS,u'increment'), None): cnv_nonNegativeInteger, @@ -1476,7 +1481,7 @@ attrconverters = { ((TEXTNS,u'sort-by-position'), None): cnv_boolean, ((TEXTNS,u'space-before'), None): cnv_string, ((TEXTNS,u'start-numbering-at'), None): cnv_string, - ((TEXTNS,u'start-value'), None): cnv_nonNegativeInteger, + # ((TEXTNS,u'start-value'), None): cnv_nonNegativeInteger, ((TEXTNS,u'start-value'), None): cnv_positiveInteger, ((TEXTNS,u'string-value'), None): cnv_string, ((TEXTNS,u'string-value-if-false'), None): cnv_string, @@ -1488,7 +1493,7 @@ attrconverters = { ((TEXTNS,u'table-name'), None): cnv_string, ((TEXTNS,u'table-type'), None): cnv_string, ((TEXTNS,u'time-adjust'), None): cnv_duration, - ((TEXTNS,u'time-value'), None): cnv_dateTime, + # ((TEXTNS,u'time-value'), None): cnv_dateTime, ((TEXTNS,u'time-value'), None): cnv_time, ((TEXTNS,u'title'), None): cnv_string, ((TEXTNS,u'track-changes'), None): cnv_boolean, diff --git a/src/odf/dr3d.py b/src/odf/dr3d.py index 2e71619b36..51aab02df6 100644 --- a/src/odf/dr3d.py +++ b/src/odf/dr3d.py @@ -19,7 +19,6 @@ # from .namespaces import DR3DNS -from .element import Element from .draw import StyleRefElement # Autogenerated @@ -33,7 +32,7 @@ def Extrude(**args): return StyleRefElement(qname=(DR3DNS,'extrude'), **args) -def Light(Element): +def Light(Element, **args): return StyleRefElement(qname=(DR3DNS,'light'), **args) diff --git a/src/odf/easyliststyle.py b/src/odf/easyliststyle.py index 9d0c8c1ecc..05cb8b6208 100644 --- a/src/odf/easyliststyle.py +++ b/src/odf/easyliststyle.py @@ -20,8 +20,8 @@ # import re -from style import Style, TextProperties, ListLevelProperties -from text import ListStyle,ListLevelStyleNumber,ListLevelStyleBullet +from .style import ListLevelProperties +from .text import ListStyle,ListLevelStyleNumber,ListLevelStyleBullet """ Create a element from a string or array. @@ -51,7 +51,6 @@ def styleFromList(styleName, specArray, spacing, showAllLevels): bullet = "" numPrefix = "" numSuffix = "" - numberFormat = "" cssLengthNum = 0 cssLengthUnits = "" numbered = False @@ -60,7 +59,7 @@ def styleFromList(styleName, specArray, spacing, showAllLevels): numFormatPattern = re.compile("([1IiAa])") cssLengthPattern = re.compile("([^a-z]+)\\s*([a-z]+)?") m = cssLengthPattern.search(spacing) - if (m != None): + if (m is not None): cssLengthNum = float(m.group(1)) if (m.lastindex == 2): cssLengthUnits = m.group(2) @@ -68,8 +67,7 @@ def styleFromList(styleName, specArray, spacing, showAllLevels): while i < len(specArray): specification = specArray[i] m = numFormatPattern.search(specification) - if (m != None): - numberFormat = m.group(1) + if (m is not None): numPrefix = specification[0:m.start(1)] numSuffix = specification[m.end(1):] bullet = "" @@ -82,7 +80,6 @@ def styleFromList(styleName, specArray, spacing, showAllLevels): bullet = specification numPrefix = "" numSuffix = "" - numberFormat = "" displayLevels = 1 numbered = False if (numbered): diff --git a/src/odf/element.py b/src/odf/element.py index 7d5163bdba..2fe707d294 100644 --- a/src/odf/element.py +++ b/src/odf/element.py @@ -23,7 +23,7 @@ # Whatever license applies to that file also applies to this file. # import xml.dom -from xml.dom.minicompat import * +from xml.dom.minicompat import defproperty, EmptyNodeList from .namespaces import nsdict from . import grammar from .attrconverters import AttrConverters @@ -315,8 +315,6 @@ class Element(Node): self.addCDATA(cdata) allowed_attrs = self.allowed_attributes() - if allowed_attrs is not None: - allowed_args = [a[1].lower().replace('-','') for a in allowed_attrs] self.attributes={} # Load the attributes from the 'attributes' argument if attributes: @@ -407,7 +405,7 @@ class Element(Node): """ Removes an attribute by name. """ allowed_attrs = self.allowed_attributes() if allowed_attrs is None: - if type(attr) == type(()): + if isinstance(attr, tuple): prefix, localname = attr self.removeAttrNS(prefix, localname) else: @@ -430,7 +428,7 @@ class Element(Node): """ allowed_attrs = self.allowed_attributes() if allowed_attrs is None: - if type(attr) == type(()): + if isinstance(attr, tuple): prefix, localname = attr self.setAttrNS(prefix, localname, value) else: @@ -450,15 +448,10 @@ class Element(Node): It will not check that the attribute is legal according to the schema. Must overwrite, If attribute already exists. """ - allowed_attrs = self.allowed_attributes() - prefix = self.get_nsprefix(namespace) -# if allowed_attrs and (namespace, localpart) not in allowed_attrs: -# raise AttributeError, "Attribute %s:%s is not allowed in element <%s>" % ( prefix, localpart, self.tagName) c = AttrConverters() self.attributes[(namespace, localpart)] = c.convert((namespace, localpart), value, self) def getAttrNS(self, namespace, localpart): - prefix = self.get_nsprefix(namespace) return self.attributes.get((namespace, localpart)) def removeAttrNS(self, namespace, localpart): @@ -469,7 +462,7 @@ class Element(Node): """ allowed_attrs = self.allowed_attributes() if allowed_attrs is None: - if type(attr) == type(()): + if isinstance(attr, tuple): prefix, localname = attr return self.getAttrNS(prefix, localname) else: diff --git a/src/odf/elementtypes.py b/src/odf/elementtypes.py index 9450b08510..af8d4055a9 100644 --- a/src/odf/elementtypes.py +++ b/src/odf/elementtypes.py @@ -19,7 +19,10 @@ # Contributor(s): # -from .namespaces import * +from .namespaces import ( + ANIMNS, CHARTNS, DR3DNS, DRAWNS, FORMNS, MANIFESTNS, METANS, NUMBERNS, OFFICENS, + PRESENTATIONNS, SCRIPTNS, STYLENS, SVGNS, TABLENS, TEXTNS +) # Inline element don't cause a box # They are analogous to the HTML elements SPAN, B, I etc. diff --git a/src/odf/grammar.py b/src/odf/grammar.py index 5bd3d81302..759cc1e0e0 100644 --- a/src/odf/grammar.py +++ b/src/odf/grammar.py @@ -23,7 +23,11 @@ Currently it contains the legal child elements of a given element. To be used for validation check in the API """ -from .namespaces import * +from .namespaces import ( + ANIMNS, CHARTNS, CONFIGNS, DCNS, DR3DNS, DRAWNS, FORMNS, MANIFESTNS, MATHNS, + METANS, NUMBERNS, OFFICENS, PRESENTATIONNS, SCRIPTNS, STYLENS, SVGNS, TABLENS, + TEXTNS, XFORMSNS, XLINKNS, SMILNS, FONS +) # The following code is generated from the RelaxNG schema with this notice: @@ -64,24 +68,24 @@ allowed_children = { (DCNS,u'title') : ( ), # Completes Dublin Core start -# (DCNS,'contributor') : ( -# ), -# (DCNS,'coverage') : ( -# ), -# (DCNS,'format') : ( -# ), -# (DCNS,'identifier') : ( -# ), -# (DCNS,'publisher') : ( -# ), -# (DCNS,'relation') : ( -# ), -# (DCNS,'rights') : ( -# ), -# (DCNS,'source') : ( -# ), -# (DCNS,'type') : ( -# ), + # (DCNS,'contributor') : ( + # ), + # (DCNS,'coverage') : ( + # ), + # (DCNS,'format') : ( + # ), + # (DCNS,'identifier') : ( + # ), + # (DCNS,'publisher') : ( + # ), + # (DCNS,'relation') : ( + # ), + # (DCNS,'rights') : ( + # ), + # (DCNS,'source') : ( + # ), + # (DCNS,'type') : ( + # ), # Completes Dublin Core end (MATHNS,u'math') : None, @@ -981,15 +985,15 @@ allowed_children = { (DCNS,u'subject'), (DCNS,u'title'), # Completes Dublin Core start -# (DCNS,'contributor'), -# (DCNS,'coverage'), -# (DCNS,'format'), -# (DCNS,'identifier'), -# (DCNS,'publisher'), -# (DCNS,'relation'), -# (DCNS,'rights'), -# (DCNS,'source'), -# (DCNS,'type'), +# (DCNS,'contributor'), +# (DCNS,'coverage'), +# (DCNS,'format'), +# (DCNS,'identifier'), +# (DCNS,'publisher'), +# (DCNS,'relation'), +# (DCNS,'rights'), +# (DCNS,'source'), +# (DCNS,'type'), # Completes Dublin Core end (METANS,u'auto-reload'), (METANS,u'creation-date'), @@ -3245,15 +3249,15 @@ allows_text = ( (DCNS,u'subject'), (DCNS,u'title'), # Completes Dublin Core start -# (DCNS,'contributor'), -# (DCNS,'coverage'), -# (DCNS,'format'), -# (DCNS,'identifier'), -# (DCNS,'publisher'), -# (DCNS,'relation'), -# (DCNS,'rights'), -# (DCNS,'source'), -# (DCNS,'type'), +# (DCNS,'contributor'), +# (DCNS,'coverage'), +# (DCNS,'format'), +# (DCNS,'identifier'), +# (DCNS,'publisher'), +# (DCNS,'relation'), +# (DCNS,'rights'), +# (DCNS,'source'), +# (DCNS,'type'), # Completes Dublin Core end (FORMNS,u'item'), (FORMNS,u'option'), @@ -4239,24 +4243,24 @@ allowed_attributes = { (DCNS,u'title'):( ), # Completes Dublin Core start -# (DCNS,'contributor') : ( -# ), -# (DCNS,'coverage') : ( -# ), -# (DCNS,'format') : ( -# ), -# (DCNS,'identifier') : ( -# ), -# (DCNS,'publisher') : ( -# ), -# (DCNS,'relation') : ( -# ), -# (DCNS,'rights') : ( -# ), -# (DCNS,'source') : ( -# ), -# (DCNS,'type') : ( -# ), +# (DCNS,'contributor') : ( +# ), +# (DCNS,'coverage') : ( +# ), +# (DCNS,'format') : ( +# ), +# (DCNS,'identifier') : ( +# ), +# (DCNS,'publisher') : ( +# ), +# (DCNS,'relation') : ( +# ), +# (DCNS,'rights') : ( +# ), +# (DCNS,'source') : ( +# ), +# (DCNS,'type') : ( +# ), # Completes Dublin Core end (MATHNS,u'math'): None, (XFORMSNS,u'model'): None, diff --git a/src/odf/load.py b/src/odf/load.py index a540213718..8bcc019b49 100644 --- a/src/odf/load.py +++ b/src/odf/load.py @@ -49,7 +49,7 @@ class LoadParser(handler.ContentHandler): self.parse = False def characters(self, data): - if self.parse == False: + if self.parse is False: return self.data.append(data) @@ -58,7 +58,7 @@ class LoadParser(handler.ContentHandler): self.parse = True if self.doc._parsing != "styles.xml" and tag == (OFFICENS, 'font-face-decls'): self.parse = False - if self.parse == False: + if self.parse is False: return self.level = self.level + 1 @@ -98,7 +98,7 @@ class LoadParser(handler.ContentHandler): self.parent = e def endElementNS(self, tag, qname): - if self.parse == False: + if self.parse is False: return self.level = self.level - 1 # Changed by Kovid to deal with tags with only whitespace diff --git a/src/odf/namespaces.py b/src/odf/namespaces.py index cff8a74992..be7be4c4b1 100644 --- a/src/odf/namespaces.py +++ b/src/odf/namespaces.py @@ -24,7 +24,7 @@ CHARTNS = u"urn:oasis:names:tc:opendocument:xmlns:chart:1.0" CHARTOOONS = u"http://openoffice.org/2010/chart" CONFIGNS = u"urn:oasis:names:tc:opendocument:xmlns:config:1.0" CSS3TNS = u"http://www.w3.org/TR/css3-text/" -#DBNS = u"http://openoffice.org/2004/database" +# DBNS = u"http://openoffice.org/2004/database" DBNS = u"urn:oasis:names:tc:opendocument:xmlns:database:1.0" DCNS = u"http://purl.org/dc/elements/1.1/" DOMNS = u"http://www.w3.org/2001/xml-events" diff --git a/src/odf/odf2moinmoin.py b/src/odf/odf2moinmoin.py index 70fd3ba141..6dcede7104 100644 --- a/src/odf/odf2moinmoin.py +++ b/src/odf/odf2moinmoin.py @@ -22,7 +22,7 @@ import zipfile, xml.dom.minidom from .namespaces import nsdict -from .elementtypes import * +from .elementtypes import empty_elements, inline_elements IGNORED_TAGS = [ 'draw:a' @@ -186,11 +186,6 @@ class ODF2MoinMoin(object): textProps = TextProps() - if parent: - parentProp = self.textStyles.get(parent, None) - if parentProp: - textProp = parentProp - textPropEl = style.getElementsByTagName("style:text-properties") if not textPropEl: return textProps @@ -274,8 +269,7 @@ class ODF2MoinMoin(object): prop = ListProperties() if style.hasChildNodes(): subitems = [el for el in style.childNodes - if el.nodeType == xml.dom.Node.ELEMENT_NODE - and el.tagName == "text:list-level-style-number"] + if el.nodeType == xml.dom.Node.ELEMENT_NODE and el.tagName == "text:list-level-style-number"] if len(subitems) > 0: prop.setOrdered(True) @@ -311,8 +305,7 @@ class ODF2MoinMoin(object): for i in range(numLines): if (lines[i].strip() or i == numLines-1 or i == 0 or - not (lines[i-1].startswith(" ") - and lines[i+1].startswith(" "))): + not (lines[i-1].startswith(" ") and lines[i+1].startswith(" "))): buffer.append("\n" + lines[i]) return ''.join(buffer) diff --git a/src/odf/odf2xhtml.py b/src/odf/odf2xhtml.py index 66a46a616e..f4f3ade70e 100644 --- a/src/odf/odf2xhtml.py +++ b/src/odf/odf2xhtml.py @@ -1638,7 +1638,7 @@ dl.notes dd:last-of-type { page-break-after: avoid } else: if addsuffix: outputfile = outputfile + ".html" - outputfp = file(outputfile, "w") + outputfp = open(outputfile, "wb") outputfp.write(self.xhtml().encode('us-ascii','xmlcharrefreplace')) outputfp.close() diff --git a/src/odf/odfmanifest.py b/src/odf/odfmanifest.py index b7bfed57dd..c40ba0e816 100644 --- a/src/odf/odfmanifest.py +++ b/src/odf/odfmanifest.py @@ -24,7 +24,6 @@ from __future__ import print_function import zipfile from xml.sax import make_parser,handler from xml.sax.xmlreader import InputSource -import xml.sax.saxutils import io MANIFESTNS="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" diff --git a/src/odf/opendocument.py b/src/odf/opendocument.py index 5d56ea2bf6..cea176ca06 100644 --- a/src/odf/opendocument.py +++ b/src/odf/opendocument.py @@ -20,16 +20,28 @@ __doc__="""Use OpenDocument to generate your documents.""" -import zipfile, time, sys, mimetypes -from .namespaces import * -from . import manifest, meta, element -from .office import * -from .attrconverters import make_NCName -from xml.sax.xmlreader import InputSource -from .odfmanifest import manifestlist +import mimetypes +import sys +import time +import zipfile from io import BytesIO +from xml.sax.xmlreader import InputSource + from polyglot.io import PolyglotBytesIO, PolyglotStringIO +from . import element, manifest, meta +from .attrconverters import make_NCName +from .namespaces import ( + CHARTNS, DRAWNS, METANS, OFFICENS, PRESENTATIONNS, STYLENS, TABLENS, TEXTNS, + TOOLSVERSION +) +from .odfmanifest import manifestlist +from .office import ( + AutomaticStyles, Body, Chart, Document, DocumentContent, DocumentMeta, + DocumentSettings, DocumentStyles, Drawing, FontFaceDecls, Image, MasterStyles, + Meta, Presentation, Scripts, Settings, Spreadsheet, Styles, Text +) + __version__= TOOLSVERSION _XMLPROLOGUE = u"\n" @@ -160,7 +172,7 @@ class OpenDocument: if not filename: return xml.getvalue() else: - f=file(filename,'wb') + f=open(filename,'wb') f.write(xml.getvalue()) f.close() @@ -365,11 +377,9 @@ class OpenDocument: return ".%s" % document.folder def _savePictures(self, object, folder): - hasPictures = False for arcname, picturerec in object.Pictures.items(): what_it_is, fileobj, mediatype = picturerec self.manifest.addElement(manifest.FileEntry(fullpath="%s%s" % (folder ,arcname), mediatype=mediatype)) - hasPictures = True if what_it_is == IS_FILENAME: self._z.write(fileobj, arcname, zipfile.ZIP_STORED) else: @@ -516,7 +526,7 @@ class OpenDocument: def createCDATASection(self, data): """ Method to create a CDATA section """ - return element.CDATASection(cdata) + return element.CDATASection(data) def getMediaType(self): """ Returns the media type """ diff --git a/src/odf/style.py b/src/odf/style.py index 16b8936089..3a311c30e5 100644 --- a/src/odf/style.py +++ b/src/odf/style.py @@ -24,7 +24,7 @@ from .element import Element def StyleElement(**args): e = Element(**args) - if args.get('check_grammar', True) == True: + if args.get('check_grammar', True) is True: if 'displayname' not in args: e.setAttrNS(STYLENS,'display-name', args.get('name')) return e diff --git a/src/odf/svg.py b/src/odf/svg.py index ab2048c690..9ace75eb80 100644 --- a/src/odf/svg.py +++ b/src/odf/svg.py @@ -20,7 +20,7 @@ from .namespaces import SVGNS from .element import Element -from draw import DrawElement +from .draw import DrawElement # Autogenerated diff --git a/src/odf/teletype.py b/src/odf/teletype.py index 6829cb0cda..957647b9fe 100644 --- a/src/odf/teletype.py +++ b/src/odf/teletype.py @@ -27,9 +27,8 @@ the appropriate , , or elements. This module takes care of that problem. """ -from odf.element import Node -import odf.opendocument -from odf.text import S,LineBreak,Tab +from .element import Node +from .text import S,LineBreak,Tab class WhitespaceText(object): diff --git a/src/odf/thumbnail.py b/src/odf/thumbnail.py index 6ca0c81158..afb290d95a 100644 --- a/src/odf/thumbnail.py +++ b/src/odf/thumbnail.py @@ -424,6 +424,6 @@ def thumbnail(): if __name__ == "__main__": icon = thumbnail() - f = file("thumbnail.png","wb") + f = open("thumbnail.png","wb") f.write(icon) f.close()