Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from lxml import etree as ET
- import re, os
- class importArgs():
- """
- File for collecting the arguments for imports in the calc conversion tool
- """
- def __init__(self):
- self.args_list = [] # collected output lines
- self.returnVars = []
- def __iter__(self):
- return iter(self.args_list)
- def out(self, s):
- """Output the indented string to the output list."""
- self.args_list.append(s)
- def loop(self, elem):
- """Helper method to loop through the child elements."""
- for e in elem:
- self.parse(e)
- def parseImportXML(self, script, folder):
- """Reads the XML file and starts parsing from the root element."""
- try:
- tree = ET.parse(script)
- root = tree.getroot()
- self.parse(root, folder)
- root.clear()
- except IOError:
- print ("Error : Cannot parse script " + script)
- def parse(self, elem, folder):
- importList = [var for var in self.get_elem(elem, 'import')]
- for i in importList:
- self.parse_import(i, folder)
- def get_elem(self, elem, tag):
- """Return the element matching `tag`."""
- # If an element has any children loop them
- if len(elem):
- for e in elem:
- # Recursively call this func, yield each result:
- for tag in self.get_elem(e, tag):
- yield tag
- # Get the import element
- if elem.tag == 'import':
- yield elem
- def parse_import(self, elem, folder):
- '''
- Provides the import arguments and return values to the scripts during the calculator converter process
- @param elem: <Import> xml element
- @type elem: etree element
- @param folder: the path to the folder of the script this import applies to
- @type folder: string
- '''
- # need a check here to test if the folder is the same as the elem.attrib['path']
- print 'folder: {0} \n path: {1}'.format(folder, elem.attrib['path'])
- if folder == elem.attrib['path']:
- print 'matched import on line {0}'.format(unicode(elem.sourceline))
- a = elem.attrib
- argumentsTag = elem.find('arguments')
- returnTag = elem.find('return')
- path = a['path'].split('/')
- path = '{0}{1}'.format('etk.scripts.', '.'.join(path))
- year = re.findall(r"\.(\d{4})\.", path) # find a 4 digit folder name in the path directly following the '.'
- year = ''.join(year) # convert list to string
- path = re.sub(year, '{0}{1}'.format('yr_',year), path)
- self.out('import {0}'.format(path))
- self.out('calc = {path}.{method}()'.format(path= path, method= elem.attrib['stageID']))
- for arg in argumentsTag:
- # variables & values
- try:
- self.args_list.append(arg.attrib['id'])
- #else: print 'Problem or Comment in Args, {0}, on line {1}'.format(arg.text, unicode(elem.sourceline))
- except KeyError:
- print('KeyError on line {0} with: {1}'.format(arg.sourceline, arg.tag))
- for r in returnTag:
- # inside executeMethod return tag
- try: self.returnVars.append(r.attrib['id'])
- except KeyError:
- print('KeyError on line {0} with: {1}'.format(r.sourceline, r.tag))
- else:
- print 'not equal yet'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement