# -*- coding: utf-8 -*-"""Helper functions... note:: This module is "ZERO-DEPENDENCY"."""importtypingasTimporthashlibtry:fromrequestsimportResponseexceptImportErrorase:# pragma: no coverpassfrom.loggerimportloggerdefsha256_of_bytes(b:bytes)->str:# pragma: no coversha256=hashlib.sha256()sha256.update(b)returnsha256.hexdigest()
[docs]defextract_digit_tokens(text:str)->T.List[str]:""" Extract all consecutive digit tokens from a string. Example: >>> extract_digit_tokens("1.23.456") ["1", "23", "456"] """forcinlist(text):ifnotc.isdigit():text=text.replace(c," ")return[token.strip()fortokenintext.split()iftoken.strip()]
[docs]defbump_version(current_version:str,major:bool=False,minor:bool=False,patch:bool=False,minor_start_from:int=0,micro_start_from:int=0,):""" Bump a semantic version. The current version has to be in x.y.z format, where x, y, z are integers. :param current_version: current version string. :param major: bump major version. :param minor: bump minor version. :param patch: bump patch version. :param minor_start_from: if bumping major version, minor start from this number. :param micro_start_from: if bumping minor version, micro start from this number. """ifsum([patch,minor,major])!=1:raiseValueError("Only one and exact one of 'patch', 'minor', 'major' can be True")# get the current versionmajor_ver,minor_ver,micro_ver=current_version.split(".")major_ver,minor_ver,micro_ver=int(major_ver),int(minor_ver),int(micro_ver)# update versionifmajor:major_ver+=1minor_ver,micro_ver=minor_start_from,micro_start_fromelifminor:minor_ver+=1micro_ver=micro_start_fromelifpatch:micro_ver+=1else:# pragma: no coverraiseNotImplementedErrorreturnf"{major_ver}.{minor_ver}.{micro_ver}"
defprint_command(args:T.List[str]):cmd=" ".join(args)logger.info(f"run command: {cmd}")defraise_http_response_error(response:"Response"):# pragma: no coverprint(f"status = {response.status_code}")print(f"body = {response.text}")raiseException("HTTP request failed, see error details above.")