o
    d:                     @   s8  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ e Ze  Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$										d(d d!Z%d"d# Z&d$d% Z'd&d' Z(dS ))    N)fnmatch)Pathisfilejoin)parse_qs   )	_validate)AttributeDict)get_relative_path)context_value)get_appc           
         s   t j g d}| dd }g }tj r" fddt D }d}d}|D ],}|d\}}}	|	 |v rT||ksD||	ddkrH|  S |d	krN|}|d
krT|}q(|rY|S |S )z
    Return:
    - A page specific image: `assets/<module>.<extension>` is used, e.g. `assets/weekly_analytics.png`
    - A generic app image at `assets/app.<extension>`
    - A logo at `assets/logo.<extension>`
    )apngavifgifjpegjpgpngsvgwebp.c                    s   g | ]}t t |r|qS  r   ).0fassets_folderr   P/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/dash/_pages.py
<listcomp>%   s
    z _infer_image.<locals>.<listcomp>N_-applogo)
CONFIGr   splitospathexistslistdir	partitionlowerreplace)
modulevalid_extensionspage_idfiles_in_assetsapp_file	logo_filefnfn_without_extensionr   	extensionr   r   r   _infer_image   s4   
r5   c                 C   s   |  dd dd S )Nr   r   r    )r$   r+   
capitalizemodule_namer   r   r   _module_name_to_page_name?   s   r:   c                 C   s   |d u r.t jr!ttt jj}| |d dddd }n| dddd }nt	dd|}|
ds@d| }|S |}|S )Nr   r   r    r   /<.*?>none)r#   pages_folderstrr   namer$   r+   r*   resub
startswith)r9   templatepages_moduler&   r   r   r   _infer_pathC   s   rF   c                 C   s    | t jv ott j|  jjdkS )Nz__init__.py)sysmodulesr   __file__r@   r8   r   r   r   _module_name_is_packageW   s   
rJ   c                 C   s"   t | ddtjtjdS )N.py r   )r?   r+   stripr%   sep)r&   r   r   r   _path_to_module_name^   s   "rO   c                 C   s   |  tjd }t|}tjtj}tj|r#tjt	|d  }ntj}t|}| d| }t
tjr>tj d| }|S )Nr   r   )r$   r#   r>   rO   flaskhelpersget_root_pathr@   rC   lenrJ   )	page_pathrelative_pathr,   	proj_rootparent_pathparent_moduler9   r   r   r   _infer_module_nameb   s   
rY   c                 C   sl   | rt | dkr| d dkr| dd  } ni S i }t|  D ]\}}t |dkr-|d n|}|||< q|S )Nr   ?r   )rS   r   items)search	parsed_qskvr   r   r   _parse_query_strings   s   
r`   c                 C   sj   t dd|}t dd|}t| |sdS t d|}t || }t|d tr,|d n|}tt||S )z
    creates the dict of path variables passed to the layout
    e.g. path_template= "/asset/<asset_id>"
         if pathname provided by the browser is "/assets/a100"
         returns **{"asset_id": "a100"}
    r<   *z(.*)Nz<(.*?)>r   )rA   rB   r   findall
isinstancetupledictzip)pathnamepath_templatewildcard_patternvar_pattern	var_names	variablesr   r   r   _parse_path_variables   s   

rm   c                    s    fdd}|S )Nc                      s   t j ddS )Ni-  )code)rP   redirectr   redirect_tor   r   ro      s   z+_create_redirect_function.<locals>.redirectr   )rq   ro   r   rp   r   _create_redirect_function   s   rr   c              	   C   sL   t  }| r t| r"| D ]}||}|j||t|| qd S d S d S )N)r   rS   r   serveradd_url_rulerr   )redirect_fromr&   r!   ro   fullnamer   r   r   _set_redirect   s   
rw   c                 K   s  t  dr	dS tt tt| |||dur|nt| |||dur&|nt| d}|j	||dur5|n|d d |j	d|rB|nd|||
d| |j	||durU|nt
| |d |j	t|	|d	 d
 |t| < |d rvt|d  |
dur|
t|  d< tdd t D }t D ]}|d	 dkr|sdn|d |d< t|d	 |d< qtt dd dD ]	}t|d  qdS )a  
    Assigns the variables to `dash.page_registry` as an `OrderedDict`
    (ordered by `order`).

    `dash.page_registry` is used by `pages_plugin` to set up the layouts as
    a multi-page Dash app. This includes the URL routing callbacks
    (using `dcc.Location`) and the HTML templates to include title,
    meta description, and the meta description image.

    `dash.page_registry` can also be used by Dash developers to create the
    page navigation links or by template authors.

    - `module`:
       The module path where this page's `layout` is defined. Often `__name__`.

    - `path`:
       URL Path, e.g. `/` or `/home-page`.
       If not supplied, will be inferred from the `path_template` or `module`,
       e.g. based on path_template: `/asset/<asset_id` to `/asset/none`
       e.g. based on module: `pages.weekly_analytics` to `/weekly-analytics`

    - `relative_path`:
        The path with `requests_pathname_prefix` prefixed before it.
        Use this path when specifying local URL paths that will work
        in environments regardless of what `requests_pathname_prefix` is.
        In some deployment environments, like Dash Enterprise,
        `requests_pathname_prefix` is set to the application name,
        e.g. `my-dash-app`.
        When working locally, `requests_pathname_prefix` might be unset and
        so a relative URL like `/page-2` can just be `/page-2`.
        However, when the app is deployed to a URL like `/my-dash-app`, then
        `relative_path` will be `/my-dash-app/page-2`.

    - `path_template`:
       Add variables to a URL by marking sections with <variable_name>. The layout function
       then receives the <variable_name> as a keyword argument.
       e.g. path_template= "/asset/<asset_id>"
            then if pathname in browser is "/assets/a100" then layout will receive **{"asset_id":"a100"}

    - `name`:
       The name of the link.
       If not supplied, will be inferred from `module`,
       e.g. `pages.weekly_analytics` to `Weekly analytics`

    - `order`:
       The order of the pages in `page_registry`.
       If not supplied, then the filename is used and the page with path `/` has
       order `0`

    - `title`:
       (string or function) The name of the page <title>. That is, what appears in the browser title.
       If not supplied, will use the supplied `name` or will be inferred by module,
       e.g. `pages.weekly_analytics` to `Weekly analytics`

    - `description`:
       (string or function) The <meta type="description"></meta>.
       If not supplied, then nothing is supplied.

    - `image`:
       The meta description image used by social media platforms.
       If not supplied, then it looks for the following images in `assets/`:
        - A page specific image: `assets/<module>.<extension>` is used, e.g. `assets/weekly_analytics.png`
        - A generic app image at `assets/app.<extension>`
        - A logo at `assets/logo.<extension>`
        When inferring the image file, it will look for the following extensions:
        APNG, AVIF, GIF, JPEG, JPG, PNG, SVG, WebP.

    -  `image_url`:
       Overrides the image property and sets the `<image>` meta tag to the provided image URL.

    - `redirect_from`:
       A list of paths that should redirect to this page.
       For example: `redirect_from=['/v2', '/v3']`

    - `layout`:
       The layout function or component for this page.
       If not supplied, then looks for `layout` from within the supplied `module`.

    - `**kwargs`:
       Arbitrary keyword arguments that can be stored

    ***

    `page_registry` stores the original property that was passed in under
    `supplied_<property>` and the coerced property under `<property>`.
    For example, if this was called:
    ```
    register_page(
        'pages.historical_outlook',
        name='Our historical view',
        custom_key='custom value'
    )
    ```
    Then this will appear in `page_registry`:
    ```
    OrderedDict([
        (
            'pages.historical_outlook',
            dict(
                module='pages.historical_outlook',

                supplied_path=None,
                path='/historical-outlook',

                supplied_name='Our historical view',
                name='Our historical view',

                supplied_title=None,
                title='Our historical view'

                supplied_layout=None,
                layout=<function pages.historical_outlook.layout>,

                custom_key='custom value'
            )
        ),
    ])
    ```
    ignore_register_pageN)r,   supplied_pathrh   r&   supplied_namer@   r@   )supplied_titletitlerL   )descriptionordersupplied_ordersupplied_layout)supplied_imageimage	image_urlr&   )ru   rh   layoutc                 s   s    | ]	}|d  duV  qdS )r   Nr   )r   pr   r   r   	<genexpr>\  s    
z register_page.<locals>.<genexpr>r;   r   r   r~   rU   c                 S   s>   | d d u t | d ttfr| d ntdt| d | d fS )Nr~   infr,   )rc   intfloatr?   )ir   r   r   <lambda>j  s
   
 
zregister_page.<locals>.<lambda>)keyr,   r   )r   getr	   validate_use_pagesr#   re   validate_module_namerF   r:   updater5   rw   PAGE_REGISTRYvalidate_templateanyvaluesr   sortedmove_to_end)r,   r&   rh   r@   r~   r|   r}   r   r   ru   r   kwargspageorder_suppliedr   r   r   r   register_page   s`    


	r   c                 C   sh   d }t  D ])}|d r |d d}t| |}|r ||f  S | |d dkr/||f  S qi d fS )Nrh   r;   r&   )r   r   rM   rm   )path_idpath_variablesr   template_idr   r   r   _path_to_paget  s   
