def __call__(self, filepath: str): if not os.path.exists(filepath): trace(f'Error: {filepath!r} does not exists', TypeError) elif not os.path.isfile(filepath): trace(f'Error: {filepath!r} is not a file', TypeError) self.fobj = open(filepath, 'r') return self.fobj
def __call__(self, value: Union[str, dict]): try: if isinstance(value, dict): return value return json.loads(value) except: trace(f'{value!r} was not valid JSON', TypeError)
def __call__(self, value: str): patterns = ('%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S') for p in patterns: try: return datetime.strptime(value, p) except: pass trace(f'{value!r} was {type(value).__name__!r} and not {"str"!r} or {"int"!r}', TypeError)
def __call__(self, value: Union[str, int]): if isinstance(value, str): vals = value.split(',') if len(vals) == 1: return list(range(int(vals[0]))) elif len(vals) == 2: return list(range(int(vals[0]), int(vals[1]))) else: trace(f'Unable to find a start or stop value based on given: {value!r}', TypeError) elif isinstance(value, int): return list(range(value)) else: trace(f'{value!r} was {type(value).__name__!r} and not {"str"!r} or {"int"!r}', TypeError)
def get_values(self, cmdl: list): """get_values - overloaded function, this method will create the values to be unpacked into the Cmd function. If --help is anywhere is cmdl, the help message will be printed. Args: cmdl {list} - command line at current state """ if '--help' in cmdl: self._print_help() if hasattr(self, 'params') and hasattr(self.params, 'order'): while len(cmdl) < len(self.params.order): cmdl.append('') for index in range(0, len(self.params.order)): if isinstance(self.params.order[index], Arg): if cmdl[index].startswith('-'): msg = f'An {"opt"!r} was found: {cmdl[index]!r}, ' msg += f'instead of type {"arg"!r}. Order of cmd parameters might be incorrect.' trace(msg, AssertionError, color='red') if len(cmdl) >= index + 1 and cmdl[index + 1]: self.values.append(self.params.order[index].type( cmdl[index + 1])) if isinstance(self.params.order[index], Opt): values = [] for cmdl_index in range(0, len(cmdl)): if cmdl[cmdl_index] == self.params.order[index].name or \ cmdl[cmdl_index] == self.params.order[index].short_name: if len(cmdl) >= cmdl_index + 1 and cmdl[cmdl_index + 1]: values.append(self.params.order[index].type( cmdl[cmdl_index + 1])) if self.params.order[index].required and not values: msg = f'{self.params.order[index].name!r} is required' trace(msg, AssertionError, color='red') if len(values) > 0: if self.params.order[index].multiple: self.values.append(values) else: self.values.append(values[0]) elif self.params.order[index].default is None: self.values.append(self.params.order[index].default) elif self.params.order[index].default: self.values.append(self.params.order[index].type( self.params.order[index].default)) if isinstance(self.params.order[index], Flg): matches = re.findall(self.regex_patterns[index], ' '.join(cmdl)) self.values.append( True) if matches else self.values.append(False)
def __call__(self, value: str): if value and isinstance(value, str): if os.path.exists(value): if os.path.isfile(value): with open(value, 'r') as fin: return SHA256_PATTERN.findall(fin.read()) else: trace(f'expected path to be a file, got {value!r}, {type(value).__name__!r}', TypeError) else: if SHA256_PATTERN.match(value): return value else: trace(f'{value!r} is not a valid sha256', TypeError) else: trace(f'expected string for sha256 type conversion, got {value!r} of type {type(value).__name__}')
def __call__(self, value: str): if value not in self.choices: trace(f'Error: {value!r} was not found in choices: {", ".join(self.choices)!r}', TypeError) return value
def __call__(self, value: str): if URL_PATTERN.match(value): return value trace(f'{value!r} is not a valid URL', TypeError)
def get_values(self, cmdl: list): """get_values - overloaded function, from the command line state, get the command to invoke and set name Args: cmdl {list} -- cmdl state """ cur_state = [] cmd_names = self.get_command_names() for index in range(0, len(cmdl)): if cmdl[index] in cmd_names: cur_state = cmdl[:index] self.invoke = cmdl[index] self.cmdl = cmdl[index:] break if '--help' in cur_state or (len(cur_state) == 0 and all('--help' in c for c in self.cmdl)): self._print_help() if not self.invoke: cur_state = self.cmdl if hasattr(self, 'params') and hasattr(self.params, 'order'): while len(cur_state) < len(self.params.order): cur_state.append('') for index in range(0, len(self.params.order)): if isinstance(self.params.order[index], Arg): if cur_state[index].startswith('-'): msg = f'An {"opt"!r} was found: {cur_state[index]!r}, ' msg += f'instead of type {"arg"!r}. Order of cmd parameters might be incorrect.' trace(msg, AssertionError, color='red') if cur_state[index]: self.values.append(self.params.order[index].type( cur_state[index])) if isinstance(self.params.order[index], Opt): values = [] for cmdl_index in range(0, len(cmdl)): if cmdl[cmdl_index] == self.params.order[index].name or \ cmdl[cmdl_index] == self.params.order[index].short_name: if len(cmdl) >= cmdl_index + 1 and cmdl[cmdl_index + 1]: values.append(self.params.order[index].type( cmdl[cmdl_index + 1])) if self.params.order[index].required and not values: msg = f'{self.params.order[index].name!r} is required' trace(msg, AssertionError, color='red') if len(values) > 0: if self.params.order[index].multiple: self.values.append(values) else: self.values.append(values[0]) elif self.params.order[index].default is None: self.values.append(self.params.order[index].default) elif self.params.order[index].default: self.values.append(self.params.order[index].type( self.params.order[index].default)) if isinstance(self.params.order[index], Flg): matches = re.findall(self.regex_patterns[index], ' '.join(cur_state)) self.values.append( True) if matches else self.values.append(False)
def _print_help(self): """_print_help - protected method to print the built help string this could in theory be overloaded to also add content to help string before print """ trace(self.help)