Exemple #1
0
def _cmp_values(base_op, first, second):
    if is_boolean(first) or is_boolean(second):
        return base_op(1 if boolean(first) else 0, 1 if boolean(second) else 0)
    elif is_number(first) or is_number(second):
        return base_op(number(first), number(second))
    else:
        return base_op(string(first), string(second))
Exemple #2
0
def equals(first, second):
    first_type = object_type(first)
    second_type = object_type(second)
    try:
        reverse = first_type > second_type
        op = equality_ops_table[first_type if not reverse else second_type][second_type if not reverse else first_type]
        return boolean(op(first if not reverse else second, second if not reverse else first))
    except TypeError:
        msg = 'type mismatch comparing {0} and {1} for equality'
        raise HqueryEvaluationError(msg.format(object_type_name(first_type), object_type_name(second_type)))
Exemple #3
0
def contains(*args):
    argc = len(args)
    if argc < 2 or argc > 3:
        raise HqueryEvaluationError('contains() function expects two or three arguments; {0} passed'.format(argc))
    if argc == 3:
        flags = args[2]
    else:
        flags = ''

    pattern = re.escape(string_value(args[1]))
    to_search = string_value(args[0])
    return boolean(bool(re.search(pattern, to_search, flags=_xpath_flags_to_re_flags(flags))))
Exemple #4
0
def class_(*args):
    if len(args) == 1:
        tag = get_context_node()
        name = args[0]
    elif len(args) == 2:
        HqueryEvaluationError.must_be_node_set(args[0])
        tag = args[0][0]
        name = args[1]
    else:
        raise HqueryEvaluationError('class() expects one or two arguments; got {0}'.format(len(args)))

    return boolean(name in tag['class'])
Exemple #5
0
def class_(*args):
    if len(args) == 1:
        tag = get_context_node()
        name = args[0]
    elif len(args) == 2:
        HqueryEvaluationError.must_be_node_set(args[0])
        tag = args[0][0]
        name = args[1]
    else:
        raise HqueryEvaluationError(
            'class() expects one or two arguments; got {0}'.format(len(args)))

    return boolean(name in tag['class'])
Exemple #6
0
def matches(*args):
    scenario = len(args)
    flags = 0

    if scenario < 1 or scenario > 3:
        raise HqueryEvaluationError('matches() called with {0} arguments; expected one, two or three.'.format(scenario))

    if scenario == 1:
        input = string_value(get_context_node())
        pattern = args[0]
    else:
        input = string_value(args[0])
        pattern = args[1]
        if scenario == 3:
            flags = _xpath_flags_to_re_flags(args[2])

    return boolean(re.search(pattern, input, flags))
Exemple #7
0
def matches(*args):
    scenario = len(args)
    flags = 0

    if scenario < 1 or scenario > 3:
        raise HqueryEvaluationError(
            'matches() called with {0} arguments; expected one, two or three.'.
            format(scenario))

    if scenario == 1:
        input = string_value(get_context_node())
        pattern = args[0]
    else:
        input = string_value(args[0])
        pattern = args[1]
        if scenario == 3:
            flags = _xpath_flags_to_re_flags(args[2])

    return boolean(re.search(pattern, input, flags))
Exemple #8
0
 def evaluate(self, first, second):
     first_type = object_type(first)
     second_type = object_type(second)
     cmp = comparison_method_table[first_type][second_type]
     return boolean(cmp(self.base_op, first, second))
Exemple #9
0
def starts_with(left, right):
    return boolean(string_value(left).startswith(string_value(right)))
Exemple #10
0
def odd():
    return boolean(peek_context().position % 2 == 1)
Exemple #11
0
def even():
    return boolean(peek_context().position % 2 == 0)
Exemple #12
0
def not_equals(first, second):
    return boolean(not bool(equals(first, second)))
Exemple #13
0
def _eq_bool_vs_primitive(bool_val, other_val):
    verbose_print('Comparing boolean value {0} with non-node-set value {1} (coerced to {2})'.format(bool_val, other_val, boolean(other_val)))
    return bool_val == boolean(other_val)
Exemple #14
0
def _eq_node_set_vs_bool(bool_val, nodes_val):
    return bool_val == boolean(nodes_val)
Exemple #15
0
def odd():
    return boolean(peek_context().position % 2 == 1)
Exemple #16
0
def even():
    return boolean(peek_context().position % 2 == 0)