HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/ipalib/__pycache__/ipajson.cpython-39.opt-1.pyc
a

�N(i�@s�ddlZddlmZddlZddlmZddlZddlZddl	m
Z
ddlmZmZddl
mZddlmZddlmZdd	lmZejr�eZGd
d�de�Zdd
d�Zejefdd�Zdd�ZdS)�N)�x509)�Decimal)�LDAP_GENERALIZED_TIME_FORMAT)�capabilitiesr)�Encoding)�DN)�DNSName)�	Principalcs~eZdZdZdZe�Zef�fdd�	Zdd�Zefdd�Z	d	d
�Z
dd�Zd
d�Zefdd�Z
eejfdd�Zdd�Z�ZS)�_JSONPrimera�Fast JSON primer and pre-converter

    Prepare a data structure for JSON serialization. In an ideal world, priming
    could be handled by the default hook of json.dumps(). Unfortunately the
    hook treats Python 2 str as text while IPA considers str as bytes.

    The primer uses a couple of tricks to archive maximum performance:

    * O(1) type look instead of O(n) chain of costly isinstance() calls
    * __missing__ and __mro__ with caching to handle subclasses
    * inline code with minor code duplication (func lookup in enc_list/dict)
    * avoid surplus function calls (e.g. func is _identity, obj.__class__
      instead if type(obj))
    * function default arguments to turn global into local lookups
    * avoid re-creation of bound method objects (e.g. result.append)
    * on-demand lookup of client capabilities with cached values

    Depending on the client version number, the primer converts:

    * bytes -> {'__base64__': b64encode}
    * datetime -> {'__datetime__': LDAP_GENERALIZED_TIME}
    * DNSName -> {'__dns_name__': unicode}

    The _ipa_obj_hook() functions unserializes the marked JSON objects to
    bytes, datetime and DNSName.

    :see: _ipa_obj_hook
    )�version�
_cap_datetime�_cap_dnsnamec"s�tt|���||_d|_d|_|�t|t|t	|t
d�|t|ttt
tttt|jtj|jt|jt|jt|jt|jtj|jtj|ji�dS�N) �superr
�__init__rrr
�update�unicode�bool�int�type�floatrr�strr	r�_enc_dnsname�datetime�
_enc_datetime�bytes�
_enc_bytes�list�	_enc_list�tuple�dict�	_enc_dictrZIPACertificate�_enc_certificate�crypto_x509ZCertificateSigningRequest)�selfr�	_identity��	__class__��2/usr/lib/python3.9/site-packages/ipalib/ipajson.pyr7s,�z_JSONPrimer.__init__cCsd|jD]$}||vr||||<||Sq|D]&}t||�r0||||<||Sq0t|��dSr)�__mro__�
issubclass�	TypeError)r$�typ�cr(r(r)�__missing__Os

z_JSONPrimer.__missing__cCs||j}||ur|S||�Srr&)r$�objr%�funcr(r(r)�convert\s
z_JSONPrimer.convertcCsB|j}|dur"t�|jd�}||_|r4d|�t�iS|�t�SdS)NZdatetime_values�__datetime__)rr�client_has_capabilityr�strftimer�r$�valZcapr(r(r)ras�z_JSONPrimer._enc_datetimecCs>|j}|dur"t�|jd�}||_|r2dt|�iSt|�SdS)NZdns_name_values�__dns_name__)r
rr4rrr6r(r(r)rls�z_JSONPrimer._enc_dnsnamecCs"t�|�}tjs|�d�}d|iS)N�ascii�
__base64__)�base64Z	b64encode�sixZPY2�decode)r$r7Zencodedr(r(r)rws

z_JSONPrimer._enc_bytescCs:g}|j}|D]&}||j}|||ur*|n||��q|Sr)�appendr')r$r7r%�resultr>�vr1r(r(r)r}s
z_JSONPrimer._enc_listcCs<i}||�D]*\}}||j}||ur*|n||�||<q|Srr&)r$r7r%�
_iteritemsr?�kr@r1r(r(r)r!�s

z_JSONPrimer._enc_dictcCs|�|�tj��Sr)rZpublic_bytes�
x509_EncodingZDER)r$r7r(r(r)r"�sz_JSONPrimer._enc_certificate)�__name__�
__module__�__qualname__�__doc__�	__slots__�objectr%rr/r2rrrrr<�	iteritemsr!r"�
__classcell__r(r(r&r)r
s
r
FcCs0t|��|�}|r"tj|ddd�St�|�SdS)aVSerialize a Python object structure to JSON

    :param object val: Python object structure
    :param str version: client version
    :param bool pretty_print: indent and sort JSON (warning: slow!)
    :return: text
    :note: pretty printing triggers a slow path in Python's JSON module. Only
           use pretty_print in debug mode.
    �T)�indentZ	sort_keysN)r
r2�json�dumps)r7rZpretty_printr?r(r(r)�json_encode_binary�s
rPcCstd|vrt�|d�Sd|vr0tj�|dt�Sd|vrDt|d�S||�D]\}}|j|urLt|�||<qL|SdS)z,JSON object hook

    :see: _JSONPrimer
    r:r3r8N)r;Z	b64decoder�strptimerrr'r)ZdctrAZ_listrBr@r(r(r)�
_ipa_obj_hook�s�
rRcCs"t|t�r|�d�}tj|td�S)z�Convert serialized JSON string back to Python data structure

    :param val: JSON string
    :type val: str, bytes
    :return: Python data structure
    :see: _ipa_obj_hook, _JSONPrimer
    zutf-8)Zobject_hook)�
isinstancerr=rN�loadsrR)r7r(r(r)�json_decode_binary�s

rU)F) r;Zcryptographyrr#rZdecimalrrNr<Zipalib.constantsrZipalibrZipalib.x509rrCZipapython.dnrZipapython.dnsutilrZipapython.kerberosr	ZPY3rrr r
rPrJrrRrUr(r(r(r)�<module>s"z