
    \h                         S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
r
S SKrSS jrSS jrSS	 jrSS
 jrS rS rS rSS jrS rg)    )
exceptions)	from_file)service_mapping)alloy_constants)get_error_messageNc                     U (       af  [         R                  R                  U R                  S   5      n[	        XR                  S   5        [         R                  R                  U5      S   $ g)aA  
Returns the path where the oci config is located. If no context were provided, returns None.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Returns:
    str: A path to the directory that the oci config is located [~/.oci], or None if the config is not found.
config_fileprofiler   N)ospath
expanduserobjr   splitctxr   s     8D:\TimeStamps\venv\Lib\site-packages\alloy/alloy_util.py_get_dot_oci_pathr      sR     ww!!#''-"89$	*+ww}}T"1%%	     c                 H    U (       a7  [         R                  R                  [        U 5      [        R
                  5      O,[         R                  R                  [        R                  5      n[         R                  R                  U5      (       a  U$ g! [         a     gf = f)a  
Assumes that the service config file is named "alloy-config.json."
Finds and returns the path to the service config if it exists in the same directory as the config file; otherwise, None.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Returns:
    str: A valid path to the service config file, or None if the service config is not found.
N)
r   r   joinr   r   DEFAULT_ALLOY_CONFIG_NAMEr   ALLOY_CONFIG_DEFAULT_LOCATION	Exceptionisfiler   s     r   get_service_config_pathr   !   sw    berww||-c2O4]4]^##O$Q$QR 	 
ww~~d   s   A+B 
B! B!c                 $   [        U 5      nU(       d-  [        R                  " [        [        R
                  5      5      e [        US5       n[        R                  " U5      sSSS5        $ ! , (       d  f       g= f! [         a  nUeSnAff = f)a  
Finds the service config file from the same directory as the oci config file. Returns the content of the service config as a dict.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Raises:
    exceptions.ConfigFileNotFound: alloy-config.json isn't found in the same directory as the oci config file.
    e: _description_

Returns:
    dict: The content of the alloy-config.json.
rN)
r   r   ConfigFileNotFoundr   r   NO_SERVICE_CONFIGopenjsonloadr   )r   r   fes       r   get_service_configr%   :   sg     #3'D++,=o>_>_,`aa$_99Q< __ s6   A> A-#	A> -
A;7A> ;A> >
BB

Bc                     [        U 5      nUR                  S5      (       a  US   $ [        R                  " [	        [
        R                  5      5      e)aY  
Finds and returns the name of the provider as specified in the service config file.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Raises:
    exceptions.InvalidConfig: Provider name is not specified in the service config file as `provider`.

Returns:
    str: Name of the provider.
alloyProvider)r%   getr   InvalidConfigr   r   PROVIDER_NAME_MISSINGr   configs     r   get_provider_namer-   U   sE      $Fzz/""o&&

"
"#4_5Z5Z#[
\\r   c                     [        U 5      nUR                  S5      (       a  US   $ [        R                  " [	        [
        R                  5      5      e)aq  
Finds and returns the list of subscribed services as specified in the service config file.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Raises:
    exceptions.InvalidConfig: Subscribed services is not specified in the service config file as `services`.

Returns:
    [str]: A list of subscribed services.
services)r%   r(   r   r)   r   r   SUBSCRIBED_SERVICES_MISSINGr+   s     r   get_subscribed_servicesr1   l   sE      $Fzz*j!!

"
"#4_5`5`#a
bbr   c                     U (       a7  [         R                  R                  [        U 5      [        R
                  5      $ [        R                  $ )aP  
Finds service_mapping.json from the same directory as the config file and returns the path.
If no context were provided, returns the default path [~/.oci/service_mapping.json].

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Returns:
    str: A path to service_mapping.json.
)r   r   r   r   r   "DEFAULT_SUBSCRIBED_SERVICE_MAPPING+DEFAULT_SUBSCRIBED_SERVICE_MAPPING_LOCATION)r   s    r   get_service_mapping_pathr5      s4     ww||-c2O4f4fggFFFr   c                    [        U 5      n0 n[        R                  " 5        H  u  p4US   U;   d  M  XBU'   M     [        [	        U 5      S5       n[
        R                  " X%5        SSS5        U$ ! , (       d  f       U$ = f)aZ  
Creates a service_mapping.json file with content similar to service_mapping.py, except it only contains information of the subscribed
services according to alloy-config.json.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Returns:
    {str: str}: A dictionary of subscribed services.
r   zw+N)r1   r   itemsr    r5   r!   dump)r   subscribed_servicesr/   kvr#   s         r   write_subscribed_servicesr<      sy     2#6H%%'Q4&&QK ( 
&s+T	2a		( 
3 O 
3	2 Os   A11
B c                     [        [        U 5      S5       n[        R                  " U5      sSSS5        $ ! , (       d  f       g= f! [        [        R
                  4 a  nU (       a  [        U 5      s SnA$ UeSnAff = f)ag  
Returns a service_mapping-equivalent (dict[str, str]) of the subscribed services.

Only attempts to write service_mapping.json in the same directory as oci config if context were given.
Otherwise, raises json errors if service_mapping.json was not found or is corrupted.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Raises:
    FileNotFoundError: service_mapping.json is not found and ctx was not provided.
    JSONDecodeError: service_mapping.json is corrupted and ctx was not provided.

Returns:
    {str: str}: A dictionary of subscribed services.
r   N)r    r5   r!   r"   FileNotFoundErrorJSONDecodeErrorr<   )r   r#   r$   s      r   read_subscribed_servicesr@      s]    "*3/599Q< 655t334 ,S11s=   A 7	A 
AA A B "A;3B 9A;;B c                 L    [        U 5      n[        UR                  " 5       5      $ )a  
Finds and returns a list of subscribed services in alloy config file.

Args:
    ctx (click.Context): A click.Context should include information such as the path to oci config.

Raises:
    FileNotFoundError: service_mapping.json is not found and ctx was not provided.
    JSONDecodeError: service_mapping.json is corrupted and ctx was not provided.

Returns:
    [str]: A list of subscribed services.
)r@   listkeys)r   r   s     r   get_subscribed_services_listrD      s"     /s3O$$&''r   )N)ocir   
oci.configr   oci_cli.service_mappingr   alloyr   alloy.error_messagesr   r!   os.pathr   r   r   r%   r-   r1   r5   r<   r@   rD    r   r   <module>rL      sL   
    3 ! 2  &"26].c.G 04(r   