# 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 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 is_number(obj):
#     return obj.__class__.__name__ == 'number'
# 
# def is_boolean(obj):
#     return obj.__class__.__name__ == 'boolean'
# 
# Path: hq/hquery/sequences.py
# def make_node_set(node_set, reverse=False):
#     ids = set()
# 
#     def is_unique_id(node):
#         node_id = id(node)
#         if node_id in ids:
#             return False
#         else:
#             ids.add(node_id)
#             return True
# 
#     if not isinstance(node_set, list):
#         node_set = [node_set]
# 
#     non_node_member = next(filterfalse(is_any_node, node_set), False)
#     if non_node_member:
#         format_str = 'Constructed node set that includes {0} object "{1}"'
#         raise HqueryEvaluationError(format_str.format(object_type_name(non_node_member), non_node_member))
# 
#     node_set = list(sorted(filter(is_unique_id, node_set), key=lambda n: n.hq_doc_index, reverse=reverse))
# 
#     return node_set
# 
# 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)
# 
# def is_tag_node(obj):
#     return obj.__class__.__name__ == 'Tag'
# 
# def is_attribute_node(obj):
#     return isinstance(obj, AttributeNode)


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


class ComputedHtmlElementConstructor:

    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 element constructor already has contents')
        self.contents = expression_fn


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


# 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 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 is_number(obj):
#     return obj.__class__.__name__ == 'number'
# 
# def is_boolean(obj):
#     return obj.__class__.__name__ == 'boolean'
# 
# Path: hq/hquery/sequences.py
# def make_node_set(node_set, reverse=False):
#     ids = set()
# 
#     def is_unique_id(node):
#         node_id = id(node)
#         if node_id in ids:
#             return False
#         else:
#             ids.add(node_id)
#             return True
# 
#     if not isinstance(node_set, list):
#         node_set = [node_set]
# 
#     non_node_member = next(filterfalse(is_any_node, node_set), False)
#     if non_node_member:
#         format_str = 'Constructed node set that includes {0} object "{1}"'
#         raise HqueryEvaluationError(format_str.format(object_type_name(non_node_member), non_node_member))
# 
#     node_set = list(sorted(filter(is_unique_id, node_set), key=lambda n: n.hq_doc_index, reverse=reverse))
# 
#     return node_set
# 
# 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)
# 
# def is_tag_node(obj):
#     return obj.__class__.__name__ == 'Tag'
# 
# def is_attribute_node(obj):
#     return isinstance(obj, AttributeNode)


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



class ComputedHtmlElementConstructor:

    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 element constructor already has contents')
        self.contents = expression_fn


    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 []: