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.

settings.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. self.settings.read(["defaults.ini", "settings.ini"])
  23. def print_settings(self):
  24. """Pretty-print the contents of the configuration as JSON."""
  25. for section in self.settings.sections():
  26. print "[{}]".format(section)
  27. for opt in self.settings.options(section):
  28. print "{} = {}".format(opt, repr(self.settings.get(section, opt)))
  29. print ""
  30. def load_plugins(self):
  31. """Load all the plugins specified by the config file."""
  32. for plugin in self.plugins.blueprints.split("\n"):
  33. plugin = plugin.strip()
  34. if not plugin:
  35. continue
  36. load_plugin(plugin)
  37. for custom in self.plugins.custom.split("\n"):
  38. custom = custom.strip()
  39. if not custom:
  40. continue
  41. load_plugin(custom, as_blueprint=False)
  42. def __getattr__(self, section):
  43. """Attribute access for the config object.
  44. You can access config settings via Config.<section>.<name>, for example
  45. Config.site.notify_email and Config.blog.posts_per_page. All results are
  46. returned as strings per ConfigParser, so cast them if you need to."""
  47. return AttrDict(dict(self.settings.items(section)))
  48. Config = ConfigHandler()