o
    jav                     @   s  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ 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 d	dlmZmZmZ d	dlmZmZ d	dlm Z m!Z! dZ"dZ#dZ$dZ%dZ&dZ'dZ(e#de$de%de&de(de'diZ)e*dej+Z,e
dZ-de" de# de$ d e% d!e& d"e( d#e' d$Z.d%e-d&e-fd'd(Z/d%e0d&ejfd)d*Z1d%ejd&e0fd+d,Z2e/fd-ed.d/d0e0d1eegef d&e	e3e0  f
d2d3Z4G d4d5 d5eZ5G d6d/ d/ee!Z6dS )7z~
Anything related to Extensible Metadata Platform (XMP) metadata.

https://en.wikipedia.org/wiki/Extensible_Metadata_Platform
    N)Iterator)AnyCallableOptionalTypeVarUnioncast)ExpatBuilderNS)Document)Element)
ExpatErrorXMLParserType   )XmpInformationProtocol)
StreamTypedeprecate_with_replacementdeprecation_no_replacement)PdfReadErrorXmpDocumentError)ContentStream	PdfObjectz+http://www.w3.org/1999/02/22-rdf-syntax-ns#z http://purl.org/dc/elements/1.1/zhttp://ns.adobe.com/xap/1.0/zhttp://ns.adobe.com/pdf/1.3/zhttp://ns.adobe.com/xap/1.0/mm/zhttp://ns.adobe.com/pdfx/1.3/zhttp://www.aiim.org/pdfa/ns/id/dcxmppdfxmpMMpdfaidpdfxa{  
        (?P<year>[0-9]{4})
        (-
            (?P<month>[0-9]{2})
            (-
                (?P<day>[0-9]+)
                (T
                    (?P<hour>[0-9]{2}):
                    (?P<minute>[0-9]{2})
                    (:(?P<second>[0-9]{2}(.[0-9]+)?))?
                    (?P<tzd>Z|[-+][0-9]{2}:[0-9]{2})
                )?
            )?
        )?
        Ku   <?xpacket begin="﻿" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="pypdf">
    <rdf:RDF xmlns:rdf="z?">
        <rdf:Description rdf:about=""
            xmlns:dc="z"
            xmlns:xmp="z"
            xmlns:pdf="z"
            xmlns:xmpMM="z"
            xmlns:pdfaid="z"
            xmlns:pdfx="zM">
        </rdf:Description>
    </rdf:RDF>
</x:xmpmeta>
<?xpacket end="r"?>valuereturnc                 C   s   | S N r   r!   r!   O/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/pypdf/xmp.py	_identityk   s   r$   c              	   C   s$  t | }|d u rtd|  t|d}t|dpd}t|dp'd}t|dp0d}t|dp9d}t|d	pCd}|tj}|| d
 }	t|}
t|	}|dp_d}t		||||||
|}|dkrdd |
dD \}}|d9 }|dk r|d9 }|t	j||d }|S )NzInvalid date format: yearmonth1dayhour0minutesecondi@B tzdZc                 s   s    | ]}t |V  qd S r    )int).0xr!   r!   r#   	<genexpr>   s    z"_converter_date.<locals>.<genexpr>:r   )hoursminutes)iso8601match
ValueErrorr/   groupdecimalDecimalto_integralROUND_FLOORdatetimesplit	timedelta)r   matchesr%   r&   r(   r)   r+   r,   seconds_decmilliseconds_decsecondsmillisecondsr-   dt	tzd_hourstzd_minutesr!   r!   r#   _converter_dateo   s,   
rJ   c                 C   s:   | j dur|  dur| tjj} | jdd} | dS )zFormat a datetime as UTC with trailing 'Z'.

    - If the input is timezone-aware, convert to UTC first.
    - If naive, assume UTC.
    N)tzinfoz%Y-%m-%dT%H:%M:%S.%fZ)rK   	utcoffset
