From 91459e815de26e98e84b37e842295b332f10425f Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 7 Apr 2018 11:27:30 +0200 Subject: Add annotations --- patternutils/commands/patterncfg.py | 14 +++++++------- patternutils/config.py | 9 +++------ patternutils/template.py | 14 +++++++------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py index da90ead..e92ce7d 100644 --- a/patternutils/commands/patterncfg.py +++ b/patternutils/commands/patterncfg.py @@ -8,12 +8,12 @@ from .. import utils @click.group(context_settings={'help_option_names': ('-h', '--help', '-?')}) -def patterncfg(): +def patterncfg() -> None: pass @patterncfg.command(name='list', help='list all patternutils template files') -def list(): +def list() -> None: for d in os.listdir(config.CONFIG_PATH): click.secho(d, fg='blue') for f in os.listdir(os.path.join(config.CONFIG_PATH, d)): @@ -26,9 +26,9 @@ def list(): @click.argument('command', type=click.Choice(['pmatch', 'tmv', 'targs'])) @click.argument('name') def add( - file_type, - command, - name + file_type: str, + command: str, + name: str, ) -> None: if command == 'pmatch': fileext = 'regex' @@ -44,7 +44,7 @@ def add( @patterncfg.command(name='remove', help='remove template file') @click.argument('command', type=click.Choice(['pmatch', 'tmv', 'targs'])) @click.argument('name') -def remove(command, name): +def remove(command: str, name: str) -> None: files = config.find_templates(name, command) try: os.remove(files[0]) @@ -56,7 +56,7 @@ def remove(command, name): @patterncfg.command(name='view', help='view template file') @click.option('-p', '--program', default=None, type=click.Choice(['pmatch', 'tmv', 'targs'])) @click.argument('name') -def view(name, program): +def view(name: str, program: str) -> None: if program is not None: files = config.find_templates(name, program) diff --git a/patternutils/config.py b/patternutils/config.py index a8feb36..0daa4bc 100644 --- a/patternutils/config.py +++ b/patternutils/config.py @@ -34,12 +34,9 @@ def load_template(template_name: str, command_name: str) -> Tuple[str, str]: return output_pattern, template_engine -def find_templates(template_name: str, command_name: Union[str, None]) -> List[str]: +def find_templates(template_name: str, command_name: str=None) -> List[str]: template_glob = '.'.join((template_name, '*')) - if command_name is None: - command_glob = '*' - else: - command_glob = os.path.join(CONFIG_PATH, command_name) - + command_glob = '*' if command_name is None else command_name + command_glob = os.path.join(CONFIG_PATH, command_glob) return glob.glob(os.path.join(command_glob, template_glob)) diff --git a/patternutils/template.py b/patternutils/template.py index a40630b..2bd670c 100644 --- a/patternutils/template.py +++ b/patternutils/template.py @@ -1,4 +1,3 @@ - from typing import Callable try: @@ -6,25 +5,26 @@ try: except ImportError: jinja_feature = False else: - import datetime import shlex + from datetime import datetime + jinja_feature = True jinja_env = jinja2.Environment() - def to_datetime(value, format='%Y-%m-%d'): + def to_datetime(value: str, format: str='%Y-%m-%d') -> datetime: return datetime.strptime(value, format) - def format_datetime(value, format='%Y-%m-%d'): + def format_datetime(value: datetime, format: str='%Y-%m-%d') -> str: return value.strftime(format) - def to_int(value, base=10): + def to_int(value: str, base: int=10) -> int: return int(value, base=base) - def to_float(value): + def to_float(value: str) -> float: return float(value) - def quote(value): + def quote(value: str) -> str: return shlex.quote(value) jinja_env.filters['datetime'] = to_datetime -- cgit v1.3.1