Introduce nemubot v3.2
- New licence: AGPL3 instead of GPL3 - Import is now based on finder and loader instead of sys.path - Modules used hooks to treat message instead of treating all messages - Remove a lot of builtins from the prompt - Prompt: ^C and ^D have now correct feature (nothing and exit)
This commit is contained in:
parent
a2b273d09b
commit
e4d4e68c45
11 changed files with 973 additions and 186 deletions
74
xmlparser/__init__.py
Normal file
74
xmlparser/__init__.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
||||
# Copyright (C) 2012 Mercier Pierre-Olivier
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import imp
|
||||
import xml.sax
|
||||
|
||||
from . import node as module_state
|
||||
|
||||
class ModuleStatesFile(xml.sax.ContentHandler):
|
||||
def startDocument(self):
|
||||
self.root = None
|
||||
self.stack = list()
|
||||
|
||||
def startElement(self, name, attrs):
|
||||
cur = module_state.ModuleState(name)
|
||||
|
||||
for name in attrs.keys():
|
||||
cur.setAttribute(name, attrs.getValue(name))
|
||||
|
||||
self.stack.append(cur)
|
||||
|
||||
def characters(self, content):
|
||||
self.stack[len(self.stack)-1].content += content
|
||||
|
||||
def endElement(self, name):
|
||||
child = self.stack.pop()
|
||||
size = len(self.stack)
|
||||
if size > 0:
|
||||
self.stack[size - 1].content = self.stack[size - 1].content.strip()
|
||||
self.stack[size - 1].addChild(child)
|
||||
else:
|
||||
self.root = child
|
||||
|
||||
def parse_file(filename):
|
||||
parser = xml.sax.make_parser()
|
||||
mod = ModuleStatesFile()
|
||||
parser.setContentHandler(mod)
|
||||
try:
|
||||
parser.parse(open(filename, "r"))
|
||||
return mod.root
|
||||
except IOError:
|
||||
return module_state.ModuleState("nemubotstate")
|
||||
except:
|
||||
if mod.root is None:
|
||||
return module_state.ModuleState("nemubotstate")
|
||||
else:
|
||||
return mod.root
|
||||
|
||||
def parse_string(string):
|
||||
mod = ModuleStatesFile()
|
||||
try:
|
||||
xml.sax.parseString(string, mod)
|
||||
return mod.root
|
||||
except:
|
||||
if mod.root is None:
|
||||
return module_state.ModuleState("nemubotstate")
|
||||
else:
|
||||
return mod.root
|
||||
170
xmlparser/node.py
Normal file
170
xmlparser/node.py
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
# coding=utf-8
|
||||
|
||||
import xml.sax
|
||||
from datetime import datetime
|
||||
from datetime import date
|
||||
import time
|
||||
|
||||
class ModuleState:
|
||||
"""Tiny tree representation of an XML file"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.content = ""
|
||||
self.attributes = dict()
|
||||
self.childs = list()
|
||||
self.index = dict()
|
||||
self.index_fieldname = None
|
||||
self.index_tagname = None
|
||||
|
||||
def getName(self):
|
||||
"""Get the name of the current node"""
|
||||
return self.name
|
||||
|
||||
def display(self, level = 0):
|
||||
ret = ""
|
||||
out = list()
|
||||
for k in self.attributes:
|
||||
out.append("%s : %s" % (k, self.attributes[k]))
|
||||
ret += "%s%s { %s } = '%s'\n" % (' ' * level, self.name, ' ; '.join(out), self.content)
|
||||
for c in self.childs:
|
||||
ret += c.display(level + 2)
|
||||
return ret
|
||||
|
||||
def __str__(self):
|
||||
return self.display()
|
||||
|
||||
def __getitem__(self, i):
|
||||
"""Return the attribute asked"""
|
||||
return self.getAttribute(i)
|
||||
|
||||
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]
|
||||
else:
|
||||
try:
|
||||
return datetime.fromtimestamp(float(self.attributes[name]))
|
||||
except ValueError:
|
||||
while True:
|
||||
try:
|
||||
return datetime.fromtimestamp(time.mktime(time.strptime(self.attributes[name][:19], "%Y-%m-%d %H:%M:%S")))
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
return None
|
||||
|
||||
def getInt(self, name):
|
||||
"""Get the asked argument and return it as an integer"""
|
||||
if name in self.attributes.keys():
|
||||
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 getContent(self):
|
||||
return self.content
|
||||
|
||||
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:
|
||||
ret = child
|
||||
return ret
|
||||
|
||||
def getFirstNode(self, tagname):
|
||||
"""Get a unique node (or the last one) with the given tagname"""
|
||||
for child in self.childs:
|
||||
if tagname is None or tagname == child.name:
|
||||
return child
|
||||
return None
|
||||
|
||||
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):
|
||||
attribs[att] = str(time.mktime(self.attributes[att].timetuple()))
|
||||
else:
|
||||
attribs[att] = str(self.attributes[att])
|
||||
attrs = xml.sax.xmlreader.AttributesImpl(attribs)
|
||||
|
||||
gen.startElement(self.name, attrs)
|
||||
|
||||
for child in self.childs:
|
||||
child.save_node(gen)
|
||||
|
||||
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()
|
||||
self.save_node(gen)
|
||||
gen.endDocument()
|
||||
Loading…
Add table
Add a link
Reference in a new issue