
    x
h3H                        d dl mZmZmZmZ d dlmZ d dlmZ d dl	Z	ddl
mZmZmZmZmZ dZerad dlZddl
mZmZmZmZmZmZmZmZ d d	lmZ e	j:                  rej<                  ej>                  d
f   Z nej<                  ej>                  df   Z  G d de!      Z" G d de!      Z# G d d
e!      Z$ G d de!      Z% G d de!      Z& G d de!      Z'y)    )absolute_importdivisionprint_functionunicode_literals)OrderedDict)StrictVersionN   )
doc_unwrapis_aliasis_composite_typeis_list_typeis_nullable_typeF)Alias
AnnotationAnnotationTypeDataTypeListNullableStructUserDefined)AstRouteDefApiNamespaces   ApiNamespacec                   (    e Zd ZdZd Zd Zd Zd Zy)ApizL
    A full description of an API's namespaces, data types, and routes.
    c                 P    t        |      | _        t               | _        d | _        y N)r   versionr   
namespacesroute_schema)selfr   s     Q/var/www/html/lionshead-backend/venv/lib/python3.12/site-packages/stone/ir/api.py__init__zApi.__init__.   s    $W-%-     c                 l    || j                   vrt        |      | j                   |<   | j                   |   S )z
        Only creates a namespace if it hasn't yet been defined.

        :param str name: Name of the namespace.

        :return ApiNamespace:
        )r   r   r    names     r!   ensure_namespacezApi.ensure_namespace4   s2     t&$0$6DOOD!t$$r#   c                     t               }t        | j                  j                               D ]  }| j                  |   ||<    || _        | j                  j	                         D ]  }|j                           y)zj
        Alphabetizes namespaces and routes to make spec parsing order mostly
        irrelevant.
        N)r   sortedr   keysvalues	normalize)r    ordered_namespacesnamespace_name	namespaces       r!   r,   zApi.normalizeA   sf     )]$T__%9%9%;<N151P~. =,//1I! 2r#   c                 .    | j                   J || _         y r   )r   )r    r   s     r!   add_route_schemazApi.add_route_schemaP   s      ((((r#   N)__name__
__module____qualname____doc__r"   r'   r,   r1    r#   r!   r   r   *   s    !%")r#   r   c                       e Zd ZdZd Zy)_ImportReasonz5
    Tracks the reason a namespace was imported.
    c                 <    d| _         d| _        d| _        d| _        y )NF)alias	data_type
annotationannotation_typer    s    r!   r"   z_ImportReason.__init__[   s    
$r#   Nr2   r3   r4   r5   r"   r6   r#   r!   r8   r8   V   s    %r#   r8   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
	 	 	 	 dd	Zd
 Zd Zd Zd Z	 	 	 ddZd Zd Zd Zy)r   zQ
    Represents a category of API endpoints and their associated data types.
    c                     || _         d | _        g | _        i | _        i | _        g | _        i | _        g | _        i | _        g | _	        i | _
        g | _        i | _        i | _        y r   )r&   docroutesroute_by_nameroutes_by_name
data_typesdata_type_by_namealiasesalias_by_nameannotationsannotation_by_nameannotation_typesannotation_type_by_name_imported_namespacesr%   s     r!   r"   zApiNamespace.__init__h   sm    	 !#"$ "')$$&!r#   c                     t        |t        j                        sJ t        |             t	        |      dz   }| j
                  || _        y| xj
                  |z  c_        y)aU  Adds a docstring for this namespace.

        The input docstring is normalized to have no leading whitespace and
        no trailing whitespace except for a newline at the end.

        If a docstring already exists, the new normalized docstring is appended
        to the end of the existing one with two newlines separating them.
        
N)
isinstancesix	text_typetyper
   rB   )r    	docstringnormalized_docstrings      r!   add_doczApiNamespace.add_docz   sQ     )S]]3DT)_D3))4t;88+DHHH,,Hr#   c                 \   | j                   j                  |       |j                  dk(  r|| j                  |j                  <   |j                  | j
                  vr!t               | j
                  |j                  <   || j
                  |j                     j                  |j                  <   y )Nr	   )rC   appendr   rD   r&   rE   ApiRoutesByVersion
at_version)r    routes     r!   	add_routezApiNamespace.add_route   s    5!==A-2Duzz*::T000.@.BD

