27"""Convert a version string to an internal representation. 28 The parsed format can be compared quickly using the standard Python functions. 29 - Version := DottedList ("-" Mod DottedList?)* 30 - DottedList := (Integer ("." Integer)*) 31 @type version_string: str 32 @rtype: tuple (opaque) 33 @raise SafeException: if the string isn't a valid version 34 @since: 0.24 (moved from L{reader}, from where it is still available):""" 35ifversion_stringisNone:returnNone 36parts=_version_re.split(version_string) 37ifparts[-1]=='': 38delparts[-1]# Ends with a modifier 39else: 40parts.append('') 41ifnotparts: 42raiseSafeException(_("Empty version string!")) 43l=len(parts) 44try: 45forxinrange(0,l,2): 46part=parts[x] 47ifpart: 48parts[x]=list(map(int,parts[x].split('.'))) 49else: 50parts[x]=[]# (because ''.split('.') == [''], not []) 51forxinrange(1,l,2): 52parts[x]=_version_mod_to_value[parts[x]] 53returnparts 54exceptValueErrorasex: 55raiseSafeException(_("Invalid version format in '%(version_string)s': %(exception)s")%{'version_string':version_string,'exception':ex}) 56exceptKeyErrorasex: 57raiseSafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s")%{'version_string':version_string,'exception':str(ex).strip("u")})
75"""Parse a range expression. 76 @param r: the range expression 77 @type r: str 78 @return: a function which returns whether a parsed version is in the range 79 @rtype: parsed_version -> bool 80 @since: 1.13""" 81parts=r.split('..',1) 82iflen(parts)==1: 83ifr.startswith('!'): 84v=parse_version(r[1:]) 85returnlambdax:x!=v 86else: 87v=parse_version(r) 88returnlambdax:x==v 89 90start,end=parts 91ifstart: 92start=parse_version(start) 93else: 94start=None 95ifend: 96ifnotend.startswith('!'): 97raiseSafeException("End of range must be exclusive (use '..!{end}', not '..{end}')".format(end=end)) 98end=parse_version(end[1:]) 99else:100end=None101102deftest(v):103ifstartisnotNoneandv<start:returnFalse104ifendisnotNoneandv>=end:returnFalse105returnTrue
110"""Parse an expression of the form "RANGE | RANGE | ...".111 @param expr: the expression to parse112 @type expr: str113 @return: a function which tests whether a parsed version is in the range114 @rtype: parsed_version -> bool115 @since: 1.13"""116tests=[parse_version_range(r.strip())forrinexpr.split('|')]117returnlambdav:any(test(v)fortestintests)