astimezoner?   timezoneutcreplacestrftimer"   r!   r!   r#   _format_datetime_utc   s   
rR   elementselfXmpInformation	list_type	converterc           	      C   sX   |  t|}g }t|r*|D ]}| tdD ]}||}||}|| qq|S d S )Nli)getElementsByTagNameNSRDF_NAMESPACElen	_get_textappend)	rS   rT   rV   rW   
containersretval	containeritemr   r!   r!   r#   _generic_get   s   
rb   c                       sd   e Zd ZdZdededee dee dedee dee d	d
fddZded	d
f fddZ	  Z
S )_XmpBuilderac  
    Custom XML parser denying all entity declarations.

    This is a stripped down and typed version inspired by what *defusedxml* does.

    Why do we need this? The default limits of *libexpat* used by Python only block exponential entity expansion,
    but not cases like quadratic entity expansion which can still cause quite some memory usage.
    entity_nameis_parameter_entityr   base	system_id	public_idnotation_namer   Nc                 C   s   t d|)NzForbidden entities: )r   )rT   rd   re   r   rf   rg   rh   ri   r!   r!   r#   !custom_entity_declaration_handler   s   
z-_XmpBuilder.custom_entity_declaration_handlerparserc                    s   t  | | j|_d S r    )superinstallrj   EntityDeclHandler)rT   rk   	__class__r!   r#   rm      s   z_XmpBuilder.install)__name__
__module____qualname____doc__strboolr   rj   r   rm   __classcell__r!   r!   ro   r#   rc      s(    		
rc   c                   @   s  e Zd ZdZdeddfddZedddZ	dded	e	de
ef ddfd
dZde
de
de
dee fddZde
de
dee fddZdede
fddZefde
de
dee
gef dee fddZde
de
deee
  fddZefde
