
                         t   d 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	 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 ddlmZ ddlZdZ G d dej,                        Z G d de      Z G d de      Zd Zd Zd Z G d de      Z G d de      Z G d de	j>                        Z d Z!y) z'The meta cache command library support.    )absolute_import)division)unicode_literals)	apis_util)parser_completer)walker)
completers)
exceptions)module_util)	resources)
file_cache)resource_cacheNzresource://c                       e Zd ZdZy)ErrorzBase cache exception.N__name__
__module____qualname____doc__     1lib/googlecloudsdk/command_lib/meta/cache_util.pyr   r   '   s    r   r   c                       e Zd ZdZy)NoTablesMatchedz$No table names matched the patterns.Nr   r   r   r   r   r   +   s    ,r   r   c                   \    e Zd ZdZej
                  ej                  dZddZ	d Z
d Zd Zy)	GetCachezBContext manager for opening a cache given a cache identifier name.)fileresourcec                 .    || _         || _        d| _        y)a  Constructor.

    Args:
      name: The cache name to operate on. May be prefixed by "resource://" for
        resource cache names or "file://" for persistent file cache names. If
        only the prefix is specified then the default cache name for that prefix
        is used.
      create: Creates the persistent cache if it exists if True.

    Raises:
      CacheNotFound: If the cache does not exist.

    Returns:
      The cache object.
    N)_name_create_cache)selfnamecreates      r   __init__zGetCache.__init__7   s      DJDLDKr   c                 x    	  ||| j                         S # t        j                  $ r}t        |      d }~ww xY w)N)r%   )r!   cache_exceptionsr   )r#   cache_classr$   es       r   
_OpenCachezGetCache._OpenCacheK   s7    dll33!! !Hns    949c                    | j                   rt        j                  | j                        D ]g  \  }}| j                   j	                  |dz         s%| j                   t        |      dz   d  }|sd }| j                  ||      | _        | j                  c S  | j                  t        j                  | j                         | _        | j                  S )Nz://   )
r    six	iteritems_TYPES
startswithlenr+   r"   r   ResourceCache)r#   cache_idr)   r$   s       r   	__enter__zGetCache.__enter__Q   s    zz#&==#=
(K::  E!12CMA-./$DT:$+
 $> //.">">

KDK;;r   c                 @    | j                   j                  |d u        y )N)commit)r"   Close)r#   typvalue	tracebacks       r   __exit__zGetCache.__exit__^   s    KKSD[)r   N)F)r   r   r   r   r   Cacher   r3   r0   r&   r+   r5   r<   r   r   r   r   r   /   s4    J  ..&
(*r   r   c                  z    	 t        j                          y# t        j                  $ r} t        |       d} ~ ww xY w)z8Deletes the resource cache regardless of implementation.N)r   Deleter(   r   )r*   s    r   r?   r?   b   s9     
 
		 
(Ns    :5:c                 \    | j                  ddt        dj                  t                     y)z-Adds the persistent cache flag to the parser.z--cache
CACHE_NAMEzThe cache name to operate on. May be prefixed by "{}" for resource cache names. If only the prefix is specified then the default cache name for that prefix is used.)metavardefaulthelpN)add_argument_CACHE_RI_DEFAULTformat)parsers    r   AddCacheFlagrI   k   s0    ::@&!;#  %r   c                     d}	 | j                         D ]?  }|t        j                  k(  r n*|j                  j	                  d      s4|j                  }A |st        |       rd}|S # t
        $ r Y w xY w)z0Returns the completer type name given its class.N	Completerfunction)mror	   ResourceCompleterr   endswithAttributeErrorcallable)completer_classcompleter_typets      r   _GetCompleterTyperU   w   sv    .	  "	
j**	*	
		[	)	 # 
H_5N		 
 		s   AA' A' '	A32A3c                       e Zd Zd Zy)_CompleterModulec                 X    || _         || _        || _        || _        g | _        i | _        y N)module_path
