def parse(text, parameters): try: # create XML object from xml string parsed_xml = etree.fromstring(text) except etree.XMLSyntaxError as error: Utilities.logErrors(Utilities.formatLxmlErrorMessage(error, text=text), parameters) sys.exit(1) # If the node has parameters, then add them to the global parameters dictionary parameters = Utilities.mergeDictionaries( nodeParametersToDictionary(parsed_xml), parameters) return parsed_xml, parameters
def parse(text, parameters): # @BUG If the command line flag --language is used may need to recompute dictionary # load cached language and grammar or create from scratch if needed grammar, dictionary = Utilities.cache( 'RoL-language-grammar', lambda: prepareGrammarEngine(parameters)) if parameters['Inputs']['RoL']['debug']['grammar']: print(grammar) # @NOTE could not pickle the language itself. Is there a way to solve this? # create the grammar language = parsley.makeGrammar( grammar, { 'xml': xml, 'xmlInfix': xmlInfix, 'dictionary': dictionary, 'miniLanguage': lambda x, y, z: miniLanguage(x, y, z, parameters) }) try: # parse the text against the grammar parsed_xml_text = ''.join(language(text).main()) except parsley.ParseError as error: Utilities.logErrors(Utilities.formatParsleyErrorMessage(error), parameters) sys.exit(1) try: # create XML object from xml string parsed_xml = etree.fromstring(parsed_xml_text) except etree.XMLSyntaxError as error: Utilities.logErrors(Utilities.formatLxmlErrorMessage(error), parameters) sys.exit(1) # If the node has parameters, then add them to the global parameters dictionary parameters = Utilities.mergeDictionaries( nodeParametersToDictionary(parsed_xml), parameters) # apply semantic changes parsed_xml = semanticTransformations(parsed_xml) return parsed_xml, parameters
def test_mergeDictionaries(self): self.assertEqual( Utilities.mergeDictionaries({'a': { 'b': '1' }}, {'a': { 'b': '1' }}), {'a': { 'b': '1' }}) self.assertEqual(Utilities.mergeDictionaries({'a': '1'}, {'b': '2'}), { 'a': '1', 'b': '2' }) self.assertEqual( Utilities.mergeDictionaries({ 'a': '1', 'b': '2' }, { 'a': '2', 'c': '3' }), { 'a': '1', 'b': '2', 'c': '3' }) self.assertEqual( Utilities.mergeDictionaries({ 'a': { 'b': '1', 'c': '3' }, 'b': '2' }, { 'a': { 'b': '2', 'f': '5' }, 'z': '9' }), { 'a': { 'b': '1', 'f': '5', 'c': '3' }, 'z': '9', 'b': '2' }) self.assertEqual( Utilities.mergeDictionaries( { 'a': { 'b': { 'x': '1', 'w': '6' }, 'c': '3' }, 'b': '2' }, { 'a': { 'b': { 'x': '2', 'y': '7' }, 'f': '5' }, 'z': '9' }), { 'a': { 'b': { 'x': '1', 'w': '6', 'y': '7' }, 'f': '5', 'c': '3' }, 'z': '9', 'b': '2' })