+DIEJJ'225==Ar#   c                 l    | j                   j                  |       || j                  |j                  <   y r   )rF   rY   rG   r&   )r    r;   s     r!   add_data_typezApiNamespace.add_data_type   s(    y)1:y~~.r#   c                 l    | j                   j                  |       || j                  |j                  <   y r   )rH   rY   rI   r&   )r    r:   s     r!   	add_aliaszApiNamespace.add_alias   s(    E").5::&r#   c                 l    | j                   j                  |       || j                  |j                  <   y r   )rJ   rY   rK   r&   )r    r<   s     r!   add_annotationzApiNamespace.add_annotation   s*    
+3=
0r#   c                 l    | j                   j                  |       || j                  |j                  <   y r   )rL   rY   rM   r&   )r    r=   s     r!   add_annotation_typez ApiNamespace.add_annotation_type   s,    $$_5=L$$_%9%9:r#   c                     | j                   |j                   k7  sJ d       | j                  j                  |t                     }|rd|_        |rd|_        |rd|_        |rd|_        yy)a  
        Keeps track of namespaces that this namespace imports.

        Args:
            namespace (Namespace): The imported namespace.
            imported_alias (bool): Set if this namespace references an alias
                in the imported namespace.
            imported_data_type (bool): Set if this namespace references a
                data type in the imported namespace.
            imported_annotation (bool): Set if this namespace references a
                annotation in the imported namespace.
            imported_annotation_type (bool): Set if this namespace references an
                annotation in the imported namespace, possibly indirectly (by
                referencing an annotation elsewhere that has this type).
        zNamespace cannot import itself.TN)r&   rN   
