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