Example #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))
Example #2
0
    def evaluate(self):
        result = dict()

        for item in make_sequence(
                self.contents()) if self.contents is not None else []:
            if isinstance(item, HashKeyValue):
                if is_sequence(item.value) and len(item.value) == 1:
                    item.value = item.value[0]

                if is_number(item.value) or is_boolean(item.value):
                    result[item.key] = item.value.value
                elif is_hash(item.value) or is_array(item.value):
                    result[item.key] = item.value.contents
                else:
                    result[item.key] = string_value(item.value)
            elif is_tag_node(item):
                self._gab('adding element "{0}" to contents'.format(item.name))
                self._process_tag(result, item)
            elif is_text_node(item) or is_string(item):
                self._gab('adding text "{0}" to contents'.format(
                    debug_dump_long_string(string_value(item))))
                result['text'] = self._append_to_text(
                    result['text'] if 'text' in result else '',
                    string_value(item))
            else:
                value_desc = debug_dump_node(item) if is_any_node(
                    item) else object_type_name(item)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed JSON hash constructor'
                    .format(value_desc))

            self._process_filters(result)

        return JsonHash(result)
Example #3
0
    def evaluate(self):
        result = dict()

        for item in make_sequence(self.contents()) if self.contents is not None else []:
            if isinstance(item, HashKeyValue):
                if is_sequence(item.value) and len(item.value) == 1:
                    item.value = item.value[0]

                if is_number(item.value) or is_boolean(item.value):
                    result[item.key] = item.value.value
                elif is_hash(item.value) or is_array(item.value):
                    result[item.key] = item.value.contents
                else:
                    result[item.key] = string_value(item.value)
            elif is_tag_node(item):
                self._gab('adding element "{0}" to contents'.format(item.name))
                self._process_tag(result, item)
            elif is_text_node(item) or is_string(item):
                self._gab('adding text "{0}" to contents'.format(debug_dump_long_string(string_value(item))))
                result['text'] = self._append_to_text(result['text'] if 'text' in result else '', string_value(item))
            else:
                value_desc = debug_dump_node(item) if is_any_node(item) else object_type_name(item)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed JSON hash constructor'.format(value_desc)
                )

            self._process_filters(result)

        return JsonHash(result)
Example #4
0
 def __init__(self, obj):
     if is_node_set(obj):
         self.value = len(obj) > 0
     elif is_number(obj):
         f = float(obj)
         self.value = bool(f) and not isnan(f)
     else:
         self.value = bool(obj)
Example #5
0
 def __init__(self, obj):
     if is_node_set(obj):
         self.value = len(obj) > 0
     elif is_number(obj):
         f = float(obj)
         self.value = bool(f) and not isnan(f)
     else:
         self.value = bool(obj)
Example #6
0
            def accept_context_node():
                context = peek_context()

                format_str = u'Evaluating predicate expression for context node at position {0} of {1}: {2}.'
                verbose_print(lambda: format_str.format(context.position, context.size, debug_dump_node(context.node)))

                value = expression_fn()
                if is_number(value):
                    accept = number(context.position) == value
                else:
                    accept = bool(value)

                verbose_print(lambda: u'{0} node {1}'.format('Accepted' if accept else 'Rejected',
                                                             debug_dump_node(context.node)))
                return [context.node] if accept else []
Example #7
0
    def evaluate(self):
        result = ''

        for value in make_sequence(self.contents()) if self.contents is not None else []:
            if is_string(value) or is_number(value) or is_boolean(value):
                result = self._append_to_contents(result, str(value))
            elif is_attribute_node(value):
                result = self._append_to_contents(result, value.value)
            elif is_tag_node(value):
                result = self._append_to_contents(result, string_value(value))
            else:
                value_desc = debug_dump_node(value) if is_any_node(value) else object_type_name(value)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed attribute constructor'.format(value_desc)
                )

        return AttributeNode(self.name, result)
