ó 9(Zc@`s dZddlmZmZmZmZddlZddlZddlmZm Z m Z ddl m Z ddl mZmZddlmZdd lmZejd krÊdd lmZmZndd lmZmZejd(krRdd lmZmZeZedd„Z ed„Z!defd„ƒYZ"dd)d„ƒYZ#n1ddlm#Z#eZedd„Z ed„Z!i dd6dd6dd 6dd6dd6dd6dd6dd6dd 6dd!6dd"6dd#6Z$d$e%fd%„ƒYZ&d&e%fd'„ƒYZ'dS(*u Miscellaneous data helpers, including functions for converting integers to and from bytes and UTC timezone. Exports the following items: - OrderedDict() - int_from_bytes() - int_to_bytes() - timezone.utc - inet_ntop() - inet_pton() - uri_to_iri() - iri_to_uri() i(tunicode_literalstdivisiontabsolute_importtprint_functionN(tdatetimetdatettimei(tunwrap(t iri_to_urit uri_to_iri(t OrderedDict(t type_nameuwin32(t inet_ntopt inet_ptoni(t timedeltattzinfocC`s;t}|rd|dkrdt}ttjtdt|ƒƒdƒdƒ}|d|>d|>}nd|}t|ƒd@r‹d|}n|jdƒ}|rË| rËt|dd!ƒd@rËd |}n|d k r|ræd }nd }||t|ƒ|}n0|r7t|dd!ƒd@dkr7d |}n|S( uu Converts an integer to a byte string :param value: The integer to convert :param signed: If the byte string should be encoded using two's complement :param width: None == auto, otherwise an integer of the byte width for the return value :return: A byte string iu%xg@iiu0uhexi€tsÿN( tFalsetTruetinttmathtceiltlentabstdecodetordtNone(tvaluetsignedtwidthtis_negtbitsthex_strtoutputtpad_char((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt int_to_bytes)s&-  $   # cC`si|dkrdSt|jdƒdƒ}|s2|St|dd!ƒd@ret|ƒd}|d|>S|S(u Converts a byte string to an integer :param value: The byte string to convert :param signed: If the byte string should be interpreted using two's complement :return: An integer tiuhexiii€i(tlongtencodeRR(RRtnumtbit_len((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pytint_from_bytesVs  tutccB`s#eZd„Zd„Zd„ZRS(cC`sdS(Ns UTC+00:00((tselft_((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyttznameuscC`s tdƒS(Ni(R(R+R,((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt utcoffsetxscC`s tdƒS(Ni(R(R+R,((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pytdst{s(t__name__t __module__R-R.R/(((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR*ss  ttimezonecB`seZeƒZRS((R0R1R*(((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR2~s(R2cC`s¤|dkr‹|rc|dkr7t|dƒjƒ}n |jƒ}|ddkro|d7}qon |jƒ}tj|dƒp…d}n|j|ddd|ƒS(uu Converts an integer to a byte string :param value: The integer to convert :param signed: If the byte string should be encoded using two's complement :param width: None == auto, otherwise an integer of the byte width for the return value :return: A byte string iiit byteorderubigRN(RRt bit_lengthRRtto_bytes(RRRt bits_required((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR#Šs    cC`stj|dd|ƒS(u Converts a byte string to an integer :param value: The byte string to convert :param signed: If the byte string should be interpreted using two's complement :return: An integer ubigR(Rt from_bytes(RR((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR)©siiiiiiiiii i i i t extended_datecB`s¹eZdZdZdZdZd„Zd„Zd„Z d„Z dddd„Z d„Z d„Z d„Zd „Zd „Zd „Zd „Zd „Zd„Zd„Zd„ZRS(u† A datetime.date-like object that can represent the year 0. This is just to handle 0000-01-01 found in some certificates. cC`sŒ|dkrtdƒ‚n|dks3|dkrBtdƒ‚n|dks^|t|krmtdƒ‚n||_||_||_dS(u¬ :param year: The integer 0 :param month: An integer from 1 to 12 :param day: An integer from 1 to 31 iuyear must be 0ii umonth is out of rangeuday is out of rangeN(t ValueErrort_DAYS_PER_MONTH_YEAR_0tyeartmonthtday(R+R;R<R=((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__init__Ôs   cC`sß|jddƒ}td|j|jƒ}d|kr~|jdƒ}|jddƒ}|jddƒ}|jd|ƒ}nd|krÒ|jdƒ}|jddƒ}|jddƒ}|jd|ƒ}n|j|ƒS( uÌ Performs strftime(), always returning a unicode string :param format: A strftime() format string :return: A unicode string of the formatted date u%Yu0000iÐu%cu2000u%u%%u%x(treplaceRR<R=tstrftime(R+tformatttemptc_outtx_out((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt_formatís   cC`s |jdƒS(uµ Formats the date as %Y-%m-%d :return: The date formatted to %Y-%m-%d as a unicode string in Python 3 and a byte string in Python 2 u 0000-%m-%d(R@(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt isoformat s cC`s&|j|ƒ}tr"|jdƒS|S(uï Formats the date using strftime() :param format: The strftime() format string :return: The formatted date as a unicode string in Python 3 and a byte string in Python 2 uutf-8(REtpy2R&(R+RAR!((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR@s  cC`ss|dkr|j}n|dkr0|j}n|dkrH|j}n|dkr]t}nt}||||ƒS(uÚ Returns a new datetime.date or asn1crypto.util.extended_date object with the specified components replaced :return: A datetime.date or asn1crypto.util.extended_date object iN(RR;R<R=RR8(R+R;R<R=tcls((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR?'s        cC`str|jƒS|jƒSdS(N(RGt __bytes__t __unicode__(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__str__Bs cC`s|jƒjdƒS(Nuutf-8(RJR&(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRIHscC`s |jdƒS(Nu%Y-%m-%d(RE(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRJKscC`s)t||jƒstS|j|ƒdkS(Ni(t isinstancet __class__Rt__cmp__(R+tother((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__eq__NscC`s|j|ƒ S(N(RP(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__ne__SscC`sttdt|ƒƒƒ‚dS(Nu¦ An asn1crypto.util.extended_date object can only be compared to an asn1crypto.util.extended_date or datetime.date object, not %s (t TypeErrorRR (R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt_comparison_errorVscC`s‰t|tƒrdSt||jƒs5|j|ƒn|j|j|jf}|j|j|jf}||krudS||kr…dSdS(Niÿÿÿÿii(RLRRMRSR;R<R=(R+ROtsttot((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRN_s    cC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__lt__wscC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__le__zscC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__gt__}scC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyt__ge__€sN(R0R1t__doc__RR;R<R=R>RERFR@R?RKRIRJRPRQRSRNRVRWRXRY(((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR8Ês(            textended_datetimec B`s%eZdZdZdZdZdZdZdZ dZ dZ dddddd„Z d„Z d„Zd„Zd„Zd„Zd„Zd d „Zd „Zddddddddd „Zd „Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Zd„Z RS(uŠ A datetime.datetime-like object that can represent the year 0. This is just to handle 0000-01-01 found in some certificates. ic C`sU|dkrtdƒ‚n|dks3|dkrBtdƒ‚n|dks^|t|krmtdƒ‚n|dks…|dkr”tdƒ‚n|dks¬|d kr»td ƒ‚n|dksÓ|d krâtd ƒ‚n|dksú|d kr td ƒ‚n||_||_||_||_||_||_||_||_ dS(u£ :param year: The integer 0 :param month: An integer from 1 to 12 :param day: An integer from 1 to 31 :param hour: An integer from 0 to 23 :param minute: An integer from 0 to 59 :param second: An integer from 0 to 59 :param microsecond: An integer from 0 to 999999 iuyear must be 0ii umonth is out of rangeuday is out of rangeiuhour is out of rangei;uminute is out of rangeusecond is out of rangei?Bumicrosecond is out of rangeN( R9R:R;R<R=thourtminutetsecondt microsecondR( R+R;R<R=R\R]R^R_R((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR>“s,        cC`st|j|j|jƒS(uS :return: An asn1crypto.util.extended_date of the date (R8R;R<R=(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRÉscC`s%t|j|j|j|j|jƒS(uI :return: A datetime.time object of the time (RR\R]R^R_R(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRÑscC`s/|jdkrdS|jj|jddƒƒS(u\ :return: None or a datetime.timedelta() of the offset from UTC R;iÐN(RRR.R?(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR.ÙscC`s/|jdkrdS|jj|jddƒƒS(ui :return: None or a datetime.timedelta() of the daylight savings time offset R;iÐN(RRR/R?(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR/ãscC`s/|jdkrdS|jj|jddƒƒS(u‘ :return: None or the name of the timezone as a unicode string in Python 3 and a byte string in Python 2 R;iÐN(RRR-R?(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR-ísc C`sý|jddƒ}td|j|j|j|j|j|j|jƒ}d|krœ|j dƒ}|jddƒ}|jddƒ}|jd|ƒ}nd|krð|j dƒ}|jddƒ}|jddƒ}|jd|ƒ}n|j |ƒS( uÐ Performs strftime(), always returning a unicode string :param format: A strftime() format string :return: A unicode string of the formatted datetime u%Yu0000iÐu%cu2000u%u%%u%x( R?RR<R=R\R]R^R_RR@(R+RARBRCRD((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyREøs*    uTcC`s1|jdkr |jd|ƒS|jd|ƒS(uj Formats the date as "%Y-%m-%d %H:%M:%S" with the sep param between the date and time portions :param set: A single character of the separator to place between the date and time :return: The formatted datetime as a unicode string in Python 3 and a byte string in Python 2 iu0000-%%m-%%d%s%%H:%%M:%%Su0000-%%m-%%d%s%%H:%%M:%%S.%%f(R_R@(R+tsep((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRFscC`s&|j|ƒ}tr"|jdƒS|S(uï Formats the date using strftime() :param format: The strftime() format string :return: The formatted date as a unicode string in Python 3 and a byte string in Python 2 uutf-8(RERGR&(R+RAR!((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR@1s  c C`sú|dkr|j}n|dkr0|j}n|dkrH|j}n|dkr`|j}n|dkrx|j}n|dkr|j}n|dkr¨|j}n|dkrÀ|j}n|dkrÕt } nt } | ||||||||ƒS(uê Returns a new datetime.datetime or asn1crypto.util.extended_datetime object with the specified components replaced :return: A datetime.datetime or asn1crypto.util.extended_datetime object iN( RR;R<R=R\R]R^R_RRR[( R+R;R<R=R\R]R^R_RRH((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR?Bs8                  cC`str|jƒS|jƒSdS(N(RGRIRJ(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRKms cC`s|jƒjdƒS(Nuutf-8(RJR&(R+((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRIsscC`s/d}|jdkr"|d7}n|j|ƒS(Nu%Y-%m-%d %H:%M:%Siu.%f(R_RE(R+RA((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRJvs cC`s)t||jƒstS|j|ƒdkS(Ni(RLRMRRN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRP|scC`s|j|ƒ S(N(RP(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRQscC`sttdt|ƒƒƒ‚dS(u¥ Raises a TypeError about the other object not being suitable for comparison :param other: The object being compared to u¾ An asn1crypto.util.extended_datetime object can only be compared to an asn1crypto.util.extended_datetime or datetime.datetime object, not %s N(RRRR (R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRS„s cC`s|jƒ}|jƒ}|dk r0|dksH|dkrW|dk rWtdƒ‚nt|tƒrjdSt||jƒsŒ|j|ƒn|j|j|j |j |j |j |j |f}|j|j|j |j |j |j |j |f}||krdS||krdSdS(Nu5can't compare offset-naive and offset-aware datetimesiÿÿÿÿii(R.RRRRLRRMRSR;R<R=R\R]R^R_(R+ROtsotooRTRU((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRN–s:  0    cC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRV¾scC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRWÁscC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRXÄscC`s|j|ƒdkS(Ni(RN(R+RO((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyRYÇsN(!R0R1RZRR;R<R=R\R]R^R_RR>RRR.R/R-RERFR@R?RKRIRJRPRQRSRNRVRWRXRY(((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pyR[„s>6   '  *       (   (i(((RZt __future__RRRRRtsysRRRt_errorsRt_iriRR t _ordereddictR t_typesR tplatformt_inetR R tsockett version_infoRRRRGRRR#R)R*R2R:tobjectR8R[(((s3/tmp/pip-build-wDUJoH/asn1crypto/asn1crypto/util.pytsJ"  -    º