From: Thadeu Lima de Souza Cascardo Date: Sat, 15 Mar 2014 00:50:37 +0000 (-0300) Subject: Adicionada abstração de items. X-Git-Url: http://git.cascardo.info/?p=cascardo%2Firpf-gui.git;a=commitdiff_plain;h=5217d1fcdb67426d3def8a0e04fd9b992debf8e1 Adicionada abstração de items. Items são utilizados em muitos lugares na declaração. Esta classe permite criar mais facilmente novas classes para suportar novos elementos. --- diff --git a/src/items.py b/src/items.py new file mode 100644 index 0000000..03dd9a7 --- /dev/null +++ b/src/items.py @@ -0,0 +1,81 @@ +# coding=utf-8 +# +# 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 Item: + def __init__(self, el): + self.el = el + + def get_attr(self, attr): + if attr in self.el.attributes.keys(): + return self.el.attributes[attr].nodeValue + return None + + def set_attr(self, attr, val): + self.el.setAttribute(attr, val) + +class Items: + def __init__(self, element): + self.element = element + self.items = [] + + for i in self.element.getElementsByTagName("item"): + self.items.append(Item(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.setAttribute(attr, val) + + def get_attr(self, attr): + return self._get_attr(self.element, attr) + + def set_attr(self, attr, val): + self._set_attr(self.element, attr, val) + + def add_item(self, item): + self.items.append(item) + self.element.appendChild(item.el) + + def new_item(self): + item = Item(self.element.ownerDocument.createElement("item")) + self.add_item(item) + return item + + def remove_item(self, i): + self.items.pop(i) + els = self.element.getElementsByTagName("item") + self.element.removeChild(els[i]) + +if __name__ == '__main__': + doc = xml.dom.minidom.DOMImplementation().createDocument("", "doc", "") + el = doc.createElement("items") + + items = Items(el) + items.new_item().set_attr("value", "0,00") + items.set_attr("total", "0,00") + + print items.element.toprettyxml() + + for i in items.items: + print i.get_attr("value") + +# vim:tabstop=4:expandtab:smartindent diff --git a/test/test_items.py b/test/test_items.py new file mode 100644 index 0000000..35d4dae --- /dev/null +++ b/test/test_items.py @@ -0,0 +1,30 @@ +# -*- mode: python; encoding: utf-8; -*- +import unittest +import sys +import os + +import dirs +dirs.default_irpf_dir = dirs.IRPFTestDir() + +from items import Items +import xml.dom.minidom + +class TestItems(unittest.TestCase): + + def setUp(self): + doc = xml.dom.minidom.DOMImplementation().createDocument("", "doc", "") + el = doc.createElement("items") + + self.items = Items(el) + self.items.new_item().set_attr("value", "0,00") + self.items.set_attr("total", "9.999,99") + + def test_GetBens(self): + self.assertEqual(self.items.get_attr('total'), '9.999,99') + self.assertEqual(self.items.items[0].get_attr('value'), '0,00') + +if __name__ == '__main__': + unittest.main() + +# vim:tabstop=4:expandtab:smartindent:fenc=utf8 +