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; -*-
18 import xml.dom.minidom
25 class OcupacaoForm(form.TipoForm):
26 def __init__(self, ocupacoes, contribuinte):
27 form.TipoForm.__init__(self, u"Ocupações", "ocupacaoPrincipal", contribuinte, ocupacoes, (0, 3))
29 class ContribuinteForm(form.StringForm):
30 def __init__(self, name, attr, contribuinte):
31 self.contribuinte = contribuinte
33 form.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
34 def set_value(self, value):
35 form.StringForm.set_value(self, value)
36 self.contribuinte.set_campo_contribuinte(self.attr, value)
39 def __init__(self, cpf):
40 irpf_dir = dirs.get_default_irpf_dir()
41 self.cpf = self._minimize_cpf(cpf)
43 if not self._validate_cpf(self.cpf):
44 raise RuntimeError("Invalid CPF: " + self.cpf)
46 if not os.path.exists(irpf_dir.get_resource_dir()):
47 raise RuntimeError("O caminho para o resource não existe: " + \
48 irpf_dir.get_resource_dir())
50 if not os.path.exists(irpf_dir.get_userdata_dir()):
51 raise RuntimeError("O caminho para os dados não existe: " + \
52 irpf_dir.get_userdata_dir())
54 self.cpf_file = irpf_dir.get_userdata_file("%s/%s.xml" % (self.cpf, self.cpf))
55 ncpf = self._normalize_cpf(self.cpf)
56 self.declaracoes = declaracoes.Declaracoes()
57 self.declaracao = self.declaracoes.find("cpf", ncpf)
58 self.dados = xml.dom.minidom.parse(self.cpf_file)
59 self.contribuinte = self.dados.getElementsByTagName("contribuinte")[0]
61 # CPF normalizado se parece com 000.000.000-00
62 def _normalize_cpf(self, cpf):
65 if len(ncpf) == 3 or len(ncpf) == 7:
71 if ord(i) >= ord('0') and ord(i) <= ord('9'):
74 raise RuntimeError("Invalid CPF")
77 # CPF minimizado se parece com 01234567890
78 def _minimize_cpf(self, cpf):
79 return self._minimize_valor(cpf)
81 def _minimize_valor(self, valor):
82 nvalor = ''.join(e for e in valor if e.isalnum())
85 def _validate_cpf(self, cpf):
88 return self._validate_generico(cpf)
90 def _validate_generico(self, valor):
91 def calcula_digito_verificador(numero):
96 if i > len(numero) - 1:
98 soma = soma + int(numero[i]) * ( n - i)
107 return numero + str(dv)
109 mcpf = self._minimize_valor(valor)
110 cpf_sem_dv = mcpf[:-2]
112 primeiro_dv = str(calcula_digito_verificador(cpf_sem_dv))
113 segundo_dv = calcula_digito_verificador(primeiro_dv)
115 return segundo_dv == mcpf
118 self.dados.writexml(open(self.cpf_file, "w"))
119 self.declaracoes.save()
121 def _get_attr(self, el, attr):
122 if attr in el.attributes.keys():
123 return el.attributes[attr].nodeValue
126 def _set_attr(self, el, attr, val):
127 el.attributes[attr].nodeValue = val
129 def get_declaracao(self, attr):
130 return self.declaracao.get_attr(attr)
132 def set_declaracao(self, attr, val):
133 self.declaracao.set_attr(attr, val)
136 return self.get_declaracao("nome")
138 def set_nome(self, nome):
139 self.set_declaracao("nome", nome)
141 def get_campo_contribuinte(self, attr):
143 return self.get_nome()
144 return self._get_attr(self.contribuinte, attr)
146 def set_campo_contribuinte(self, attr, val):
150 self._set_attr(self.contribuinte, attr, val)
152 def get_attr(self, attr):
153 return self.get_campo_contribuinte(attr)
155 def set_attr(self, attr, val):
156 self.set_campo_contribuinte(attr, val)
160 ocup = ocupacoes.Ocupacoes()
161 form.append(ContribuinteForm("Nome", "nome", self))
162 form.append(OcupacaoForm(ocup, self))
163 for i in self.attributes:
164 form.append(ContribuinteForm(i, i, self))
195 if __name__ == '__main__':
197 contribuinte = Contribuinte(sys.argv[1])
198 print "Carregando CPF " + contribuinte._normalize_cpf(sys.argv[1])
200 if len(sys.argv) == 4:
201 print "Valor anterior: " + contribuinte.get_campo_contribuinte(sys.argv[2])
202 contribuinte.set_campo_contribuinte(sys.argv[2], sys.argv[3])
203 print "Valor atual: " + contribuinte.get_campo_contribuinte(sys.argv[2])
206 elif len(sys.argv) == 3:
208 valor = contribuinte.get_campo_contribuinte(campo)
210 print ("Valor de " + campo + ": " + valor)
212 print ("Campo " + campo + " retornou vazio")
214 print "\nCONTRIBUINTE:"
215 for i in Contribuinte.attributes:
216 val = contribuinte.get_campo_contribuinte(i)
220 print "\nDECLARACAO:"
221 for i in declaracoes.Declaracoes.attributes:
222 val = contribuinte.get_declaracao(i)
227 # vim:tabstop=4:expandtab:smartindent