de
deegef deee  fddZde
de
deeeef  fddZedeee
  fdd Zejd!eee
  ddfd"d Zedee
 fd#d$Zejd%ee
 ddfd&d$Zedeee
  fd'd(Z e jd!eee
  ddfd)d(Z edeee!j!  fd*d+Z"e"jd!eee	e
e!j!f   ddfd,d+Z"edeee
e
f  fd-d.Z#e#jd!eee
e
f  ddfd/d.Z#edee
 fd0d1Z$e$jd%ee
 ddfd2d1Z$edee
 fd3d4Z%e%jd%ee
 ddfd5d4Z%edeee
  fd6d7Z&e&jd!eee
  ddfd8d7Z&edeee
  fd9d:Z'e'jd!eee
  ddfd;d:Z'edeee
  fd<d=Z(e(jd!eee
  ddfd>d=Z(edeee
e
f  fd?d@Z)e)jd!eee
e
f  ddfdAd@Z)edee
 fdBdCZ*e*jd%ee
 ddfdDdCZ*edeee
  fdEdFZ+e+jd!eee
  ddfdGdFZ+edeee
e
f  fdHdIZ,e,jd!eee
e
f  ddfdJdIZ,edeee
  fdKdLZ-e-jd!eee
  ddfdMdLZ-edee
 fdNdOZ.e.jd%ee
 ddfdPdOZ.edee
 fdQdRZ/e/jd%ee
 ddfdSdRZ/edee
 fdTdUZ0e0jd%ee
 ddfdVdUZ0edee!j! fdWdXZ1e1jd%ee!j! ddfdYdXZ1edee!j! fdZd[Z2e2jd%ee!j! ddfd\d[Z2edee!j! fd]d^Z3e3jd%ee!j! ddfd_d^Z3edee
 fd`daZ4e4jd%ee
 ddfdbdaZ4edee
 fdcddZ5e5jd%ee
 ddfdeddZ5edee
 fdfdgZ6e6jd%ee
 ddfdhdgZ6edee
 fdidjZ7e7jd%ee
 ddfdkdjZ7edee
 fdldmZ8e8jd%ee
 ddfdndmZ8edeeef fdodpZ9dde
defdrdsZ:de
de
ddfdtduZ;de
de
d%ee
 ddfdvdwZ<de
de
d!eee
  ddfdxdyZ=de
de
d!eee
  ddfdzd{Z>de
de
d!eee
e
f  ddfd|d}Z?de
de
fd~dZ@dddZAdS )rU   z
    An object that represents Extensible Metadata Platform (XMP) metadata.
    Usually accessed by :py:attr:`xmp_metadata()<pypdf.PdfReader.xmp_metadata>`.

    Raises:
      PdfReadError: if XML is invalid

    streamr   Nc              
   C   sh   || _ z| j  }t |}W n ttfy% } ztd| d }~ww |tdd | _	i | _
d S )Nz#XML in XmpInformation was invalid: RDFr   )rx   get_datarc   parseStringAttributeErrorr   r   rY   rZ   rdf_rootcache)rT   rx   datadoc_rooter!   r!   r#   __init__   s   

zXmpInformation.__init__c                 C   s"   t dd}|td | |S )z
        Create a new XmpInformation object with minimal structure.

        Returns:
            A new XmpInformation instance with empty metadata fields.
        Nutf-8)r   set_data_MINIMAL_XMPencode)clsrx   r!   r!   r#   create   s   
zXmpInformation.createencryption_keyc                 C   s.   t ddd |d urtdd | j| d S )NXmpInformation.write_to_streamzPdfWriter.xmp_metadataz6.0.0z/the encryption_key parameter of write_to_streamz5.0.0)r   r   rx   write_to_stream)rT   rx   r   r!   r!   r#   r      s   r   	about_uri	namespacenamec                 c   sV    | j tdD ] }|td|kr(|||}|d ur|V  |||E d H  qd S NDescriptionabout)r}   rY   rZ   getAttributeNSgetAttributeNodeNS)rT   r   r   r   descattrr!   r!   r#   get_element   s   zXmpInformation.get_elementc                 c   sx    | j tdD ]1}|td|kr9t|jjD ]}|j|}|r*|j|kr*|V  q|j	D ]
}|j|kr8|V  q.qd S r   )
r}   rY   rZ   r   range
attributeslengthra   namespaceURI
childNodes)rT   r   r   r   ir   childr!   r!   r#   get_nodes_in_namespace   s   

z%XmpInformation.get_nodes_in_namespacerS   c                 C   s*   d}|j D ]}|j|jkr||j7 }q|S N )r   nodeType	TEXT_NODEr   )rT   rS   textr   r!   r!   r#   r\     s   

zXmpInformation._get_textrW   c                 C   s|   | j |i |}|r|S d }| d||D ]}|j|jkr#|j}n| |} |d ur1||}| j |i }|||< |S r   )r~   getr   r   ATTRIBUTE_NODE	nodeValuer\   
setdefault)rT   r   r   rW   cachedr   rS   ns_cacher!   r!   r#   _get_single_value  s   
z XmpInformation._get_single_valuec           	      C   s   | j |i |}|rttt |S g }| d||D ]}t|| dd }d ur/|| q| |}|	| q| j 
|i }|||< |S )Nr   BagrV   )r~   r   r   listru   r   rb   extendr\   r]   r   )	rT   r   r   r   r_   rS   bagsr   r   r!   r!   r#   _getter_bag"  s   
zXmpInformation._getter_bagc                 C   s   | j |i |}|rttt |S g }| d||D ]1}t|| d|d }d ur0|| qt|| dd }d urA|| q|| |}	|	|	 q| j 
|i }
||
|< |S )Nr   Seq)rV   rW   r   r   )r~   r   r   r   r   r   rb   r   r\   r]   r   )rT   r   r   rW   r   r_   rS   seqsr   r   r   r!   r!   r#   _get_seq_values1  s   zXmpInformation._get_seq_valuesc                 C   s   | j |i |}|rttttf |S i }| d||D ].}|td}t|rE|D ]}|tdD ]}| 	|}	|	||
d< q4q,q| 	||d< q| j |i }
||
|< |S )Nr   AltrX   xml:langz	x-default)r~   r   r   dictr   r   rY   rZ   r[   r\   getAttributer   )rT   r   r   r   r_   rS   altsaltra   r   r   r!   r!   r#   _get_langalt_valuesN  s"   
z"XmpInformation._get_langalt_valuesc                 C      |  tdS )z6Contributors to the resource (other than the authors).contributorr   DC_NAMESPACErT   r!   r!   r#   dc_contributor`     zXmpInformation.dc_contributorvaluesc                 C      |  td| d S )Nr   _set_bag_valuesr   rT   r   r!   r!   r#   r   e     c                 C   r   )z4Text describing the extent or scope of the resource.coverager   r   r   r!   r!   r#   dc_coveragei  r   zXmpInformation.dc_coverager   c                 C   r   )Nr   _set_single_valuer   rT   r   r!   r!   r#   r   n  r   c                 C   r   )zVA sorted array of names of the authors of the resource, listed in order of precedence.creator)r   r   r   r!   r!   r#   
dc_creatorr  r   zXmpInformation.dc_creatorc                 C   r   )Nr   )_set_seq_valuesr   r   r!   r!   r#   r   w  r   c                 C      |  tdtS )zXA sorted array of dates of significance to the resource. The dates and times are in UTC.date)r   r   rJ   r   r!   r!   r#   dc_date{     zXmpInformation.dc_datec                 C   sd   |d u r|  tdd  d S g }|D ]}t|tjr!|t| q|t| q|  td| d S )Nr   )r   r   
isinstancer?   r]   rR   ru   )rT   r   date_stringsr   r!   r!   r#   r     s   c                 C   r   )zSA language-keyed dictionary of textual descriptions of the content of the resource.descriptionr   r   r   r!   r!   r#   dc_description  r   zXmpInformation.dc_descriptionc                 C   r   )Nr   _set_langalt_valuesr   r   r!   r!   r#   r     r   c                 C   r   )zThe mime-type of the resource.formatr   r   r!   r!   r#   	dc_format  r   zXmpInformation.dc_formatc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z"Unique identifier of the resource.
identifierr   r   r!   r!   r#   dc_identifier  r   zXmpInformation.dc_identifierc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )zAAn unordered array specifying the languages used in the resource.languager   r   r!   r!   r#   dc_language  r   zXmpInformation.dc_languagec                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z&An unordered array of publisher names.	publisherr   r   r!   r!   r#   dc_publisher  r   zXmpInformation.dc_publisherc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )zLAn unordered array of text descriptions of relationships to other documents.relationr   r   r!   r!   r#   dc_relation  r   zXmpInformation.dc_relationc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z`A language-keyed dictionary of textual descriptions of the rights the user has to this resource.rightsr   r   r!   r!   r#   	dc_rights  r   zXmpInformation.dc_rightsc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )zCUnique identifier of the work from which this resource was derived.sourcer   r   r!   r!   r#   	dc_source  r   zXmpInformation.dc_sourcec                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z\An unordered array of descriptive phrases or keywords that specify the topic of the content.subjectr   r   r!   r!   r#   
dc_subject  r   zXmpInformation.dc_subjectc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z9A language-keyed dictionary of the title of the resource.titler   r   r!   r!   r#   dc_title  r   zXmpInformation.dc_titlec                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z@An unordered array of textual descriptions of the document type.typer   r   r!   r!   r#   dc_type  r   zXmpInformation.dc_typec                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z:An unformatted text string representing document keywords.Keywordsr   PDF_NAMESPACEr   r!   r!   r#   pdf_keywords  r   zXmpInformation.pdf_keywordsc                 C   r   )Nr   r   r   r   r!   r!   r#   r     r   c                 C   r   )z-The PDF file version, for example 1.0 or 1.3.
PDFVersionr   r   r!   r!   r#   pdf_pdfversion  r   zXmpInformation.pdf_pdfversionc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )z6The name of the tool that saved the document as a PDF.Producerr   r   r!   r!   r#   pdf_producer  r   zXmpInformation.pdf_producerc                 C   r   )Nr   r   r   r!   r!   r#   r     r   c                 C   r   )zYThe date and time the resource was originally created. Returned as a UTC datetime object.
CreateDater   XMP_NAMESPACErJ   r   r!   r!   r#   xmp_create_date  r   zXmpInformation.xmp_create_datec                 C   0   |rt |}| td| d S | tdd  d S )Nr   rR   r   r   rT   r   date_strr!   r!   r#   r        c                 C   r   )zTThe date and time the resource was last modified. Returned as a UTC datetime object.
ModifyDater   r   r!   r!   r#   xmp_modify_date  r   zXmpInformation.xmp_modify_datec                 C   r   )Nr  r   r   r!   r!   r#   r    r  c                 C   r   )zjThe date and time that any metadata for this resource was last changed. Returned as a UTC datetime object.MetadataDater   r   r!   r!   r#   xmp_metadata_date%  r   z XmpInformation.xmp_metadata_datec                 C   r   )Nr  r   r   r!   r!   r#   r  *  r  c                 C   r   )z=The name of the first known tool used to create the resource.CreatorTool)r   r   r   r!   r!   r#   xmp_creator_tool2  r   zXmpInformation.xmp_creator_toolc                 C   r   )Nr  )r   r   r   r!   r!   r#   r  7  r   c                 C   r   )zGThe common identifier for all versions and renditions of this resource.
DocumentIDr   XMPMM_NAMESPACEr   r!   r!   r#   xmpmm_document_id;  r   z XmpInformation.xmpmm_document_idc                 C   r   )Nr  r   r
  r   r!   r!   r#   r  @  r   c                 C   r   )zZAn identifier for a specific incarnation of a document, updated each time a file is saved.
