o
    /hj!                     @   sP   d Z dgZddlZddlZddlmZ ddlmZ ddlmZ G dd dZ	dS )	zA validating CSSParser	CSSParser    N)css   )	tokenize2)path2urlc                   @   sd   e Zd ZdZ						dddZdd Zdd	d
Z	dddZ	dddZdddZ	dddZ
dS )r   a  Parse a CSS StyleSheet from URL, string or file and return a DOM Level 2
    CSS StyleSheet object.

    Usage::

        parser = CSSParser()
        # optionally
        parser.setFetcher(fetcher)
        sheet = parser.parseFile('test1.css', 'ascii')
        print sheet.cssText
    NTc                 C   sf   |dur
t j| |durt j| t jj| _|r|| _nd| _tj|d| _	| 
| || _dS )ac  
        :param log:
            logging object
        :param loglevel:
            logging loglevel
        :param raiseExceptions:
            if log should simply log (default) or raise errors during
            parsing. Later while working with the resulting sheets
            the setting used in cssutils.log.raiseExeptions is used
        :param fetcher:
            see ``setFetcher(fetcher)``
        :param parseComments:
            if comments should be added to CSS DOM or simply omitted
        :param validate:
            if parsing should validate, may be overwritten in parse methods
        NF)
doComments)cssutilslogsetLogsetLevelraiseExceptions_CSSParser__globalRaising_CSSParser__parseRaisingr   	Tokenizer_CSSParser__tokenizer
setFetcher	_validate)selfr	   loglevelr   fetcherparseCommentsvalidate r   B/var/www/html/myenv/lib/python3.10/site-packages/cssutils/parse.py__init__   s   


zCSSParser.__init__c                 C   s    |r	| j tj_dS | jtj_dS )zsduring parse exceptions may be handled differently depending on
        init parameter ``raiseExceptions``
        N)r   r   r	   r   r   )r   parser   r   r   __parseSettingF   s   zCSSParser.__parseSettingutf-8c                 C   sH   |  d t|tr||}|du r| j}tj||d}|  d |S )a  Parse given `cssText` which is assumed to be the content of
        a HTML style attribute.

        :param cssText:
            CSS string to parse
        :param encoding:
            It will be used to decode `cssText` if given as a (byte)
            string.
        :param validate:
            If given defines if validation is used. Uses CSSParser settings as
            fallback
        :returns:
            :class:`~cssutils.css.CSSStyleDeclaration`
        TN)
validatingF)_CSSParser__parseSetting
isinstancebytesdecoder   r   CSSStyleDeclaration)r   cssTextencodingr   styler   r   r   
parseStyleO   s   



zCSSParser.parseStylec                 C   s   |  d t|trtd||dd }|du r| j}tjj|tj	
|||d}|| j |j| jj|dd|d |  d	 |S )
a^  Parse `cssText` as :class:`~cssutils.css.CSSStyleSheet`.
        Errors may be raised (e.g. UnicodeDecodeError).

        :param cssText:
            CSS string to parse
        :param encoding:
            If ``None`` the encoding will be read from BOM or an @charset
            rule or defaults to UTF-8.
            If given overrides any found encoding including the ones for
            imported sheets.
            It also will be used to decode `cssText` if given as a (byte)
            string.
        :param href:
            The ``href`` attribute to assign to the parsed style sheet.
            Used to resolve other urls in the parsed sheet like @import hrefs.
        :param media:
            The ``media`` attribute to assign to the parsed style sheet
            (may be a MediaList, list or a string).
        :param title:
            The ``title`` attribute to assign to the parsed style sheet.
        :param validate:
            If given defines if validation is used. Uses CSSParser settings as
            fallback
        :returns:
            :class:`~cssutils.css.CSSStyleSheet`.
        Tr   )r%   r   N)hrefmediatitler   )	fullsheet)encodingOverrideF)r   r    r!   codecs
getdecoderr   r   r   CSSStyleSheetstylesheets	MediaList_setFetcher_CSSParser__fetcher_setCssTextWithEncodingOverrider   tokenize)r   r$   r%   r(   r)   r*   r   sheetr   r   r   parseStringh   s$   



zCSSParser.parseStringc           	      C   sT   |st |}t|d}| }W d   n1 sw   Y  | j||||||dS )a  Retrieve content from `filename` and parse it. Errors may be raised
        (e.g. IOError).

        :param filename:
            of the CSS file to parse, if no `href` is given filename is
            converted to a (file:) URL and set as ``href`` of resulting
            stylesheet.
            If `href` is given it is set as ``sheet.href``. Either way
            ``sheet.href`` is used to resolve e.g. stylesheet imports via
            @import rules.
        :param encoding:
            Value ``None`` defaults to encoding detection via BOM or an
            @charset rule.
            Other values override detected encoding for the sheet at
            `filename` including any imported sheets.
        :returns:
            :class:`~cssutils.css.CSSStyleSheet`.
        rbNr%   r(   r)   r*   r   )r   openreadr7   )	r   filenamer%   r(   r)   r*   r   fdr   r   r   r   	parseFile   s   
zCSSParser.parseFilec                 C   sH   t jj|| j|d\}}}|dkrd}|dur"| j||||||dS dS )a!  Retrieve content from URL `href` and parse it. Errors may be raised
        (e.g. URLError).

        :param href:
            URL of the CSS file to parse, will also be set as ``href`` of
            resulting stylesheet
        :param encoding:
            Value ``None`` defaults to encoding detection via HTTP, BOM or an
            @charset rule.
            A value overrides detected encoding for the sheet at ``href``
            including any imported sheets.
        :returns:
            :class:`~cssutils.css.CSSStyleSheet`.
        )r   overrideEncoding   Nr9   )r   util_readUrlr3   r7   )r   r(   r%   r)   r*   r   enctypetextr   r   r   parseUrl   s   zCSSParser.parseUrlc                 C   s
   || _ dS )a/  Replace the default URL fetch function with a custom one.

        :param fetcher:
            A function which gets a single parameter

            ``url``
                the URL to read

            and must return ``(encoding, content)`` where ``encoding`` is the
            HTTP charset normally given via the Content-Type header (which may
            simply omit the charset in which case ``encoding`` would be
            ``None``) and ``content`` being the string (or unicode) content.

            The Mimetype should be 'text/css' but this has to be checked by the
            fetcher itself (the default fetcher emits a warning if encountering
            a different mimetype).

            Calling ``setFetcher`` with ``fetcher=None`` resets cssutils
            to use its default function.
        N)r3   )r   r   r   r   r   r      s   
zCSSParser.setFetcher)NNNNTT)r   N)NNNNN)NNNN)N)__name__
__module____qualname____doc__r   r   r'   r7   r>   rE   r   r   r   r   r   r      s"    
+
	
5

& )
rI   __all__r-   r   r    r   helperr   r   r   r   r   r   <module>   s    