Advertisement
marksweb

ImportArgs

Jun 12th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. from lxml import etree as ET
  2. import re, os
  3.  
  4. class importArgs():
  5.     """
  6.     File for collecting the arguments for imports in the calc conversion tool
  7.     """
  8.  
  9.     def __init__(self):
  10.         self.args_list = []  # collected output lines
  11.         self.returnVars = []
  12.  
  13.     def __iter__(self):
  14.         return iter(self.args_list)
  15.  
  16.     def out(self, s):
  17.         """Output the indented string to the output list."""
  18.         self.args_list.append(s)
  19.  
  20.     def loop(self, elem):
  21.         """Helper method to loop through the child elements."""
  22.         for e in elem:
  23.             self.parse(e)
  24.  
  25.     def parseImportXML(self, script, folder):
  26.         """Reads the XML file and starts parsing from the root element."""
  27.         try:
  28.             tree = ET.parse(script)
  29.             root = tree.getroot()
  30.             self.parse(root, folder)
  31.             root.clear()
  32.         except IOError:
  33.             print ("Error : Cannot parse script " + script)
  34.  
  35.     def parse(self, elem, folder):
  36.         importList = [var for var in self.get_elem(elem, 'import')]
  37.         for i in importList:
  38.             self.parse_import(i, folder)
  39.  
  40.     def get_elem(self, elem, tag):
  41.         """Return the element matching `tag`."""
  42.  
  43.         # If an element has any children loop them
  44.         if len(elem):
  45.             for e in elem:
  46.                 # Recursively call this func, yield each result:
  47.                 for tag in self.get_elem(e, tag):
  48.                     yield tag
  49.         # Get the import element
  50.         if elem.tag == 'import':
  51.             yield elem
  52.  
  53.     def parse_import(self, elem, folder):
  54.         '''
  55.         Provides the import arguments and return values to the scripts during the calculator converter process
  56.         @param elem: <Import> xml element
  57.         @type elem: etree element
  58.         @param folder: the path to the folder of the script this import applies to
  59.         @type folder: string
  60.         '''
  61.         # need a check here to test if the folder is the same as the elem.attrib['path']
  62.         print 'folder: {0} \n path: {1}'.format(folder, elem.attrib['path'])
  63.         if folder == elem.attrib['path']:
  64.             print 'matched import on line {0}'.format(unicode(elem.sourceline))
  65.             a = elem.attrib
  66.             argumentsTag = elem.find('arguments')
  67.             returnTag = elem.find('return')
  68.  
  69.             path = a['path'].split('/')
  70.             path = '{0}{1}'.format('etk.scripts.', '.'.join(path))
  71.             year = re.findall(r"\.(\d{4})\.", path) # find a 4 digit folder name in the path directly following the '.'
  72.             year = ''.join(year)    # convert list to string
  73.             path = re.sub(year, '{0}{1}'.format('yr_',year), path)
  74.  
  75.             self.out('import {0}'.format(path))
  76.             self.out('calc = {path}.{method}()'.format(path= path, method= elem.attrib['stageID']))
  77.  
  78.             for arg in argumentsTag:
  79.                 # variables & values
  80.                 try:
  81.                     self.args_list.append(arg.attrib['id'])
  82.                 #else: print 'Problem or Comment in Args, {0}, on line {1}'.format(arg.text, unicode(elem.sourceline))
  83.                 except KeyError:
  84.                     print('KeyError on line {0} with: {1}'.format(arg.sourceline, arg.tag))
  85.  
  86.             for r in returnTag:
  87.                 # inside executeMethod return tag
  88.                 try: self.returnVars.append(r.attrib['id'])
  89.                 except KeyError:
  90.                     print('KeyError on line {0} with: {1}'.format(r.sourceline, r.tag))
  91.         else:
  92.             print 'not equal yet'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement