o
    wi$                     @  s   d dl mZ d dlZd dlmZm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 d d	lmZmZ d d
lmZ d dlmZ d dlmZ d)ddZG dd deZd*ddZ	d+d,ddZG dd dejZ				d-d.d'd(Z dS )/    )annotationsN)IterableMapping)Enum)Any)urlparse)gql)TypeInfo)astvisitor)ValidationContext)one)Apireturnstrc                 C  sX   d}t | }t|| }|std|jd^}}|jd}d| d| }|S )a  Convert an S3 HTTP(S) URL to an S3 URI.

    Arguments:
        url (str): The S3 URL to convert, in the format
                   'http(s)://<bucket>.s3.<region>.amazonaws.com/<key>'.
                   or 'http(s)://<bucket>.s3.amazonaws.com/<key>'

    Returns:
        str: The corresponding S3 URI in the format 's3://<bucket>/<key>'.

    Raises:
        ValueError: If the provided URL is not a valid S3 URL.
    z ^https?://.*s3.*amazonaws\.com.*zInvalid S3 URL./zs3://)	r   rematchgeturl
ValueErrornetlocsplitpathlstrip)url
s3_pattern
parsed_urlr   bucket_name_keys3_uri r"   K/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/public/utils.pyparse_s3_url_to_s3_uri   s   r$   c                   @  s   e Zd ZdZdZdZdS )PathTypezWe have lots of different paths users pass in to fetch artifacts, projects, etc.

    This enum is used for specifying what format the path is in given a string path.
    PROJECTARTIFACTN)__name__
__module____qualname____doc__r&   r'   r"   r"   r"   r#   r%   3   s    r%   r   	path_typec                 C  sT   ddl m} | d}|tjkrdnd}t||kr(|dd \}}||r(|S dS )a  Parse the org from a registry path.

    Essentially fetching the "entity" from the path but for Registries the entity is actually the org.

    Args:
        path (str): The path to parse. Can be a project path <entity>/<project> or <project> or an
        artifact path like <entity>/<project>/<artifact> or <project>/<artifact> or <artifact>
        path_type (PathType): The type of path to parse.
    r   )is_artifact_registry_projectr         N )wandb.sdk.artifacts._validatorsr-   r   r%   r'   len)r   r,   r-   partsexpected_partsorgprojectr"   r"   r#   parse_org_from_registry_path=   s   

r7   settingsdictdefault_entity
str | Nonec                 C  s\   |  d }du r,|  dp|}|du rtdt |}t|tdtdd}|j}|S )a  Fetch the org from either the settings or deriving it from the entity.

    Returns the org from the settings if available. If no org is passed in or set, the entity is used to fetch the org.

    Args:
        organization (str | None): The organization to fetch the org for.
        settings (dict): The settings to fetch the org for.
        default_entity (str | None): The default entity to fetch the org for.
    organizationNentityz@No entity specified and can't fetch organization from the entityzRNo organizations found for entity. Please specify an organization in the settings.zXMultiple organizations found for entity. Please specify an organization in the settings.)	too_shorttoo_long)getr   InternalApi(_fetch_orgs_and_org_entities_from_entityr   display_name)r8   r:   r<   r=   entity_orgs
entity_orgr"   r"   r#   !fetch_org_from_settings_or_entityS   s$   	rF   c                   @  s~   e Zd ZdZ				d(d)d	d
Zd*ddZd+ddZd,ddZd-ddZd.ddZ	d/ddZ
d0d!d"Zd1d$d%Zd1d&d'ZdS )2_GQLCompatRewriterz]GraphQL AST visitor to rewrite queries/mutations to be compatible with older server versions.Nomit_variablesIterable[str] | Noneomit_fragmentsomit_fieldsrename_fieldsMapping[str, str] | Nonec                 C  s<   t |pd| _t |pd| _t |pd| _t|pi | _d S )Nr"   )setrH   rJ   rK   r9   rL   )selfrH   rJ   rK   rL   r"   r"   r#   __init__w   s   z_GQLCompatRewriter.__init__nodeast.Documentr   r   c                   sX   t d |td dddd |jD }fdd|D }||B   fdd|jD |_d S )N)schema)rS   r
   	type_infoc                 S  s   h | ]
}t |tjr|qS r"   )
isinstancer
   OperationDefinition.0dfnr"   r"   r#   	<setcomp>   s
    z4_GQLCompatRewriter.leave_Document.<locals>.<setcomp>c                   s    h | ]}  |D ]}|q	qS r"   )$get_recursively_referenced_fragments)rX   opfrag)ctxr"   r#   rZ      s    c                   s   g | ]}| v r|qS r"   r"   rW   )allowed_defnsr"   r#   