Example #8
0
 def _make_array_item(self, value):
     if is_tag_node(value):
         self._gab(lambda: 'appending text contents of element "{0}" to array'.format(debug_dump_anything(value)))
         return string_value(value)
     elif is_text_node(value) or is_string(value):
         value = string_value(value)
         self._gab(lambda: u'appending text "{0}" to array'.format(debug_dump_anything(value)))
         return value
     elif is_boolean(value) or is_number(value):
         self._gab(lambda: 'appending {0} to array'.format(debug_dump_anything(value)))
         return value.value
     elif is_hash(value):
         self._gab(lambda: u'appending JSON {0} to array'.format(debug_dump_anything(value)))
         return value.contents
     else:
         raise HqueryEvaluationError("Can't use {0} as contents in a computed JSON array constructor".format(
             debug_dump_anything(value)))
Example #9
0
    def evaluate(self):
        soup = BeautifulSoup('<{0}></{0}>'.format(self.name), 'html.parser')
        result = getattr(soup, self.name)

        for value in make_sequence(self.contents()) if self.contents is not None else []:
            if is_tag_node(value):
                result.append(self._clone_tag(value))
            elif is_attribute_node(value):
                result[value.name] = value.value
            elif is_string(value) or is_number(value) or is_boolean(value):
                result.append(str(value))
            else:
                value_desc = debug_dump_node(value) if is_any_node(value) else object_type_name(value)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed element constructor'.format(value_desc)
                )

        return make_node_set(result)
Example #10
0
    def evaluate(self):
        result = ''

        for value in make_sequence(
                self.contents()) if self.contents is not None else []:
            if is_string(value) or is_number(value) or is_boolean(value):
                result = self._append_to_contents(result, str(value))
            elif is_attribute_node(value):
                result = self._append_to_contents(result, value.value)
            elif is_tag_node(value):
                result = self._append_to_contents(result, string_value(value))
            else:
                value_desc = debug_dump_node(value) if is_any_node(
                    value) else object_type_name(value)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed attribute constructor'
                    .format(value_desc))

        return AttributeNode(self.name, result)
Example #11
0
    def evaluate(self):
        soup = BeautifulSoup('<{0}></{0}>'.format(self.name), 'html.parser')
        result = getattr(soup, self.name)

        for value in make_sequence(
                self.contents()) if self.contents is not None else []:
            if is_tag_node(value):
                result.append(self._clone_tag(value))
            elif is_attribute_node(value):
                result[value.name] = value.value
            elif is_string(value) or is_number(value) or is_boolean(value):
                result.append(str(value))
            else:
                value_desc = debug_dump_node(value) if is_any_node(
                    value) else object_type_name(value)
                raise HqueryEvaluationError(
                    'Cannot use {0} as a content object in a computed element constructor'
                    .format(value_desc))

        return make_node_set(result)
Example #12
0
 def _make_array_item(self, value):
     if is_tag_node(value):
         self._gab(
             lambda: 'appending text contents of element "{0}" to array'.
             format(debug_dump_anything(value)))
         return string_value(value)
     elif is_text_node(value) or is_string(value):
         value = string_value(value)
         self._gab(lambda: u'appending text "{0}" to array'.format(
             debug_dump_anything(value)))
         return value
     elif is_boolean(value) or is_number(value):
         self._gab(lambda: 'appending {0} to array'.format(
             debug_dump_anything(value)))
         return value.value
     elif is_hash(value):
         self._gab(lambda: u'appending JSON {0} to array'.format(
             debug_dump_anything(value)))
         return value.contents
     else:
         raise HqueryEvaluationError(
             "Can't use {0} as contents in a computed JSON array constructor"
             .format(debug_dump_anything(value)))
Example #13
0
 def _value_of_other_operand(other):
     return other.value if is_number(other) else other
Example #14
0
 def _value_of_other_operand(other):
     return other.value if is_number(other) else other