B #aqU@sPdZddlmZmZmZmZddlZddlZddlmZm Z m Z m Z ddl m Z ddlmZmZddlmZdd lmZejd krdd lmZmZndd lmZmZejd krd!ddZd"ddZGddde Zee de_n ddlmZd#ddZd$ddZddZGddde Z e Z!iZ"ddZ#Gddde$Z%Gdd d e$Z&dS)%a5 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 - utc_with_dst - create_timezone() - inet_ntop() - inet_pton() - uri_to_iri() - iri_to_uri() )unicode_literalsdivisionabsolute_importprint_functionN)datetimedate timedeltatzinfo)unwrap) iri_to_uri uri_to_iri) OrderedDict) type_namewin32) inet_ntop inet_pton)FcCs|dkr|dkrdSd}|r^|dkr^d}tttdt|dd}|d|>d|>}d|}t|d@rzd |}|d }|r|st|ddd @rd |}|d k rt||krtd|rd}nd }||t||}n(|rt|ddd @dkrd|}|S)a 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: If None, the minimal possible size (but at least 1), otherwise an integer of the byte width for the return value :return: A byte string rFTz%xg@r 0hexNzint too big to convert)intmathceillenabsdecodeord OverflowError)valuesignedwidthZis_negbitshex_stroutputZpad_charr)6/tmp/pip-target-jgxl_w8r/lib/python/asn1crypto/util.py int_to_bytes's. "    r+cCsT|dkr dSt|dd}|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 rrrr rr)longencoder!r)r#r$numZbit_lenr)r)r*int_from_bytesYs  r0c@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)timezonez Implements datetime.timezone for py2. Only full minute offsets are supported. DST is not supported. NcCsvtdd|kr tddks*ntd|jds:|jrBtd||_|dk rX||_n|sdd|_ndt||_dS) z :param offset: A timedelta with this timezone's offset from UTC :param name: Name of the timezone; if None, generate one. i)hoursz!Offset must be in [-23:59, 23:59]<zOffset must be full minutesNUTC)r ValueErrorseconds microseconds_offset_name_format_offset)selfoffsetnamer)r)r*__init__}s "ztimezone.__init__cCst|tkrdS|j|jkS)z Compare two timezones :param other: The other timezone to compare to :return: A boolean F)typer1r9)r<otherr)r)r*__eq__s ztimezone.__eq__cCs |j|jfS)z Called by tzinfo.__reduce__ to support pickle and copy. :return: offset and name, to be used for __init__ )r9r:)r<r)r)r*__getinitargs__sztimezone.__getinitargs__cCs|jS)z :param dt: A datetime object; ignored. :return: Name of this timezone )r:)r<dtr)r)r*tznames ztimezone.tznamecCs|jS)z :param dt: A datetime object; ignored. :return: A timedelta object with the offset from UTC )r9)r<rDr)r)r* utcoffsets ztimezone.utcoffsetcCstdS)z :param dt: A datetime object; ignored. :return: Zero timedelta r)r)r<rDr)r)r*dsts z timezone.dst)N) __name__ __module__ __qualname____doc__r?rBrCrErFrGr)r)r)r*r1vs    r1)r1cCsn|dkr^|rD|dkr&t|d}n|}|ddkrL|d7}n|}t|dp\d}|j|d|dS)a 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: If None, the minimal possible size (but at least 1), 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 rL)r$)r from_bytes)r#r$r)r)r*r0scCsJ|dkr dS|jdd|jd}|dkr0dnd}|dtt|dS) zC Format a timedelta into "[+-]HH:MM" format or "" for None Nr3r4r-+z %02d:%02d)daysr7divmodr)offZminssignr)r)r*r;s r;c@s(eZdZdZddZddZddZdS) _UtcWithDstzK Utc class where dst does not return None; required for astimezone cCsdS)Nr5r))r<rDr)r)r*rEsz_UtcWithDst.tznamecCstdS)Nr)r)r<rDr)r)r*rFsz_UtcWithDst.utcoffsetcCstdS)Nr)r)r<rDr)r)r*rGsz_UtcWithDst.dstN)rHrIrJrKrErFrGr)r)r)r*rXsrXcCs6y t|}Wn$tk r0t|}t|<YnX|S)a Returns a new datetime.timezone object with the given offset. Uses cached objects if possible. :param offset: A datetime.timedelta object; It needs to be in full minutes and between -23:59 and +23:59. :return: A datetime.timezone object )_timezone_cacheKeyErrorr1)r=tzr)r)r*create_timezone&s  r\c@seZdZdZddZeddZeddZedd Zd d Z d d Z d#ddZ ddZ ddZ ddZddZddZddZddZdd Zd!d"ZdS)$ extended_dateaB A datetime.datetime-like object that represents the year 0. This is just to handle 0000-01-01 found in some certificates. Python's datetime does not support year 0. The proleptic gregorian calendar repeats itself every 400 years. Therefore, the simplest way to format is to substitute year 2000. cCs"|dkrtdtd|||_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 0iN)r6r_y2k)r<yearmonthdayr)r)r*r?Cs zextended_date.__init__cCsdS)z4 :return: The integer 0 rr))r<r)r)r*r_Tszextended_date.yearcCs|jjS)z> :return: An integer from 1 to 12 )r^r`)r<r)r)r*r`]szextended_date.monthcCs|jjS)z> :return: An integer from 1 to 31 )r^ra)r<r)r)r*rafszextended_date.daycCs:|j|}|jjdd|}dddt||DS)z Formats the date using strftime() :param format: A strftime() format string :return: A str, the formatted date as a unicode string in Python 3 and a byte string in Python 2 i)r_rQcss&|]\}}||fdkrdn|VqdS))24rNr)).0c2c4r)r)r* sz)extended_date.strftime..)r^strftimereplacejoinzip)r<formaty2ky4kr)r)r*rhos zextended_date.strftimecCs |dS)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)rh)r<r)r)r* isoformats zextended_date.isoformatNcCsH|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)r_r`rarr])r<r_r`raclsr)r)r*ris zextended_date.replacecCs |dS)z_ :return: A str representing this extended_date, e.g. "0000-01-01" z%Y-%m-%d)rh)r<r)r)r*__str__szextended_date.__str__cCst||jsdS||dkS)z Compare two extended_date objects :param other: The other extended_date to compare to :return: A boolean Fr) isinstance __class____cmp__)r<rAr)r)r*rBs zextended_date.__eq__cCs || S)z Compare two extended_date objects :param other: The other extended_date to compare to :return: A boolean )rB)r<rAr)r)r*__ne__s zextended_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)r<rAr)r)r*_comparison_errorszextended_date._comparison_errorcCsHt|trdSt||js$|||j|jkr4dS|j|jkrDdSdS)z Compare two extended_date or datetime.date objects :param other: The other extended_date object to compare to :return: An integer smaller than, equal to, or larger than 0 r r)rrrrsrwr^)r<rAr)r)r*rts     zextended_date.__cmp__cCs||dkS)Nr)rt)r<rAr)r)r*__lt__szextended_date.__lt__cCs||dkS)Nr)rt)r<rAr)r)r*__le__szextended_date.__le__cCs||dkS)Nr)rt)r<rAr)r)r*__gt__szextended_date.__gt__cCs||dkS)Nr)rt)r<rAr)r)r*__ge__szextended_date.__ge__)NNN)rHrIrJrKr?propertyr_r`rarhrorirqrBrurwrtryrzr{r|r)r)r)r*r]9s"    r]c@s4eZdZdZdZdeZddZeddZedd Z ed d Z ed d Z eddZ eddZ eddZeddZddZddZddZddZdBdd ZdCd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Z d:d;Z!dd?Z#e$d@dAZ%d!S)Dextended_datetimeaB A datetime.datetime-like object that represents the year 0. This is just to handle 0000-01-01 found in some certificates. Python's datetime does not support year 0. The proleptic gregorian calendar repeats itself every 400 years. Therefore, the simplest way to format is to substitute year 2000. i:cOs$|dkrtdtd|||_dS)z :param year: The integer 0 :param args: Other positional arguments; see datetime.datetime. :param kwargs: Other keyword arguments; see datetime.datetime. rzyear must be 0N)r)r6rr^)r<r_argskwargsr)r)r*r?s zextended_datetime.__init__cCsdS)z4 :return: The integer 0 rr))r<r)r)r*r_szextended_datetime.yearcCs|jjS)z> :return: An integer from 1 to 12 )r^r`)r<r)r)r*r`"szextended_datetime.monthcCs|jjS)z> :return: An integer from 1 to 31 )r^ra)r<r)r)r*ra+szextended_datetime.daycCs|jjS)z> :return: An integer from 1 to 24 )r^hour)r<r)r)r*r4szextended_datetime.hourcCs|jjS)z> :return: An integer from 1 to 60 )r^minute)r<r)r)r*r=szextended_datetime.minutecCs|jjS)z> :return: An integer from 1 to 60 )r^second)r<r)r)r*rFszextended_datetime.secondcCs|jjS)zB :return: An integer from 0 to 999999 )r^ microsecond)r<r)r)r*rOszextended_datetime.microsecondcCs|jjS)zh :return: If object is timezone aware, a datetime.tzinfo object, else None. )r^r )r<r)r)r*r Xszextended_datetime.tzinfocCs |jS)zk :return: If object is timezone aware, a datetime.timedelta object, else None. )r^rF)r<r)r)r*rFaszextended_datetime.utcoffsetcCs |jS)z= :return: A datetime.time object )r^time)r<r)r)r*riszextended_datetime.timecCstd|j|jS)zS :return: An asn1crypto.util.extended_date of the date r)r]r`ra)r<r)r)r*rqszextended_datetime.datecCs:|j|}|jjdd|}dddt||DS)z Performs strftime(), always returning a str :param format: A strftime() format string :return: A str of the formatted datetime i)r_rQcss&|]\}}||fdkrdn|VqdS))rbrcrNr))rdrerfr)r)r*rgsz-extended_datetime.strftime..)r^rhrirjrk)r<rlrmrnr)r)r*rhys zextended_datetime.strftimeTcCsBd|j|j||j|j|jf}|jr2|d|j7}|t|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 z0000-%02d-%02d%c%02d:%02d:%02dz.%06d)r`rarrrrr;rF)r<sepsr)r)r*roszextended_datetime.isoformatNcOs0|r|jj|f||St|jjd||S)a Returns a new datetime.datetime or asn1crypto.util.extended_datetime object with the specified components replaced :param year: The new year to substitute. None to keep it. :param args: Other positional arguments; see datetime.datetime.replace. :param kwargs: Other keyword arguments; see datetime.datetime.replace. :return: A datetime.datetime or asn1crypto.util.extended_datetime object )r)r^rir~from_y2k)r<r_rrr)r)r*riszextended_datetime.replacecCst|j|S)z Convert this extended_datetime to another timezone. :param tz: A datetime.tzinfo object. :return: A new extended_datetime or datetime.datetime object )r~rr^ astimezone)r<r[r)r)r*rs zextended_datetime.astimezonecCs|j|jdS)z Return POSIX timestamp. Only supported in python >= 3.3 :return: A float representing the seconds since 1970-01-01 UTC. This will be a negative value. iQ)r^ timestampDAYS_IN_2000_YEARS)r<r)r)r*rszextended_datetime.timestampcCs |jddS)zy :return: A str representing this extended_datetime, e.g. "0000-01-01 00:00:00.000001-10:00"  )r)ro)r<r)r)r*rqszextended_datetime.__str__cCs:t||jtfsdS|jdk|jdkkr,dS||dkS)z Compare two extended_datetime objects :param other: The other extended_datetime to compare to :return: A boolean FNr)rrrsrr rt)r<rAr)r)r*rBs zextended_datetime.__eq__cCs || S)z Compare two extended_datetime objects :param other: The other extended_datetime to compare to :return: A boolean )rB)r<rAr)r)r*rus zextended_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)rvr r)r<rAr)r)r*rws z#extended_datetime._comparison_errorcCsbt||jtfs|||jdk|jdkkr6td||}td}||krRdS||kr^dSdS)a Compare two extended_datetime or datetime.datetime objects :param other: The other extended_datetime or datetime.datetime object to compare to :return: An integer smaller than, equal to, or larger than 0 Nz5can't compare offset-naive and offset-aware datetimesrrxr )rrrsrrwr rvr)r<rAZdiffZzeror)r)r*rts  zextended_datetime.__cmp__cCs||dkS)Nr)rt)r<rAr)r)r*ry szextended_datetime.__lt__cCs||dkS)Nr)rt)r<rAr)r)r*rz#szextended_datetime.__le__cCs||dkS)Nr)rt)r<rAr)r)r*r{&szextended_datetime.__gt__cCs||dkS)Nr)rt)r<rAr)r)r*r|)szextended_datetime.__ge__cCst|j|S)z Adds a timedelta :param other: A datetime.timedelta object to add. :return: A new extended_datetime or datetime.datetime object. )r~rr^)r<rAr)r)r*__add__,s zextended_datetime.__add__cCsTt|trt|j|St|tr0|j|jSt|trP|j|t|jdStS)aM Subtracts a timedelta or another datetime. :param other: A datetime.timedelta or datetime.datetime or extended_datetime object to subtract. :return: If a timedelta is passed, a new extended_datetime or datetime.datetime object. Else a datetime.timedelta object. )rT)rrrr~rr^rrNotImplemented)r<rAr)r)r*__sub__9s    zextended_datetime.__sub__cCs || S)Nr))r<rAr)r)r*__rsub__Pszextended_datetime.__rsub__c Cs@|jd}|dkrt}n|}|||j|j|j|j|j|j|jS)z Revert substitution of year 2000. :param value: A datetime.datetime object which is 2000 years in the future. :return: A new extended_datetime or datetime.datetime object. ir) r_rr`rarrrrr )rpr#r_Znew_clsr)r)r*rSs zextended_datetime.from_y2k)r)N)&rHrIrJrKZDAYS_IN_400_YEARSrr?r}r_r`rarrrrr rFrrrhrorirrrqrBrurwrtryrzr{r|rrr classmethodrr)r)r)r*r~sB       r~)FN)F)FN)F)'rK __future__rrrrrsysrrrr _errorsr Z_irir r Z _ordereddictr_typesrplatformZ_inetrrsocket version_infor+r0r1utcr;rXZ utc_with_dstrYr\objectr]r~r)r)r)r*s6      2 Y    B