InstanceIDr	  r   r!   r!   r#   xmpmm_instance_idD  r   z XmpInformation.xmpmm_instance_idc                 C   r   )Nr  r  r   r!   r!   r#   r  I  r   c                 C   r   )zMThe part of the PDF/A standard that the document conforms to (e.g., 1, 2, 3).partr   PDFAID_NAMESPACEr   r!   r!   r#   pdfaid_partM  r   zXmpInformation.pdfaid_partc                 C   r   )Nr  r   r  r   r!   r!   r#   r  R  r   c                 C   r   )zFThe conformance level within the PDF/A standard (e.g., 'A', 'B', 'U').conformancer  r   r!   r!   r#   pdfaid_conformanceV  r   z!XmpInformation.pdfaid_conformancec                 C   r   )Nr  r  r   r!   r!   r#   r  [  r   c                 C   s   t | dsRi | _| dtD ]C}|j}	 |d}|dkrn|d| tt||d |d  d	d
 ||d d  }q|j|j	krG|j
}n| |}|| j|< q| jS )z
        Retrieve custom metadata properties defined in the undocumented pdfx
        metadata schema.

        Returns:
            A dictionary of key/value items for custom metadata properties.

        _custom_propertiesr   Tu   ↂr4   Nr         )rf   )hasattrr  r   PDFX_NAMESPACE	localNamefindchrr/   r   r   r   r\   )rT   nodekeyidxr   r!   r!   r#   custom_properties_  s(   





