3 L]{F @sXdZddlmZmZmZmZddlZddlZddlmZm Z m Z ddl m Z ddl mZmZddlmZdd lmZejd krdd lmZmZndd lmZmZejd#krdd lmZmZdZd$ddZd%ddZGdddeZGdddZ n$ddlm Z dZd&ddZd'ddZddddddddddddd Z!Gdd d e"Z#Gd!d"d"e"Z$dS)(a 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() )unicode_literalsdivisionabsolute_importprint_functionN)datetimedatetime)unwrap) iri_to_uri uri_to_iri) OrderedDict) type_namewin32) inet_ntop inet_pton) timedeltatzinfoTFcCsd}|rJ|dkrJd}ttjtdt|dd}|d|>d|>}d|}t|d@rfd|}|jd }|r| rt|ddd @rd |}|d k r|rd }nd }||t||}n$|rt|ddd @dkrd |}|S)au 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 FrTz%xg@r 0hexN)intmathceillenabsdecodeord)valuesignedwidthZis_negbitshex_stroutputZpad_charr(7/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/util.py int_to_bytes)s& "  r*cCsT|dkr dSt|jdd}|s$|St|ddd@rPt|d}|d|>S|S)a 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 rrr rr)longencoder!r)r"r#numZbit_lenr(r(r)int_from_bytesVs  r0c@s$eZdZddZddZddZdS)utccCsdS)Ns UTC+00:00r()self_r(r(r)tznameusz utc.tznamecCstdS)Nr)r)r2r3r(r(r) utcoffsetxsz utc.utcoffsetcCstdS)Nr)r)r2r3r(r(r)dst{szutc.dstN)__name__ __module__ __qualname__r4r5r6r(r(r(r)r1ssr1c@seZdZeZdS)timezoneN)r7r8r9r1r(r(r(r)r:~sr:)r:cCsn|dkr^|rD|dkr&t|dj}n|j}|ddkrL|d7}n|j}tj|dp\d}|j|d|dS)au 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 Nrr rbig) byteorderr#)r bit_lengthrrto_bytes)r"r#r$Z bits_requiredr(r(r)r*s  cCstj|d|dS)a 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 r;)r#)r from_bytes)r"r#r(r(r)r0s) r rr c@seZdZdZdZdZdZddZddZddZ d d Z d#d d Z d dZ ddZ ddZddZddZddZddZddZddZdd Zd!d"ZdS)$ extended_datez A datetime.date-like object that can represent the year 0. This is just to handle 0000-01-01 found in some certificates. NcCsZ|dkrtd|dks |dkr(td|dks<|t|krDtd||_||_||_dS)z :param year: The integer 0 :param month: An integer from 1 to 12 :param day: An integer from 1 to 31 rzyear must be 0r rKzmonth is out of rangezday is out of rangeN) ValueError_DAYS_PER_MONTH_YEAR_0yearmonthday)r2rOrPrQr(r(r)__init__s zextended_date.__init__cCs|jdd}td|j|j}d|krR|jd}|jdd}|jdd}|jd|}d|kr|jd}|jdd}|jdd}|jd|}|j|S) z Performs strftime(), always returning a unicode string :param format: A strftime() format string :return: A unicode string of the formatted date z%Y0000iz%c2000%z%%z%x)replacerrPrQstrftime)r2formattempc_outx_outr(r(r)_formats         zextended_date._formatcCs |jdS)z 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 z 0000-%m-%d)rW)r2r(r(r) isoformat s zextended_date.isoformatcCs|j|}tr|jdS|S)z 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 zutf-8)r\py2r.)r2rXr'r(r(r)rWs  zextended_date.strftimecCsH|dkr|j}|dkr|j}|dkr*|j}|dkr8t}nt}||||S)z 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 Nr)rOrPrQrrL)r2rOrPrQclsr(r(r)rV's zextended_date.replacecCstr |jS|jSdS)N)r^ __bytes__ __unicode__)r2r(r(r)__str__Bszextended_date.__str__cCs|jjdS)Nzutf-8)rar.)r2r(r(r)r`Hszextended_date.__bytes__cCs |jdS)Nz%Y-%m-%d)r\)r2r(r(r)raKszextended_date.__unicode__cCst||jsdS|j|dkS)NFr) isinstance __class____cmp__)r2otherr(r(r)__eq__Ns zextended_date.__eq__cCs |j| S)N)rg)r2rfr(r(r)__ne__Sszextended_date.__ne__cCsttdt|dS)Nz An asn1crypto.util.extended_date object can only be compared to an asn1crypto.util.extended_date or datetime.date object, not %s ) TypeErrorr r)r2rfr(r(r)_comparison_errorVszextended_date._comparison_errorcCs`t|trdSt||js$|j||j|j|jf}|j|j|jf}||krPdS||kr\dSdS)Nr rrk)rcrrdrjrOrPrQ)r2rfstotr(r(r)re_s   zextended_date.__cmp__cCs|j|dkS)Nr)re)r2rfr(r(r)__lt__wszextended_date.__lt__cCs|j|dkS)Nr)re)r2rfr(r(r)__le__zszextended_date.__le__cCs|j|dkS)Nr)re)r2rfr(r(r)__gt__}szextended_date.__gt__cCs|j|dkS)Nr)re)r2rfr(r(r)__ge__szextended_date.__ge__)NNN)r7r8r9__doc__rOrPrQrRr\r]rWrVrbr`rargrhrjrernrorprqr(r(r(r)rLs(   rLc@seZdZdZdZdZdZdZdZdZ dZ dZ d/ddZ ddZ dd Zd d Zd d ZddZddZd0ddZddZd1ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Z dS)2extended_datetimez A datetime.datetime-like object that can represent the year 0. This is just to handle 0000-01-01 found in some certificates. Nrc Cs|dkrtd|dks |dkr(td|dks<|t|krDtd|dksT|dkr\td|dksl|d krttd |dks|d krtd |dks|d krtd ||_||_||_||_||_||_||_||_ dS)a :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 rzyear must be 0r rKzmonth is out of rangezday is out of rangezhour is out of range;zminute is out of rangezsecond is out of rangei?Bzmicrosecond is out of rangeN) rMrNrOrPrQhourminutesecond microsecondr) r2rOrPrQrvrwrxryrr(r(r)rRs,zextended_datetime.__init__cCst|j|j|jS)zS :return: An asn1crypto.util.extended_date of the date )rLrOrPrQ)r2r(r(r)rszextended_datetime.datecCst|j|j|j|j|jS)zI :return: A datetime.time object of the time )rrvrwrxryr)r2r(r(r)rszextended_datetime.timecCs"|jdkrdS|jj|jddS)z\ :return: None or a datetime.timedelta() of the offset from UTC Ni)rO)rr5rV)r2r(r(r)r5s zextended_datetime.utcoffsetcCs"|jdkrdS|jj|jddS)zi :return: None or a datetime.timedelta() of the daylight savings time offset Ni)rO)rr6rV)r2r(r(r)r6s zextended_datetime.dstcCs"|jdkrdS|jj|jddS)z :return: None or the name of the timezone as a unicode string in Python 3 and a byte string in Python 2 Ni)rO)rr4rV)r2r(r(r)r4s zextended_datetime.tznamec Cs|jdd}td|j|j|j|j|j|j|j}d|krf|j d}|jdd}|jdd}|jd|}d|kr|j d}|jdd}|jdd}|jd|}|j |S) z Performs strftime(), always returning a unicode string :param format: A strftime() format string :return: A unicode string of the formatted datetime z%YrSiz%crTrUz%%z%x) rVrrPrQrvrwrxryrrW)r2rXrYrZr[r(r(r)r\s*         zextended_datetime._formatTcCs&|jdkr|jd|S|jd|S)aj 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 rz0000-%%m-%%d%s%%H:%%M:%%Sz0000-%%m-%%d%s%%H:%%M:%%S.%%f)ryrW)r2sepr(r(r)r]s zextended_datetime.isoformatcCs|j|}tr|jdS|S)z 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 zutf-8)r\r^r.)r2rXr'r(r(r)rW1s  zextended_datetime.strftimec Cs|dkr|j}|dkr|j}|dkr*|j}|dkr8|j}|dkrF|j}|dkrT|j}|dkrb|j}|dkrp|j}|dkr~t} nt } | ||||||||S)z 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 Nr) rOrPrQrvrwrxryrrrs) r2rOrPrQrvrwrxryrr_r(r(r)rVBs8 zextended_datetime.replacecCstr |jS|jSdS)N)r^r`ra)r2r(r(r)rbmszextended_datetime.__str__cCs|jjdS)Nzutf-8)rar.)r2r(r(r)r`sszextended_datetime.__bytes__cCs d}|jdkr|d7}|j|S)Nz%Y-%m-%d %H:%M:%Srz.%f)ryr\)r2rXr(r(r)ravs zextended_datetime.__unicode__cCst||jsdS|j|dkS)NFr)rcrdre)r2rfr(r(r)rg|s zextended_datetime.__eq__cCs |j| S)N)rg)r2rfr(r(r)rhszextended_datetime.__ne__cCsttdt|dS)z Raises a TypeError about the other object not being suitable for comparison :param other: The object being compared to z An asn1crypto.util.extended_datetime object can only be compared to an asn1crypto.util.extended_datetime or datetime.datetime object, not %s N)rir r)r2rfr(r(r)rjs z#extended_datetime._comparison_errorcCs|j}|j}|dk r |dks0|dkr8|dk r8tdt|trFdSt||js\|j||j|j|j|j |j |j |j |f}|j|j|j|j |j |j |j |f}||krdS||krdSdS)Nz5can't compare offset-naive and offset-aware datetimesr rrkrk) r5rircrrdrjrOrPrQrvrwrxry)r2rfsoZoorlrmr(r(r)res:    zextended_datetime.__cmp__cCs|j|dkS)Nr)re)r2rfr(r(r)rnszextended_datetime.__lt__cCs|j|dkS)Nr)re)r2rfr(r(r)roszextended_datetime.__le__cCs|j|dkS)Nr)re)r2rfr(r(r)rpszextended_datetime.__gt__cCs|j|dkS)Nr)re)r2rfr(r(r)rqszextended_datetime.__ge__)rrrrN)rz)NNNNNNNN)!r7r8r9rrrOrPrQrvrwrxryrrRrrr5r6r4r\r]rWrVrbr`rargrhrjrernrorprqr(r(r(r)rss> 6   '  *(rs)r)FN)F)FN)F)%rr __future__rrrrrsysrrr_errorsr Z_irir r Z _ordereddictr _typesrplatformZ_inetrrsocket version_inforrr^r*r0r1r:rNobjectrLrsr(r(r(r)sJ      -     ;