Add comments, fix small bugs in module_state
This commit is contained in:
parent
5fed225358
commit
ba71834b47
@ -6,6 +6,8 @@ from datetime import date
|
||||
import time
|
||||
|
||||
class ModuleState:
|
||||
"""Tiny tree representation of an XML file"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.attributes = dict()
|
||||
@ -15,21 +17,26 @@ class ModuleState:
|
||||
self.index_tagname = None
|
||||
|
||||
def getName(self):
|
||||
"""Get the name of the current node"""
|
||||
return self.name
|
||||
|
||||
def __getitem__(self, i):
|
||||
"""Return the attribute asked"""
|
||||
return self.getAttribute(i)
|
||||
|
||||
def __contains__(self, i):
|
||||
return i in self.index
|
||||
def __setitem__(self, i, c):
|
||||
"""Set the attribute"""
|
||||
return self.setAttribute(i, c)
|
||||
|
||||
def getAttribute(self, name):
|
||||
"""Get the asked argument or return None if doesn't exist"""
|
||||
if name in self.attributes:
|
||||
return self.attributes[name]
|
||||
else:
|
||||
return None
|
||||
|
||||
def getDate(self, name):
|
||||
"""Get the asked argument and return it as a date"""
|
||||
if name in self.attributes.keys():
|
||||
if isinstance(self.attributes[name], datetime):
|
||||
return self.attributes[name]
|
||||
@ -39,28 +46,39 @@ class ModuleState:
|
||||
return None
|
||||
|
||||
def getInt(self, name):
|
||||
"""Get the asked argument and return it as an integer"""
|
||||
if name in self.attributes.keys():
|
||||
return int(self.attributes[name])
|
||||
return int(float(self.attributes[name]))
|
||||
else:
|
||||
return None
|
||||
|
||||
def setIndex(self, fieldname = "name", tagname = None):
|
||||
"""Defines an hash table to accelerate childs search. You have just to define a common attribute"""
|
||||
self.index = dict()
|
||||
self.index_fieldname = fieldname
|
||||
self.index_tagname = tagname
|
||||
for child in self.childs:
|
||||
if (tagname is None or tagname == child.name) and child.hasAttribute(fieldname):
|
||||
self.index[child[fieldname]] = child
|
||||
|
||||
def __contains__(self, i):
|
||||
"""Return true if i is found in the index"""
|
||||
return i in self.index
|
||||
|
||||
def hasAttribute(self, name):
|
||||
"""DOM like method"""
|
||||
return (name in self.attributes)
|
||||
|
||||
def setAttribute(self, name, value):
|
||||
"""DOM like method"""
|
||||
self.attributes[name] = value
|
||||
|
||||
def getChilds(self):
|
||||
"""Return a full list of direct child of this node"""
|
||||
return self.childs
|
||||
|
||||
def getNode(self, tagname):
|
||||
"""Get a unique node (or the last one) with the given tagname"""
|
||||
ret = None
|
||||
for child in self.childs:
|
||||
if tagname is None or tagname == child.name:
|
||||
@ -68,18 +86,35 @@ class ModuleState:
|
||||
return ret
|
||||
|
||||
def getNodes(self, tagname):
|
||||
"""Get all direct childs that have the given tagname"""
|
||||
ret = list()
|
||||
for child in self.childs:
|
||||
if tagname is None or tagname == child.name:
|
||||
ret.append(child)
|
||||
return ret
|
||||
|
||||
def hasNode(self, tagname):
|
||||
"""Return True if at least one node with the given tagname exists"""
|
||||
ret = list()
|
||||
for child in self.childs:
|
||||
if tagname is None or tagname == child.name:
|
||||
return True
|
||||
return False
|
||||
|
||||
def addChild(self, child):
|
||||
"""Add a child to this node"""
|
||||
self.childs.append(child)
|
||||
if self.index_fieldname is not None:
|
||||
self.setIndex(self.index_fieldname, self.index_tagname)
|
||||
|
||||
def delChild(self, child):
|
||||
"""Remove the given child from this node"""
|
||||
self.childs.remove(child)
|
||||
if self.index_fieldname is not None:
|
||||
self.setIndex(self.index_fieldname, self.index_tagname)
|
||||
|
||||
def save_node(self, gen):
|
||||
"""Serialize this node as a XML node"""
|
||||
attribs = {}
|
||||
for att in self.attributes.keys():
|
||||
if isinstance(self.attributes[att], datetime):
|
||||
@ -96,6 +131,7 @@ class ModuleState:
|
||||
gen.endElement(self.name)
|
||||
|
||||
def save(self, filename):
|
||||
"""Save the current node as root node in a XML file"""
|
||||
with open(filename,"w") as f:
|
||||
gen = xml.sax.saxutils.XMLGenerator(f, "utf-8")
|
||||
gen.startDocument()
|
||||
|
@ -2,10 +2,11 @@
|
||||
# coding=utf-8
|
||||
|
||||
import os
|
||||
import imp
|
||||
import xml.sax
|
||||
|
||||
from module_exception import ModuleException
|
||||
from module_state import ModuleState
|
||||
module_state = __import__("module_state")
|
||||
imp.reload(module_state)
|
||||
|
||||
class ModuleStatesFile(xml.sax.ContentHandler):
|
||||
def startDocument(self):
|
||||
@ -13,7 +14,7 @@ class ModuleStatesFile(xml.sax.ContentHandler):
|
||||
self.stack = list()
|
||||
|
||||
def startElement(self, name, attrs):
|
||||
cur = ModuleState(name)
|
||||
cur = module_state.ModuleState(name)
|
||||
|
||||
for name in attrs.keys():
|
||||
cur.setAttribute(name, attrs.getValue(name))
|
||||
@ -36,4 +37,4 @@ def parse_file(filename):
|
||||
parser.parse(open(filename, "r"))
|
||||
return mod.root
|
||||
except:
|
||||
return ModuleState("nemubotstate")
|
||||
return module_state.ModuleState("nemubotstate")
|
||||
|
Loading…
Reference in New Issue
Block a user