Beispiel #1
0
    def test_delimiter_re(self, io_out):
        # test the output : only the main shape is tested (i.e --- [ ] ---)
        io_out['log'].delimiter(self.test_str)
        assert re.search('^-*\s\[\s.+\s\]\s-*$',
                         io_out['out'].getvalue()) is not None

        out = io.StringIO()  # build a new stream/Log couple for this case
        log = text.Log(output=out)
        log.delimiter(self.test_str, width=1)
        assert re.search('^-*\s\[\s.+\s\]\s-*$', out.getvalue()) is not None

        out = io.StringIO()
        log = text.Log(output=out)
        log.delimiter(self.test_str, width=60)
        assert len(out.getvalue()) - 1 == 60  # exclude ending \n
    def __init__(self, module, ability, app_model, module_factory,
                 view=output.Log()):
        """ Subshell to control an ability

        This is the "level 2" shell, used to setup and run an ability
        selected with the ShellCtrl shell.

        :param module: the model used to handle the ability
        :param ability: the ability to instantiate (AbilityBase)
        :param app_model: an AppModel that represents the software
            configuration
        :param view: a ViewsInterface subclass to handle the interface display
        """
        super(ShellUseCtrl, self).__init__()
        self.logger = logging.getLogger(__name__)
        self._module = module
        self._ability = ability

        self.logger.debug('Initialize [{}] in package "{}"'.format(
            self._ability.get_name(),
            self._module.get_module_path())
        )

        self._app_model = app_model
        self._module_factory = module_factory
        self._set_new_module_inst()
        self._view = view
Beispiel #3
0
    def __init__(self,
                 cli_args,
                 rem_args,
                 app_model,
                 module_factory,
                 view=output.Log()):
        """ Handle the command line interface of the software

        :param cli_args: argparse successfully parsed arguments
        :param rem_args: list of argument not parsed by argparse
        :param app_model: an AppModel that represent the software configuration
        :param module_factory: the module factory to pass to the instantiated
            abilities
        :param view: a ViewsInterface subclass to handle user interface display
        """
        super(CmdLineCtrl, self).__init__()
        self._cli_args = cli_args
        self._rem_args = rem_args
        self._parsed_args = None
        self._argparser = None
        self._app_model = app_model
        self._module_factory = module_factory
        self._module_list_model = modulelistmodel.ModuleListModel(
            self._app_model.get_packages())
        self._view = view
Beispiel #4
0
    def io_out(self):
        """ Build the Log object to be tested

        A custom string stream is used to validate its outputs
        """
        out = io.StringIO()
        yield {'out': out, 'log': text.Log(output=out)}
        out.close()
Beispiel #5
0
    def get_module(module_path, view=output.Log()):
        """ Return a ModuleModel based on a path

        This is mostly used to get the python module of an ability package
        :param module_path: path to the module (absolute or relative)
        :param view: a ViewsInterface subclass to handle user interface display. Needed to instantiate the ModuleModel
        """
        pkg = ModuleFactory.load_module(module_path)
        return ability_module.AbilityModule(pkg, module_path, view)
Beispiel #6
0
    def __init__(self, app_model, module_factory, view=output.Log()):
        """ Handle the interactive shell

        This is the "level 1" shell, used to browse and select available
        abilities. It use the Cmd python library to provide commands,
        help messages and completion.

        A second shell is called by the do_use() method : ShellUseCtrl.

        :param app_model: an AppModel that represent the software configuration
        :param view: a ViewInterface subclass to handle user interface display
        """
        super(ShellCtrl, self).__init__()

        self._view = view
        self.logger = logging.getLogger(__name__)

        # documentation view customization
        self.ruler = "="
        self.doc_header = "Documented commands (type help <topic>):"
        self.misc_header = "Miscellaneous help topics:"
        self.undoc_header = "Undocumented commands:"
        self._app_model = app_model
        self._module_factory = module_factory
        self._search_and_opt_name = '-a'
        self._search_or_opt_name = '-o'

        self.prompt = self._app_model.app_prompt_l1
        try:
            self._history_path = self._app_model.get_hist_file_path()
        except ex.ConfHistFileNotAccessible as e:
            self._view.error('{}'.format(e))
            raise ex.ExitPw()
        readline.read_history_file(self._history_path)

        try:
            l_pkg = self._app_model.get_packages()
        except ex.ConfNone:
            l_pkg = []
        self._module_list_model = module_list_model.ModuleListModel(l_pkg)
        # load the search result indexes with default values
        self._module_list_model.get_module_list()

        signal.signal(signal.SIGINT, self._handle_ctrlc)

        self.doc_leader = \
            """
    {} ({}) - v.{}
    {}

    Use the 'help' or '?' command to discover available commands.
    Use 'tab' to complete any part of your input.

    """.format(self._app_model.app_name,
               self._app_model.app_name_abbrev,
               self._app_model.app_version,
               self._app_model.app_slogan)
