Commit b8ef41df authored by Gabriel Monnerat's avatar Gabriel Monnerat

Clean up the code to use OFS.XMLExportImport library to read XML instead if use a custom parser.

parent ccec16d1
...@@ -30,6 +30,9 @@ import tarfile ...@@ -30,6 +30,9 @@ import tarfile
import xml.parsers.expat import xml.parsers.expat
import xml.dom.minidom import xml.dom.minidom
from urllib import url2pathname from urllib import url2pathname
from ZODB.DemoStorage import DemoStorage
from ZODB import DB
from OFS.XMLExportImport import importXML
if int(os.environ.get('erp5_report_new_simulation_failures') or 0): if int(os.environ.get('erp5_report_new_simulation_failures') or 0):
newSimulationExpectedFailure = lambda test: test newSimulationExpectedFailure = lambda test: test
...@@ -98,64 +101,25 @@ class BusinessTemplateInfoBase: ...@@ -98,64 +101,25 @@ class BusinessTemplateInfoBase:
self.allowed_content_types[portal_type].append(item.childNodes[0].data) self.allowed_content_types[portal_type].append(item.childNodes[0].data)
def setUpActions(self): def setUpActions(self):
class Handler:
cur_key = None def parse(file_path):
old_tag = None db = DB(DemoStorage())
cur_tag = None _connection = db.open()
key_val = None action_information = importXML(_connection, file_path)
value_val = None action_information.__repr__()
for key, value in action_information.__dict__.iteritems():
def __init__(self): if value not in (None, "") and key in ('action', 'condition') :
self.data = {} setattr(action_information, key, value.text)
actions = action_information.__dict__.copy()
def start(self, name, attrs): db.close()
if not name in ('item', 'key', 'value', 'string', 'int', 'float'): return actions
return
self.old_tag = self.cur_tag
self.cur_tag = name
if name=='key':
self.cur_key = name
def end(self, name):
self.cur_tag = None
if name=='item':
self.data[self.key_val] = self.value_val
self.cur_key = None
self.key_val = None
self.value_val = None
def char(self, data):
if self.cur_tag in ('string', 'int', 'float'):
f = getattr(self, 'to%s' % self.cur_tag)
if self.old_tag=='key':
self.key_val = f(data)
elif self.old_tag=='value':
self.value_val = f(data)
def tostring(self, value):
return str(value)
def toint(self, value):
return int(value)
def tofloat(self, value):
return float(value)
def parse(source):
handler = Handler()
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = handler.start
p.EndElementHandler = handler.end
p.CharacterDataHandler = handler.char
p.Parse(source)
return handler.data
name = '%s/ActionTemplateItem/portal_types/' % self.getPrefix() name = '%s/ActionTemplateItem/portal_types/' % self.getPrefix()
for i in self.findFileInfosByName(startswith=name, endswith='.xml'): for i in self.findFileInfosByName(startswith=name, endswith='.xml'):
portal_type = url2pathname(self.getFileInfoName(i).split('/')[-2]) portal_type = url2pathname(self.getFileInfoName(i).split('/')[-2])
if not portal_type in self.actions: if not portal_type in self.actions:
self.actions[portal_type] = [] self.actions[portal_type] = []
data = parse(self.readFileInfo(i)) data = parse(i)
self.actions[portal_type].append(data) self.actions[portal_type].append(data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment