3 # Copyright 2013-2014 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; -*-
19 import xml.dom.minidom
27 def __init__(self, cpf):
28 irpf_dir = dirs.get_default_irpf_dir()
29 self.cpf = self._minimize_cpf(cpf)
31 if not self._validate_cpf(self.cpf):
32 raise RuntimeError("Invalid CPF: " + self.cpf)
34 if not os.path.exists(irpf_dir.get_resource_dir()):
35 raise RuntimeError("O caminho para o resource não existe: " + \
36 irpf_dir.get_resource_dir())
38 if not os.path.exists(irpf_dir.get_userdata_dir()):
39 raise RuntimeError("O caminho para os dados não existe: " + \
40 irpf_dir.get_userdata_dir())
42 self.cpf_file = irpf_dir.get_userdata_file("%s/%s.xml" % (self.cpf, self.cpf))
43 ncpf = self._normalize_cpf(self.cpf)
44 self.declaracoes = declaracoes.Declaracoes()
45 self.declaracao = self.declaracoes.find("cpf", ncpf)
46 self.dados = xml.dom.minidom.parse(self.cpf_file)
47 self.contribuinte = self.dados.getElementsByTagName("contribuinte")[0]
49 # CPF normalizado se parece com 000.000.000-00
50 def _normalize_cpf(self, cpf):
53 if len(ncpf) == 3 or len(ncpf) == 7:
59 if ord(i) >= ord('0') and ord(i) <= ord('9'):
62 raise RuntimeError("Invalid CPF")
65 # CPF minimizado se parece com 01234567890
66 def _minimize_cpf(self, cpf):
67 return self._minimize_valor(cpf)
69 def _minimize_valor(self, valor):
70 nvalor = ''.join(e for e in valor if e.isalnum())
73 def _validate_cpf(self, cpf):
76 return self._validate_generico(cpf)
78 def _validate_generico(self, valor):
79 def calcula_digito_verificador(numero):
84 if i > len(numero) - 1:
86 soma = soma + int(numero[i]) * ( n - i)
95 return numero + str(dv)
97 mcpf = self._minimize_valor(valor)
98 cpf_sem_dv = mcpf[:-2]
100 primeiro_dv = str(calcula_digito_verificador(cpf_sem_dv))
101 segundo_dv = calcula_digito_verificador(primeiro_dv)
103 return segundo_dv == mcpf
106 self.dados.writexml(open(self.cpf_file, "w"))
107 self.declaracoes.save()
109 def _get_attr(self, el, attr):
110 if attr in el.attributes.keys():
111 return el.attributes[attr].nodeValue
114 def _set_attr(self, el, attr, val):
115 el.attributes[attr].nodeValue = val
117 def get_declaracao(self, attr):
118 return self.declaracao.get_attr(attr)
120 def set_declaracao(self, attr, val):
121 self.declaracao.set_attr(attr, val)
124 return self.get_declaracao("nome")
126 def set_nome(self, nome):
127 self.set_declaracao("nome", nome)
129 def get_campo_contribuinte(self, attr):
131 return self.get_nome()
132 return self._get_attr(self.contribuinte, attr)
134 def set_campo_contribuinte(self, attr, val):
138 self._set_attr(self.contribuinte, attr, val)
140 def get_attr(self, attr):
141 return self.get_campo_contribuinte(attr)
143 def set_attr(self, attr, val):
144 self.set_campo_contribuinte(attr, val)
148 f.append(form.AttrForm("Nome", "nome", self))
149 f.append(ocupacoes.OcupacaoForm(self))
150 for i in self.attributes:
151 f.append(form.AttrForm(i, i, self))
182 if __name__ == '__main__':
184 contribuinte = Contribuinte(sys.argv[1])
185 print "Carregando CPF " + contribuinte._normalize_cpf(sys.argv[1])
187 if len(sys.argv) == 4:
188 print "Valor anterior: " + contribuinte.get_campo_contribuinte(sys.argv[2])
189 contribuinte.set_campo_contribuinte(sys.argv[2], sys.argv[3])
190 print "Valor atual: " + contribuinte.get_campo_contribuinte(sys.argv[2])
193 elif len(sys.argv) == 3:
195 valor = contribuinte.get_campo_contribuinte(campo)
197 print ("Valor de " + campo + ": " + valor)
199 print ("Campo " + campo + " retornou vazio")
201 print "\nCONTRIBUINTE:"
202 for i in Contribuinte.attributes:
203 val = contribuinte.get_campo_contribuinte(i)
207 print "\nDECLARACAO:"
208 for i in declaracoes.Declaracoes.attributes:
209 val = contribuinte.get_declaracao(i)
214 # vim:tabstop=4:expandtab:smartindent