o
    i                     @   sH  U d Z ddlZddlmZ ddl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mZ ddlmZ daejdB ed	< d
Zeed e d ZdZdededefddZdedeeeef fddZdedeeeef  fddZdejddfddZ e!ee
ejgdf f Z"de"fddZ#ddee dB ddfddZ$dS ) zPydicom command line interface program

Each subcommand is a module within pydicom.cli, which
defines an add_subparser(subparsers) function to set argparse
attributes, and calls set_defaults(func=callback_function)

    N)entry_points)castAny)Callable)dcmread)get_charset_filesget_testdata_file)Dataset
subparsersz\w+(\[(-)?\d+\])?z(\.z)*$a[  File specification, in format [pydicom::]filename[::element]. If `pydicom::` prefix is present, then use the pydicom test file with that name. If `element` is given, use only that data element within the file. Examples: path/to/your_file.dcm, your_file.dcm::StudyDate, pydicom::rtplan.dcm::BeamSequence[0], yourplan.dcm::BeamSequence[0].BeamNumberdselementreturnc              
   C   sb   z
t d| d| iW S  ty   td| d ty0 } ztd| d| d }~ww )Nzds.r   zData element 'z' is not in the dataset'z' has an index error: )evalAttributeErrorargparseArgumentTypeError
IndexError)r   r   e r   F/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/cli/main.pyeval_element/   s   
r   filespecc                 C   s^   |  d^ }}|sd|dfS |d dkrdnd}|r |d |s'||dfS |d||fS )zParse the filespec format into prefix, filename, element

    Format is [prefix::filename::element]

    Note that ':' can also exist in valid filename, e.g. r'c:	emp	est.dcm'
    z:: r   pydicom)splitpopjoin)r   prefix_filelastprefixr   r   r   filespec_parts:   s   


r!   c           
   
   C   s,  t | \}}}z	ttt|}W n ty   d}Y nw |s4zt|}|r)|d }W n	 ty3   Y nw |dkr:|}|rJt|sJt	
d| dzt|dd}W n2 tyn   |rad| d	nd}t	
d
| d|  ty } zt	
d| d| d}~ww |s|dfgS t||}	||	fgS )ai  Utility to return a dataset and an optional data element value within it

    Note: this is used as an argparse 'type' for adding parsing arguments.

    Parameters
    ----------
    filespec: str
        A filename with optional `pydicom::` prefix and optional data element,
        in format:
            [pydicom::]<filename>[::<element>]
        If an element is specified, it must be a path to a data element,
        sequence item (dataset), or a sequence.
        Examples:
            your_file.dcm
            your_file.dcm::StudyDate
            pydicom::rtplan.dcm::BeamSequence[0]
            pydicom::rtplan.dcm::BeamSequence[0].BeamLimitingDeviceSequence

    Returns
    -------
    List[Tuple[Dataset, Any]]
        Matching pairs of (dataset, data element value)
        This usually is a single pair, but a list is returned for future
        ability to work across multiple files.

    Note
    ----
        This function is meant to be used in a call to an `argparse` library's
        `add_argument` call for subparsers, with name="filespec" and
        `type=filespec_parser`. When used that way, the resulting args.filespec
        will contain the return values of this function
        (e.g. use `ds, element_val = args.filespec` after parsing arguments)
        See the `pydicom.cli.show` module for an example.

    Raises
    ------
    argparse.ArgumentTypeError
        If the filename does not exist in local path or in pydicom test files,
        or if the optional element is not a valid expression,
        or if the optional element is a valid expression but does not exist
        within the dataset
    r   r   r   zComponent 'zD' is not valid syntax for a data element, sequence, or sequence itemT)forcez, 
but 'pydicom::z' test data file is availablezFile 'z' not foundzError reading 'z': N)r!   r   strr   
ValueErrorr   NotImplementedErrorre_file_spec_objectmatchr   r   r   FileNotFoundError	Exceptionr   )
r   r    filenamer   pydicom_filenamechar_filenamesr   extrar   data_elem_valr   r   r   filespec_parserR   sJ   +



r/   argsc                 C   sp   t d u r
td d S tt j }| jr#| j|v r#t j| j   d S td |d tdd|  d S )NzNo subcommands are availablez;Use pydicom help [subcommand] to show help for a subcommandhelpzAvailable subcommands: z, )	r
   printlistchoiceskeys
subcommand
print_helpremover   )r0   subcommandsr   r   r   help_command   s   
r:   c                  C   s&   i } t ddD ]	}| | |j< q| S )Npydicom_subcommands)group)r   loadname)r9   entry_pointr   r   r   get_subcommand_entry_points   s   r@   c                 C   s   t j d }tjdd| dd}|jddatjdd	d}|jd
ddd |j	t
d t }| D ]}|t q4|| }t|sJ|  dS || dS )zEntry point for 'pydicom' command line interface

    Parameters
    ----------
    args : List[str], optional
        Command-line arguments to parse.  If ``None``, then :attr:`sys.argv`
        is used.
    r   r   z'pydicom command line utilities (Python ))progdescriptionzsubcommand help)r1   r1   zdisplay help for subcommandsr6   ?zSubcommand to show help for)nargsr1   )funcN)sysversionr   r   ArgumentParseradd_subparsersr
   
add_parseradd_argumentset_defaultsr:   r@   values
parse_argsvarsr7   rF   )r0   
py_versionparserhelp_parserr9   r6   nsr   r   r   main   s$   


rU   )N)%__doc__r   importlib.metadatar   rerG   typingr   r   collections.abcr   r   r   pydicom.data.data_managerr   r   pydicom.datasetr	   r
   _SubParsersAction__annotations__re_kywd_or_itemcompiler&   filespec_helpr#   r   tupler!   r3   r/   	Namespacer:   dictSubCommandTyper@   rU   r   r   r   r   <module>   s.   [ 