<listcomp>   s    z5_GQLCompatRewriter.leave_Document.<locals>.<listcomp>)r   r	   definitions)rO   rQ   r   __operation_defnsused_fragment_defnsr"   )r_   r^   r#   leave_Document   s   
z!_GQLCompatRewriter.leave_Documentast.Variablec                 O     |j j| jv r
tjS d S NnamevaluerH   r   REMOVErO   rQ   r   rb   r"   r"   r#   enter_Variable      z!_GQLCompatRewriter.enter_Variableast.VariableDefinitionc                 O     |j tju r	tjS d S rh   )variabler   rl   rm   r"   r"   r#   leave_VariableDefinition   s   	z+_GQLCompatRewriter.leave_VariableDefinitionast.ObjectFieldc                 O  rq   rh   )rk   r   rl   rm   r"   r"   r#   leave_ObjectField   s   z$_GQLCompatRewriter.leave_ObjectFieldast.Argumentc                 O  rg   rh   ri   rm   r"   r"   r#   enter_Argument   ro   z!_GQLCompatRewriter.enter_Argumentast.FragmentDefinitionc                 O  rg   rh   rj   rk   rJ   r   rl   rm   r"   r"   r#   enter_FragmentDefinition   ro   z+_GQLCompatRewriter.enter_FragmentDefinitionast.FragmentSpreadc                 O  rg   rh   ry   rm   r"   r"   r#   enter_FragmentSpread   ro   z'_GQLCompatRewriter.enter_FragmentSpread	ast.Fieldc                 O  s8   |j j| jv r
tjS | j|j j }r||j _d S d S rh   )rj   rk   rK   r   rl   rL   r@   )rO   rQ   r   rb   new_namer"   r"   r#   enter_Field   s
   z_GQLCompatRewriter.enter_Fieldc                 O  s    |j d ur|j jstjS d S d S rh   )selection_set
selectionsr   rl   rm   r"   r"   r#   leave_Field   s   z_GQLCompatRewriter.leave_FieldNNNN)rH   rI   rJ   rI   rK   rI   rL   rM   )rQ   rR   r   r   )rQ   rf   r   r   )rQ   rp   r   r   )rQ   rt   r   r   )rQ   rv   r   r   )rQ   rx   r   r   )rQ   r{   r   r   )rQ   r}   r   r   )r(   r)   r*   r+   rP   re   rn   rs   ru   rw   rz   r|   r   r   r"   r"   r"   r#   rG   t   s     







rG   request_stringrH   rI   rJ   rK   rL   rM   rR   c                 C  s8   t | }|s|s|s|s|S t||||d}t||S )a  Rewrite a GraphQL request string to ensure compatibility with older server versions.

    Args:
        request_string (str): The GraphQL request string to rewrite.
        omit_variables (Iterable[str] | None): Names of variables to remove from the request string.
        omit_fragments (Iterable[str] | None): Names of fragments to remove from the request string.
        omit_fields (Iterable[str] | None): Names of fields to remove from the request string.
        rename_fields (Mapping[str, str] | None):
            A mapping of fields to rename in the request string, given as `{old_name -> new_name}`.

    Returns:
        str: Modified GraphQL request string with fragments on omitted types removed.
    )rH   rJ   rK   rL   )r   rG   r   visit)r   rH   rJ   rK   rL   docrewriterr"   r"   r#   
gql_compat   s   r   )r   r   )r   r   r,   r%   r   r   rh   )r8   r9   r:   r;   r   r   r   )r   r   rH   rI   rJ   rI   rK   rI   rL   rM   r   rR   )!
__future__r   r   collections.abcr   r   enumr   typingr   urllib.parser   	wandb_gqlr   wandb_graphqlr	   wandb_graphql.languager
   r   #wandb_graphql.validation.validationr   wandb._iterutilsr   wandb.sdk.internal.internal_apir   rA   r$   r%   r7   rF   VisitorrG   r   r"   r"   r"   r#   <module>   s.    
!

!Y