gws refactoring

type tips
string formatting
This commit is contained in:
Martin Blazik
2020-10-02 17:51:59 +02:00
parent 53064ee588
commit 80cd56c7ed

34
bin/gws
View File

@@ -7,6 +7,9 @@ could load shortcuts from environment
import os import os
from sys import argv, exit from sys import argv, exit
from typing import Tuple, List, Generator, Iterable, Sequence, Callable
Args = Sequence[str]
shortcuts = { shortcuts = {
'bt': ['compileTestKotlin'], 'bt': ['compileTestKotlin'],
@@ -30,7 +33,7 @@ Use cases
gws cbx :microservices:afm-exec-api gws cbx :microservices:afm-exec-api
Options Options
--dry doesn't start gw app --show doesn't start gw app
""" """
@@ -44,14 +47,19 @@ def show_help():
def show_shortcuts(): def show_shortcuts():
print('Shortcuts') print('Shortcuts')
for key, value in shortcuts.items(): 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)) 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) iterator = iter(args)
while True: while True:
try: try:
@@ -64,11 +72,11 @@ def split_options(args):
break 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) 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 = [] tasks = []
options = [] options = []
for item in split_options(args): for item in split_options(args):
@@ -82,26 +90,28 @@ def remake(args, modules):
return tasks + modules + options return tasks + modules + options
def prepare_args(args): def prepare_args(args: Args) -> List[str]:
modules = list(filter(lambda value: value.startswith(':'), args)) 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 = flat_map(lambda arg: shortcuts.get(arg, [arg]), args)
args = list(args) args = list(args)
args = remake(args, modules) args = remake(args, modules)
return list(args) return list(args)
def prepare_command(args): def prepare_command(args: Args) -> str:
args = prepare_args(args) args = prepare_args(args)
return 'gw ' + ' '.join(args) return 'gw ' + ' '.join(args)
def main(args): def main(args: List[str]):
if not args: if not args:
show_help() show_help()
just_show, args = try_remove(args, '--show')
cmd = prepare_command(args) cmd = prepare_command(args)
print('## ' + cmd) print(cmd)
if '--dry' not in args: if not just_show:
print()
os.system(cmd) os.system(cmd)