From 80cd56c7ed09383a4971f0604f023504029ff5a0 Mon Sep 17 00:00:00 2001 From: Martin Blazik Date: Fri, 2 Oct 2020 17:51:59 +0200 Subject: [PATCH] gws refactoring type tips string formatting --- bin/gws | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/bin/gws b/bin/gws index 76569a5..d4b1d7d 100755 --- a/bin/gws +++ b/bin/gws @@ -7,6 +7,9 @@ could load shortcuts from environment import os from sys import argv, exit +from typing import Tuple, List, Generator, Iterable, Sequence, Callable + +Args = Sequence[str] shortcuts = { 'bt': ['compileTestKotlin'], @@ -30,7 +33,7 @@ Use cases gws cbx :microservices:afm-exec-api Options - --dry doesn't start gw app + --show doesn't start gw app """ @@ -44,14 +47,19 @@ def show_help(): def show_shortcuts(): print('Shortcuts') for key, value in shortcuts.items(): - print(' {} ... {}'.format(key, ' '.join(value))) + print(f" {key} ... {' '.join(value)}") -def flat_map(f, xs): +def flat_map(f: Callable, xs: Sequence) -> Iterable: return (y for ys in xs for y in f(ys)) -def split_options(args): +def try_remove(lst: List, value) -> Tuple[bool, List]: + filtered = list(filter(lambda x: x != value, lst)) + return len(lst) != len(filtered), filtered + + +def split_options(args: Args) -> Generator[List[str], None, None]: iterator = iter(args) while True: try: @@ -64,11 +72,11 @@ def split_options(args): break -def mix_task_modules(modules, tasks): +def mix_task_modules(modules: Sequence, tasks: Sequence) -> Generator[str, None, None]: return (module + ':' + task for module in modules for task in tasks) -def remake(args, modules): +def remake(args: Args, modules: List[str]) -> List[str]: tasks = [] options = [] for item in split_options(args): @@ -82,26 +90,28 @@ def remake(args, modules): return tasks + modules + options -def prepare_args(args): +def prepare_args(args: Args) -> List[str]: modules = list(filter(lambda value: value.startswith(':'), args)) - args = (arg for arg in argv[1:] if arg not in modules) + args = (arg for arg in args if arg not in modules) args = flat_map(lambda arg: shortcuts.get(arg, [arg]), args) args = list(args) args = remake(args, modules) return list(args) -def prepare_command(args): +def prepare_command(args: Args) -> str: args = prepare_args(args) return 'gw ' + ' '.join(args) -def main(args): +def main(args: List[str]): if not args: show_help() + just_show, args = try_remove(args, '--show') cmd = prepare_command(args) - print('## ' + cmd) - if '--dry' not in args: + print(cmd) + if not just_show: + print() os.system(cmd)