#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2023/6/6 21:59:48

@author: LiuKuan
@copyright: Apache License, Version 2.0
'''
import os
from ..fs import file_read_text, file_write_text
from ..net.requests_ import req_text
# ===============================================================================
r'''
'''
# ===============================================================================
__all__ = ['load_html', 'load_html_text',
           'load_xml', 'load_xml_text',
           'load_html_url']

def load_html(filename, base_url=None):
    text = file_read_text(filename)
    return load_html_text(text, base_url=base_url)

def load_html_text(text, base_url=None):
    from lxml import etree
    return etree.HTML(text, base_url=base_url)

def load_xml(filename, base_url=None):
    text = file_read_text(filename)
    return load_xml_text(text, base_url=base_url)

def load_xml_text(text, base_url=None):
    from lxml import etree
    return etree.XML(text, base_url=base_url)

def load_html_url(url, cache_file=None, **kws):
    text = None
    base_url = kws.pop('base_url', None)
    if cache_file:
        if os.path.exists(cache_file):
            text = file_read_text(cache_file)
        else:
            text = req_text(url, **kws)
            file_write_text(cache_file, text)
    else:
        text = req_text(url, **kws)
    return load_html_text(text, base_url)