z XmpInformation.custom_propertiesr   c                 C   sn   | j tdD ]}|td|kr|  S q| j j}|du r"td|td}|td| | j | |S )zBGet or create an rdf:Description element with the given about URI.r   r   NXMP Document is Nonezrdf:Descriptionz	rdf:about)	r}   rY   rZ   r   ownerDocumentr   createElementNSsetAttributeNSappendChild)rT   r   r   docr!   r!   r#   _get_or_create_description~  s   z)XmpInformation._get_or_create_descriptionc                 C   s*   | j |}|r||v r||= dS dS dS )z<Remove a cached value for a given namespace/name if present.N)r~   r   )rT   r   r   r   r!   r!   r#   _clear_cache_entry  s   
z!XmpInformation._clear_cache_entryc                 C   s   |  || |  }t|||}|D ]}|| q||| }r)|| |durZ| jj}|du r9t	d| 
|}	|||	 d| }|t|}
||
 || |   dS )z&Set or remove a single metadata value.Nr"  r3   )r)  r(  r   rY   removeChildr   removeAttributeNoder}   r#  r   _get_namespace_prefixr$  createTextNoderu   r&  _update_stream)rT   r   r   r   r   existing_elementselemexisting_attrr'  prefix	text_noder!   r!   r#   r     s"   



