- 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))
- self.contribuinte = self.dados.getElementsByTagName("contribuinte")[0]
- 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_attr(self, el, attr):
- if attr in el.attributes.keys():
- return el.attributes[attr].nodeValue
- return None
- def _set_attr(self, el, attr, val):
- el.attributes[attr].nodeValue = val
- def get_declaracao(self, attr):
- return self._get_attr(self.declaracao, attr)
- def set_declaracao(self, attr, val):
- self._set_attr(self.declaracao, attr, val)
- def get_nome(self):
- return self.get_declaracao("nome")
- def set_nome(self, nome):
- self.set_declaracao("nome", nome)
- def get_contribuinte(self, attr):
- if attr == "nome":
- return self.get_nome()
- return self._get_attr(self.contribuinte, attr)
- def set_contribuinte(self, attr, val):
- if attr == "nome":
- self.set_nome(val)
- self._set_attr(self.contribuinte, attr, val)
+ 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))
+ self.contribuinte = self.dados.getElementsByTagName("contribuinte")[0]
+ 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_attr(self, el, attr):
+ if attr in el.attributes.keys():
+ return el.attributes[attr].nodeValue
+ return None
+ def _set_attr(self, el, attr, val):
+ el.attributes[attr].nodeValue = val
+ def get_declaracao(self, attr):
+ return self._get_attr(self.declaracao, attr)
+ def set_declaracao(self, attr, val):
+ self._set_attr(self.declaracao, attr, val)
+ def get_nome(self):
+ return self.get_declaracao("nome")
+ def set_nome(self, nome):
+ self.set_declaracao("nome", nome)
+ def get_contribuinte(self, attr):
+ if attr == "nome":
+ return self.get_nome()
+ return self._get_attr(self.contribuinte, attr)
+ def set_contribuinte(self, attr, val):
+ if attr == "nome":
+ self.set_nome(val)
+ self._set_attr(self.contribuinte, attr, val)