B
    %dW                 @   s  d Z ddlmZmZ ddlZddlmZmZmZ ddl	Z	ddl
mZ ddlmZ ddlZddlZddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! dd	l"m#Z#m$Z$m%Z% er<dd
l&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl"m6Z6m7Z7 neZ6dd Z7G dd deZ8edddZ9e ee d df df Z:dZ;edZ<e e=e>f e?ddd Z@e=e ed! ed" ed# ed$ f d%d&d'ZAe>e?d(d)d*ZBe?e>d+d,d-ZCe>e>d.d/d0ZDG d1d2 d2eZEeEdd3ZFeGeGed4d5d6ZHe e>ef ee?e?f d7d8d9ZIe	Jd:ZKe	Jd;ZLe>eee?e?f d<d=d>ZMG d?d@ d@eNZOe7G dAd de6ZPe7G dBdC dCe6ZQG dDd deePZRdS )Ez$Module for general utility functions    )ABCabstractmethodN)IterableListIterableObjActor)deque)digits)datetime	timedeltatzinfo)AnyCallableDequeIteratorGeneric
NamedTupleoverloadSequenceTYPE_CHECKINGTupleTypeTypeVarUnioncast)Has_id_attributeLiteral_T)BytesIOStringIO   )Commit)Blob)	TagObject)TreeTraversedTreeTup)Popen)	Submodule)Protocolruntime_checkablec             C   s   | S )N )fr)   r)   ]/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/git/objects/util.pyr(   7   s    r(   c               @   s.   e Zd ZU eed< ed ed< ed ed< dS )
TraverseNTdepth)Traversabler!   item)r.   NsrcN)__name__
__module____qualname__int__annotations__r   r)   r)   r)   r+   r,   ;   s   
r,   T_TIobjTraversableIterableObj)bound)r.   Nr.   r$   )get_object_type_by_name
parse_dateparse_actor_and_dateProcessStreamAdapterr.   altz_to_utctz_strutctz_to_altzverify_utctzr   tzoffsetutc)modestrreturnc             C   sP   d}xFt t| dd D ].\}}ttttf |}|t||d > 7 }qW |S )a_  
    :param modestr: string like 755 or 644 or 100644 - only the last 6 chars will be used
    :return:
        String identifying a mode compatible to the mode methods ids of the
        stat module regarding the rwx permissions for user, group and other,
        special flags and file system flags, i.e. whether it is a symlink
        for example.r   iN   )	enumeratereversedr   r   strr4   )rB   mode	iterationcharr)   r)   r+   mode_str_to_int]   s
    rK   r    r"   r#   r!   )object_type_namerC   c             C   s|   | dkrddl m} |jS | dkr4ddl m} |jS | dkrNddl m} |jS | dkrhdd	l m} |jS t	d
| 
  dS )z
    :return: type suitable to handle the given object type name.
        Use the type to create new instances.

    :param object_type_name: Member of TYPES

    :raise ValueError: In case object_type_name is unknowns   commitr   )commits   tag)tags   blob)blobs   tree)treez%Cannot handle unknown object type: %sN) rM   r    rN   r"   rO   r!   rP   r#   
ValueErrordecode)rL   rM   rN   rO   rP   r)   r)   r+   r9   m   s    
r9   )utctzrC   c             C   s:   t | }t|d d t|d d  }|dk r4|S | S )zConvert a git timezone offset into a timezone offset west of
    UTC in seconds (compatible with time.altzone).

    :param utctz: git utc timezone string, i.e. +0200
    d   i  <   r   )r4   abs)rT   Z	int_utctzsecondsr)   r)   r+   r>      s     r>   )altzrC   c             C   s:   t | d }t | d d }| dkr(dnd}d|||S )zConvert a timezone offset west of UTC in seconds into a git timezone offset string

    :param altz: timezone offset in seconds west of UTC
    i  rV   -+z{}{:02}{:02})rW   format)rY   hoursminutessignr)   r)   r+   r=      s    r=   )offsetrC   c             C   sd   t d|  }t| dkr|| d dkr,|| d tks\| d tks\| d tks\| d tkr`|| S )	z=:raise ValueError: if offset is incorrect
    :return: offsetz"Invalid timezone offset format: %s   r   z+-r      rD      )rR   lenr   )r`   Zfmt_excr)   r)   r+   r?      s    0r?   c               @   s   e Zd Zdeedef ddddZeed  eeef f dddZ	ee
