o
    #i
                     @   s   d Z ddlZddlmZ ddlmZmZ ddlmZ	m
Z
 ddlmZ ddlmZ dd	lmZmZ d
d Zdd ZG dd dZdS )zO
When defining hostconfs you need to use the ``patterns`` and ``host`` helpers
    N)settings)ImproperlyConfiguredViewDoesNotExist)get_callableget_mod_func)	smart_str)cached_property   )normalize_schemenormalize_portc              
   C   s:   zt | W S  ty } zt|jd ddd}~ww )z
    Convert a string version of a function name to the callable object.

    If the lookup_view is not an import path, it is assumed to be a URL pattern
    label and the original string is returned.
    r   ViewCallableN)actual_get_callabler   r   argsreplace)lookup_viewexc r   \/var/www/GraceOrthoHospital/venv_grace/lib/python3.10/site-packages/django_hosts/defaults.pyr      s   
r   c                 G   sh   g }|D ]-}t |ttfrt|d| i}n||  |j}|dd |D v r,td| || q|S )a  
    The function to define the list of hosts (aka hostconfs), e.g.::

        from django_hosts import patterns

        host_patterns = patterns('path.to',
            (r'www', 'urls.default', 'default'),
            (r'api', 'urls.api', 'api'),
        )

    :param prefix: the URLconf prefix to pass to the host object
    :type prefix: str
    :param \*args: a list of :class:`~django_hosts.defaults.hosts` instances
                   or an iterable thereof
    prefixc                 S   s   g | ]}|j qS r   )name).0hr   r   r   
<listcomp>4   s    zpatterns.<locals>.<listcomp>zDuplicate host name: %s)
isinstancelisttuplehost
add_prefixr   r   append)r   r   hostsargr   r   r   r   patterns   s   
r"   c                   @   sT   e Zd ZdZ		dddZdd Zedd	 Zed
d Ze	dd Z
dddZdS )r   a  
    The host object used in host conf together with the
    :func:`django_hosts.defaults.patterns` function, e.g.::

        from django_hosts import patterns, host

        host_patterns = patterns('path.to',
            host(r'www', 'urls.default', name='default'),
            host(r'api', 'urls.api', name='api'),
            host(r'admin', 'urls.admin', name='admin', scheme='https://'),
        )

    :param regex: a regular expression to be used to match the request's
                  host.
    :type regex: str
    :param urlconf: the dotted path of a URLconf module of the host
    :type urlconf: str
    :param callback: a callable or the dotted path of a callable to be used
                     when matching has happened
    :type callback: callable or str
    :param prefix: the prefix to apply to the ``urlconf`` parameter
    :type prefix: str
    :param scheme: the scheme to prepend host names with during reversing,
                   e.g. when using the host_url() template tag. Defaults to
                   :attr:`~django.conf.settings.HOST_SCHEME`.
    :param port: the port to append to host names during reversing,
                 e.g. when using the host_url() template tag. Defaults to
                 :attr:`~django.conf.settings.HOST_PORT`.
    :type scheme: str
    N c           
      C   s   || _ ttddd}|rd| nd}	t| |	 d| _|| _|| _|| _	|| _
t|r3|| _nd|| _| _| | dS )z
        Compile hosts. We add a literal fullstop to the end of every
        pattern to avoid rather unwieldy escaping in every definition.
        The pattern is also suffixed by the PARENT_HOST setting if it exists.
        PARENT_HOSTr#   .z\.z(\.|:|$)N)regexgetattrr   lstriprecompilecompiled_regexurlconfr   _scheme_portcallable	_callback_callback_strr   )
selfr&   r,   r   callbackr   schemeportparent_hostsuffixr   r   r   __init__Y   s   zhost.__init__c                 C   s&   t d| jj| j| j| j| j| jf S )Nz.<%s %s: regex=%r urlconf=%r scheme=%r port=%r>)r   	__class____name__r   r&   r,   r4   r5   r2   r   r   r   __repr__n   s
   zhost.__repr__c                 C   "   | j d u rttdd| _ t| j S )NHOST_SCHEMEz//)r-   r'   r   r
   r;   r   r   r   r4   s      

zhost.schemec                 C   r=   )N	HOST_PORTr#   )r.   r'   r   r   r;   r   r   r   r5   y   r?   z	host.portc              
   C   s   | j d ur| j S | jd u rdd S z
t| j| _ W | j S  ty8 } zt| j\}}td|t|f d }~w tyU } zt| j\}}td||t|f d }~ww )Nc                  _   s   d S )Nr   )r   kwargsr   r   r   <lambda>   s    zhost.callback.<locals>.<lambda>z$Could not import '%s'. Error was: %sz?Tried importing '%s' from module '%s' but failed. Error was: %s)r0   r1   r   ImportErrorr   r   strAttributeError)r2   r   mod_name_	func_namer   r   r   r3      s(   


zhost.callbackc                 C   s"   |r| dd | j | _dS dS )zC
        Adds the prefix string to a string-based urlconf.
        r%   N)rstripr,   )r2   r   r   r   r   r      s   zhost.add_prefix)Nr#   NN)r#   )r:   
__module____qualname____doc__r8   r<   r   r4   r5   propertyr3   r   r   r   r   r   r   :   s    



r   )rL   r)   django.confr   django.core.exceptionsr   r   django.urlsr   r   r   django.utils.encodingr   django.utils.functionalr   utilsr
   r   r"   r   r   r   r   r   <module>   s    