From 0cf934965e01857344ab6caf22c51dcd19e64979 Mon Sep 17 00:00:00 2001 From: ldericher Date: Wed, 19 Aug 2020 03:22:03 +0200 Subject: [PATCH] Project class --- src/kiwi/subcommands/disable.py | 23 +------ src/kiwi/subcommands/enable.py | 23 +------ src/kiwi/subcommands/utils/project.py | 99 +++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 src/kiwi/subcommands/utils/project.py diff --git a/src/kiwi/subcommands/disable.py b/src/kiwi/subcommands/disable.py index ccdd477..c7a00ea 100644 --- a/src/kiwi/subcommands/disable.py +++ b/src/kiwi/subcommands/disable.py @@ -1,9 +1,5 @@ -# system -import logging -import os - # local -from .utils.misc import get_project_names, get_project_dir, get_project_down_dir +from .utils.project import Project from ._subcommand import ProjectCommand @@ -19,20 +15,7 @@ class DisableCommand(ProjectCommand): def run(self, runner, config, args): result = True - for project_name in get_project_names(args): - project_dir = get_project_dir(config, project_name) - project_down_dir = get_project_down_dir(config, project_name) - - if os.path.isdir(project_dir): - logging.info(f"Disabling project '{project_name}'") - os.rename(project_dir, project_down_dir) - - elif os.path.isdir(project_down_dir): - logging.warning(f"Project '{project_name}' is already disabled!") - result = False - - else: - logging.warning(f"Project '{project_name}' not found in instance!") - result = False + for project in Project.from_args(args): + result = project.disable() return result diff --git a/src/kiwi/subcommands/enable.py b/src/kiwi/subcommands/enable.py index 1d67124..6778c4d 100644 --- a/src/kiwi/subcommands/enable.py +++ b/src/kiwi/subcommands/enable.py @@ -1,9 +1,5 @@ -# system -import logging -import os - # local -from .utils.misc import get_project_names, get_project_dir, get_project_down_dir +from .utils.project import Project from ._subcommand import ProjectCommand @@ -19,20 +15,7 @@ class EnableCommand(ProjectCommand): def run(self, runner, config, args): result = True - for project_name in get_project_names(args): - project_dir = get_project_dir(config, project_name) - project_down_dir = get_project_down_dir(config, project_name) - - if os.path.isdir(project_down_dir): - logging.info(f"Enabling project '{project_name}'") - os.rename(project_down_dir, project_dir) - - elif os.path.isdir(project_dir): - logging.warning(f"Project '{project_name}' is already enabled!") - result = False - - else: - logging.warning(f"Project '{project_name}' not found in instance!") - result = False + for project in Project.from_args(args): + result = project.enable() return result diff --git a/src/kiwi/subcommands/utils/project.py b/src/kiwi/subcommands/utils/project.py new file mode 100644 index 0000000..15d5377 --- /dev/null +++ b/src/kiwi/subcommands/utils/project.py @@ -0,0 +1,99 @@ +import logging +import os + +from kiwi._constants import CONF_DIRECTORY_NAME +from kiwi.config import LoadedConfig + + +class Project: + __name = None + __config = None + + def __init__(self, name): + self.__name = name + self.__config = LoadedConfig.get() + + @classmethod + def from_names(cls, names): + return [cls(name) for name in names] + + @classmethod + def all(cls): + # current directory content + content = os.listdir() + + # filter subdirectories + dirs = [dir_name for dir_name in content if os.path.isdir(dir_name)] + + # filter by suffix + project_dirs = [dir_name for dir_name in dirs if dir_name.endswith(cls.__config['markers:project'])] + + # remove suffix + project_names = [project_name[:-len(cls.__config['markers:project'])] for project_name in project_dirs] + + return cls.from_names(project_names) + + @classmethod + def from_args(cls, args): + if args is not None and 'projects' in args: + if isinstance(args.projects, list) and args.projects: + return cls.from_names(args.projects) + elif isinstance(args.projects, str): + return cls.from_names([args.projects]) + + return [] + + def get_name(self): + return self.__name + + def dir_name(self): + return f"{self.__name}{self.__config['markers:project']}" + + def down_dir_name(self): + return f"{self.dir_name()}{self.__config['markers:down']}" + + def conf_dir_name(self): + return os.path.join(self.dir_name(), CONF_DIRECTORY_NAME) + + def target_dir_name(self): + return os.path.join(self.__config['runtime:storage'], self.dir_name()) + + def exists(self): + return os.path.isdir(self.dir_name()) or os.path.isdir(self.down_dir_name()) + + def is_enabled(self): + return os.path.isdir(self.dir_name()) + + def is_disabled(self): + return os.path.isdir(self.down_dir_name()) + + def has_configs(self): + return os.path.isdir(self.dir_name()) + + def enable(self): + if self.is_disabled(): + logging.info(f"Enabling project '{self.get_name()}'") + os.rename(self.down_dir_name(), self.dir_name()) + + elif self.is_enabled(): + logging.warning(f"Project '{self.get_name()}' is enabled!") + + else: + logging.warning(f"Project '{self.get_name()}' not found in instance!") + return False + + return True + + def disable(self): + if self.is_enabled(): + logging.info(f"Disabling project '{self.get_name()}'") + os.rename(self.dir_name(), self.down_dir_name()) + + elif self.is_disabled(): + logging.warning(f"Project '{self.get_name()}' is disabled!") + + else: + logging.warning(f"Project '{self.get_name()}' not found in instance!") + return False + + return True \ No newline at end of file