rophako/rophako/settings.py

55 líneas
1.9 KiB
Python
Original Vista normal Histórico

2014-12-04 23:06:44 +00:00
# -*- coding: utf-8 -*-
2015-07-10 07:27:13 +00:00
from __future__ import unicode_literals, print_function, absolute_import
2014-12-04 23:06:44 +00:00
import os
import datetime
2015-07-10 05:46:51 +00:00
from yamlsettings import YamlSettings
2014-12-04 23:06:44 +00:00
from rophako.plugin import load_plugin
# Get the base directory of the git root.
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
class ConfigHandler(object):
settings = None
def load_settings(self):
2015-07-10 05:46:51 +00:00
"""Load the settings and make them available in the global config."""
settings_file = os.environ.get("ROPHAKO_SETTINGS", "settings.yml")
project_settings = YamlSettings("defaults.yml", settings_file,
default_section="rophako")
self.settings = project_settings.get_settings()
# Extrapolate {basedir} in certain keys.
# TODO: find a better way...
self.site.site_root = self.site.site_root.format(basedir=basedir)
self.emoticons.root_private = self.emoticons.root_private.format(
basedir=basedir
)
self.photo.root_private = self.photo.root_private.format(basedir=basedir)
2015-07-10 06:23:44 +00:00
self.blog.copyright = self.blog.copyright.format(
year=datetime.datetime.utcnow().strftime("%Y")
)
2014-12-04 23:06:44 +00:00
def print_settings(self):
2015-07-10 05:46:51 +00:00
"""Pretty-print the contents of the configuration."""
2015-07-10 07:27:13 +00:00
print(self.settings)
2014-12-04 23:06:44 +00:00
def load_plugins(self):
"""Load all the plugins specified by the config file."""
2015-07-10 05:46:51 +00:00
for plugin in self.blueprints:
2014-12-04 23:06:44 +00:00
plugin = plugin.strip()
2015-07-10 05:46:51 +00:00
if not plugin: continue
2014-12-04 23:06:44 +00:00
load_plugin(plugin)
2015-07-10 05:46:51 +00:00
for custom in self.custom:
2014-12-04 23:06:44 +00:00
custom = custom.strip()
2015-07-10 05:46:51 +00:00
if not custom: continue
2014-12-04 23:06:44 +00:00
load_plugin(custom, as_blueprint=False)
def __getattr__(self, section):
2015-07-10 05:46:51 +00:00
"""Attribute accessor for the config object. Acts as a simple pass-thru
to YamlSettings."""
return getattr(self.settings, section)
2014-12-04 23:06:44 +00:00
Config = ConfigHandler()