# Path: chemdataextractor/utils.py
# def flatten(x):
#     """Return a single flat list containing elements from nested lists."""
#     result = []
#     for el in x:
#         if hasattr(el, '__iter__') and not isinstance(el, six.string_types):
#             result.extend(flatten(el))
#         else:
#             result.append(el)
#     return result
# 
# Path: chemdataextractor/scrape/csstranslator.py
# class CssHTMLTranslator(TranslatorMixin, HTMLTranslator):
#     pass
# 
# class CssXmlTranslator(TranslatorMixin, GenericTranslator):
#     pass


from collections import Sequence
from copy import deepcopy
from bs4 import UnicodeDammit
from lxml.etree import XMLParser, fromstring, tostring
from lxml.html import HTMLParser
from ..utils import flatten
from .csstranslator import CssHTMLTranslator, CssXmlTranslator
import logging
import re
import six
            return tostring(root, method=self.fmt if raw else 'text', encoding=six.text_type, with_tail=False)
        except (AttributeError, TypeError) as e:
            #log.warn(e)
            return six.text_type(self._root)

    @classmethod
    def _get_encoding(cls, input_string, encoding):
        converted = UnicodeDammit(input_string, [encoding] if encoding else [])
        # Not worth raising exception? lxml will raise if parse fails.
        # if not converted.unicode_markup:
        #     raise UnicodeDecodeError('Failed to detect encoding')
        return converted.original_encoding


class SelectorList(Sequence):
    """Wrapper around a list of Selectors to allow selecting from all at once."""

    def __init__(self, *selectors):
        self.selectors = list(selectors)

    def __getitem__(self, index):
        return self.selectors[index]

    def __len__(self):
        return len(self.selectors)

    def xpath(self, xpath):
        return self.__class__(*flatten([x.xpath(xpath) for x in self.selectors]))

    def re(self, regex):

# Path: chemdataextractor/utils.py
# def flatten(x):
#     """Return a single flat list containing elements from nested lists."""
#     result = []
#     for el in x:
#         if hasattr(el, '__iter__') and not isinstance(el, six.string_types):
#             result.extend(flatten(el))
#         else:
#             result.append(el)
#     return result
# 
# Path: chemdataextractor/scrape/csstranslator.py
# class CssHTMLTranslator(TranslatorMixin, HTMLTranslator):
#     pass
# 
# class CssXmlTranslator(TranslatorMixin, GenericTranslator):
#     pass


# Path: chemdataextractor/scrape/selector.py
from collections import Sequence
from copy import deepcopy
from bs4 import UnicodeDammit
from lxml.etree import XMLParser, fromstring, tostring
from lxml.html import HTMLParser
from ..utils import flatten
from .csstranslator import CssHTMLTranslator, CssXmlTranslator
import logging
import re
import six

            return tostring(root, method=self.fmt if raw else 'text', encoding=six.text_type, with_tail=False)
        except (AttributeError, TypeError) as e:
            #log.warn(e)
            return six.text_type(self._root)

    @classmethod
    def _get_encoding(cls, input_string, encoding):
        converted = UnicodeDammit(input_string, [encoding] if encoding else [])
        # Not worth raising exception? lxml will raise if parse fails.
        # if not converted.unicode_markup:
        #     raise UnicodeDecodeError('Failed to detect encoding')
        return converted.original_encoding


class SelectorList(Sequence):
    """Wrapper around a list of Selectors to allow selecting from all at once."""

    def __init__(self, *selectors):
        self.selectors = list(selectors)

    def __getitem__(self, index):
        return self.selectors[index]

    def __len__(self):
        return len(self.selectors)

    def xpath(self, xpath):
        return self.__class__(*flatten([x.xpath(xpath) for x in self.selectors]))

    def re(self, regex):

        return flatten([x.re(regex) for x in self.selectors])