summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2015-02-18 13:20:25 +0100
committerÉtienne Loks <etienne.loks@proxience.com>2015-02-18 13:22:11 +0100
commitc2b5bcedad6f2afe5472319b4552ecb4558a3d40 (patch)
tree555de0595e2d8f2680f07b84acda057907de3966 /ishtar_common
parent25fe0cf58967a64541bc26386d50f2b3dfbd340f (diff)
downloadIshtar-c2b5bcedad6f2afe5472319b4552ecb4558a3d40.tar.bz2
Ishtar-c2b5bcedad6f2afe5472319b4552ecb4558a3d40.zip
Fix date localisation to ooo_parsing (refs #2286)
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/ooo_replace.py44
-rw-r--r--ishtar_common/tests.py12
-rw-r--r--ishtar_common/tests/test-file.odtbin9571 -> 10142 bytes
3 files changed, 38 insertions, 18 deletions
diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py
index 7baad2439..76ff81e73 100644
--- a/ishtar_common/ooo_replace.py
+++ b/ishtar_common/ooo_replace.py
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
-# Copyright (C) 2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
+# Copyright (C) 2013-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -81,36 +81,48 @@ def _format_value(value, default_value):
return value
VAR_EXPR = u"###VAR %s###"
-WHOLE_KEY_FILTER = u"((?: )*(?:<[^#>]*>)*(?: )*(?:[-a-zA-Z0-9_])+(?: )*)"
+WHOLE_KEY_FILTER = u"((?:(?: )*(?:<[^#>]*>)*(?: )*(?:[-a-zA-Z0-9_])*(?: )*)*)"
RE_VAR = re.compile(VAR_EXPR % WHOLE_KEY_FILTER)
IF_EXPR = u"###IF %s###(.*)###ENDIF###"
RE_IF = re.compile(IF_EXPR % WHOLE_KEY_FILTER)
-KEY_FILTER = u"(?: )*(<.*>)*(?: )*([-a-zA-Z0-9_]*)(?: )*"
-KEY_FILTER = re.compile(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>
key = base_key[:]
key = key.strip()
- m = KEY_FILTER.match(key)
- if not m:
- return '', key
- groups = m.groups()
- return groups[1], groups[0]
-
-def _ishtar_parsing(context, value):
+ tags, new_key = '', key[:]
+ for tag in TAG_FILTER.findall(key):
+ tags += tag
+ new_key = new_key.replace(tag, '')
+ full_key = ''
+ for k in KEY_FILTER.findall(new_key):
+ if not k:
+ continue
+ full_key += k
+ return full_key, tags
+
+def _ishtar_parsing(context, value, default_value=''):
"""
###VAR nom_var### for displaying a variable name
###IF nom_var### ###ENDIF### for conditionnal display
Be carreful nested condition are not yet managed!
"""
- for regexp, sub_exp in ((RE_IF, IF_EXPR),
- (RE_VAR, VAR_EXPR)):
+ for regexp, sub_exp, if_cond in ((RE_IF, IF_EXPR, True),
+ (RE_VAR, VAR_EXPR, False)):
for base_key in regexp.findall(value):
- v = ""
+ v, val = "", None
+ if if_cond: # the value inside the if is parsed
+ base_key, val = base_key
key, extra_markers = _filter_key(base_key)
if key in context and context[key]:
- v = unicode(context[key])
+ if if_cond:
+ v = _ishtar_parsing(context, val, default_value)
+ else:
+ v = _format_value(context[key], default_value)
# to preserve a consistent OOO file put extra_markers
if extra_markers:
v += extra_markers
@@ -148,7 +160,7 @@ def ooo_replace(infile, outfile, context, default_value=''):
str_io = StringIO()
content.write(str_io)
value = str_io.getvalue()
- value = _ishtar_parsing(context, value).encode('utf-8')
+ value = _ishtar_parsing(context, value, default_value).encode('utf-8')
for f in inzip.infolist():
if f.filename == 'content.xml':
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 06d0ca3ec..b8369ad03 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -17,9 +17,10 @@
# See the file COPYING for details.
-import tempfile
+import tempfile, datetime
from zipfile import ZipFile, ZIP_DEFLATED
+from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.template.defaultfilters import slugify
@@ -30,13 +31,20 @@ from ishtar_common.ooo_replace import ooo_replace
class OOOGenerationTest(TestCase):
def testGeneration(self):
- context = {'test-var':u"Testé", 'test-var2':u""}
+ context = {'test_var':u"Testé", 'test_var2':u"",
+ "test_date":datetime.date(2015, 1, 1)}
tmp = tempfile.TemporaryFile()
ooo_replace("../ishtar_common/tests/test-file.odt", tmp, context)
inzip = ZipFile(tmp, 'r', ZIP_DEFLATED)
value = inzip.read('content.xml')
self.assertTrue("Testé" in value)
self.assertTrue("testé 2" not in value)
+ self.assertTrue("2015" in value)
+ lg, ct = settings.LANGUAGE_CODE.split('-')
+ if lg == 'fr':
+ self.assertTrue('janvier' in value)
+ if lg == 'en':
+ self.assertTrue('january' in value)
class MergeTest(TestCase):
def setUp(self):
diff --git a/ishtar_common/tests/test-file.odt b/ishtar_common/tests/test-file.odt
index 2e73fe0ee..2dcc18179 100644
--- a/ishtar_common/tests/test-file.odt
+++ b/ishtar_common/tests/test-file.odt
Binary files differ