df edd	d
Zee
df edddZee
df edddZdS )r@   N)secs_west_of_utcnamerC   c             C   s   t | d| _|pd| _d S )N)rX   Zfixed)r
   _offset_name)selfre   rf   r)   r)   r+   __init__   s    ztzoffset.__init__)rC   c             C   s   t | j  | jffS )N)r@   rg   total_secondsrh   )ri   r)   r)   r+   
__reduce__   s    ztzoffset.__reduce__)dtrC   c             C   s   | j S )N)rg   )ri   rm   r)   r)   r+   	utcoffset   s    ztzoffset.utcoffsetc             C   s   | j S )N)rh   )ri   rm   r)   r)   r+   tzname   s    ztzoffset.tznamec             C   s   t S )N)ZERO)ri   rm   r)   r)   r+   dst   s    ztzoffset.dst)N)r1   r2   r3   floatr   rG   rj   r   r   rl   r	   r
   rn   ro   rq   r)   r)   r)   r+   r@      s
   "r@   UTC)	timestamp	tz_offsetrC   c             C   s8   t | t}y|t|}|S  tk
r2   |S X dS )zAConverts a timestamp + tz_offset into an aware datetime instance.N)r	   fromtimestamprA   
astimezoner@   rR   )rt   ru   Zutc_dtZlocal_dtr)   r)   r+   from_timestamp   s    rx   )string_daterC   c             C   sB  t | trP| jrBtt|  }t|  }t| t	
 |fS td|  y| ddkr| ddkr|  \}}|dr|dd }t|}|tt|fS d}| d	 d
krt| d	d }| dd } t|}g }d}d| kr|d | d}nF|d |d |d |d | d}|dkrL| d}|dksZt| |d d }| d| }	t|d}
xp|D ]h}yFt|	|}t|j|j|j|
j|
j|
j|j|j|
j f	}t||fS  tk
r   wY nX qW tdW n> t!k
r< } ztd|  dt"|  |W dd}~X Y nX dS )a  
    Parse the given date as one of the following

        * aware datetime instance
        * Git internal format: timestamp offset
        * RFC 2822: Thu, 07 Apr 2005 22:13:13 +0200.
        * ISO 8601 2005-04-07T22:13:13
            The T can be a space as well

    :return: Tuple(int(timestamp_UTC), int(offset)), both in seconds since epoch
    :raise ValueError: If the format could not be understood
    :note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY.
    z,string_date datetime object without tzinfo,  r   :@Nz+0000z-+i,z%a, %d %b %Yz%Y-%m-%dz%Y.%m.%dz%m/%d/%Yz%d.%m.%YTz%H:%M:%Szno format matchedz!Unsupported date format or type: z, type=)#
isinstancer	   r   r   r
   rn   r4   rk   rw   rA   rt   rR   countrfindsplit
