Example #1
0
    def load_module(self, name):
        """
        Load a single module by name from this section.
        """
        logging.info("Loading module %s from section %s",
            name, self.name)

        if not self.has_module(name):
            raise Exception("No such such module: %s" % name)

        filenames = itertools.chain(*[path_expand_recursive(m)
            for m in self.modules])
        for filename in filenames:
            if filename.endswith(".py") and posixpath.exists(filename):
                basename = posixpath.basename(filename)
                basename = basename.replace(".py", "")
                if basename == name:
                    globals = {}
                    exec(open(filename).read(), globals)
                    if "factory" not in globals:
                        raise Exception("Variable 'factory' not found in: %s" \
                            % filename)

                    module = globals["factory"]()
                    module.__module__ = name

                    config_name = "/".join([self.name, name])
                    config = self._config.parent.get_section(config_name)

                    # Set configuration values
                    variables = get_variables(module)
                    environ = dict([(k.lower(), v) for k, v in os.environ.items()])
                    for attribute, variable in variables.items():
                        if config and attribute.name in config:
                            value = config.get(attribute.name)
                            variable.set(value)
                        else:
                            value = variable.get()
                            if isinstance(value, str):
                                value = value % environ
                                variable.set(value)
                            elif isinstance(value, list):
                                value = [v % environ for v in value]
                                variable.set(value)

                    # Check required attributes
                    for attribute, variable in variables.items():
                        value = variable.get()
                        if value is None and variable._required:
                            raise Exception("Configuration '%s' missing " \
                                "required attribute: %s" \
                                % (config_name, attribute.name))

                    return module

        raise Exception("Failed to find module '%s' in: %s" % (name, filenames))
Example #2
0
 def prompt_begin(self, interface):
     for key, value in get_variables(self).items():
         name = key.name.upper()
         if name not in os.environ:
             os.environ[name] = value.get()