setdefaultr8   r:   r;   r<   r=   )r    r/   imported_aliasimported_data_typeimported_annotationimported_annotation_typereasons          r!   add_imported_namespacez#ApiNamespace.add_imported_namespace   sn    , yyINN* 	.-	.***55iQFL#F $F#%)F" $r#   c                 h     g t                fd j                  D ]
  } |        S )ad  
        Returns a list of all data types used in the namespace. Because the
        inheritance of data types can be modeled as a DAG, the list will be a
        linearization of the DAG. It's ideal to generate data types in this
        order so that composite types that reference other composite types are
        defined in the correct order.
        c                     | v ry | j                   k7  ry t        |       r| j                  r | j                         j                  |        j	                  |        y r   )r/   r   parent_typerY   add)r;   r_   linearized_data_typesseen_data_typesr    s    r!   r_   z8ApiNamespace.linearize_data_types.<locals>.add_data_type   sY    O+$$, +	0E0Ei334!((3	*r#   )setrF   )r    r;   r_   rr   rs   s   ` @@@r!   linearize_data_typesz!ApiNamespace.linearize_data_types   s7     !#%
	+ I)$ ) %$r#   c                 h     g t                fd j                  D ]
  } |        S )z
        Returns a list of all aliases used in the namespace. The aliases are
        ordered to ensure that if they reference other aliases those aliases
        come earlier in the list.
        c                     | v ry | j                   k7  ry t        | j                        r | j                         j                  |        j	                  |        y r   )r/   r   r;   rY   rq   )r:   ra   linearized_aliasesseen_aliasesr    s    r!   ra   z1ApiNamespace.linearize_aliases.<locals>.add_alias   sQ    $D((%//*%%e,U#r#   )rt   rH   )r    r:   ra   rx   ry   s   ` @@@r!   linearize_aliaseszApiNamespace.linearize_aliases   s7      u		$ \\Ee " "!r#   c                 |    t               }| j                  D ]  }|| j                  |      z  } t        |d       S )a  
        Returns a list of all user-defined data types that are referenced as
        either an argument, result, or error of a route. If a List or Nullable
        data type is referenced, then the contained data type is returned
        assuming it's a user-defined type.
        c                     | j                   S r   r&   dts    r!   <lambda>z6ApiNamespace.get_route_io_data_types.<locals>.<lambda>  s    r#   key)rt   rC   !get_route_io_data_types_for_router)   )r    rF   r\   s      r!   get_route_io_data_typesz$ApiNamespace.get_route_io_data_types  s<     U
[[E$@@GGJ !j&899r#   c                 6   t               }|j                  |j                  |j                  fD ]h  }t	        |      st        |      r&|}|j                  }t	        |      rt        |      r&t        |      st        |      sV|}|j                  |       j |S )zV
        Given a route, returns a set of its argument/result/error datatypes.
        )
rt   arg_data_typeresult_data_typeerror_data_typer   r   r;   r   r   rq   )r    r\   rF   dtypedata_list_typedata_user_types         r!   r   z.ApiNamespace.get_route_io_data_types_for_route  s    
 U
))5+A+A5CXCXYEu%)9%)@!&&00 u%)9%)@ !'8E?!&~. Z r#   c                 P   g }| j                   j                         D ]s  \  }}|r|j                  s|s%|j                  s|j                  s|j                  s<|s%|j                  s|j                  s|j
                  sc|j                  |       u |j                  d        |S )a  
        Returns a list of Namespace objects. A namespace is a member of this
        list if it is imported by the current namespace and a data type is
        referenced from it. Namespaces are in ASCII order by name.

        Args:
            must_have_imported_data_type (bool): If true, result does not
                include namespaces that were not imported for data types.
            consider_annotations (bool): If false, result does not include
                namespaces that were only imported for annotations
            consider_annotation_types (bool): If false, result does not
                include namespaces that were only imported for annotation types.

        Returns:
            List[Namespace]: A list of imported namespaces.
        c                     | j                   S r   r}   ns    r!   r   z6ApiNamespace.get_imported_namespaces.<locals>.<lambda>@  s    qvvr#   r   )rN   itemsr;   r:   r=   r<   rY   sort)r    must_have_imported_data_typeconsider_annotationsconsider_annotation_typesimported_namespacesimported_namespacerl   s          r!   get_imported_namespacesz$ApiNamespace.get_imported_namespaces  s    * !*.*C*C*I*I*K&+F4D4D($$8N8N-$$8I8I&&'9: +L 	  %5 6""r#   c                     t        | j                         d       }t               }|D ]-  }|j                  | k7  s|j	                  |j                         / t        |d       S )a  
        Returns a list of Namespace objects. A namespace is a member of this
        list if it is imported by the current namespace and has a data type
        from it referenced as an argument, result, or error of a route.
        Namespaces are in ASCII order by name.
        c                     | j                   S r   r}   r~   s    r!   r   zBApiNamespace.get_namespaces_imported_by_route_io.<locals>.<lambda>L  s    RWWr#   r   c                     | j                   S r   r}   r   s    r!   r   zBApiNamespace.get_namespaces_imported_by_route_io.<locals>.<lambda>Q  s    166r#   )r)   r   rt   r/   rq   )r    namespace_data_typesreferenced_namespacesr;   s       r!   #get_namespaces_imported_by_route_ioz0ApiNamespace.get_namespaces_imported_by_route_ioC  sb      &d&B&B&D*< > #-I""d*%)))*=*=> . +1ABBr#   c                     | j                   j                  d        | j                  j                  d        | j                  j                  d        | j                  j                  d        y)zQ
        Alphabetizes routes to make route declaration order irrelevant.
        c                     | j                   S r   r}   )r\   s    r!   r   z(ApiNamespace.normalize.<locals>.<lambda>X  s    5::r#   r   c                     | j                   S r   r}   )r;   s    r!   r   z(ApiNamespace.normalize.<locals>.<lambda>Y  s    9>>r#   c                     | j                   S r   r}   )r:   s    r!   r   z(ApiNamespace.normalize.<locals>.<lambda>Z  s    EJJr#   c                     | j                   S r   r}   )r<   s    r!   r   z(ApiNamespace.normalize.<locals>.<lambda>[  s    Z__r#   N)rC   r   rF   rH   rJ   r>   s    r!   r,   zApiNamespace.normalizeS  s^    
 	56!AB67"DEr#   c                 J    t        d      j                  | j                        S )NzApiNamespace({!r}))strformatr&   r>   s    r!   __repr__zApiNamespace.__repr__]  s    '(//		::r#   N)FFFF)FFF)r2   r3   r4   r5   r"   rW   r]   r_   ra   rc   re   rm   ru   rz   r   r   r   r   r,   r   r6   r#   r!   r   r   c   sw    '$-"J;
/
>
M /427388= *D%:"4:  >C5::?$#LC F;r#   c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)ApiRoutez%
    Represents an API endpoint.
    c                     || _         || _        || _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        y)z
        :param str name: Designated name of the endpoint.
        :param int version: Designated version of the endpoint.
        :param ast_node: Raw route definition from the parser.
        N)
