From cf17321c014181a71ca8d115a97c8e8c304d26b8 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Wed, 22 Jun 2022 14:09:24 +0300 Subject: [PATCH] Fixed complexity --- slpkg/main.py | 191 +++++++++++++++++++++++++++----------------------- 1 file changed, 103 insertions(+), 88 deletions(-) diff --git a/slpkg/main.py b/slpkg/main.py index caf5af78..8709040d 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -21,115 +21,112 @@ from slpkg.update_repository import UpdateRepository class Argparse: args: list - def flags(self): - self.flags = [] - yes = '--yes' - jobs = '--jobs' - resolve_off = '--resolve-off' - reinstall = '--reinstall' - - if yes in self.args: - self.args.remove(yes) - self.flags.append(yes) - - if jobs in self.args: - self.args.remove(jobs) - self.flags.append(jobs) - - if resolve_off in self.args: - self.args.remove(resolve_off) - self.flags.append(resolve_off) - - if reinstall in self.args: - self.args.remove(reinstall) - self.flags.append(reinstall) - - def command(self): - - self.flags() - check = Check() + def __post_init__(self): + self.flag() + self.check = Check() if len(self.args) <= 0: usage(1) + def flag(self): + self.flags = [] + + options = ['--yes', + '--jobs', + '--resolve-off', + '--reinstall' + ] + + for option in options: + if option in self.args: + self.args.remove(option) + self.flags.append(option) + + def help(self): if len(self.args) == 1: - if self.args[0] in ['--help', '-h']: - usage(0) + usage(0) + usage(1) - if self.args[0] in ['--version', '-v']: - version = Version() - version.view() - raise SystemExit() + def version(self): + if len(self.args) == 1: + version = Version() + version.view() + raise SystemExit() + usage(1) - if self.args[0] == 'clean-logs': - logs = CleanLogsDependencies(self.flags) - logs.clean() - raise SystemExit() - - if self.args[0] == 'clean-tmp': - path = Configs.tmp_path - tmp_slpkg = Configs.tmp_slpkg - folder = Configs.prog_name - utils = Utilities() - utils.remove_folder_if_exists(path, folder) - utils.create_folder(tmp_slpkg, 'build/') - raise SystemExit() - - # Update repository - if self.args[0] == 'update': - update = UpdateRepository() - update.sbo() - raise SystemExit() - - usage(1) + def update(self): + if len(self.args) == 1: + update = UpdateRepository() + update.sbo() + raise SystemExit() + usage(1) + def build(self): if len(self.args) >= 2: - # Build slackbuilds - if self.args[0] == 'build': - packages = list(set(self.args[1:])) + packages = list(set(self.args[1:])) - check.exists(packages) - check.unsupported(packages) + self.check.exists(packages) + self.check.unsupported(packages) - build = Slackbuilds(packages, self.flags, False) - build.execute() - raise SystemExit() + build = Slackbuilds(packages, self.flags, False) + build.execute() + raise SystemExit() + usage(1) - # Install packages - if self.args[0] == 'install': - packages = list(set(self.args[1:])) + def install(self): + if len(self.args) >= 2: + packages = list(set(self.args[1:])) - check.exists(packages) - check.unsupported(packages) + self.check.exists(packages) + self.check.unsupported(packages) - install = Slackbuilds(packages, self.flags, True) - install.execute() - raise SystemExit() + install = Slackbuilds(packages, self.flags, True) + install.execute() + raise SystemExit() + usage(1) - # Remove packages - if self.args[0] == 'remove': - packages = list(set(self.args[1:])) - packages = check.blacklist(packages) + def remove(self): + if len(self.args) >= 2: + packages = list(set(self.args[1:])) + packages = self.check.blacklist(packages) - check.installed(packages) + self.check.installed(packages) - remove = RemovePackages(packages, self.flags) - remove.remove() - raise SystemExit() + remove = RemovePackages(packages, self.flags) + remove.remove() + raise SystemExit() + usage(1) - # Search package - if self.args[0] == 'search': - packages = list(set(self.args[1:])) - packages = check.blacklist(packages) + def search(self): + # Search package + if len(self.args) >= 2: + packages = list(set(self.args[1:])) + packages = self.check.blacklist(packages) - check.exists(packages) + self.check.exists(packages) - search = Search() - search.package(packages) + search = Search() + search.package(packages) + raise SystemExit() + usage(1) - raise SystemExit() + def clean_logs(self): + if len(self.args) == 1: + logs = CleanLogsDependencies(self.flags) + logs.clean() + raise SystemExit() + usage(1) - usage(1) + def clean_tmp(self): + if len(self.args) == 1: + path = Configs.tmp_path + tmp_slpkg = Configs.tmp_slpkg + folder = Configs.prog_name + utils = Utilities() + utils.remove_folder_if_exists(path, folder) + utils.create_folder(tmp_slpkg, 'build/') + raise SystemExit() + usage(1) def main(): @@ -137,7 +134,25 @@ def main(): args.pop(0) argparse = Argparse(args) - argparse.command() + + arguments = { + '-h': argparse.help, + '--help': argparse.help, + '-v': argparse.version, + '--version': argparse.version, + 'update': argparse.update, + 'build': argparse.build, + 'install': argparse.install, + 'remove': argparse.remove, + 'search': argparse.search, + 'clean-logs': argparse.clean_logs, + 'clean-tmp': argparse.clean_tmp, + } + + try: + arguments[args[0]]() + except KeyError: + usage(1) if __name__ == '__main__':