A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.3KB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import datetime
  5. from attrdict import AttrDict
  6. from ConfigParser import ConfigParser
  7. from rophako.plugin import load_plugin
  8. # Get the base directory of the git root.
  9. basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
  10. # https://github.com/bcj/AttrDict/issues/20
  11. if not hasattr(AttrDict, "copy"):
  12. setattr(AttrDict, "copy", lambda self: self._mapping.copy())
  13. class ConfigHandler(object):
  14. settings = None
  15. def load_settings(self):
  16. """Load the settings files and make them available in the global config."""
  17. self.settings = ConfigParser(dict_type=AttrDict)
  18. # Set dynamic default variables.
  19. self.settings.set("DEFAULT", "_basedir", basedir)
  20. self.settings.set("DEFAULT", "_year", str(datetime.datetime.now().strftime("%Y")))
  21. # Read the defaults and then apply the custom settings on top.
  22. settings_file = os.environ.get("ROPHAKO_SETTINGS", "settings.ini")
  23. self.settings.read(["defaults.ini", settings_file])
  24. def print_settings(self):
  25. """Pretty-print the contents of the configuration as JSON."""
  26. for section in self.settings.sections():
  27. print "[{}]".format(section)
  28. for opt in self.settings.options(section):
  29. print "{} = {}".format(opt, repr(self.settings.get(section, opt)))
  30. print ""
  31. def load_plugins(self):
  32. """Load all the plugins specified by the config file."""
  33. for plugin in self.plugins.blueprints.split("\n"):
  34. plugin = plugin.strip()
  35. if not plugin:
  36. continue
  37. load_plugin(plugin)
  38. for custom in self.plugins.custom.split("\n"):
  39. custom = custom.strip()
  40. if not custom:
  41. continue
  42. load_plugin(custom, as_blueprint=False)
  43. def __getattr__(self, section):
  44. """Attribute access for the config object.
  45. You can access config settings via Config.<section>.<name>, for example
  46. Config.site.notify_email and Config.blog.posts_per_page. All results are
  47. returned as strings per ConfigParser, so cast them if you need to."""
  48. return AttrDict(dict(self.settings.items(section)))
  49. Config = ConfigHandler()