o
    'h                     @   s   d Z g dZddlZddlZddlZddlZddlZddlmZm	Z	 e
eeeeeeeejejeedefZejddZdd Zd	d
 ZejejejdZdddZdd Z dddZ!dddZ"dS )z
Conversion.py -- Tools for converting between Python and Objective-C objects.

Conversion offers API to convert between Python and Objective-C instances of
various classes.   Currently, the focus is on Python and Objective-C
collections.
) pythonCollectionFromPropertyList propertyListFromPythonCollectionserializePropertyListdeserializePropertyListtoPythonDecimalfromPythonDecimal    N)OC_PythonFloatOC_PythonLong.ZNSDecimalSeparatorc                 C   s   t | tS )z?
    Convert a NSDecimalNumber to a Python decimal.Decimal
    )decimalDecimalZdescriptionWithLocale_DECIMAL_LOCALE)ZaNSDecimalNumber r   m/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/PyObjCTools/Conversion.pyr   /   s   r   c                 C   s   t | }tj|tS )z?
    Convert a Python decimal.Decimal to a NSDecimalNumber
    )str
FoundationNSDecimalNumberZdecimalNumberWithString_locale_r   )ZaPythonDecimalZ	value_strr   r   r   r   6   s   r   )xmlbinaryasciir   c                 C   sR   zt | }W n ty   td| w tj| |d\}}|dur't||S )a  
    Serialize a property list to an NSData object.  Format is one of the
    following strings:

    xml (default):
        NSPropertyListXMLFormat_v1_0, the XML representation

    binary:
        NSPropertyListBinaryFormat_v1_0, the efficient binary representation

    ascii:
        NSPropertyListOpenStepFormat, the old-style ASCII property list

    It is expected that this property list is comprised of Objective-C
    objects.  In most cases Python data structures will work, but
    decimal.Decimal and datetime.datetime objects are not transparently
    bridged so it will fail in that case.  If you expect to have these
    objects in your property list, then use propertyListFromPythonCollection
    before serializing it.
    zInvalid format: N)FORMATSKeyError
ValueErrorr   NSPropertyListSerializationZ-dataFromPropertyList_format_errorDescription_)ZaPropertyListformatZformatOptiondataerrr   r   r   r   H   s   r   c                 C   sB   t | tr
| d} tj| tjdd\}}}|durt||S )zs
    Deserialize a property list from a NSData, str or bytes object

    Returns an Objective-C property list.
    zutf-8N)
isinstancer   encoder   r   Z>propertyListFromData_mutabilityOption_format_errorDescription_ZNSPropertyListMutableContainersr   )ZpropertyListDataplistfmtr   r   r   r   r   l   s   


r   c                 C   s.  t | tr&tj }| D ]}t |tstdt| | |d}|||< q|S t | tt	frDtj
 }| D ]}t||d}|| q4|S t | tjtjfrXtjt|  S t | ttfrvtj }| D ]}t||d}|| qf|S t | tjrt| S t | tr| S |dur|| S tdt|  )a  
    Convert a Python collection (dict, list, tuple, string) into an
    Objective-C collection.

    If conversionHelper is defined, it must be a callable.  It will be called
    for any object encountered for which propertyListFromPythonCollection()
    cannot automatically convert the object.   The supplied helper function
    should convert the object and return the converted form.  If the conversion
    helper cannot convert the type, it should raise an exception or return
    None.
    z"Property list keys must be stringsconversionHelperNzFType '%s' encountered in Python collection; don't know how to convert.)r   dictr   ZNSMutableDictionary
dictionaryr   	TypeErrorr   listtupleZNSMutableArrayarrayappenddatetimedateNSDateZdateWithTimeIntervalSince1970_timemktime	timetupleset	frozensetZNSMutableSetaddr   r   r   PYTHON_TYPEStype)ZaPyCollectionr"   Z
collectionZaKeyconvertedValueZaValuer   r   r   r      sR   






r   c                    sZ  t | tjri }| D ]}t| |  }||t|< q
|S t | tjr+ fdd| D S t | tjrNt }| D ]}t| }t |trFt|}|	| q6|S t | tj
rXt| S t | tjrftj|  S t | tjtjfrst| S t | tr|t| S t | trt| S t | tjrt| S | tj u rdS t | tr| S  r | S tdt|  )aC  
    Converts a Foundation based property list into a Python
    collection (all members will be instances or subclasses of standard Python
    types)

    Like propertyListFromPythonCollection(), conversionHelper is an optional
    callable that will be invoked any time an encountered object cannot be
    converted.
    c                    s   g | ]}t | qS r   )r   ).0itemr!   r   r   
<listcomp>   s    z4pythonCollectionFromPropertyList.<locals>.<listcomp>NzEType '%s' encountered in ObjC collection;  don't know how to convert.)r   r   NSDictionaryr   ZNSArrayZNSSetr0   r&   r'   r2   ZNSDatabytesr,   r*   fromtimestampZtimeIntervalSince1970objcZpyobjc_unicodeZNSStringr   r	   intr   floatr   r   ZNSNullnullr3   r%   r4   )ZaCollectionr"   ZpyCollectionkr5   valuer7   r   r!   r   r      sV   






r   )r   )N)#__doc____all__r*   r   r-   r   r<   Zobjc._pythonifyr   r	   r   boolr=   r>   r&   r'   r#   r0   r+   r4   r:   r3   r9   ZdictionaryWithObject_forKey_r   r   r   ZNSPropertyListXMLFormat_v1_0ZNSPropertyListBinaryFormat_v1_0ZNSPropertyListOpenStepFormatr   r   r   r   r   r   r   r   r   <module>   sF    	
$
8