2014-12-15 18:22:46 +00:00
|
|
|
from xml.dom.minidom import parse
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
from core.xml.xmlparser0 import CoreDocumentParser0
|
|
|
|
from core.xml.xmlparser1 import CoreDocumentParser1
|
|
|
|
from core.xml.xmlutils import get_first_child_by_tag_name
|
2015-05-22 01:53:15 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
class CoreVersionParser(object):
|
|
|
|
"""
|
2014-12-15 18:22:53 +00:00
|
|
|
Helper class to check the version of Network Plan document. This
|
|
|
|
simply looks for a "Scenario" element; when present, this
|
2015-02-05 00:15:59 +00:00
|
|
|
indicates a 0.0 version document. The dom member is set in order
|
2014-12-15 18:22:53 +00:00
|
|
|
to prevent parsing a file twice (it can be passed to the
|
|
|
|
appropriate CoreDocumentParser class.)
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
DEFAULT_SCENARIO_VERSION = '1.0'
|
|
|
|
|
|
|
|
def __init__(self, filename, options):
|
2014-12-15 18:22:53 +00:00
|
|
|
if 'dom' in options:
|
|
|
|
self.dom = options['dom']
|
2014-12-15 18:22:46 +00:00
|
|
|
else:
|
2014-12-15 18:22:53 +00:00
|
|
|
self.dom = parse(filename)
|
2017-04-25 16:45:34 +01:00
|
|
|
scenario = get_first_child_by_tag_name(self.dom, 'scenario')
|
2015-05-22 01:53:15 +01:00
|
|
|
if scenario:
|
|
|
|
version = scenario.getAttribute('version')
|
|
|
|
if not version:
|
|
|
|
version = self.DEFAULT_SCENARIO_VERSION
|
|
|
|
self.version = version
|
2017-04-25 16:45:34 +01:00
|
|
|
elif get_first_child_by_tag_name(self.dom, 'Scenario'):
|
2015-05-22 01:53:15 +01:00
|
|
|
self.version = '0.0'
|
2014-12-15 18:22:53 +00:00
|
|
|
else:
|
|
|
|
self.version = 'unknown'
|
2014-12-15 18:22:46 +00:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2014-12-15 18:22:53 +00:00
|
|
|
def core_document_parser(session, filename, options):
|
2017-08-18 18:38:27 +01:00
|
|
|
"""
|
|
|
|
Retrieves the xml document parser.
|
|
|
|
|
|
|
|
:param core.session.Session session: core
|
|
|
|
:param str filename: name of file to save to or load from
|
|
|
|
:param dict options: parsing options
|
|
|
|
:return: xml document parser
|
|
|
|
"""
|
2014-12-15 18:22:53 +00:00
|
|
|
vp = CoreVersionParser(filename, options)
|
|
|
|
if 'dom' not in options:
|
|
|
|
options['dom'] = vp.dom
|
2015-05-22 01:53:15 +01:00
|
|
|
if vp.version == '0.0':
|
2015-02-05 00:15:59 +00:00
|
|
|
doc = CoreDocumentParser0(session, filename, options)
|
2015-05-22 01:53:15 +01:00
|
|
|
elif vp.version == '1.0':
|
|
|
|
doc = CoreDocumentParser1(session, filename, options)
|
2014-12-15 18:22:53 +00:00
|
|
|
else:
|
2017-07-27 22:17:33 +01:00
|
|
|
raise ValueError('unsupported document version: %s' % vp.version)
|
2014-12-15 18:22:53 +00:00
|
|
|
return doc
|