예제 #1
0
파일: rtdm.py 프로젝트: daniel12fsp/RTDM
def op_ins_del_rep(t1, t2):
    i = 0
    c1 = tree.get_children(t1)
    c2 = tree.get_children(t2)
    #Custos
    cd = ci = cr = 0
    if (not tree.equal(t1, t2)):
        ci += tree.length(t2)
        cd += tree.length(t1)
        cr += 1
        return ci, cd, cr

    while (i < len(c1) and i < len(c2)):

        node1 = get_elem(c1, i)
        node2 = get_elem(c2, i)
        ti, td, tr = op_ins_del_rep(node1, node2)
        ci += ti
        cd += td
        cr += tr
        i += 1

    if (i < len(c2)):
        for j in xrange(i, len(c2)):
            ci += tree.length(get_elem(c2, j))

    if (len(c1) > 0):
        for j in xrange(i, len(c1)):
            cd += tree.length(get_elem(c1, j))

    return ci, cd, cr
예제 #2
0
    def __call__(self, arg, check_children=None, normalize=False,
            as_string=False):

        check_children = check_children or False
        res = []
        if ET.iselement(arg):
            res.extend(check_node(self, arg))
        else:
            try:
                new_arg = get_elem(arg)
                res.extend(check_node(self, new_arg))
            except ValueError:
                # no element, so try to check the content
                try:
                    self.check_content(arg)
                except Exception as E:
                    res.append(E)

        if res:
            self.logger.debug('found %d errors', len(res))
            for e in res:
                self.logger.debug(' %s: %s', e.__class__.__name__, e.message)
            raise res[0]

        if normalize:
            if not hasattr(self, '_normalized_value'):
                raise self.error("%s has no normalized value" % self.name)
            else:
                return self._normalized_value
        else:
            return True
예제 #3
0
 def __init__(self, ch, elem=None):
     self._checker = ch
     if elem is None:
         elem = ch.dummy_element()
     else:
         elem = get_elem(elem)
     self._elem = elem
     self._checker(self._elem)
예제 #4
0
def load_checker(node, namespace=None):
    "takes an elementtree.element node and recreates the checker"
    node = get_elem(node)
    if node.tag not in LOAD_RULES:
        raise XCheckLoadError, "Cannot create checker for %s" % node.tag

    if namespace is None:
        namespace = {}

    new_atts = {}

    # Selection definition node uses delimiter, but selection check doesn't
    delimiter = node.get('delimiter', ',')

    for key in node.keys():
        if key == 'delimiter':
            if node.tag == 'list':
                val = delimiter

        val = node.get(key)

        if key=='values':
            val = map(str.strip, val.split(delimiter))

        if key in INT_ATTRIBUTES:
            val = num_or_inf(val, int)

        if key in BOOL_ATTRIBUTES:
            val = get_bool(val)

        if key in STR_OR_NONE_ATTRIBUTES:
            if val.lower() == 'none':
                val = None

        if key in ['min', 'max', 'min_value', 'max_value']:
            if node.tag == 'int':
                val = num_or_inf(val, int)
            elif node.tag == 'decimal':
                val = num_or_inf(val, float)

        if key in ['error']:
            if val in globals():
                _val = globals()[val]
            elif val in __builtins__:
                _val = __builtins__[val]
            elif val in namespace:
                _val = namespace[val]
            else:
                _val = UnmatchedError
            val = _val

        if key in ['callback']:
            if val in globals():
                _val = globals()[val]
            elif val in locals():
                _val = locals()[val]
            elif val in __builtins__:
                _val = __builtins__[val]
            elif callable(val):
                _val = val
            elif val in namespace:
                _val = namespace[val]
            else:
                try:
                    _val = eval(val)
                except Exception as E:
                    raise BadCallbackError(E)
            val = _val

        new_atts[key] = val

    ch = LOAD_RULES[node.tag](**new_atts)


    attributes = node.find('attributes')
    if attributes is not None:
        for att in attributes:
            ch.addattribute(load_checker(att, namespace))

    children = node.find('children')
    if children is not None:
        for child in children:
            ch.add_child(load_checker(child, namespace))

    return ch