
     hj!                     V    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      Z	y)	zA validating CSSParser	CSSParser    N)css   )	tokenize2)path2urlc                   V    e Zd ZdZ	 	 	 	 	 	 d
dZd ZddZ	 ddZ	 ddZddZ	dd	Z
y)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
    Nc                 N   |t         j                  j                  |       |t         j                  j                  |       t         j                  j                  | _        |r|| _        nd| _        t        j                  |      | _	        | j                  |       || _        y)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validates          N/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/cssutils/parse.py__init__zCSSParser.__init__   s    2 ?LL$LL!!(+  (||;;"1D #(D$..-H !    c                     |r | j                   t        j                  _        y| j                  t        j                  _        y)zsduring parse exceptions may be handled differently depending on
        init parameter ``raiseExceptions``
        N)r   r   r   r   r   )r   parses     r   __parseSettingzCSSParser.__parseSettingF   s+     +/+>+>HLL(+/+?+?HLL(r   c                     | j                  d       t        |t              r|j                  |      }|| j                  }t        j                  ||      }| j                  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`
        T)
validatingF)_CSSParser__parseSetting
isinstancebytesdecoder   r   CSSStyleDeclaration)r   cssTextencodingr   styles        r   
parseStylezCSSParser.parseStyleO   s^     	D!gu%nnX.G~~H''HEE"r   c                    | j                  d       t        |t              r  t        j                  d      ||      d   }|| j
                  }t        j                  j                  |t        j                  j                  |      ||      }|j                  | j                         |j                  | j                  j                  |d      |       | j                  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   )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   sheets           r   parseStringzCSSParser.parseStringh   s    : 	D!gu%.f''.wJ1MG~~H**&&007	 + 
 	$..)--%%g%>% 	. 	
 	E"r   c                     |st        |      }t        |d      5 }|j                         }ddd       | j                  |||||      S # 1 sw Y    xY w)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readr<   )	r   filenamer)   r-   r.   r/   r   fdr   s	            r   	parseFilezCSSParser.parseFile   sd    *  H%D(D!R'')C "    
 	
 "!s   A

Ac                     t         j                  j                  || j                  |      \  }}}|dk(  rd}|| j	                  ||||||      S y)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   Nr?   )r   util_readUrlr8   r<   )r   r-   r)   r.   r/   r   enctypetexts           r   parseUrlzCSSParser.parseUrl   sr     #+--"8"8$..8 #9 #
'4 a<H##!! $   r   c                     || _         y)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)r8   )r   r   s     r   r   zCSSParser.setFetcher   s    * !r   )NNNNTT)zutf-8N)NNNNN)NNNN)N)__name__
__module____qualname____doc__r   r#   r+   r<   rD   rL   r    r   r   r   r      sQ    
 )"V@4 SW2j TX$
L@!r   )
rQ   __all__r2   r   r    r   helperr   r   rR   r   r   <module>rV      s*    -     i! i!r   