Package ajango :: Package core :: Module XMLReader
[hide private]
[frames] | no frames]

Source Code for Module ajango.core.XMLReader

 1  ########################################################################### 
 2  #                                                                         # 
 3  #  Copyright (C) 2016  Rafal Kobel <rafyco1@gmail.com>                    # 
 4  #                                                                         # 
 5  #  This program is free software: you can redistribute it and/or modify   # 
 6  #  it under the terms of the GNU General Public License as published by   # 
 7  #  the Free Software Foundation, either version 3 of the License, or      # 
 8  #  (at your option) any later version.                                    # 
 9  #                                                                         # 
10  #  This program is distributed in the hope that it will be useful,        # 
11  #  but WITHOUT ANY WARRANTY; without even the implied warranty of         # 
12  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           # 
13  #  GNU General Public License for more details.                           # 
14  #                                                                         # 
15  #  You should have received a copy of the GNU General Public License      # 
16  #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  # 
17  #                                                                         # 
18  ########################################################################### 
19  """ Modul parsowania xml. """ 
20   
21  from __future__                   import print_function 
22  from xml.dom                      import minidom 
23  from django.core.management.base  import CommandError 
24  from django.utils.termcolors      import make_style 
25  from django.core.management.color import supports_color 
26  import abc 
27 28 -class XMLReader(object):
29 """ Klasa obiektu parsowania XML. """ 30 __metaclass__ = abc.ABCMeta
31 - def __init__(self, xmldoc, param=None):
32 self.xml_xmldoc = xmldoc 33 self.add_permited([]) 34 self.xml_name = "" 35 self.object = param 36 self.init() 37 self.render() 38 self.xml_permited = []
39 - def init(self):
40 """ Modul inicjalizujacy. """ 41 pass
42 - def add_permited(self, permited):
43 """ Dodanie tagow ktore moga byc parsowane wewnatrz elementu. """ 44 try: 45 self.xml_permited += permited 46 except AttributeError: 47 self.xml_permited = permited
48 - def get_xml_doc(self):
49 """ Pobranie xmldoc. """ 50 return self.xml_xmldoc
51 # pylint: disable=C0103
52 - def getAttribute(self, name):
53 """ Pobranie atrybutu dla noda. """ 54 return self.xml_xmldoc.getAttribute(name)
55 - def _is_permited(self, name):
56 """ Sprawdzenie czy element moze byc parsowany. """ 57 if self.xml_permited == []: 58 return True 59 for elem in self.xml_permited: 60 if elem.upper() == name.upper(): 61 return True 62 return False
63 - def pre_render(self):
64 """ Czynnosci do wykonania przez inicjalizacja. """ 65 pass
66 - def post_render(self):
67 """ Czynnosci do wykonania po inicjalizacji. """ 68 pass
69 - def render(self):
70 """ Renderowanie elementow. """ 71 if supports_color(): 72 yellow = make_style(fg='yellow') 73 else: 74 yellow = lambda text: text 75 print("Rendering '" + yellow(self.xml_xmldoc.tagName.upper()) + "'") 76 if (self.xml_name != "") and (self.xml_name.upper() != 77 self.get_xml_doc().tagName.upper()): 78 raise CommandError("Cannot create object from %r" % 79 self.get_xml_doc().tagName) 80 self.pre_render() 81 for elem in self.get_xml_doc().childNodes: 82 if isinstance(elem, minidom.Element): 83 if self._is_permited(elem.tagName): 84 self.check(elem.tagName.upper(), elem) 85 else: 86 raise CommandError("Unknown element: %r" % elem.tagName) 87 self.post_render()
88 @abc.abstractmethod
89 - def check(self, name, xmldoc_elem):
90 """ Oczytanie nodow wewnetrznych. """ 91 raise CommandError("XMLReader must have check method")
92