startswithr>   r?   appendAssertionErrortimestrptimecalendartimegmtm_yeartm_montm_mdaytm_hourtm_mintm_sectm_wdaytm_ydaytm_isdst	Exceptiontype)ry   rn   r`   rt   Z
offset_strZtimestamp_intZdate_formatsZsplitterZ	time_partZ	date_partZtstructfmtZdtstructZutctimeer)   r)   r+   r:      sj    












r:   z^.+? (.*) (\d+) ([+-]\d+).*$z
^.+? (.*)$)linerC   c             C   s`   d\}}}t | }|r(| \}}}n t| }|r@|dn| pFd}t|t|t|fS )zParse out the actor (author or committer) info from a line like::

        author Tom Preston-Werner <tom@mojombo.com> 1191999972 -0700

    :return: [Actor, int_seconds_since_epoch, int_timezone_offset])rQ   0r   r   rQ   )	_re_actor_epochsearchgroups_re_only_actorgroupr   Z_from_stringr4   r>   )r   Zactorepochr`   mr)   r)   r+   r;   3  s    


r;   c               @   s6   e Zd ZdZdZdeddddZeedd	d
ZdS )r<   a  Class wireing all calls to the contained Process instance.

    Use this type to hide the underlying process to provide access only to a specified
    stream. The process is usually wrapped into an AutoInterrupt class to kill
    it if the instance goes out of scope.)_proc_streamr%   N)processstream_namerC   c             C   s   || _ t||| _d S )N)r   getattrr   )ri   r   r   r)   r)   r+   rj   S  s    zProcessStreamAdapter.__init__)attrrC   c             C   s   t | j|S )N)r   r   )ri   r   r)   r)   r+   __getattr__W  s    z ProcessStreamAdapter.__getattr__)	r1   r2   r3   __doc__	__slots__rG   rj   r   r   r)   r)   r)   r+   r<   I  s   r<   c               @   s   e Zd ZdZdZeeeed  dddZ	eeeedddZ
deeeeed
  dddZeeeedddZdd dd ddddd	feed def egef eed def egef eeeeeeeed  ee f dddZdS )r.   zSimple interface to perform depth-first or breadth-first traversals
    into one direction.
    Subclasses only need to implement one function.
    Instances of the Subclass must be hashable

    Defined subclasses = [Commit, Tree, SubModule]
    r)   )r/   rC   c             C   s   t ddS )a@  
        Returns:
            Tuple of items connected to the given item.
            Must be implemented in subclass

        class Commit::     (cls, Commit) -> Tuple[Commit, ...]
        class Submodule::  (cls, Submodule) -> Iterablelist[Submodule]
        class Tree::       (cls, Tree) -> Tuple[Tree, ...]
        zTo be implemented in subclassN)NotImplementedError)clsr/   r)   r)   r+   _get_intermediate_itemsh  s    z#Traversable._get_intermediate_items)argskwargsrC   c             O   s   t jdtdd | j||S )rz   zlist_traverse() method should only be called from subclasses.Calling from Traversable abstract class will raise NotImplementedError in 3.1.20Builtin sublclasses are 'Submodule', 'Tree' and 'Commitrb   )
stacklevel)warningswarnDeprecationWarning_list_traverse)ri   r   r   r)   r)   r+   list_traversev  s
    zTraversable.list_traverseF)r    r&   r#   r!   )as_edger   r   rC   c             O   sX   t | tr| j}nd}|s@t|}|| j|d|i| |S t| j||}|S dS )a  
        :return: IterableList with the results of the traversal as produced by
            traverse()
            Commit -> IterableList['Commit']
            Submodule ->  IterableList['Submodule']
            Tree -> IterableList[Union['Submodule', 'Tree', 'Blob']]
        rQ   r   N)r   r   Z_id_attribute_r   extendtraverse)ri   r   r   r   idoutZout_listr)   r)   r+   r     s    
zTraversable._list_traversec             O   s   t jdtdd | j||S )rz   ztraverse() method should only be called from subclasses.Calling from Traversable abstract class will raise NotImplementedError in 3.1.20Builtin sublclasses are 'Submodule', 'Tree' and 'Commitrb   )r   )r   r   r   	_traverse)ri   r   r   r)   r)   r+   r     s
    zTraversable.traversec             C   s   dS )NTr)   )idr)   r)   r+   <lambda>      zTraversable.<lambda>c             C   s   dS )NFr)   )r   r   r)   r)   r+   r     r   r|   Tr   r!   )r.   r!   )	predicatepruner-   branch_first
visit_onceignore_selfr   rC   c             #   s   t  }t }	|	tdd tt dttdd fdd}
x|	r|	 \} }|r` |kr`q@|rn|  |r|| f}n }|||rq@|o k}|s|||r|V  |d }|dkr||krq@|
|	 || q@W dS )	aF  :return: iterator yielding of items found when traversing self
        :param predicate: f(i,d) returns False if item i at depth d should not be included in the result

        :param prune:
            f(i,d) return True if the search should stop at item i at depth d.
            Item i will not be returned.

        :param depth:
            define at which level the iteration should not go deeper
            if -1, there is no limit
            if 0, you would effectively only get self, the root of the iteration
            i.e. if 1, you would only get the first level of predecessors/successors

        :param branch_first:
            if True, items will be returned branch first, otherwise depth first

        :param visit_once:
            if True, items will only be returned once, although they might be encountered
            several times. Loops are prevented that way.

        :param ignore_self:
            if True, self will be ignored and automatically pruned from
            the result. Otherwise it will be the first item to be returned.
            If as_edge is True, the source of the first edge is None

        :param as_edge:
            if True, return a pair of items, first being the source, second the
            destination, i.e. tuple(src, dest) with the edge spanning from
            source to destinationr   Nr.   )stacksrc_itemr   r-   rC   c                sf    sd S |r2|  fddD  n0 fddttd ddD }| | d S )Nc             3   s   | ]}t  |V  qd S )N)r,   ).0r   )r-   r   r)   r+   	<genexpr>  s    z<Traversable._traverse.<locals>.addToStack.<locals>.<genexpr>c             3   s   | ]}t  | V  qd S )N)r,   )r   r   )r-   lstr   r)   r+   r     s    r   r|   )r   
extendleftrangerd   r   )r   r   r   r-   Zreviter)r/   ri   )r-   r   r   r+   
addToStack  s    
&z)Traversable._traverse.<locals>.addToStackr   r|   )	setr   r   r,   r   boolr4   popadd)ri   r   r   r-   r   r   r   r   visitedr   r   r   r0   rvalZskipStartItemndr)   )r/   ri   r+   r     s4    3


zTraversable._traverseN)F)r1   r2   r3   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   TraversedTupr4   r   r   r)   r)   r)   r+   r.   [  s&   	>c               @   s4   e Zd ZdZdZdd dddZdd dddZd	S )
SerializablezPDefines methods to serialize and deserialize objects from and into a data streamr)   r   )streamrC   c             C   s   t ddS )zSerialize the data of this object into the given data stream
        :note: a serialized object would ``_deserialize`` into the same object
        :param stream: a file-like object
        :return: selfzTo be implemented in subclassN)r   )ri   r   r)   r)   r+   
_serialize  s    zSerializable._serializec             C   s   t ddS )zDeserialize all information regarding this object from the stream
        :param stream: a file-like object
        :return: selfzTo be implemented in subclassN)r   )ri   r   r)   r)   r+   _deserialize"  s    zSerializable._deserializeN)r1   r2   r3   r   r   r   r   r)   r)   r)   r+   r     s   r   c                   s6  e Zd ZdZeeedf ef Zeeee	e d fddZ
eeee dddZeeeeeeeedf ef f egef eeeeeedf ef f egef eeeed	 ed
 ee d	ddZeeeeeeeedf ef f egef eeeeeedf ef f egef eeeed
 ed	 eeeedf ef  d	ddZeeeeeef egef eeeef egef eeeed	 ed	 eeeef  d	ddZdd dd dd	d	dd
feeeeef egef eeeef egef eeeeeeee eeeef  ee f d	 fddZ  ZS )r7   r)   N)ri   r   r   rC   c                s   t t| j||S )N)superr7   r   )ri   r   r   )	__class__r)   r+   r   .  s    z$TraversableIterableObj.list_traverse)ri   rC   c             C   s   d S )Nr)   )ri   r)   r)   r+   r   1  s    zTraversableIterableObj.traverseTF)	ri   r   r   r-   r   r   r   r   rC   c             C   s   d S )Nr)   )ri   r   r   r-   r   r   r   r   r)   r)   r+   r   5  s    c             C   s   d S )Nr)   )ri   r   r   r-   r   r   r   r   r)   r)   r+   r   B  s    c             C   s   d S )Nr)   )ri   r   r   r-   r   r   r   r   r)   r)   r+   r   O  s    c             C   s   dS )NTr)   )r   r   r)   r)   r+   r   ^  r   zTraversableIterableObj.<lambda>c             C   s   dS )NFr)   )r   r   r)   r)   r+   r   _  r   r|   r   c                sB   t ttt tttdtf tf  f tt| |||||||S )z3For documentation, see util.Traversable._traverse()N)r   r   r   r6   r   r   r7   r   )ri   r   r   r-   r   r   r   r   )r   r)   r+   r   \  s    "
)r1   r2   r3   r   r   r   r6   ZTIobj_tupler   r   r   r   r   r   r   r4   r   r   __classcell__r)   r)   )r   r+   r7   )  sV   &&&&$<)Sr   abcr   r   r   Zgit.utilr   r   r   recollectionsr   stringr   r   r   r	   r
   r   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	git.typesr   r   r   ior   r   rM   r    rO   r!   rN   r"   rP   r#   r$   
subprocessr%   Zsubmodule.baser&   r'   r(   r,   r6   r   __all__rp   bytesrG   r4   rK   r9   r>   r=   r?   r@   rA   rr   rx   r:   compiler   r   r;   objectr<   r.   r   r7   r)   r)   r)   r+   <module>   sb   @*

 a

 7