2012-08-14 03:51:55 +00:00
|
|
|
# -*- 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/>.
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-11-02 02:25:24 +00:00
|
|
|
import logging
|
2012-05-30 09:58:27 +00:00
|
|
|
import xml.sax
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
from . import node as module_state
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-12-06 08:00:53 +00:00
|
|
|
logger = logging.getLogger("nemubot.tools.xmlparser")
|
2014-11-02 02:25:24 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2012-05-30 09:58:27 +00:00
|
|
|
class ModuleStatesFile(xml.sax.ContentHandler):
|
2014-11-09 13:11:54 +00:00
|
|
|
def startDocument(self):
|
|
|
|
self.root = None
|
|
|
|
self.stack = list()
|
|
|
|
|
|
|
|
def startElement(self, name, attrs):
|
|
|
|
cur = module_state.ModuleState(name)
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
for name in attrs.keys():
|
|
|
|
cur.setAttribute(name, attrs.getValue(name))
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
self.stack.append(cur)
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
def characters(self, content):
|
|
|
|
self.stack[len(self.stack)-1].content += content
|
2012-05-30 09:58:27 +00:00
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
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
|
2012-06-30 14:38:39 +00:00
|
|
|
|
2012-05-30 09:58:27 +00:00
|
|
|
|
|
|
|
def parse_file(filename):
|
2014-11-09 13:11:54 +00:00
|
|
|
parser = xml.sax.make_parser()
|
|
|
|
mod = ModuleStatesFile()
|
|
|
|
parser.setContentHandler(mod)
|
|
|
|
try:
|
|
|
|
parser.parse(open(filename, "r"))
|
|
|
|
return mod.root
|
|
|
|
except IOError:
|
|
|
|
logger.exception("error occurs during XML parsing of %s", filename)
|
|
|
|
return module_state.ModuleState("nemubotstate")
|
|
|
|
except:
|
|
|
|
logger.exception("error occurs during XML parsing of %s", filename)
|
|
|
|
if mod.root is None:
|
|
|
|
return module_state.ModuleState("nemubotstate")
|
|
|
|
else:
|
|
|
|
return mod.root
|
|
|
|
|
2012-06-30 14:38:39 +00:00
|
|
|
|
|
|
|
def parse_string(string):
|
2014-11-09 13:11:54 +00:00
|
|
|
mod = ModuleStatesFile()
|
|
|
|
try:
|
|
|
|
xml.sax.parseString(string, mod)
|
|
|
|
return mod.root
|
|
|
|
except:
|
|
|
|
logger.exception("error occurs during XML parsing")
|
|
|
|
if mod.root is None:
|
|
|
|
return module_state.ModuleState("nemubotstate")
|
|
|
|
else:
|
|
|
|
return mod.root
|