예제 #1
0
    def call(self, action, options=None, headline=True, dynamic=True):
        """
        Calls the 'run()' method of the plugin module
        referenced by 'action'. The optional argument
        'options' is a dictionary of the type defined
        by 'parse_argv()'. If not provided the
        corresponding values from self.options are
        taken.
        """
        self.callStack.append(action)
        self.current_headline_state = headline
        self.current_option = None
        self.current_action = action
        if options:
            try:
                self.current_option = self.plugins[action].OPTION_ARGUMENTS
            except AttributeError:
                self.current_option = options
            else:
                self.current_option.update(options)
        elif dynamic:
            try:
                self.current_option = self.plugins[action].OPTION_ARGUMENTS
            except AttributeError:
                self.current_option = {}

        # if self.current_option:
        #     for key,value in self.current_option.items():
        #         print(key,value)
        action = action.rstrip("_")
        self.moduledepth += 5
        self.printer_list.append(apd_printer(self.moduledepth, self.plugins[action].__name__))
        self.printer_list[-1].enter()
        self.plugins[action].run(self)
        if self.current_headline_state and self.print_headlines:
            self.printer_list[-1].bottomline()
        self.current_bottomline = None
        self.printer_list[-1].exit()
        self.printer_list = self.printer_list[:-1]
        self.moduledepth -= 5
        self.delta(self)
        self.callStack.pop(-1)
        self.current_option = None
예제 #2
0
    def __init__(
        self,
        headline=None,
        bottomline=None,
        data=None,
        argvs=None,
        verbose=False,
        headlines=True,
        alpha=None,
        delta=None,
        omega=None,
        config=None,
        macro_file=None,
    ):
        """
        Initializes the Configuration instance. All arguments are
        optional and are used to control the behavior of the instance.

        headline: A string. The string is used as the argument when
                calling the printer.headline() method for the first
                time.
        bottomline: A string. The string is used as the argument when
                calling the printer.bottomline() method for the last
                time.
        data:    Any type of variable is accepted. The data can be
                accessed by the get_data() and set_data() methods.
        argvs:   A list of strings. If the cmdline arguments should
                  be controlled by the calling programm instead of
                  the user, this option can be used to overwrite
                  sys.argv.
        verbose: A boolean. Defines the initial state of the printer
                  instance used by the Configuration instance. The
                  state can be controlled after initialization by
                  calling the mute()/unmute() methods.
        headlines: A boolean. If False, called plugins do not print
                  an extra headline.
        alpha:   A function. The function must accept at least one
                  argument. The function will be called at the end
                  of the initialization and can be used to do any
                  preprocessing that is required before any plugins
                  are called. The function will get the instance
                  of the Configuration class as an argument.
        delta:   A function. The function must accept at least one
                  argument. The function will be called after every
                  call of a plugin and can be used to do any kind
                  of intermediate processing. The function will get
                  the instance of the Configuration class as an
                  argument.
        omega:   A function. The function must accept at least one
                  argument. The function will be called at the end
                  of the execute() method. The function will get
                  the instance of the Configuration class as an
                  argument.
        config:  Path to an INI file compatible with the ConfigParser
                  Module.
        macro_file: A String. Created macros will be stored in the
                    file with the path 'macro_file'.
        """
        self.callStack = []
        all_managers.append(self)
        self.actions = []
        self.options = {}
        self.print_headlines = headlines
        if alpha:
            self.alpha = alpha
        if delta:
            self.delta = delta
        if omega:
            self.omega = omega

        self.printer_list = []
        self.reserved_keys = ["v", "info", "macro"]

        self.config = ConfigParser()
        self.config.read(config)

        self.current_option = None

        self.variables = {}
        self.current_action = None
        self.bottomline = bottomline
        self.printer = apd_printer()
        self.set_variable(data)

        self.printer.first()
        self.printer.headline(headline)

        if not argvs:
            self.argv = argv
        else:
            self.argv = argvs
        if "-v" in self.argv:
            verbose = True
        if not verbose:
            self.printer.mute()

        self.macro_file = macro_file
        self.macros = {}
        if macro_file:
            self.process_macros()

        self.moduledepth = 0
        self.path = self.config.get("APD", "PluginPath")
        self.scan_for_plugins()

        self.report(False)

        self.parse_argv(self.argv)

        self.printer("\nConfiguration complete.")
        self.start()