# Path: hq/hquery/evaluation_error.py
# class HqueryEvaluationError(RuntimeError):
# 
#     @classmethod
#     def must_be_node_set(cls, obj):
#         if not is_node_set(obj):
#             raise HqueryEvaluationError('Expected a node set, but found a(n) {0}'.format(obj.__class__.__name__))
# 
#     @classmethod
#     def must_be_node_set_or_sequence(cls, obj):
#         if not (is_node_set(obj) or is_sequence(obj)):
#             raise HqueryEvaluationError('Expected a node set or sequence, but found a(n) {0}'.format(
#                 obj.__class__.__name__
#             ))
# 
# Path: hq/hquery/object_type.py
# def is_string(obj):
#     return is_a_string(obj)
# 
# def is_number(obj):
#     return obj.__class__.__name__ == 'number'
# 
# def is_boolean(obj):
#     return obj.__class__.__name__ == 'boolean'
# 
# def object_type_name(obj):
#     result = 'NULL OR UNKNOWN TYPE'
# 
#     if obj is not None:
#         if isinstance(obj, int):
#             index = obj
#         else:
#             index = object_type(obj)
#         result = TYPE_NAMES[index]
# 
#     return result
# 
# def string_value(obj):
#     if is_tag_node(obj) or is_attribute_node(obj) or is_text_node(obj):
#         return derive_text_from_node(obj, peek_context().preserve_space)
#     elif is_number(obj) or is_boolean(obj):
#         return str(obj)
#     elif is_node_set(obj):
#         return string_value(obj[0]) if len(obj) > 0 else ''
#     elif is_sequence(obj):
#         return ''.join(string_value(item) for item in obj)
#     elif is_string(obj):
#         return obj
#     else:
#         raise NotImplementedError('string_value not implemented for type "{0}"'.format(obj.__class__.__name__))
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: hq/soup_util.py
# def debug_dump_node(obj):
#     if is_root_node(obj):
#         return 'ROOT DOCUMENT'
#     elif is_tag_node(obj):
#         return u'ELEMENT {0}'.format(debug_dump_long_string(str(obj)))
#     elif is_attribute_node(obj):
#         return 'ATTRIBUTE {0}="{1}"'.format(obj.name, debug_dump_long_string(obj.value))
#     elif is_text_node(obj):
#         return u'TEXT "{0}"'.format(debug_dump_long_string(obj.string))
#     elif is_comment_node(obj):
#         return u'COMMENT "{0}"'.format(debug_dump_long_string(obj.string))
#     else:
#         return 'NODE type {0}'.format(obj.__class__.__name__)
# 
# def is_any_node(obj):
#     return is_root_node(obj) or is_tag_node(obj) or is_attribute_node(obj) or is_text_node(obj) or is_comment_node(obj)
# 
# class AttributeNode:
# 
#     def __init__(self, name, value):
#         self.name = name
#         self.value = ' '.join(value) if isinstance(value, list) else value
# 
#     def __repr__(self):
#         return 'AttributeNode("{0}", "{1}")'.format(self.name, self.value)
# 
#     @classmethod
#     def enumerate(cls, node):
#         if hasattr(node, 'hq_attrs') and _isnt_root_with_odd_ghost_hq_attrs_on_it_for_reasons_i_dont_understand(node):
#             return node.hq_attrs
#         else:
#             return []
# 
# def is_attribute_node(obj):
#     return isinstance(obj, AttributeNode)
# 
# def is_tag_node(obj):
#     return obj.__class__.__name__ == 'Tag'


from hq.hquery.evaluation_error import HqueryEvaluationError
from hq.hquery.object_type import is_string, is_number, is_boolean, object_type_name, string_value
from hq.hquery.sequences import make_sequence
from hq.hquery.syntax_error import HquerySyntaxError
from hq.soup_util import debug_dump_node, is_any_node, AttributeNode, is_attribute_node, is_tag_node


class ComputedHtmlAttributeConstructor:

    def __init__(self, name):
        self.name = name
        self.contents = None


    def set_content(self, expression_fn):
        if self.contents is not None:
            raise HquerySyntaxError('Computed attribute constructor already has contents')
        self.contents = expression_fn


    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):