collectionapi_versiontypeattachments_attachments_dict)r#   rZ   r[   r\   rS   s        r   r&   z_CompleterModule.__init__   s0    "D DO"DDIDDr   Nr   r   r   r&   r   r   r   rW   rW      s     r   rW   c                       e Zd Zd Zy)_CompleterAttachmentc                      || _         g | _        y rY   )command	arguments)r#   rd   s     r   r&   z_CompleterAttachment.__init__   s    DLDNr   Nr`   r   r   r   rb   rb      s    r   rb   c                   (     e Zd ZdZ fdZd Z xZS )_CompleterModuleGeneratorz#Constructs a CLI command dict tree.c                 :    t         t        |   |       i | _        y rY   )superrg   r&   _modules_dict)r#   cli	__class__s     r   r&   z"_CompleterModuleGenerator.__init__   s    	
#T3C8Dr   c           	      ~   d }|j                   }t        |j                  |j                  z   |      D ]  }	 |j                  }d}d}	t        |t        j                        r|j                  }t        j                  |      }
t        |t              r"	  |       }	 |j                  }	 |j                  }	|j(                  r|j(                  d   }n|j*                  j-                  dd      }| j.                  j1                  |
      }|s't3        |
||	t5        |            }|| j.                  |
<   d	j7                  |j9                               }|j:                  j1                  |      }|s5t=        |      }||j:                  |<   |j>                  jA                  |       |jB                  jA                  |        | j.                  S # t
        $ r Y w xY w# t
        $ r Y @w xY w# t
        $ r Y Cw xY w# t        j                   t"        j$                  f$ r}dj'                  |      }Y d}~d}~ww xY w)
aE  Visits each command in the CLI command tree to construct the module list.

    Args:
      command: group/command CommandCommon info.
      parent: The parent Visit() return value, None at the top level.
      is_group: True if command is a group, otherwise its is a command.

    Returns:
      The subtree module list.
    c                 "    | j                         S rY   )__repr__)actions    r   
_ActionKeyz3_CompleterModuleGenerator.Visit.<locals>._ActionKey   s    __r   )keyNz	ERROR: {}r   _-)rZ   r[   r\   rS    )"aisorted	flag_argspositional_args	completerrP   
isinstancer   ArgumentCompleterrR   r   GetModulePathr]   r[   r\   r   UnknownAPIErrorr   InvalidCollectionExceptionrG   option_stringsdestreplacerj   getrW   rU   joinGetPathr_   rb   r^   appendre   )r#   rd   parentis_grouprq   argsargrR   r[   r\   rZ   rz   r*   r$   modulecommand_path
attachments                    r   Visitz_CompleterModuleGenerator.Visit   s    ::Ddnnt';';;LL-- jk	O%5%G%G	H)99--o>k	OT	*	-%')"--J#//K 
		!!!$xxS)!!%%k2f!#!#,_=	
 +1;'XXgoo/0l++//=j),7
1;  .!!*-!!$'Y MZ U        ))446 	-"))!,*	-se   GG=G*G-	GG	G*&G=)G**G=-	G:6G=9G::G==#H< H77H<)r   r   r   r   r&   r   __classcell__)rl   s   @r   rg   rg      s    +=r   rg   c                 b    t        t        |       j                         j                               S )z@Returns the list of all attached CompleterModule objects in cli.)listrg   Walkvalues)rk   s    r   ListAttachedCompletersr      s%    	',113::<	==r   )"r   
__future__r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   googlecloudsdk.command_lib.utilr	   googlecloudsdk.corer
   r   r   googlecloudsdk.core.cacher(   r   r   r.   rF   r   r   objectr   r?   rI   rU   rW   rb   Walkerrg   r   r   r   r   <module>r      s     . &  ' 1 4 * 6 * + ) D 0 4 
 " J -e -0*v 0*f	%  v  6 D DN>r   