[lib] remove unused code: liasse (closes #254059)

authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
changeset9173466e7941
branchdefault
phasepublic
hiddenno
parent revision#17cb79468dd4 [test] use assertMultiLineEqual instead of deprecated assertStreamEqual
child revision#dd527074df74 [packaging] prepare 0.8.4, #0f10ed86ec5d Export du grand livre au format CSV (closes #256948)
files modified by this revision
MANIFEST.in
bin/pycompta-liasse
lib/liasse.py
test/test_liasse.py
# HG changeset patch
# User Nicolas Chauvat <nicolas.chauvat@logilab.fr>
# Date 1404070246 -7200
# Sun Jun 29 21:30:46 2014 +0200
# Node ID 9173466e79414c46dec5069b49b8c58b23c078b7
# Parent 17cb79468dd45ac9fa872e4d4d20d862a10b3ab6
[lib] remove unused code: liasse (closes #254059)

diff --git a/MANIFEST.in b/MANIFEST.in
@@ -7,11 +7,10 @@
1  recursive-include js *.js
2  include man/pycompta.1
3  include dtd/catalog
4  include bin/pycompta
5  include bin/pycompta-render
6 -include bin/pycompta-liasse
7  include bin/pycompta-gui
8  include ChangeLog
9  include README
10  include README.fr
11  include TODO
diff --git a/bin/pycompta-liasse b/bin/pycompta-liasse
@@ -1,5 +0,0 @@
12 -#!/usr/bin/env python
13 -import sys
14 -from pycompta.lib import liasse
15 -
16 -liasse.run(sys.argv[1:])
diff --git a/lib/liasse.py b/lib/liasse.py
@@ -1,213 +0,0 @@
17 -# -*- coding: utf-8 -*-
18 -"""
19 -Steps:
20 -- use command 'new' to make a fresh liasse.py that you put in source_dir/liasse-fiscale
21 -- use command 'labelize' to get form values to reflect form keys
22 -- modify column 'references' of liasse.py to point keys to nodes of the bilan.xml, compte-resultat.xml, ...
23 -- put missing information in column values of liasse.py
24 -- use command 'fill' to get final pdfs
25 -"""
26 -
27 -try:
28 -    import xml.etree.ElementTree as ET
29 -except ImportError:
30 -    import elementtree.ElementTree as ET
31 -import sys
32 -import os
33 -import glob
34 -import imp
35 -
36 -from logilab.common import pdf_ext
37 -
38 -# command 'new'
39 -def mk_new_liassepy(path):
40 -    forms = {}
41 -    for filename in glob.glob(os.path.join(path,'liasse-fiscale/*.pdf')):
42 -        keys = pdf_ext.extract_keys_from_pdf(filename)
43 -        fields = [(key, '', '') for key in keys]
44 -        forms[filename] = sorted(fields)
45 -
46 -    import pprint
47 -    pprint.pprint(forms) # use stream=
48 -
49 -# command 'labelize'
50 -def labelize(src, dest):
51 -    for filename in glob.glob(os.path.join(src,'liasse-fiscale/*.pdf')):
52 -        pdf_ext.testfill_pdf(filename, os.path.join(dest,'_'+os.path.basename(filename)))
53 -
54 -def get_liasse(path):
55 -    mod = imp.find_module('liasse', [os.path.join(path,'liasse-fiscale/')])
56 -    mod = imp.load_module('liasse', *mod)
57 -    return mod
58 -
59 -class HasIntRefMixIn(object):
60 -    def has_intref(self):
61 -        if isinstance(self, IntRef):
62 -            return True
63 -        if isinstance(self, Operator):
64 -            if self.left.has_intref() or self.right.has_intref():
65 -                return True
66 -        return False
67 -
68 -# handling of expressions
69 -class ExtRef(HasIntRefMixIn):
70 -    def __init__(self, tree_index, node_id, attr):
71 -        self.tree_index = tree_index
72 -        self.node_id = node_id
73 -        self.attr = attr
74 -
75 -    def __cmp__(self, other):
76 -        if isinstance(other, ExtRef) \
77 -               and self.tree_index == other.tree_index \
78 -               and self.node_id == other.node_id \
79 -               and self.attr == other.attr:
80 -            return 0
81 -        else:
82 -            return 1
83 -
84 -    def __repr__(self):
85 -        return '<ExtRef %s %s %s>' % (self.tree_index, self.node_id, self.attr)
86 -
87 -    def get_value(self, champs, trees):
88 -        elems = [elem for elem in trees[self.tree_index].findall(".//poste")
89 -                 if elem.get('id') == self.node_id]
90 -        if len(elems) != 1:
91 -            raise Exception('external node %s not found' % self.node_id)
92 -        val = elems[0].get(self.attr)[:-2]
93 -        return val
94 -
95 -class IntRef(HasIntRefMixIn):
96 -    def __init__(self, field_id):
97 -        self.field_id = field_id
98 -
99 -    def __cmp__(self, other):
100 -        if self.field_id == other.field_id:
101 -            return 0
102 -        else:
103 -            return 1
104 -
105 -    def __repr__(self):
106 -        return '<IntRef %s>' % (self.field_id)
107 -
108 -    def get_value(self, champs, trees):
109 -        for k,val,_ in champs:
110 -            if k == self.field_id:
111 -                return val
112 -        raise KeyError('reference %s not found' % self.field_id)
113 -
114 -class Operator(HasIntRefMixIn):
115 -    def __init__(self, op, left, right):
116 -        self.op = op
117 -        self.left = left
118 -        self.right = right
119 -        assert self.op in '+-', self.op
120 -
121 -    def __cmp__(self, other):
122 -        if self.op == other.op:
123 -            if self.left == other.left and self.right == other.right:
124 -                return 0
125 -        return 1
126 -
127 -    def __repr__(self):
128 -        return '<Operator %s>' % (self.op)
129 -
130 -    def get_value(self, champs, trees):
131 -        left = int(self.left.get_value(champs, trees) or 0)
132 -        right = int(self.right.get_value(champs, trees) or 0)
133 -        if self.op == '+':
134 -            return left + right
135 -        elif self.op == '-':
136 -            return left - right
137 -
138 -def tokenize(ref_expr):
139 -    for token in ref_expr.split():
140 -        if token in '+-':
141 -            yield Operator(token, None, None)
142 -        elif token.startswith('!'):
143 -            yield IntRef(token[1:])
144 -        else:
145 -            if '/' in token:
146 -                tree_index, token = token.split('/')
147 -                tree_index = int(tree_index)
148 -            else:
149 -                tree_index = 0
150 -            if '@' in token:
151 -                node_id, attr = token.split('@')
152 -                attr = 'montant-'+attr
153 -            else:
154 -                node_id, attr = token, 'montant'
155 -            yield ExtRef(tree_index, node_id, attr)
156 -
157 -def parse_ref(ref_expr):
158 -    tokens = tokenize(ref_expr)
159 -    root = None
160 -    while tokens:
161 -        try:
162 -            token = tokens.next()
163 -        except StopIteration:
164 -            break
165 -        if root is None:
166 -            if isinstance(token, Operator):
167 -                raise Exception('parse error')
168 -            else:
169 -                root = token
170 -        else:
171 -            if not isinstance(token, Operator):
172 -                raise Exception('parse error %s' % ref_expr)
173 -            else:
174 -                right = tokens.next()
175 -                token.left = root
176 -                token.right = right
177 -                root = token
178 -    return root
179 -
180 -
181 -
182 -def process_form(trees, champs, form, fields, skip_internal=False):
183 -    for key, value, ref_expr in fields:
184 -        skip = False
185 -        if value and ref_expr:
186 -            raise Exception('both value and ref defined for %s'%key)
187 -        if ref_expr:
188 -            root = parse_ref(ref_expr)
189 -            if skip_internal and root.has_intref():
190 -                continue
191 -            else:
192 -                value = root.get_value(champs, trees)
193 -        if value == 0: value = ''
194 -        champs.append( (key, str(value), ref_expr) )
195 -
196 -# command 'fill'
197 -def fill_liasse(src, dest):
198 -    module = get_liasse(src)
199 -    LIASSE = getattr(module, 'LIASSE')
200 -    FILES = getattr(module, 'FILES')
201 -    liasse = {}
202 -    for form, fields in LIASSE.items():
203 -        trees = [ET.parse(os.path.join(dest, filename))
204 -                 for filename in FILES[form]]
205 -        champs = []
206 -        process_form(trees, champs, form, fields, skip_internal=True)
207 -        process_form(trees, champs, form, fields)
208 -        liasse[form] = champs
209 -    return liasse
210 -
211 -
212 -def run(args):
213 -    if len(args) == 0:
214 -        cmd = None
215 -    else:
216 -        cmd = args[0]
217 -        src, dest = args[1:]
218 -    if cmd == 'fill':
219 -        liasse = fill_liasse(src, dest)
220 -        for form, fields in sorted(liasse.items()):
221 -            print form
222 -            pdf_ext.fill_pdf(os.path.join(src,'liasse-fiscale',form+'.pdf'),
223 -                             os.path.join(dest,form+'.pdf'), fields)
224 -    elif cmd == 'labelize':
225 -        labelize(src, dest)
226 -    else:
227 -        print 'unknown command %s\nusage: pycompta-liasse <new|labelize|fill> [args]' % cmd
228 -
229 -
diff --git a/test/test_liasse.py b/test/test_liasse.py
@@ -1,57 +0,0 @@
230 -from logilab.common import testlib
231 -from pycompta.lib import liasse
232 -
233 -try:
234 -    import xml.etree.ElementTree as ET
235 -except ImportError:
236 -    import elementtree.ElementTree as ET
237 -
238 -XMLBIT = '''<nodes>
239 -<poste id="2.1" montant="100" />
240 -<poste id="2.2" montant="200" />
241 -</nodes>
242 -'''
243 -
244 -class ExprTC(testlib.TestCase):
245 -
246 -    def test_un(self):
247 -        """build AST from expression"""
248 -        self.assertEqual( liasse.parse_ref('2.1'),
249 -                           liasse.ExtRef(0,'2.1','montant') )
250 -        expr = liasse.Operator('+',
251 -                               liasse.ExtRef(0,'2.1','montant'),
252 -                               liasse.ExtRef(0,'2.2','montant'))
253 -        self.assertEqual( liasse.parse_ref('2.1 + 2.2'), expr)
254 -
255 -    def test_getvalue(self):
256 -        """get value from tree"""
257 -        expr = liasse.parse_ref('2.1 + 2.2')
258 -        tree = ET.fromstring(XMLBIT)
259 -        val = expr.get_value([], [tree])
260 -        self.assertEqual(val, 3)
261 -
262 -    def test_getvalue2(self):
263 -        """get value from champs"""
264 -        expr = liasse.parse_ref('!a + 2.1')
265 -        champs = [ ('a', '4', '') ]
266 -        tree = ET.fromstring(XMLBIT)
267 -        val = expr.get_value(champs, [tree])
268 -        self.assertEqual(val, 5)
269 -
270 -    def test_getvalue20(self):
271 -        """substraction"""
272 -        expr = liasse.parse_ref('!a + 2.1')
273 -        champs = [ ('a', '4', '') ]
274 -        tree = ET.fromstring(XMLBIT)
275 -        val = expr.get_value(champs, [tree])
276 -        self.assertEqual(val, 5)
277 -
278 -    def test_getvalue3(self):
279 -        """sum with empty value"""
280 -        expr = liasse.parse_ref('!a + 2.1')
281 -        champs = [ ('a', '', '') ]
282 -        tree = ET.fromstring(XMLBIT)
283 -        val = expr.get_value(champs, [tree])
284 -        self.assertEqual(val, 1)
285 -if __name__ == 'main':
286 -    testlib.unittest_main()