z XmpInformation._set_single_valuec                 C      |  || |  }t|||}|D ]}|| q|rf| jj}|du r*td| |}|	|| d| }|	t
d}	|D ]}
|	t
d}|t|
}|| |	| qB||	 || |   dS )z+Set or remove bag values (unordered array).Nr"  r3   zrdf:Bagrdf:lir)  r(  r   rY   r*  r}   r#  r   r,  r$  rZ   r-  ru   r&  r.  )rT   r   r   r   r   r/  r0  r'  r2  bagr   rX   r3  r!   r!   r#   r     (   



zXmpInformation._set_bag_valuesc                 C   r4  )z.Set or remove sequence values (ordered array).Nr"  r3   zrdf:Seqr5  r6  )rT   r   r   r   r   r/  r0  r'  r2  seqr   rX   r3  r!   r!   r#   r     r8  zXmpInformation._set_seq_valuesc                 C   s   |  || |  }t|||}|D ]}|| q|rp| jj}|du r*td| |}|	|| d| }|	t
d}	| D ]!\}
}|	t
d}|d|
 |t|}|| |	| qD||	 || |   dS )z*Set or remove language alternative values.Nr"  r3   zrdf:Altr5  r   )r)  r(  r   rY   r*  r}   r#  r   r,  r$  rZ   itemssetAttributer-  ru   r&  r.  )rT   r   r   r   r   r/  r0  r'  r2  r   langr   rX   r3  r!   r!   r#   r     s*   



z"XmpInformation._set_langalt_valuesc                 C   s   t |dS )z?Get the appropriate namespace prefix for a given namespace URI.unknown)_NAMESPACE_PREFIX_MAPr   )rT   r   r!   r!   r#   r,    s   z$XmpInformation._get_namespace_prefixc                 C   s4   | j j}|du rtd|jdd}| j| dS )z/Update the stream with the current XML content.Nr"  r   )encoding)r}   r#  r   toxmlrx   r   )rT   r'  xml_datar!   r!   r#   r.    s
   zXmpInformation._update_stream)r   rU   r    )r   )r   N)Brq   rr   rs   rt   r   r   classmethodr   r   r   ru   bytesr   r   r   r   r   
XmlElementr\   r$   r   r   r   r   r   r   r   r   propertyr   setterr   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r!  r(  r)  r   r   r   r   r,  r.  r!   r!   r!   r#   rU      s0   	



"&   ""&)7rt   r?   r;   recollections.abcr   typingr   r   r   r   r   r   xml.dom.expatbuilderr	   xml.dom.minidomr
   r   rD  xml.parsers.expatr   r   
_protocolsr   _utilsr   r   r   errorsr   r   genericr   r   rZ   r   r   r   r
  r  r  r>  compileVERBOSEr7   r   r   r$   ru   rJ   rR   r   rb   rc   rU   r!   r!   r!   r#   <module>   s     		

