a
    ij                     @  s   d Z ddlmZ ddlZ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
Z
ddlmZ ddlmZ ddlZejG dd de
jZd	d
dddZd
d
dddZdS )zUtils to handle resources.    )annotationsN)Union)abstract_path)registerc                   @  sp   e Zd ZdZddddZddddZd	dd
dZejdk rVddddZ	dd Z
ejdk rledd ZdS )ResourcePathzWrapper around `zipfile.Path` compatible with `os.PathLike`.

  Note: Calling `os.fspath` on the path will extract the file so should be
  discouraged.
  str)returnc                 C  s   t ddS )a  Path string for `os.path.join`, `open`,...

    compatibility.

    Note: Calling `os.fspath` on the path extract the file, so should be
    discouraged. Prefer using `read_bytes`,... This only works for files,
    not directories.

    Returns:
      the extracted path string.
    z*zipapp not supported. Please send us a PR.N)NotImplementedErrorself r   X/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/epath/resource_utils.py
__fspath__)   s    zResourcePath.__fspath__boolc                 C  s(   t | t |ko&| j|jko&| j|jkS N)typerootat)r   otherr   r   r   __eq__9   s
    

zResourcePath.__eq__intc                 C  s   t | j| jfS r   )hashr   r   r
   r   r   r   __hash__@   s    zResourcePath.__hash__)   
   z'ResourcePath'c                 C  s   t | | j|S r   )r   r   )r   r   r   r   r   _nextE   s    zResourcePath._nextc                 G  s&   t j| jg|R  }| | j|S )z:Overwrite `joinpath` to be consistent with `pathlib.Path`.)	posixpathjoinr   r   r   resolve_dir)r   r   Znext_r   r   r   joinpathI   s    zResourcePath.joinpath)r      c                 C  s   t | jjp| jjS r   )pathlibPathr   suffixfilenamer
   r   r   r   r#   P   s    zResourcePath.suffixN)__name__
__module____qualname____doc__r   r   r   sysversion_infor   r   propertyr#   r   r   r   r   r   !   s   

r   zUnion[str, types.ModuleType]zabstract_path.Path)packager   c                 C  s   t | }t|t jjjr<ttj	|  j
}|jdkr<|j}t|tjrRt|S t|tjrzt|j|j}ttj|S tdt| d| dS )a  Returns read-only root directory path of the module.

  Used to access module resource files.

  Usage:

  ```python
  path = epath.resource_path('tensorflow_datasets') / 'README.md'
  content = path.read_text()
  ```

  This is compatible with everything, including zipapp (`.par`).

  Resource files should be in the `data=` of the `py_library(` (when using
  bazel).

  To write to your project (e.g. automatically update your code), read-only
  resource paths can be converted to read-write paths with
  `epath.to_write_path(path)`.

  Args:
    package: Module or module name.

  Returns:
    The read-only path to the root module directory
  z__init__.pyzUnknown resource path: z: N)importlib_resourcesfiles
isinstanceZ	_adaptersZCompatibilityFilesZSpecPathr!   r"   r)   modules__file__nameparentr   zipfiler   r   r   typingcast	TypeErrorr   )r,   pathr   r   r   resource_pathU   s    



r9   )r8   r   c                 C  s   | S )z4Cast the `epath.resource_path` to a read-write Path.r   )r8   r   r   r   to_write_path   s    r:   )r(   
__future__r   	itertoolsr!   r   r)   typesr5   r   r4   Zetils.epathr   r   r-   Zregister_path_clsr"   r   r9   r:   r   r   r   r   <module>   s    39