Simplifica Bens e RendimentoPJ usando Items.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Sat, 15 Mar 2014 00:57:57 +0000 (21:57 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Sat, 15 Mar 2014 00:57:57 +0000 (21:57 -0300)
src/bens.py
src/rendimentoPJ.py
test/test_bens.py

index edb57b8..f79652c 100644 (file)
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # -*- mode: python; encoding: utf-8; -*-
 import xml.dom.minidom
+import items
 
-class Bem:
-    def __init__(self, el):
-        self.bem = el
-
-    def get_attr(self, attr):
-        if attr in self.bem.attributes.keys():
-            return self.bem.attributes[attr].nodeValue
-
-        return None
-
-    def set_attr(self, attr, val):
-        self.bem.attributes[attr].nodeValue = val
-
-class Bens:
+class Bens(items.Items):
     def __init__(self, contribuinte):
-        self.contribuinte = contribuinte
-        self.bens = self.contribuinte.dados.getElementsByTagName("bens")[0]
-        self.items = []
-
-        for i in self.bens.getElementsByTagName("item"):
-            self.items.append(Bem(i))
-
-    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_bens(self, attr):
-        return self._get_attr(self.bens, attr)
-
-    def set_bens(self, attr, val):
-        self._set_attr(self.bens, attr, val)
+        contribuinte = contribuinte
+        bens = contribuinte.dados.getElementsByTagName("bens")[0]
+        items.Items.__init__(self, bens)
 
 if __name__ == '__main__':
     import sys
@@ -59,8 +30,8 @@ if __name__ == '__main__':
 
     contribuinte = Contribuinte(sys.argv[1])
     bens = Bens(contribuinte)
-    print "anterior: " + bens.get_bens("totalExercicioAnterior")
-    print "atual: " + bens.get_bens("totalExercicioAtual")
+    print "anterior: " + bens.get_attr("totalExercicioAnterior")
+    print "atual: " + bens.get_attr("totalExercicioAtual")
 
     for i in bens.items:
         print i.get_attr("valorExercicioAtual") + " " + i.get_attr("discriminacao")
index 54a5492..fa9d2b0 100644 (file)
 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # -*- mode: python; encoding: utf-8; -*-
 import xml.dom.minidom
+import items
 
-class RendimentoPJ:
-    def __init__(self, el = None):
-        if el == None:
-            self.rendimento = xml.dom.minidom.Element("item")
-        else:
-            self.rendimento = el
-
-    def get_attr(self, attr):
-        if attr in self.rendimento.attributes.keys():
-            return self.rendimento.attributes[attr].nodeValue
-        return None
-
-    def set_attr(self, attr, val):
-        self.rendimento.setAttribute(attr, val)
-
-class RendimentosPJ:
+class RendimentosPJ(items.Items):
     def __init__(self, contribuinte):
-        self.contribuinte = contribuinte
-        self.rend_PJ = self.contribuinte.dados.getElementsByTagName("rendPJ")[0]
-        self.colecao = self.rend_PJ.getElementsByTagName("colecaoRendPJTitular")[0]
-        self.items = []
-
-        for i in self.colecao.getElementsByTagName("item"):
-            self.items.append(RendimentoPJ(i))
-
-    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_colecao(self, attr):
-        return self._get_attr(self.colecao, attr)
-
-    def set_colecao(self, attr, val):
-        self._set_attr(self.colecao, attr, val)
-
-    def add_item(self, item):
-        self.items.append(item)
-        self.colecao.appendChild(item.rendimento)
-
-    def new_item(self):
-        item = RendimentoPJ(self.colecao.ownerDocument.createElement("item"))
-        self.add_item(item)
-        return item
-
-    def remove_item(self, i):
-        self.items.pop(i)
-        els = self.colecao.getElementsByTagName("item")
-        self.colecao.removeChild(els[i])
+        rend_PJ = contribuinte.dados.getElementsByTagName("rendPJ")[0]
+        colecao = rend_PJ.getElementsByTagName("colecaoRendPJTitular")[0]
+        items.Items.__init__(self, colecao)
 
 if __name__ == '__main__':
     import sys
@@ -76,8 +30,8 @@ if __name__ == '__main__':
 
     contribuinte = Contribuinte(sys.argv[1])
     rendimentos = RendimentosPJ(contribuinte)
-    print "maior fonte pagadora: " + rendimentos.get_colecao("niMaiorFontePagadora")
-    print "total rendimentos: " + rendimentos.get_colecao("totaisRendRecebidoPJ")
+    print "maior fonte pagadora: " + rendimentos.get_attr("niMaiorFontePagadora")
+    print "total rendimentos: " + rendimentos.get_attr("totaisRendRecebidoPJ")
 
     for i in rendimentos.items:
         print i.get_attr("nomeFontePagadora") + " " + i.get_attr("rendRecebidoPJ")
index 5ebe162..565b625 100644 (file)
@@ -17,8 +17,8 @@ class TestBens(unittest.TestCase):
         self.bens = Bens(self.contribuinte)
 
     def test_GetBens(self):
-        self.assertEqual(self.bens.get_bens('totalExercicioAnterior'), '5.200,00')
-        self.assertEqual(self.bens.get_bens('totalExercicioAtual'), '0,00')
+        self.assertEqual(self.bens.get_attr('totalExercicioAnterior'), '5.200,00')
+        self.assertEqual(self.bens.get_attr('totalExercicioAtual'), '0,00')
 
 if __name__ == '__main__':
     unittest.main()