r   c           	      C   s   t tjjd\}}|dd}|r| |}|r&dtjj|	dgnd }|d}|r1|n|}|d| j
}t|rJ|rG|di |n| }|dd}t|r`|r]|di |n| }d|ddd	d
dtjjd
d|d
d|d
d|pzdd
d|d
ddd
d|d
d|pdd
g
S )Nr;   r   rL   r   r|   r}   )r@   contentztwitter:cardsummary_large_image)propertyr   ztwitter:urlztwitter:titleztwitter:descriptionztwitter:imagezog:titlezog:typewebsitezog:descriptionzog:imager   )r   rP   requestr&   rM   r   get_asset_urlr   url_rootlstripr|   callableurl)	r!   
start_pager   r   assets_image_urlsupplied_image_urlr   r|   r}   r   r   r   _page_meta_tags  s2   

r   c              
   C   s  t | D ]\}}}dd |D |d d < |D ]t}|ds(|ds(|ds)qt j||}t|dd}| }d|vrG	 W d    qW d    n1 sQw   Y  t|}t	j
||}	t	j
|	}
|	j|
 |
tj|< |tv rt| d	 st||
 t|
d
t| d
< qqd S )Nc                 S   s$   g | ]}| d s| ds|qS )r   r   )rC   )r   dr   r   r   r     s   $ z._import_layouts_from_pages.<locals>.<listcomp>r   r   rK   zutf-8)encodingr   r   r   )r%   walkrC   endswithr&   r   openreadrY   	importlibutilspec_from_file_locationmodule_from_specloaderexec_modulerG   rH   r   r	   validate_pages_layoutgetattr)r>   rootdirsfilesfilerT   r   r   r9   specpage_moduler   r   r   _import_layouts_from_pages  s6   

r   )
NNNNNNNNNN))collectionsr   r%   rA   rG   r   pathlibr   os.pathr   r   urllib.parser   rP   rL   r	   _utilsr
   
_get_pathsr   _callback_contextr   _get_appr   r#   OrderedDictr   r5   r:   rF   rJ   rO   rY   r`   rm   rr   rw   r   r   r   r   r   r   r   r   <module>   sT    '
 G#