"""Object representing page revision."""## (C) Pywikibot team, 2008-2022## Distributed under the terms of the MIT license.#from__future__importannotationsimporthashlibfromcollections.abcimportMappingfromcontextlibimportsuppressfrompywikibotimportTimestamp
[docs]classRevision(Mapping):"""A structure holding information about a single revision of a Page. Each data item can be accessed either by its key or as an attribute with the attribute name equal to the key e.g.: >>> r = Revision(comment='Sample for Revision access') >>> r.comment == r['comment'] True >>> r.comment 'Sample for Revision access' .. seealso:: - :api:`Revisions` - :api:`Alldeletedrevisions` """def__init__(self,**kwargs)->None:"""Initializer."""self._data=kwargsself._upcast_dict(self._data)super().__init__()@staticmethoddef_upcast_dict(map_)->None:"""Upcast dictionary values."""withsuppress(KeyError):# enable doctestmap_['timestamp']=Timestamp.fromISOformat(map_['timestamp'])map_.update(anon='anon'inmap_)map_.update(minor='minor'inmap_)map_.update(userhidden='userhidden'inmap_)map_.update(commenthidden='commenthidden'inmap_)map_.setdefault('comment','')map_.setdefault('user','')if'slots'inmap_:# mw 1.32+mainslot=map_['slots'].get('main',{})map_['text']=mainslot.get('*')map_['contentmodel']=mainslot.get('contentmodel')else:map_['slots']=Nonemap_['text']=map_.get('*')map_.setdefault('sha1')ifmap_['sha1']isNoneandmap_['text']isnotNone:map_['sha1']=hashlib.sha1(map_['text'].encode('utf8')).hexdigest()def__len__(self)->int:"""Return the number of data items."""returnlen(self._data)def__getitem__(self,name:str):"""Return a single Revision item given by name."""ifnameinself._data:returnself._data[name]returnself.__missing__(name)# provide attribute access__getattr__=__getitem__def__iter__(self):"""Provide Revision data as iterator."""returniter(self._data)def__repr__(self)->str:"""String representation of Revision."""returnf'{self.__class__.__name__}({self._data})'def__str__(self)->str:"""Printable representation of Revision data."""returnstr(self._data)def__missing__(self,key):"""Provide backward compatibility for exceptions."""# raise AttributeError instead of KeyError for backward compatibilityraiseAttributeError("'{}' object has no attribute '{}'".format(self.__class__.__name__,key))