Beispiel #7
0
    def __init__(self, app_model, module_factory, view=output.Log()):
        """ Handle the interactive shell

        This is the "level 1" shell, used to browse and select available abilities. It use the Cmd python library to
        provide commands, help messages and completion.

        A second shell is called by the do_use() method : ShellUseCtrl.

        :param app_model: an AppModel that represent the software configuration
        :param view: a ViewInterface subclass to handle user interface display
        """
        super(ShellCtrl, self).__init__()

        # documentation view customization
        self.ruler = "="
        self.doc_header = "Documented commands (type help <topic>):"
        self.misc_header = "Miscellaneous help topics:"
        self.undoc_header = "Undocumented commands:"
        self._app_model = app_model
        self._module_factory = module_factory
        self._search_and_opt_name = '-a'
        self._search_or_opt_name = '-o'

        self.prompt = self._app_model.app_prompt_l1

        self._history_path = self._app_model.get_config(
            'Internals', 'HistFile')
        if self._history_path is None:
            self._history_path = '~/.pwhistory'
        self._history_path = os.path.expanduser(self._history_path)
        try:
            readline.read_history_file(self._history_path)
        except IOError:
            if not os.path.isfile(self._history_path):
                open(self._history_path, "w").close()  # touch file

        self._module_list_model = module_list_model.ModuleListModel(
            self._app_model.get_packages())
        self._view = view

        signal.signal(signal.SIGINT, self._handle_ctrlc)

        self.doc_leader = \
            """
    {} ({}) - v.{}
    {}
    
    Use the 'help' or '?' command to discover available commands. 
    Use 'tab' to complete any part of your input.
    
    """.format(self._app_model.app_name,
               self._app_model.app_name_abbrev,
               self._app_model.app_version,
               self._app_model.app_slogan)
    def __init__(self, paths, view=output.Log()):
        """ The model used to interact with the list of available modules

        Note: module is the model used to interact with the real ability. See ModuleModel

        :param paths: list of paths to the activated ability packages
        :param view: a ViewInterface subclass to handle user interface display
        """
        self._paths = copy.deepcopy(paths)
        self._view = view
        self._module_list = {}
        self.reload()
Beispiel #9
0
    def __init__(self, module_factory, default_opts=None, view=output.Log()):
        """ Create a new ability

        An ability is a class stored at a meaningful place in the module tree
        (the default or your) This class must be sub-classed to implement
        a new simple ability.

        :param module_factory: the factory used to instantiate dependencies
        :param default_opts: ability parameters values to be used as default
        :param view: a ViewsInterface subclass to handle the interface display
        """
        type(self)._init_opt_hash()
        type(self)._init_internal_dependencies()
        super(AbilityBase, self).__init__()
        self._module_factory = module_factory
        self._view = view
        self._cached_opt_values = {}
        self._ret_value = None
        self._alive = True
        self._started_status = False
        self._set_default_opts(default_opts)
        self.logger = logging.getLogger(__name__)
Beispiel #10
0
    def __init__(self,
                 module,
                 ability,
                 app_model,
                 module_factory,
                 view=output.Log()):
        """ Subshell to control an ability

        This is the "level 2" shell, used to setup and run an ability selected with the ShellCtrl shell.

        :param module: the model used to handle the ability
        :param ability: the ability to instantiate (AbilityBase)
        :param app_model: an AppModel that represents the software configuration
        :param view: a ViewsInterface subclass to handle the interface display
        """
        super(ShellUseCtrl, self).__init__()
        self._module = module
        self._ability = ability
        self._app_model = app_model
        self._module_factory = module_factory
        self._set_new_module_inst()
        self._view = view
Beispiel #11
0
    def __init__(self, conf_file_path, cli_args, rem_args, view=output.Log()):
        """ Main controller of the framework

        Will instantiate the correct user interface depending of its parameters

        :param conf_file_path: path (relative to pw.py or absolute) to the pw.ini file
        :param cli_args: argparse successfully parsed arguments
        :param rem_args: argparse remaining arguments
        :param view: a View object, by default Log()
        """
        super(AppCtrl, self).__init__()

        self._view = view
        self._cli_args = cli_args
        self._rem_args = rem_args

        self._app_model = app_model.AppModel(conf_file_path)
        self._module_factory = module_factory.ModuleFactory(
            self._app_model, self._view)

        self._ctrl = None

        self.nb_max_pkg = 20  # safe guard
Beispiel #12
0
 def __init__(self, app_model, view=output.Log()):
     self._app_model = app_model
     self._view = view