From 1d0bd4185a8206a6ccc26e011d10ba473c1f34c7 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 25 Apr 2013 08:03:22 -0300 Subject: [PATCH] Interpreta e salva dados do contribuinte Apenas o nome e suportado no momento. O CPF pode ser lido de varias maneiras. --- contribuinte.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 contribuinte.py diff --git a/contribuinte.py b/contribuinte.py new file mode 100644 index 0000000..ca86c04 --- /dev/null +++ b/contribuinte.py @@ -0,0 +1,89 @@ +# +# Copyright 2013 Thadeu Lima de Souza Cascardo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -*- mode: python; encoding: utf-8; -*- +import xml.dom.minidom + +class Contribuinte: + def __init__(self, cpf): + self.cpf = self._minimize_cpf(cpf) + if not self._validate_cpf(self.cpf): + raise RuntimeError("Invalid CPF") + self.declaracao = self._find_id() + self.dados = xml.dom.minidom.parse("aplicacao/dados/%s/%s.xml" % (self.cpf, self.cpf)) + def _find_id(self): + cpf = self._normalize_cpf(self.cpf) + self.declaracoes = xml.dom.minidom.parse("aplicacao/dados/iddeclaracoes.xml") + for i in self.declaracoes.childNodes[0].childNodes: + if "cpf" in i.attributes.keys(): + if i.attributes["cpf"].nodeValue == cpf: + return i + return None + # CPF normalizado se parece com 000.000.000-00 + def _normalize_cpf(self, cpf): + ncpf = "" + for i in cpf: + if len(ncpf) == 3 or len(ncpf) == 7: + ncpf += '.' + if len(ncpf) == 11: + ncpf += '-' + if len(ncpf) == 14: + break + if ord(i) >= ord('0') and ord(i) <= ord('9'): + ncpf += i + if len(ncpf) != 14: + raise RuntimeError("Invalid CPF") + return ncpf + # CPF minimizado se parece com 01234567890 + def _minimize_cpf(self, cpf): + ncpf = bytearray(self._normalize_cpf(cpf)) + del ncpf[11] + del ncpf[7] + del ncpf[3] + return str(ncpf) + def _validate_cpf(self, cpf): + ncpf = self._minimize_cpf(cpf) + if len(ncpf) != 11: + return False + 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 + if v >= 10: + v = 0 + if v != ord(ncpf[9]) - ord('0'): + return False + 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 + if v >= 10: + v = 0 + if v != ord(ncpf[10]) - ord('0'): + return False + return True + def save(self): + self.dados.writexml(open("aplicacao/dados/%s/%s.xml" % (self.cpf, self.cpf), "w")) + self.declaracoes.writexml(open("aplicacao/dados/iddeclaracoes.xml", "w")) + def get_nome(self): + if "nome" in self.declaracao.attributes.keys(): + return self.declaracao.attributes["nome"].nodeValue + return None + def set_nome(self, nome): + self.declaracao.attributes["nome"].nodeValue = nome + +if __name__ == '__main__': + import sys + contribuinte = Contribuinte(sys.argv[1]) + print contribuinte._normalize_cpf(sys.argv[1]) + print contribuinte._minimize_cpf(sys.argv[1]) + print contribuinte._validate_cpf(sys.argv[1]) + contribuinte.set_nome(sys.argv[2]) + print contribuinte.get_nome() + contribuinte.save() -- 2.20.1