# encoding: utf8
#
# spyne - Copyright (C) Spyne contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

from lxml import etree, html

from spyne.protocol.cloth import XmlCloth


class HtmlBase(XmlCloth):
    mime_type = 'text/html; charset=UTF-8'

    def __init__(self, app=None, mime_type=None, ignore_uncap=False,
                 ignore_wrappers=False, cloth=None, attr_name='spyne_id',
                 root_attr_name='spyne', cloth_parser=None, polymorphic=True,
                                                  hier_delim='.', doctype=None):
        super(HtmlBase, self).__init__(app=app, mime_type=mime_type,
            ignore_uncap=ignore_uncap, ignore_wrappers=ignore_wrappers,
            cloth=cloth, attr_name=attr_name, root_attr_name=root_attr_name,
            cloth_parser=cloth_parser, polymorphic=polymorphic)

        self.hier_delim = hier_delim
        self.doctype = doctype

    def _parse_file(self, file_name, cloth_parser):
        if cloth_parser is None:
            cloth_parser = html.HTMLParser(remove_comments=True)

        self._cloth = html.parse(self._cloth, parser=cloth_parser)
        self._cloth = self._cloth.getroot()

    def docfile(self, *args, **kwargs):
        return etree.htmlfile(*args, **kwargs)

    def write_doctype(self, ctx, parent, cloth=None):
        if self.doctype is not None:
            parent.write_doctype(self.doctype)
        elif cloth is not None:
            parent.write_doctype(cloth.getroottree().docinfo.doctype)
        else:
            return

        ctx.protocol.doctype_written = True

    @staticmethod
    def get_class_cloth(cls):
        return cls.Attributes._html_cloth

    @staticmethod
    def get_class_root_cloth(cls):
        return cls.Attributes._html_root_cloth


# encoding: utf8
#
# spyne - Copyright (C) Spyne contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

from lxml import etree, html

from spyne.protocol.cloth import XmlCloth


class HtmlBase(XmlCloth):
    mime_type = 'text/html; charset=UTF-8'

    def __init__(self, app=None, mime_type=None, ignore_uncap=False,
                 ignore_wrappers=False, cloth=None, attr_name='spyne_id',
                 root_attr_name='spyne', cloth_parser=None, polymorphic=True,
                                                  hier_delim='.', doctype=None):
        super(HtmlBase, self).__init__(app=app, mime_type=mime_type,
            ignore_uncap=ignore_uncap, ignore_wrappers=ignore_wrappers,
            cloth=cloth, attr_name=attr_name, root_attr_name=root_attr_name,
            cloth_parser=cloth_parser, polymorphic=polymorphic)

        self.hier_delim = hier_delim
        self.doctype = doctype

    def _parse_file(self, file_name, cloth_parser):
        if cloth_parser is None:
            cloth_parser = html.HTMLParser(remove_comments=True)

        cloth = html.parse(file_name, parser=cloth_parser)
        return cloth.getroot()

    def docfile(self, *args, **kwargs):
        return etree.htmlfile(*args, **kwargs)

    def write_doctype(self, ctx, parent, cloth=None):
        if self.doctype is not None:
            parent.write_doctype(self.doctype)
        elif cloth is not None:
            parent.write_doctype(cloth.getroottree().docinfo.doctype)
        else:
            return

        ctx.protocol.doctype_written = True

    @staticmethod
    def get_class_cloth(cls):
        return cls.Attributes._html_cloth

    @staticmethod
    def get_class_root_cloth(cls):
        return cls.Attributes._html_root_cloth


fix parse_file in HtmlBase