3 SLjQ@sdZdZddlZddlZejddkr`GdddeZGdddeZejjeejjenejZGd d d eZ Gd d d e Z ej je Gd dde Z GdddeZ GdddeZdS)aContains container classes to represent different protocol buffer types. This file defines container classes which represent categories of protocol buffer field types which need extra maintenance. Currently these categories are: - Repeated scalar fields - These are all repeated fields which aren't composite (e.g. they are of simple types like int32, string, etc). - Repeated composite fields - Repeated fields which are composite. This includes groups and nested messages. zpetar@google.com (Petar Petrov)Nc@sfeZdZfZdddZddZddZdd Zd d Zd d Z ddZ ddZ dZ ddZ ddZdS)MappingNc Cs"y||Stk r|SXdS)N)KeyError)selfkeydefaultr /usr/lib/python3.6/containers.pygetGsz Mapping.getc Cs*y ||Wntk r dSXdSdS)NFT)r)rrrrr __contains__Ms  zMapping.__contains__cCst|S)N)iter)rrrr iterkeysUszMapping.iterkeysccsx|D]}||VqWdS)Nr)rrrrr itervaluesXs zMapping.itervaluesccs x|D]}|||fVqWdS)Nr)rrrrr iteritems\s zMapping.iteritemscCst|S)N)list)rrrr keys`sz Mapping.keyscsfddDS)Ncsg|]}||fqSrr).0r)rrr dsz!Mapping.items..r)rr)rr itemscsz Mapping.itemscsfddDS)Ncsg|] }|qSrr)rr)rrr rgsz"Mapping.values..r)rr)rr valuesfszMapping.valuescCs(t|tjstSt|jt|jkS)N) isinstance collectionsrNotImplementeddictr)rotherrrr __eq__ls zMapping.__eq__cCs ||k S)Nr)rrrrr __ne__qszMapping.__ne__)N)__name__ __module__ __qualname__ __slots__r r r rrrrr__hash__rrrrrr rDs rc@sDeZdZfZeZefddZddZddZddZ d d d Z d S) MutableMappingc Cs<y ||}Wn tk r,||jkr(|SX||=|SdS)N)r_MutableMapping__marker)rrrvaluerrr popys  zMutableMapping.popc Cs@ytt|}Wntk r(tYnX||}||=||fS)N)nextr StopIterationr)rrr$rrr popitems zMutableMapping.popitemc Cs,yx |jqWWntk r&YnXdS)N)r(r)rrrr clears zMutableMapping.clearcOst|dkr tdjt|n |s,td|d}t|dkrH|dnf}t|trrx^|D]}||||<q\WnDt|drx8|jD]}||||<qWnx|D]\}}|||<qWx|jD]\}}|||<qWdS)Nz8update() takes at most 2 positional arguments ({} given)z,update() takes at least 1 argument (0 given)rr)len TypeErrorformatrrhasattrrr)argskwdsrrrr$rrr updates"     zMutableMapping.updateNc Cs*y||Stk r$|||<YnX|S)N)r)rrrrrr setdefaults zMutableMapping.setdefault)N) rrrr objectr#r%r(r)r2r3rrrr r"ts  r"c@sPeZdZdZddgZddZddZdd Zd d Zd d Z ddZ ddZ dS) BaseContainerzBase container class._message_listener_valuescCs||_g|_dS)z Args: message_listener: A MessageListener implementation. The RepeatedScalarFieldContainer will call this object's Modified() method when it is modified. N)r6r7)rmessage_listenerrrr __init__szBaseContainer.__init__cCs |j|S)z$Retrieves item by the specified key.)r7)rrrrr __getitem__szBaseContainer.__getitem__cCs t|jS)z0Returns the number of elements in the container.)r,r7)rrrr __len__szBaseContainer.__len__cCs ||k S)z3Checks if another instance isn't equal to this one.r)rrrrr rszBaseContainer.__ne__cCs tddS)Nzunhashable object)r-)rrrr r!szBaseContainer.__hash__cCs t|jS)N)reprr7)rrrr __repr__szBaseContainer.__repr__cOs(d|kr|jd|d<|jj||dS)NZ sort_functionZcmp)r%r7sort)rr0kwargsrrr r>szBaseContainer.sortN) rrr__doc__r r9r:r;rr!r=r>rrrr r5s r5cseZdZdZdgZfddZddZddZd d Zd d Z d dZ dddZ ddZ ddZ ddZddZddZddZZS) RepeatedScalarFieldContainerzGSimple, type-checked, list-like container for holding repeated scalars. _type_checkercstt|j|||_dS)a, Args: message_listener: A MessageListener implementation. The RepeatedScalarFieldContainer will call this object's Modified() method when it is modified. type_checker: A type_checkers.ValueChecker instance to run on elements inserted into this container. N)superrAr9rB)rr8Z type_checker) __class__rr r9s z%RepeatedScalarFieldContainer.__init__cCs*|jj|jj||jjs&|jjdS)z6Appends an item to the list. Similar to list.append().N)r7appendrB CheckValuer6dirtyModified)rr$rrr rEsz#RepeatedScalarFieldContainer.appendcCs,|jj||jj||jjs(|jjdS)zEInserts the item at the specified position. Similar to list.insert().N)r7insertrBrFr6rGrH)rrr$rrr rIsz#RepeatedScalarFieldContainer.insertc sh|dkr dSy t|}Wntk r6|s0dSYnXfdd|D}|rZjj|jjdS)zBExtends by appending the given iterable. Similar to list.extend().Ncsg|]}jj|qSr)rBrF)relem)rrr rsz7RepeatedScalarFieldContainer.extend..)r r-r7extendr6rH)relem_seqZ elem_seq_iter new_valuesr)rr rKs  z#RepeatedScalarFieldContainer.extendcCs|jj|j|jjdS)zAppends the contents of another repeated field of the same type to this one. We do not check the types of the individual fields. N)r7rKr6rH)rrrrr MergeFromsz&RepeatedScalarFieldContainer.MergeFromcCs|jj||jjdS)z8Removes an item from the list. Similar to list.remove().N)r7remover6rH)rrJrrr rOs z#RepeatedScalarFieldContainer.remover+cCs|j|}|j||S)zDRemoves and returns an item at a given index. Similar to list.pop().)r7 __delitem__)rrr$rrr r%$s  z RepeatedScalarFieldContainer.popcCsPt|tr0|jdk rtd|j|j|j|n|jj||j |<|j j dS)z(Sets the item on the specified position.NzExtended slices not supported) rslicestep ValueError __setslice__startstoprBrFr7r6rH)rrr$rrr __setitem__*s   z(RepeatedScalarFieldContainer.__setitem__cCs|j||S)zARetrieves the subset of items from between the specified indices.)r7)rrUrVrrr __getslice__4sz)RepeatedScalarFieldContainer.__getslice__cCs@g}x|D]}|j|jj|q W||j||<|jjdS)z|jj|}|jj}||j|<|SXdS)N)r7rrerFrfZ DefaultValue)rrvalrrr r:s    zScalarMap.__getitem__cCs|jj|||jkS)N)rerFr7)ritemrrr r s zScalarMap.__contains__NcCs||kr||S|SdS)Nr)rrrrrr r sz ScalarMap.getcCs0|jj|}|jj|}||j|<|jjdS)N)rerFrfr7r6rH)rrr$Z checked_keyZ checked_valuerrr rWs   zScalarMap.__setitem__cCs|j|=|jjdS)N)r7r6rH)rrrrr rPszScalarMap.__delitem__cCs t|jS)N)r,r7)rrrr r;szScalarMap.__len__cCs t|jS)N)r r7)rrrr __iter__szScalarMap.__iter__cCs t|jS)N)r<r7)rrrr r=szScalarMap.__repr__cCs|jj|j|jjdS)N)r7r2r6rH)rrrrr rNszScalarMap.MergeFromcCs|j}|j|_d|d<dS)N)r7copy)roriginalrrr InvalidateIteratorss zScalarMap.InvalidateIteratorscCs|jj|jjdS)N)r7r)r6rH)rrrr r)s zScalarMap.clearcCs|jjS)N)rgr_)rrrr GetEntryClassszScalarMap.GetEntryClass)N)rrrr@r r9r:r r rWrPr;rlr=rNror)rprrrr rds   rdc@seZdZdZdddddgZddZd d Zd d Zd$ddZddZ ddZ ddZ ddZ ddZ ddZddZddZd d!Zd"d#Zd S)% MessageMapzESimple, type-checked, dict-like container for with submessage values.rer7r6r]rgcCs"||_||_||_||_i|_dS)a Args: message_listener: A MessageListener implementation. The ScalarMap will call this object's Modified() method when it is modified. key_checker: A type_checkers.ValueChecker instance to run on keys inserted into this container. value_checker: A type_checkers.ValueChecker instance to run on values inserted into this container. entry_descriptor: The MessageDescriptor of a map entry: key and value. N)r6r]rergr7)rr8r^rhrirrr r9s zMessageMap.__init__c CsZy |j|Stk rT|jj|}|jj}|j|j||j|<|jj|SXdS)N) r7rrerFr]r_r`r6rH)rrrarrr r:'s      zMessageMap.__getitem__cCs||S)aaget_or_create() is an alias for getitem (ie. map[key]). Args: key: The key to get or create in the map. This is useful in cases where you want to be explicit that the call is mutating the map. This can avoid lint errors for statements like this that otherwise would appear to be pointless statements: msg.my_map[key] r)rrrrr get_or_create3s zMessageMap.get_or_createNcCs||kr||S|SdS)Nr)rrrrrr r DszMessageMap.getcCs ||jkS)N)r7)rrkrrr r JszMessageMap.__contains__cCs tddS)Nz5May not set values directly, call my_map[key].foo = 5)rS)rrr$rrr rWMszMessageMap.__setitem__cCs|j|=|jjdS)N)r7r6rH)rrrrr rPPszMessageMap.__delitem__cCs t|jS)N)r,r7)rrrr r;TszMessageMap.__len__cCs t|jS)N)r r7)rrrr rlWszMessageMap.__iter__cCs t|jS)N)r<r7)rrrr r=ZszMessageMap.__repr__cCs2x,|D]$}||kr||=||j||qWdS)N)ZCopyFrom)rrrrrr rN]s zMessageMap.MergeFromcCs|j}|j|_d|d<dS)N)r7rm)rrnrrr rogs zMessageMap.InvalidateIteratorscCs|jj|jjdS)N)r7r)r6rH)rrrr r)os zMessageMap.clearcCs|jjS)N)rgr_)rrrr rpsszMessageMap.GetEntryClass)N)rrrr@r r9r:rrr r rWrPr;rlr=rNror)rprrrr rq s"   rq)r@ __author__rsys version_infor4rr"registerr5rAMutableSequencer\rdrqrrrr (s0< -n [Z