Introduz classe tipos para ocupações, tipo de bens, etc.
[cascardo/irpf-gui.git] / src / tipos.py
1 # coding=utf-8
2 #
3 #   Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 # -*- mode: python; encoding: utf-8; -*-
18
19 import xml.dom.minidom
20 import dirs
21
22 class Tipos:
23     def __init__(self, fname, cols, group = -1):
24         irpf_dir = dirs.get_default_irpf_dir()
25         self.xml = xml.dom.minidom.parse(irpf_dir.get_resource_file(fname))
26         self.l = []
27         self.g = {}
28         self.cols = cols
29         self.group = group
30         self._list()
31         if group >= 0:
32             self._group()
33
34     def _list(self):
35         for i in self.xml.getElementsByTagName("ITEM"):
36             if self.cols[-1] in i.attributes.keys():
37                 item = []
38                 for j in self.cols:
39                     item.append(i.attributes[j].nodeValue)
40                 self.l.append(item)
41
42     def list(self):
43         return self.l
44
45     def _group(self):
46         for i in self.l:
47             if i[self.group] not in self.g:
48                 self.g[i[self.group]] = []
49             self.g[i[self.group]].append(i)
50
51     def groups(self):
52         if group < 0:
53             return None
54         return self.g
55
56     def get_by_code(self, code):
57         for i in self.l:
58             if i[0] == code:
59                 return i
60         return None
61
62 # vim:tabstop=4:expandtab:smartindent