def __init__(self, name, description="", logo_path=None, is_main_category=False, packages_requirements=None): self.name = name self.description = description self.logo_path = logo_path self.is_main_category = is_main_category self.default = None self.frameworks = NoneDict() self.packages_requirements = [] if packages_requirements is None else packages_requirements if self.prog_name in self.categories: logger.warning("There is already a registered category with {} as a name. Don't register the second one." .format(name)) else: self.categories[self.prog_name] = self
class BaseCategory(): """Base Category class to be inherited""" NOT_INSTALLED, PARTIALLY_INSTALLED, FULLY_INSTALLED = range(3) categories = NoneDict() def __init__(self, name, description="", logo_path=None, is_main_category=False, packages_requirements=None): self.name = name self.description = description self.logo_path = logo_path self.is_main_category = is_main_category self.default = None self.frameworks = NoneDict() self.packages_requirements = [] if packages_requirements is None else packages_requirements if self.prog_name in self.categories: logger.warning("There is already a registered category with {} as a name. Don't register the second one." .format(name)) else: self.categories[self.prog_name] = self @classproperty def main_category(self): for category in self.categories.values(): if category.is_main_category: return category return None @property def prog_name(self): """Get programmatic, path and CLI compatible names""" return self.name.lower().replace('/', '-').replace(' ', '-') @property def default_framework(self): """Get default framework""" for framework in self.frameworks.values(): if framework.is_category_default: return framework return None def register_framework(self, framework): """Register a new framework""" if framework.prog_name in self.frameworks: logger.error("There is already a registered framework with {} as a name. Don't register the second one." .format(framework.name)) else: self.frameworks[framework.prog_name] = framework @property def is_installed(self): """Return if the category is installed""" installed_frameworks = [framework for framework in self.frameworks.values() if framework.is_installed] if len(installed_frameworks) == 0: return self.NOT_INSTALLED if len(installed_frameworks) == len(self.frameworks): return self.FULLY_INSTALLED return self.PARTIALLY_INSTALLED def install_category_parser(self, parser): """Install category parser and get frameworks""" if not self.has_frameworks(): logging.debug("Skipping {} having no framework".format(self.name)) return # framework parser is directly category parser if self.is_main_category: framework_parser = parser else: self.category_parser = parser.add_parser(self.prog_name, help=self.description) framework_parser = self.category_parser.add_subparsers(dest="framework") for framework in self.frameworks.values(): framework.install_framework_parser(framework_parser) return framework_parser def has_frameworks(self): """Return if a category has at least one framework""" return len(self.frameworks) > 0 def has_one_framework(self): """Return if a category has one framework""" return len(self.frameworks) == 1 def run_for(self, args): """Running commands from args namespace""" # try to call default framework if any if not args.framework: if not self.default_framework: message = _("A default framework for category {} was requested where there is none".format(self.name)) logger.error(message) self.category_parser.print_usage() UI.return_main_screen(status_code=1) self.default_framework.run_for(args) return self.frameworks[args.framework].run_for(args)
class BaseCategory(): """Base Category class to be inherited""" NOT_INSTALLED, PARTIALLY_INSTALLED, FULLY_INSTALLED = range(3) categories = NoneDict() def __init__(self, name, description="", logo_path=None, is_main_category=False, packages_requirements=None): self.name = name self.description = description self.logo_path = logo_path self.is_main_category = is_main_category self.default = None self.frameworks = NoneDict() self.packages_requirements = [] if packages_requirements is None else packages_requirements if self.prog_name in self.categories: logger.warning( "There is already a registered category with {} as a name. Don't register the second one." .format(name)) else: self.categories[self.prog_name] = self @classproperty def main_category(self): for category in self.categories.values(): if category.is_main_category: return category return None @property def prog_name(self): """Get programmatic, path and CLI compatible names""" return self.name.lower().replace('/', '-').replace(' ', '-') @property def default_framework(self): """Get default framework""" for framework in self.frameworks.values(): if framework.is_category_default: return framework return None def register_framework(self, framework): """Register a new framework""" if framework.prog_name in self.frameworks: logger.error( "There is already a registered framework with {} as a name. Don't register the second one." .format(framework.name)) else: self.frameworks[framework.prog_name] = framework @property def is_installed(self): """Return if the category is installed""" installed_frameworks = [ framework for framework in self.frameworks.values() if framework.is_installed ] if len(installed_frameworks) == 0: return self.NOT_INSTALLED if len(installed_frameworks) == len(self.frameworks): return self.FULLY_INSTALLED return self.PARTIALLY_INSTALLED def install_category_parser(self, parser): """Install category parser and get frameworks""" if not self.has_frameworks(): logging.debug("Skipping {} having no framework".format(self.name)) return # framework parser is directly category parser if self.is_main_category: framework_parser = parser else: self.category_parser = parser.add_parser(self.prog_name, help=self.description) framework_parser = self.category_parser.add_subparsers( dest="framework") for framework in self.frameworks.values(): framework.install_framework_parser(framework_parser) return framework_parser def has_frameworks(self): """Return if a category has at least one framework""" return len(self.frameworks) > 0 def has_one_framework(self): """Return if a category has one framework""" return len(self.frameworks) == 1 def run_for(self, args): """Running commands from args namespace""" # try to call default framework if any if not args.framework: if not self.default_framework: message = _( "A default framework for category {} was requested where there is none" .format(self.name)) logger.error(message) self.category_parser.print_usage() UI.return_main_screen(status_code=2) self.default_framework.run_for(args) return self.frameworks[args.framework].run_for(args)