Separa declarações e contribuinte.
[cascardo/irpf-gui.git] / src / municipios.py
index 380ffa7..c9cd47a 100644 (file)
@@ -1,3 +1,4 @@
+# coding=utf-8
 #
 #   Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
 #
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # -*- mode: python; encoding: utf-8; -*-
 import xml.dom.minidom
+import dirs
 
 class Municipios:
-    def __init__(self, UF):
-        self.xml = xml.dom.minidom.parse("res/%s.xml" % (UF,))
+    def __init__(self):
         self.l = []
-        self._list()
+
     def _list(self):
-        for i in self.xml.childNodes[0].childNodes:
+        for i in self.xml.getElementsByTagName("ITEM"):
             if "COL3" in i.attributes.keys():
                 self.l.append((i.attributes["COL1"].nodeValue, \
                         i.attributes["COL2"].nodeValue, \
                         i.attributes["COL3"].nodeValue))
                 def list(self):
                     return self.l
+
     def get_municipio(self, code):
         for i in self.l:
             if i[0] == code:
                 return i
         return None
+
+    def carregar_estado(self, UF):
+        irpf_dir = dirs.get_default_irpf_dir()
+        self.l = []
+        self.xml = xml.dom.minidom.parse(irpf_dir.get_resource_file("%s.xml" % (UF,)))
+        self._list()
+
     def verify_cep(self, m, cep):
         l = m[2][0:7]
         h = m[2][9:16]
@@ -42,13 +51,15 @@ class Municipios:
         return False
 
 if __name__ == '__main__':
-    municipios = Municipios('MG')
+    municipios = Municipios()
+
+    municipios.carregar_estado('MG')
     m = municipios.get_municipio('4877')
     print m[1]
     print municipios.verify_cep(m, '36880000')
     print municipios.verify_cep(m, '05020000')
 
-    municipios = Municipios('SP')
+    municipios.carregar_estado('SP')
     m = municipios.get_municipio('7107')
     print m[1]
     print municipios.verify_cep(m, '05020000')