# Path: hq/hquery/evaluation_error.py
# class HqueryEvaluationError(RuntimeError):
# 
#     @classmethod
#     def must_be_node_set(cls, obj):
#         if not is_node_set(obj):
#             raise HqueryEvaluationError('Expected a node set, but found a(n) {0}'.format(obj.__class__.__name__))
# 
#     @classmethod
#     def must_be_node_set_or_sequence(cls, obj):
#         if not (is_node_set(obj) or is_sequence(obj)):
#             raise HqueryEvaluationError('Expected a node set or sequence, but found a(n) {0}'.format(
#                 obj.__class__.__name__
#             ))
# 
# Path: hq/hquery/object_type.py
# def is_string(obj):
#     return is_a_string(obj)
# 
# def is_number(obj):
#     return obj.__class__.__name__ == 'number'
# 
# def is_boolean(obj):
#     return obj.__class__.__name__ == 'boolean'
# 
# def object_type_name(obj):
#     result = 'NULL OR UNKNOWN TYPE'
# 
#     if obj is not None:
#         if isinstance(obj, int):
#             index = obj
#         else:
#             index = object_type(obj)
#         result = TYPE_NAMES[index]
# 
#     return result
# 
# def string_value(obj):
#     if is_tag_node(obj) or is_attribute_node(obj) or is_text_node(obj):
#         return derive_text_from_node(obj, peek_context().preserve_space)
#     elif is_number(obj) or is_boolean(obj):
#         return str(obj)
#     elif is_node_set(obj):
#         return string_value(obj[0]) if len(obj) > 0 else ''
#     elif is_sequence(obj):
#         return ''.join(string_value(item) for item in obj)
#     elif is_string(obj):
#         return obj
#     else:
#         raise NotImplementedError('string_value not implemented for type "{0}"'.format(obj.__class__.__name__))
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# Path: hq/hquery/syntax_error.py
# class HquerySyntaxError(ValueError):
#     pass
# 
# Path: hq/soup_util.py
# def debug_dump_node(obj):
#     if is_root_node(obj):
#         return 'ROOT DOCUMENT'
#     elif is_tag_node(obj):
#         return u'ELEMENT {0}'.format(debug_dump_long_string(str(obj)))
#     elif is_attribute_node(obj):
#         return 'ATTRIBUTE {0}="{1}"'.format(obj.name, debug_dump_long_string(obj.value))
#     elif is_text_node(obj):
#         return u'TEXT "{0}"'.format(debug_dump_long_string(obj.string))
#     elif is_comment_node(obj):
#         return u'COMMENT "{0}"'.format(debug_dump_long_string(obj.string))
#     else:
#         return 'NODE type {0}'.format(obj.__class__.__name__)
# 
# def is_any_node(obj):
#     return is_root_node(obj) or is_tag_node(obj) or is_attribute_node(obj) or is_text_node(obj) or is_comment_node(obj)
# 
# class AttributeNode:
# 
#     def __init__(self, name, value):
#         self.name = name
#         self.value = ' '.join(value) if isinstance(value, list) else value
# 
#     def __repr__(self):
#         return 'AttributeNode("{0}", "{1}")'.format(self.name, self.value)
# 
#     @classmethod
#     def enumerate(cls, node):
#         if hasattr(node, 'hq_attrs') and _isnt_root_with_odd_ghost_hq_attrs_on_it_for_reasons_i_dont_understand(node):
#             return node.hq_attrs
#         else:
#             return []
# 
# def is_attribute_node(obj):
#     return isinstance(obj, AttributeNode)
# 
# def is_tag_node(obj):
#     return obj.__class__.__name__ == 'Tag'


# Path: hq/hquery/computed_constructors/html_attribute.py
from hq.hquery.evaluation_error import HqueryEvaluationError
from hq.hquery.object_type import is_string, is_number, is_boolean, object_type_name, string_value
from hq.hquery.sequences import make_sequence
from hq.hquery.syntax_error import HquerySyntaxError
from hq.soup_util import debug_dump_node, is_any_node, AttributeNode, is_attribute_node, is_tag_node



class ComputedHtmlAttributeConstructor:

    def __init__(self, name):
        self.name = name
        self.contents = None


    def set_content(self, expression_fn):
        if self.contents is not None:
            raise HquerySyntaxError('Computed attribute constructor already has contents')
        self.contents = expression_fn


    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))