3 # Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
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.
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.
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 import xml.dom.minidom
23 def __init__(self, cpf):
24 irpf_dir = dirs.get_default_irpf_dir()
25 self.cpf = self._minimize_cpf(cpf)
27 if not self._validate_cpf(self.cpf):
28 raise RuntimeError("Invalid CPF: " + self.cpf)
30 if not os.path.exists(irpf_dir.get_resource_dir()):
31 raise RuntimeError("O caminho para o resource não existe: " + \
32 irpf_dir.get_resource_dir())
34 if not os.path.exists(irpf_dir.get_userdata_dir()):
35 raise RuntimeError("O caminho para os dados não existe: " + \
36 irpf_dir.get_userdata_dir())
38 self.cpf_file = irpf_dir.get_userdata_file("%s/%s.xml" % (self.cpf, self.cpf))
39 self.iddecl_file = irpf_dir.get_userdata_file("iddeclaracoes.xml")
40 self.declaracao = self._find_id()
41 self.dados = xml.dom.minidom.parse(self.cpf_file)
42 self.contribuinte = self.dados.getElementsByTagName("contribuinte")[0]
45 cpf = self._normalize_cpf(self.cpf)
46 self.declaracoes = xml.dom.minidom.parse(self.iddecl_file)
47 for i in self.declaracoes.childNodes[0].childNodes:
48 if "cpf" in i.attributes.keys():
49 if i.attributes["cpf"].nodeValue == cpf:
53 # CPF normalizado se parece com 000.000.000-00
54 def _normalize_cpf(self, cpf):
57 if len(ncpf) == 3 or len(ncpf) == 7:
63 if ord(i) >= ord('0') and ord(i) <= ord('9'):
66 raise RuntimeError("Invalid CPF")
69 # CPF minimizado se parece com 01234567890
70 def _minimize_cpf(self, cpf):
71 ncpf = bytearray(self._normalize_cpf(cpf))
77 def _validate_cpf(self, cpf):
78 ncpf = self._minimize_cpf(cpf)
81 v = (11 - sum(map(lambda x: x[0]*x[1], zip(range(10, 1, -1), map(lambda x: ord(x) - ord('0'), ncpf[0:9]))))) % 11
84 if v != ord(ncpf[9]) - ord('0'):
86 v = (11 - sum(map(lambda x: x[0]*x[1], zip(range(11, 1, -1), map(lambda x: ord(x) - ord('0'), ncpf[0:10]))))) % 11
89 if v != ord(ncpf[10]) - ord('0'):
94 self.dados.writexml(open(self.cpf_file, "w"))
95 self.declaracoes.writexml(open(self.iddecl_file, "w"))
96 def _get_attr(self, el, attr):
97 if attr in el.attributes.keys():
98 return el.attributes[attr].nodeValue
101 def _set_attr(self, el, attr, val):
102 el.attributes[attr].nodeValue = val
104 def get_declaracao(self, attr):
105 return self._get_attr(self.declaracao, attr)
107 def set_declaracao(self, attr, val):
108 self._set_attr(self.declaracao, attr, val)
111 return self.get_declaracao("nome")
113 def set_nome(self, nome):
114 self.set_declaracao("nome", nome)
116 def get_campo_contribuinte(self, attr):
118 return self.get_nome()
119 return self._get_attr(self.contribuinte, attr)
121 def set_campo_contribuinte(self, attr, val):
125 self._set_attr(self.contribuinte, attr, val)
127 contribuinte_attributes = [
155 declaracao_attributes = [
157 "declaracaoRetificadora",
163 "numeroReciboDecAnterior",
164 "resultadoDeclaracao",
171 if __name__ == '__main__':
173 contribuinte = Contribuinte(sys.argv[1])
174 print "Carregando CPF " + contribuinte._normalize_cpf(sys.argv[1])
176 if len(sys.argv) == 4:
177 print "Valor anterior: " + contribuinte.get_campo_contribuinte(sys.argv[2])
178 contribuinte.set_campo_contribuinte(sys.argv[2], sys.argv[3])
179 print "Valor atual: " + contribuinte.get_campo_contribuinte(sys.argv[2])
182 elif len(sys.argv) == 3:
184 valor = contribuinte.get_campo_contribuinte(campo)
186 print ("Valor de " + campo + ": " + valor)
188 print ("Campo " + campo + " retornou vazio")
190 print "\nCONTRIBUINTE:"
191 for i in contribuinte_attributes:
192 val = contribuinte.get_campo_contribuinte(i)
196 print "\nDECLARACAO:"
197 for i in declaracao_attributes:
198 val = contribuinte.get_declaracao(i)
203 # vim:tabstop=4:expandtab:smartindent