Example #1
0
    def prep_template(self, user_widget_fname, default_filters, default_tests):
        """Setups the jinja2 template environment."""

        # set up jinja2 template environment in the SITE Config object
        env = Environment(loader=FileSystemLoader(self.VOLT.TEMPLATE_DIR))
        # combine filters and tests
        self.SITE.FILTERS = tuple(set(self.SITE.FILTERS + default_filters))
        self.SITE.TESTS = tuple(set(self.SITE.TESTS + default_tests))

        # import filters and tests
        default_widget = path_import(DEFAULT_WIDGET, DEFAULT_CONF_DIR)
        try:
            user_widget = path_import(user_widget_fname, self.VOLT.ROOT_DIR)
        except ImportError:
            # set user_widget to None if the user does not define any widgets
            # to prevent getattr below from crashing
            user_widget = None

        for func_type in 'FILTERS', 'TESTS':
            for func_name in getattr(self.SITE, func_type):
                # user-defined functions take precedence
                if hasattr(user_widget, func_name):
                    func = getattr(user_widget, func_name)
                else:
                    func = getattr(default_widget, func_name)

                target = getattr(env, func_type.lower())
                target[func_name] = func

        setattr(self.SITE, 'TEMPLATE_ENV', env)
Example #2
0
    def get_processor(self, processor_name, processor_type, \
            volt_dir=os.path.dirname(__file__)):
        """Returns the engine or plugin class used in site generation.

        processor_name -- String denoting engine or plugin name.
        processor_type -- String denoting processor type. Must be 'engines'
                          or 'plugins'.
        volt_dir -- String denoting absolute path to Volt's installation
                    directory.
        
        This method tries to load engines or plugins from the user's Volt
        project directory first. Failing that, it will try to import engines
        or plugins from Volt's installation directory.

        """
        # check first if processor type is 'plugins' or 'engines'
        assert processor_type in ['engines', 'plugins'], \
            "Processor type must be 'engines' or 'plugins'"

        # load engine or plugin
        # user_path has priority over volt_path
        user_dir = self.config.VOLT.ROOT_DIR
        user_path = os.path.join(user_dir, processor_type)
        volt_path = os.path.join(volt_dir, processor_type[:-1], 'builtins')

        mod = path_import(processor_name, [user_path, volt_path])

        if processor_type == 'engines':
            cls_name = getattr(mod, 'ENGINE')
        else:
            cls_name = getattr(mod, 'PLUGIN')

        return getattr(mod, cls_name)
Example #3
0
    def __init__(self):
        """Resolves the unified Config values to use for a Volt run."""
        default = path_import(DEFAULT_CONF, DEFAULT_CONF_DIR)
        root_dir = self.get_root_dir(default.VOLT.USER_CONF)

        user_conf_fname = default.VOLT.USER_CONF.split('.')[0]
        user_widget_fname = default.VOLT.USER_WIDGET.split('.')[0]

        default.VOLT.USER_CONF = os.path.join(root_dir, \
                default.VOLT.USER_CONF)
        default.VOLT.USER_WIDGET = os.path.join(root_dir, \
                default.VOLT.USER_WIDGET)

        # for combining default and user jinja2 filters and tests
        default_filters = default.SITE.FILTERS
        default_tests = default.SITE.TESTS

        # asset dir is always inside template dir
        default.VOLT.ASSET_DIR = os.path.join(default.VOLT.TEMPLATE_DIR, \
                default.VOLT.ASSET_DIR)

        user = path_import(user_conf_fname, root_dir)
        for item in 'VOLT', 'SITE':
            # load from default first and override if present in user
            obj = getattr(default, item)
            if hasattr(user, item):
                obj.update(getattr(user, item))
            for opt in obj:
                # set directory items to absolute paths if endswith _DIR
                if opt.endswith('_DIR'):
                    obj[opt] = os.path.join(root_dir, obj[opt])
                # strip '/'s from URL options
                if opt.endswith('URL'):
                    obj[opt] = obj[opt].strip('/')
            setattr(self, item, obj)

        # set root dir as config in VOLT
        setattr(self.VOLT, 'ROOT_DIR', root_dir)

        self.prep_template(user_widget_fname, default_filters, default_tests)

        self.logger.debug('initialized: UnifiedConfig')
Example #4
0
    def prime(self):
        """Consolidates default engine Config and user-defined Config.

        In addition to consolidating Config values, this method also sets
        the values of CONTENT_DIR, and *_TEMPLATE to absolute directory paths.

        """
        # get user config object
        conf_name = os.path.splitext(os.path.basename(CONFIG.VOLT.USER_CONF))[0]
        user_conf = path_import(conf_name, CONFIG.VOLT.ROOT_DIR)

        # custom engines must define an entry name for the user's voltconf
        if not hasattr (self, 'USER_CONF_ENTRY'):
            message = "%s must define a %s value as a class attribute." % \
                    (type(self).__name__, 'USER_CONF_ENTRY')
            self.logger.error(message)

        # use default config if the user does not specify any
        try:
            user_config = getattr(user_conf, self.USER_CONF_ENTRY)
        except AttributeError:
            user_config = Config()

        # to ensure proper Config consolidation
        if not isinstance(user_config, Config):
            message = "User Config object '%s' must be a Config instance." % \
                    self.USER_CONF_ENTRY
            self.logger.error(message)
            raise TypeError(message)
        else:
            self.config.update(user_config)

        # check attributes that must exist
        for attr in _REQUIRED_ENGINE_CONFIG:
            try:
                getattr(self.config, attr)
            except AttributeError:
                message = "%s Config '%s' value is undefined." % \
                        (type(self).__name__, attr)
                self.logger.error(message)
                self.logger.debug(format_exc())
                raise

        # set engine config paths to absolute paths
        self.config.CONTENT_DIR = os.path.join(CONFIG.VOLT.CONTENT_DIR, \
                self.config.CONTENT_DIR)
        for template in [x for x in self.config.keys() if x.endswith('_TEMPLATE')]:
                self.config[template] = os.path.join(CONFIG.VOLT.TEMPLATE_DIR, \
                        self.config[template])
Example #5
0
File: core.py Project: spaetz/volt
    def prime(self):
        """Consolidates default plugin Config and user-defined Config."""

        # only override defaults if USER_CONF_ENTRY is defined
        if self.USER_CONF_ENTRY is not None:
            # get user config object
            conf_name = os.path.splitext(os.path.basename(CONFIG.VOLT.USER_CONF))[0]
            voltconf = path_import(conf_name, CONFIG.VOLT.ROOT_DIR)

            # use default Config if the user does not list any
            try:
                user_config = getattr(voltconf, self.USER_CONF_ENTRY)
            except AttributeError:
                user_config = Config()

            # to ensure proper Config consolidation
            if not isinstance(user_config, Config):
                raise TypeError("User Config object '%s' must be a Config instance." % self.USER_CONF_ENTRY)
            else:
                self.config.update(user_config)
Example #6
0
 def widgets_mod(self):
     self.logger.debug('imported: widgets module')
     return path_import('widgets', self.config.VOLT.ROOT_DIR)