U
    Ph                     @  sn   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
mZmZmZ edZdd ZG d	d
 d
ZdS )    )annotations)
namedtuple)	iskeyword)dedentindent)AnyCallableIterableTypeVarTc                 C  s   |   ot|  S )zNReturns True if `name` is a valid Python variable name and also not a keyword.)isidentifierr   )name r   P/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/utils/component_store.pyis_variable   s    r   c                   @  s   e Zd ZdZeddZddddddZddd	d	d
ddZddddddZe	ddddZ
dddddZddddZddddZdd  Zdd!dd"d#Zdd!dd$d%Zd&S )'ComponentStorea  
    Represents a storage object for other objects (specifically functions) keyed to a name with a description.

    These objects act as global named places for storing components for objects parameterised by component names.
    Typically this is functions although other objects can be added. Printing a component store will produce a
    list of members along with their docstring information if present.

    Example:

    .. code-block:: python

        TestStore = ComponentStore("Test Store", "A test store for demo purposes")

        @TestStore.add_def("my_func_name", "Some description of your function")
        def _my_func(a, b):
            '''A description of your function here.'''
            return a * b

        print(TestStore)  # will print out name, description, and 'my_func_name' with the docstring

        func = TestStore["my_func_name"]
        result = func(7, 6)

    
_Component)descriptionvaluestrNone)r   r   returnc                 C  s8   i | _ || _|| _d| d| d| jp(d  | _d S )NComponent Store '': 
 )
componentsr   r   __doc__strip)selfr   r   r   r   r   __init__7   s    zComponentStore.__init__r   )r   descr   r   c                 C  s&   t |std| ||| j|< |S )zGStore the object `value` under the name `name` with description `desc`.z1Name of component must be valid Python identifier)r   
ValueErrorr   r   )r   r   r!   r   r   r   r   add>   s    zComponentStore.addr   )r   r!   r   c                   s    fdd}|S )z]Returns a decorator which stores the decorated function under `name` with description `desc`.c                   s     | S )z'Decorator to add a function to a store.)r#   )funcr!   r   r   r   r   decoI   s    z$ComponentStore.add_def.<locals>.decor   )r   r   r!   r&   r   r%   r   add_defF   s    zComponentStore.add_defztuple[str, ...])r   c                 C  s
   t | jS )z-
        Produces all factory names.
        )tupler   r   r   r   r   namesO   s    zComponentStore.namesbool)r   r   c                 C  s
   || j kS )z)Returns True if the given name is stored.)r   r   r   r   r   r   __contains__W   s    zComponentStore.__contains__intc                 C  s
   t | jS )z(Returns the number of stored components.)lenr   r)   r   r   r   __len__[   s    zComponentStore.__len__r	   c                 c  s$   | j  D ]\}}||jfV  q
dS )zYields name/component pairs.N)r   itemsr   )r   kvr   r   r   __iter___   s    zComponentStore.__iter__c                 C  s   d| j  d| j d}| j D ]j\}}|d| d7 }t|jdrz|jjrztt|jj	d
 d}|d| d7 }q |d	|j 7 }q |S )
Nr   r   z
Available components:z
* :r   r   z     )r   r   r   r1   hasattrr   r   r   r   lstriprstrip)r   resultr2   r3   docr   r   r   __str__d   s    zComponentStore.__str__r   c                 C  s$   || j kr| j | jS | |S dS )/Returns the stored object under the given name.N)r   r   __getattribute__r,   r   r   r   __getattr__q   s    
zComponentStore.__getattr__c                 C  s*   || j kr| j | jS td| ddS )r=   zComponent 'z' not foundN)r   r   r"   r,   r   r   r   __getitem__x   s    
zComponentStore.__getitem__N)__name__
__module____qualname__r   r   r   r    r#   r'   propertyr*   r-   r0   r4   r<   r?   r@   r   r   r   r   r      s   
	r   N)
__future__r   collectionsr   keywordr   textwrapr   r   typingr   r   r	   r
   r   r   r   r   r   r   r   <module>   s   