Beispiel #1
0
 def get_rule_elements(self, rule):
     ref_elements = False
     for i in rule['import']:
         exec(i)
     if rule['elements']:
         pass # aleady contain elements...
     elif rule['elements_function']:
         exec('rule["elements"] = %s' % rule['elements_function'])
     else:
         rule['elements'] = rule['root'].xpath_query(rule['xpath'], rule['remove_tag_name'])
     if rule.get('ref_file_name', False):
         ref_elements = True
         if not rule.has_key('ref_xfile'):
             rule['ref_xfile']=ElementTree.open_xfile(rule['ref_file_name'])
             rule['ref_root']=rule['ref_xfile'].getroot_extend()
     if rule['ref_elements']:
         ref_elements = True
         # Nothing to do here
     elif rule.get('ref_elements_function', False):
         ref_elements = True
         exec('rule["ref_elements"] = %s' % rule['ref_elements_function'])
     elif rule.has_key('ref_xpath'):
         rule['ref_elements'] = rule['ref_root'].xpath_query(rule['ref_xpath'], rule['ref_remove_tag_name'])
     if rule['required'] and not rule['elements']:
         raise Exception, u"No elements were found for:\n%s " % rule
     if rule['required'] and ref_elements and not rule['ref_elements']:
         raise Exception, u"No ref_elements were found for:\n%s" % rule
     if ref_elements and len(rule['ref_elements']) != 1:
         raise Exception, u"ref_elements must contain single element in action %s, rule:\n%s" % \
             (rule['action'],rule)
     elif ref_elements and not rule['ref_xpath']:
         raise Exception, u"ref_xpath is required when ref_elements exists !! \n%s" % rule
     elif ref_elements and rule['action'] == 'clone' and not rule.get('pre_clone_id_xpath', False) \
             and not rule.get('post_clone_id_xpath', False):
         raise Exception, u"both pre/post clone_id_xpath must exists !! \n%s" % rule
     elif ref_elements and len(rule['elements']) > 1:
         raise Exception, u"%s operation are limited to single element only !! \n%s" % (rule['action'], rule)
     return rule