r&   r   	_ast_node
deprecatedraw_docrB   r   r   r   attrs)r    r&   r   ast_nodes       r!   r"   zApiRoute.__init__g  sO     	! ! $#
r#   c                 x    || _         || _        t        |      | _        || _        || _        || _        || _        y)a]  
        Converts a forward reference definition of a route into a full
        definition.

        :param DeprecationInfo deprecated: Set if this route is deprecated.
        :param str doc: Description of the endpoint.
        :type arg_data_type: :class:`stone.data_type.DataType`
        :type result_data_type: :class:`stone.data_type.DataType`
        :type error_data_type: :class:`stone.data_type.DataType`
        :param dict attrs: Map of string keys to values that are either int,
            float, bool, str, or None. These are the route attributes assigned
            in the spec.
        N)r   r   r
   rB   r   r   r   r   )r    r   rB   r   r   r   r   s          r!   set_attributeszApiRoute.set_attributes~  s<     %c?* 0.
r#   c                     | j                   dk(  r| j                  S dj                  | j                  | j                         S )z
        Get user-friendly representation of the route.

        :return: Route name with version suffix. The version suffix is omitted for version 1.
        r	   z{}:{})r   r&   r   r>   s    r!   name_with_versionzApiRoute.name_with_version  s3     <<199>>$))T\\::r#   c                 @    dj                  | j                               S )NzApiRoute({}))r   r   r>   s    r!   r   zApiRoute.__repr__  s    $$T%;%;%=>>r#   c                 x   t        |t              st        dj                  |            t        |t              st        dj                  |            |j                  |j                  k  s|j
                  |j
                  k  ry|j                  |j                  kD  s|j
                  |j
                  kD  ryy)Nz Expected ApiRoute for object: {}r	   r   )rQ   r   	TypeErrorr   r&   r   )r    lhsrhss      r!   _comparezApiRoute._compare  s    #x(>EEcJKK#x(>EEcJKK88chh#++";XX CKK#++$=r#   c                 ,    | j                  | |      dk  S Nr   r   r    others     r!   __lt__zApiRoute.__lt__      }}T5)A--r#   c                 ,    | j                  | |      dkD  S r   r   r   s     r!   __gt__zApiRoute.__gt__  r   r#   c                 ,    | j                  | |      dk(  S r   r   r   s     r!   __eq__zApiRoute.__eq__      }}T5)Q..r#   c                 ,    | j                  | |      dk  S r   r   r   s     r!   __le__zApiRoute.__le__  r   r#   c                 ,    | j                  | |      dk\  S r   r   r   s     r!   __ge__zApiRoute.__ge__  r   r#   c                 ,    | j                  | |      dk7  S r   r   r   s     r!   __ne__zApiRoute.__ne__  r   r#   c                    t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  t        | j                        f
      S r   )hashr&   r   r   r   r   rB   r   r   r   idr   r>   s    r!   __hash__zApiRoute.__hash__  sd    IILLNNOOLLHH!!  tzzN
  	r#   N)r2   r3   r4   r5   r"   r   r   r   r   r   r   r   r   r   r   r   r6   r#   r!   r   r   b  sC    ..	;?..////r#   r   c                       e Zd ZddZy)DeprecationInfoNc                 V    | t        |t              sJ t        |             || _        y)zR
        :param ApiRoute by: The route that replaces this deprecated one.
        N)rQ   r   reprby)r    r   s     r!   r"   zDeprecationInfo.__init__  s(    
 zZH5?tBx?5r#   r   )r2   r3   r4   r"   r6   r#   r!   r   r     s    r#   r   c                       e Zd ZdZd Zy)rZ   zD
    Represents routes of different versions for a common name.
    c                     i | _         y)zR
        :param at_version: The dict mapping a version number to a route.
        N)r[   r>   s    r!   r"   zApiRoutesByVersion.__init__  s    
 r#   Nr?   r6   r#   r!   rZ   rZ     s    r#   rZ   )(
__future__r   r   r   r   collectionsr   distutils.versionr   rR   rF   r
   r   r   r   r   _MYPYtypingr   r   r   r   r   DataTypeListr   r   r   stone.frontend.astr   PY3DictTextNamespaceDictobjectr   r8   r   r   r   rZ   r6   r#   r!   <module>r      s    R R # + 
  		 	 	 /
 wwFKK$?@FKK$@A))& ))X
%F 
%|;6 |;~fv fRf 
 
r#   