summaryrefslogtreecommitdiff
path: root/ishtar_common/ooo_replace.py
blob: 08ee17b6394ed59ecea357b13b90bfb5a991412c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2013 É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
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

import locale, re
from zipfile import ZipFile, ZIP_DEFLATED
from cStringIO import StringIO
from xml.etree.cElementTree import ElementTree, fromstring

from django.conf import settings

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:
        value = value[5:]
    if value.startswith('"') and value.endswith('"') and len(value) > 1:
        value = value[1:-1]
    elif value in context:
        value = _format_value(context[value], default_value)
    else:
        value = None
    return value

def _parse_condition(condition, context, default_value):
    # parse only == and != operator
    operator = ""
    if "!=" in condition:
        operator = "!="
    elif "==" in condition:
        operator = "=="
    else:
        return
    var1, var2 = condition.split(operator)
    var1 = _set_value_from_formula(var1, context, default_value)
    var2 = _set_value_from_formula(var2, context, default_value)
    res = var1 == var2
    if operator == '!=':
        res = not res
    return res

def _format_value(value, default_value):
    if hasattr(value, 'strftime'):
        c_locale = settings.LANGUAGE_CODE.split('-')
        if len(c_locale) == 2:
            c_locale[1] = c_locale[1].upper()
        c_locale = "_".join(c_locale)
        if locale.getlocale()[0] != c_locale:
            for loc in (c_locale, c_locale+'.utf8'):
                try:
                    locale.setlocale(locale.LC_ALL, loc)
                    break
                except:
                    pass
        try:
            if settings.DATE_FORMAT:
                value = value.strftime(settings.DATE_FORMAT).lower()
            else:
                value = value.strftime('%x')
        except ValueError:
            value = unicode(value)
        if locale.getlocale()[1]:
            value = value.decode(locale.getlocale()[1])
    value = unicode(value) if value else default_value
    return value

VAR_EXPR = u"###VAR %s###"
RE_VAR = re.compile(u"###VAR %s###" % u"([-a-zA-Z0-9_]+)")
IF_EXPR = u"###IF %s###(.*)###ENDIF###"
RE_IF = re.compile(IF_EXPR % u"([-a-zA-Z0-9_]+)")

def _ishtar_parsing(context, 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 key, val in RE_IF.findall(value):
        v = ""
        if key in context and context[key]:
            v = _ishtar_parsing(context, val)
        value = re.sub(IF_EXPR % key, v, value)
    for key in RE_VAR.findall(value):
        v = ""
        if key in context and context[key]:
            v = unicode(context[key])
        value = re.sub(VAR_EXPR % key, v, value)
    return value

def ooo_replace(infile, outfile, context, default_value=''):
    inzip = ZipFile(infile, 'r', ZIP_DEFLATED)
    outzip = ZipFile(outfile, 'w', ZIP_DEFLATED)

    # regular ooo parsing
    content = ElementTree(fromstring(inzip.read('content.xml')))
    missing_keys = set()
    for xp in ('variable-set', 'variable-get'):
        for p in content.findall(".//"+OOO_NS+xp):
            name = p.get(OOO_NS+"name")
            if name in context:
                value = context[name]
                p.text = _format_value(value, default_value)
            else:
                if default_value != None:
                    p.text = default_value
                missing_keys.add(name)
    for p in content.findall(".//"+OOO_NS+"conditional-text"):
        condition = p.get(OOO_NS+"condition")
        res = 'true' if _parse_condition(condition, context, default_value) \
              else 'false'
        value = p.get(OOO_NS+'string-value-if-' + res)
        value = _format_value(value, default_value)
        if value.strip() in context:
            value = context[value.strip()]
        p.text = value

    # raw content parsing
    str_io = StringIO()
    content.write(str_io)
    value = str_io.getvalue()
    value = _ishtar_parsing(context, value).encode('utf-8')

    for f in inzip.infolist():
        if f.filename == 'content.xml':
            outzip.writestr('content.xml', value)
        else:
            outzip.writestr(f, inzip.read(f.filename))
    inzip.close()
    outzip.close()
    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"}
    ooo_replace(infile, outfile, rep, default_value="")