From 3014e7057cc9ac207657c8fbafc2534d89485188 Mon Sep 17 00:00:00 2001 From: ldericher Date: Tue, 18 Aug 2020 15:56:22 +0200 Subject: [PATCH] readability --- src/kiwi/subcommands/_subcommand.py | 12 ++++---- src/kiwi/subcommands/utils/dockercommand.py | 4 +-- src/kiwi/subcommands/utils/misc.py | 33 ++++++++++++++++----- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/kiwi/subcommands/_subcommand.py b/src/kiwi/subcommands/_subcommand.py index 75d8d0c..be50e67 100644 --- a/src/kiwi/subcommands/_subcommand.py +++ b/src/kiwi/subcommands/_subcommand.py @@ -2,7 +2,7 @@ import logging # local -from .utils.misc import get_project_name, get_services, list_projects +from .utils.misc import get_first_project_name, get_services, list_projects # parent from ..parser import Parser @@ -42,8 +42,8 @@ class ProjectCommand(SubCommand): projects = "a project" - if not str(num_projects) == '1': - projects = "projects" + if not num_projects == 1: + projects = "project(s)" self._sub_parser.add_argument( 'projects', metavar='project', nargs=num_projects, type=str, @@ -62,8 +62,8 @@ class ServiceCommand(ProjectCommand): services = "a service" - if not str(num_services) == '1': - services = "services" + if not num_services == 1: + services = "service(s)" self._sub_parser.add_argument( 'services', metavar='service', nargs=num_services, type=str, @@ -104,7 +104,7 @@ class FlexCommand(ServiceCommand): pass def run(self, runner, config, args): - project_name = get_project_name(args) + project_name = get_first_project_name(args) services = get_services(args) if project_name is None: diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index 6093dfd..62ad1b0 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -5,7 +5,7 @@ import subprocess # local from .executable import Executable -from .misc import get_project_dir, get_project_name +from .misc import get_project_dir, get_first_project_name # parent from ..._constants import CONF_DIRECTORY_NAME @@ -13,7 +13,7 @@ from ..._constants import CONF_DIRECTORY_NAME def _update_kwargs(config, args, **kwargs): # project given in args: command affects a project in this instance - project_name = get_project_name(args) + project_name = get_first_project_name(args) if project_name is not None: # execute command in project directory kwargs['cwd'] = get_project_dir(config, project_name) diff --git a/src/kiwi/subcommands/utils/misc.py b/src/kiwi/subcommands/utils/misc.py index 93d5e9b..eb607fe 100644 --- a/src/kiwi/subcommands/utils/misc.py +++ b/src/kiwi/subcommands/utils/misc.py @@ -1,14 +1,27 @@ import os -def get_project_name(args): - """get project name from CLI args""" +def get_project_names(args): + """get project names from CLI args""" if args is not None and 'projects' in args: - if isinstance(args.projects, list) and len(args.projects) > 0: - return args.projects[0] - else: - return args.projects + if isinstance(args.projects, list): + if args.projects: + return args.projects + else: + return None + elif isinstance(args.projects, str): + return [args.projects] + + return None + + +def get_first_project_name(args): + """get first project name from CLI args""" + + names = get_project_names(args) + if names is not None: + return names[0] return None @@ -28,10 +41,16 @@ def get_project_dir(config, project_name): return f"{project_name}{config['markers:project']}" +def get_project_down_dir(config, project_name): + """get project directory""" + + return f"{get_project_dir(config, project_name)}{config['markers:down']}" + + def get_target_dir(config, project_name): """get project's target directory""" - return f"{config['runtime:storage']}{get_project_dir(config, project_name)}" + return os.path.join(config['runtime:storage'], get_project_dir(config, project_name)) def list_projects(config):