<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import re <EOL> from itertools import izip_longest <EOL> class Revision ( object ) : <EOL> """<STR_LIT>""" <EOL> class BadRevision ( Exception ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def _parse_atom ( cls , atom ) : <EOL> try : <EOL> return int ( atom ) <EOL> except ValueError : <EOL> return atom <EOL> @ classmethod <EOL> def semver ( cls , rev ) : <EOL> """<STR_LIT>""" <EOL> def parse_extra ( delimiter , value ) : <EOL> if not value : <EOL> return None , None <EOL> else : <EOL> components = value . split ( delimiter , <NUM_LIT:1> ) <EOL> return components [ <NUM_LIT:0> ] , None if len ( components ) == <NUM_LIT:1> else components [ <NUM_LIT:1> ] <EOL> def parse_patch ( patch ) : <EOL> patch , pre_release = parse_extra ( '<STR_LIT:->' , patch ) <EOL> if pre_release : <EOL> pre_release , build = parse_extra ( '<STR_LIT:+>' , pre_release ) <EOL> else : <EOL> patch , build = parse_extra ( '<STR_LIT:+>' , patch ) <EOL> return patch , pre_release , build <EOL> def parse_components ( value ) : <EOL> if not value : <EOL> yield None <EOL> else : <EOL> for atom in value . split ( '<STR_LIT:.>' ) : <EOL> yield cls . _parse_atom ( atom ) <EOL> try : <EOL> major , minor , patch = rev . split ( '<STR_LIT:.>' , <NUM_LIT:2> ) <EOL> patch , pre_release , build = parse_patch ( patch ) <EOL> components = [ int ( major ) , int ( minor ) , int ( patch ) ] <EOL> components . extend ( parse_components ( pre_release ) ) <EOL> components . extend ( parse_components ( build ) ) <EOL> return cls ( * components ) <EOL> except ValueError : <EOL> raise cls . BadRevision ( "<STR_LIT>" . format ( rev ) ) <EOL> @ classmethod <EOL> def lenient ( cls , rev ) : <EOL> """<STR_LIT>""" <EOL> rev = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , rev ) <EOL> rev = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , rev ) <EOL> return cls ( * map ( cls . _parse_atom , re . split ( r'<STR_LIT>' , rev ) ) ) <EOL> def __init__ ( self , * components ) : <EOL> self . _components = components <EOL> @ property <EOL> def components ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . _components ) <EOL> def __cmp__ ( self , other ) : <EOL> for ours , theirs in izip_longest ( self . _components , other . _components , fillvalue = <NUM_LIT:0> ) : <EOL> difference = cmp ( ours , theirs ) <EOL> if difference != <NUM_LIT:0> : <EOL> return difference <EOL> return <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , '<STR_LIT:U+002CU+0020>' . join ( map ( repr , self . _components ) ) ) <EOL> def __eq__ ( self , other ) : <EOL> return hasattr ( other , '<STR_LIT>' ) and tuple ( self . _components ) == tuple ( other . _components ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _components ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:.>' . join ( str ( c ) for c in self . _components ) </s>
