o
    gi                     @   sN   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ G dd dZdS )	u|  
Some categories of objects in a PDF file can be referred to by name rather than by object reference. The
correspondence between names and objects is established by the document’s name dictionary (PDF 1.2),
located by means of the Names entry in the document’s catalog (see 7.7.2, "Document Catalog"). Each entry in
this dictionary designates the root of a name tree (see 7.9.6, "Name Trees") defining names for a particular
category of objects.

A name tree serves a similar purpose to a dictionary—associating keys and values—but by different means.
A name tree differs from a dictionary in the following important ways:
- Unlike the keys in a dictionary, which are name objects, those in a name tree are strings.
- The keys are ordered.
- The values associated with the keys may be objects of any type. Stream objects shall be specified by
indirect object references (7.3.8, "Stream Objects"). The dictionary, array, and string objects should be
specified by indirect object references, and other PDF objects (nulls, numbers, booleans, and names)
should be specified as direct objects.
- The data structure can represent an arbitrarily large collection of key-value pairs, which can be looked up
efficiently without requiring the entire data structure to be read from the PDF file. (In contrast, a dictionary
can be subject to an implementation limit on the number of entries it can contain.)
    N)
Dictionary)List)Name)Stringc                   @   s   e Zd ZdZdedefddZdd Zdd	 Zd
ede	de
jfddZd
ede	de
jfddZde
je
jee
jf  fddZde
je fddZde	de
jdd fddZde
je
j fddZdS )NameTreez
    A name tree is similar to a dictionary that associates keys and values but the keys in a name tree are strings and are ordered
    documentnamec                 C   s   || _ || _d S N)	_document_name)selfr   r    r   O/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/document/name_tree.py__init__(   s   
zNameTree.__init__c                 C   s   t |  S r	   )len_get_root_or_emptyr   r   r   r   __len__0   s   zNameTree.__len__c                 C   sf   d| j v s	J dd| j d v sJ dd| j d d v s!J d| j d d d }|tdt S )NXRefNo XREF found in this PDFTrailer(No /Trailer dictionary found in the XREFRoot)No /Root dictionary found in the /TrailerNames)r
   getr   r   )r   rootr   r   r   r   3   s   zNameTree._get_root_or_emptyparentkeyvaluec                 C   s   d S r	   r   )r   r   r   r   r   r   r   _put_existing>   s   zNameTree._put_existingc                 C   s   t  }t||td< t |td< tddD ]}|d t| qt |td< |td t| | jdkrK|td | td|td< | jd	krY|td | |d
 | d S )NFLimitsr      r   EmbeddedFilesEFType
JavaScriptKids)r   r   r   r   rangeappendr   )r   r   r   r   kid_r   r   r   _put_newB   s   

zNameTree._put_newreturnc           
      C   s.  d| j v s	J dd| j d v sJ dd| j d d v s!J d| j d d d }d|vr5t |td< |d }|| j g}g }g }t|dkr|d }|d d	|v rb|d	 D ]}|| qZd
|v rt|d
 d }t|d
 d }	|	|kr||d
 d  ||d d  t|dksIt||S )z
        This function returns all key/value pairs in this NameTree
        :return:    all key/value pairs in this NameTree
        r   r   r   r   r   r   r   r   r(   r"      )	r
   r   r   r   r   popr*   strzip)
r   r   namesnodes_to_visitkeysvaluesnklower_limitupper_limitr   r   r   items[   s8   

zNameTree.itemsc                 C      dd |   D S )zo
        This function returns the keys in this NameTree
        :return:    the keys in this NameTree
        c                 S   s   g | ]\}}|qS r   r   .0r8   vr   r   r   
<listcomp>       z!NameTree.keys.<locals>.<listcomp>r;   r   r   r   r   r5         zNameTree.keysc           	         sJ  d| j v s	J dd| j d v sJ dd| j d d v s!J d| j d d d }d|vr5t |td< |d }| j|vrNt || j< t || j td< || j }d|v r|d D ]'}t|d	 d
 }t|d	 d }||krrq[|   k r||k rn q[|} nq[	 t fdd|d D d
kr| | | | S | | | | S )z
        This function adds a key/value pair in this NameTree
        :param key:     the key
        :param value:   the value
        :return:        self
        r   r   r   r   r   r   r   r(   r"   r   r/   c                    s8   g | ]}|d  d |d  d   kr krn n|qS )r"   r   r/   r   )r>   xr   r   r   r@      s   8 z NameTree.put.<locals>.<listcomp>)	r
   r   r   r   r   r1   r   r-   r    )	r   r   r   r   r3   r   r8   r9   r:   r   rE   r   put   sB   

zNameTree.putc                 C   r<   )zs
        This function returns the values in this NameTree
        :return:    the values in this NameTree
        c                 S   s   g | ]\}}|qS r   r   r=   r   r   r   r@      rA   z#NameTree.values.<locals>.<listcomp>rB   r   r   r   r   r6      rC   zNameTree.valuesN)__name__
__module____qualname____doc__r   r   r   r   r   r1   typingAnyr    r-   IterableTupler   r;   r   r5   rF   r6   r   r   r   r   r      s     &3r   )rJ   rK   borb.io.read.typesr   r   r   r   r   r   r   r   r   <module>   s   