diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2015-08-18 11:41:46 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2015-08-18 11:41:46 +0200 | 
| commit | df41373f7eba2cce4e4d3a5ca77ba29c20f98f6b (patch) | |
| tree | 34e7ae83c3b5157f102377aa1d51d62a22e16cd2 /ishtar_common/ooo_replace.py | |
| parent | 4d5c4d09003fe3ac43e537b06b8a11ca8de19f92 (diff) | |
| download | Ishtar-df41373f7eba2cce4e4d3a5ca77ba29c20f98f6b.tar.bz2 Ishtar-df41373f7eba2cce4e4d3a5ca77ba29c20f98f6b.zip | |
PEP and formating
Diffstat (limited to 'ishtar_common/ooo_replace.py')
| -rw-r--r-- | ishtar_common/ooo_replace.py | 29 | 
1 files changed, 20 insertions, 9 deletions
| diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py index 54ecfced4..cf1c89051 100644 --- a/ishtar_common/ooo_replace.py +++ b/ishtar_common/ooo_replace.py @@ -17,7 +17,8 @@  # See the file COPYING for details. -import locale, re +import locale +import re  from zipfile import ZipFile, ZIP_DEFLATED  from cStringIO import StringIO  from xml.etree.cElementTree import ElementTree, fromstring @@ -25,6 +26,7 @@ from xml.etree.cElementTree import ElementTree, fromstring  from django.conf import settings  from ooo_translation import ooo_translation +  def translate_context(context, locale):      if locale not in ooo_translation:          return context @@ -38,6 +40,7 @@ def translate_context(context, locale):  OOO_NS = "{urn:oasis:names:tc:opendocument:xmlns:text:1.0}" +  def _set_value_from_formula(value, context, default_value):      value = value.strip()      if value.startswith("ooow:") and len(value) >= 5: @@ -50,6 +53,7 @@ def _set_value_from_formula(value, context, default_value):          value = None      return value +  def _parse_condition(condition, context, default_value):      # parse only == and != operator      operator = "" @@ -67,6 +71,7 @@ def _parse_condition(condition, context, default_value):          res = not res      return res +  def _format_value(value, default_value):      if hasattr(value, 'strftime'):          c_locale = settings.LANGUAGE_CODE.split('-') @@ -101,10 +106,11 @@ RE_IF = re.compile(IF_EXPR % (WHOLE_KEY_FILTER, WHOLE_KEY_FILTER))  TAG_FILTER = re.compile(u"(<[^<^>]*>)")  KEY_FILTER = re.compile(u"([-a-zA-Z0-9_]*)") +  def _filter_key(base_key):      # return (key, extra_marker)      # manage strange key such as: -    # test_<text:span text:style-name="T1">date</text:span> +    # test_<text:span text:style-name="T1">date</text:span>      key = base_key[:]      key = key.strip()      tags, new_key = '', key[:] @@ -118,6 +124,7 @@ def _filter_key(base_key):          full_key += k      return full_key, tags +  def _custom_parsing(context, value, default_value=''):      """      ###VAR nom_var### for displaying a variable name @@ -128,7 +135,7 @@ def _custom_parsing(context, value, default_value=''):                                       (RE_VAR, VAR_EXPR, False)):          for base_key in regexp.findall(value[:]):              v, val = "", None -            if if_cond: # the value inside the if is parsed +            if if_cond:  # the value inside the if is parsed                  pre_tag, base_key, val = base_key              else:                  pre_tag, base_key = base_key @@ -147,6 +154,7 @@ def _custom_parsing(context, value, default_value=''):              value = re.sub(sub_exp % (pre_tag, base_key), v, value)      return value +  def _ooo_replace(content, context, missing_keys, default_value=''):      # regular ooo parsing @@ -157,7 +165,7 @@ def _ooo_replace(content, context, missing_keys, default_value=''):                  value = context[name]                  p.text = _format_value(value, default_value)              else: -                if default_value != None: +                if default_value is not None:                      p.text = default_value                  missing_keys.add(name)      for p in content.findall(".//"+OOO_NS+"conditional-text"): @@ -177,6 +185,7 @@ def _ooo_replace(content, context, missing_keys, default_value=''):      value = _custom_parsing(context, value, default_value).encode('utf-8')      return value +  def ooo_replace(infile, outfile, context, default_value=''):      inzip = ZipFile(infile, 'r', ZIP_DEFLATED)      outzip = ZipFile(outfile, 'w', ZIP_DEFLATED) @@ -199,9 +208,11 @@ def ooo_replace(infile, outfile, context, default_value=''):      return missing_keys  if __name__ == '__main__': -    infile = "../archaeological_files/tests/AR_dossier_DRAC_modele_ishtar_1-MOD.odt" -    outfile = "../archaeological_files/tests/AR_dossier_DRAC_modele_ishtar-test.odt" -    rep = {"file_incharge_surname":u"Yann", -           "file_incharge_name":u"Le Jeune", -           "fileact_ref":u"ref"} +    infile = "../archaeological_files/tests/"\ +             "AR_dossier_DRAC_modele_ishtar_1-MOD.odt" +    outfile = "../archaeological_files/tests/"\ +              "AR_dossier_DRAC_modele_ishtar-test.odt" +    rep = {"file_incharge_surname": u"Yann", +           "file_incharge_name": u"Le Jeune", +           "fileact_ref": u"ref"}      ooo_replace(infile, outfile, rep, default_value="") | 
