PageRenderTime 269ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/solace/settings.py

https://bitbucket.org/plurk/solace/
Python | 90 lines | 50 code | 16 blank | 24 comment | 14 complexity | daad4eb064b2cf7d405dac66895eff9d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- coding: utf-8 -*-
  2. """
  3. solace.settings
  4. ~~~~~~~~~~~~~~~
  5. This module just stores the solace settings.
  6. :copyright: (c) 2009 by Plurk Inc., see AUTHORS for more details.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from __future__ import with_statement
  10. del with_statement
  11. # propagate early. That way we can import "from solace import settings"
  12. # when the settings is not yet set up. This is needed because during
  13. # bootstrapping we're have carefully crafted circular dependencies between
  14. # the settings and the internationalization support module.
  15. import sys, solace
  16. solace.settings = sys.modules['solace.settings']
  17. del sys, solace
  18. #: i18n support, leave in place for custom settings modules
  19. from solace.i18n import lazy_gettext as _
  20. def configure(**values):
  21. """Configuration shortcut."""
  22. for key, value in values.iteritems():
  23. if key.startswith('_') or not key.isupper():
  24. raise TypeError('invalid configuration variable %r' % key)
  25. d[key] = value
  26. def revert_to_default():
  27. """Reverts the known settings to the defaults."""
  28. from os.path import join, dirname
  29. configure_from_file(join(dirname(__file__), 'default_settings.cfg'))
  30. def autodiscover_settings():
  31. """Finds settings in the environment."""
  32. import os
  33. if 'SOLACE_SETTINGS_FILE' in os.environ:
  34. configure_from_file(os.environ['SOLACE_SETTINGS_FILE'])
  35. def configure_from_file(filename):
  36. """Configures from a file."""
  37. d = globals()
  38. ns = dict(d)
  39. execfile(filename, ns)
  40. for key, value in ns.iteritems():
  41. if not key.startswith('_') and key.isupper():
  42. d[key] = value
  43. def describe_settings():
  44. """Describes the settings. Returns a list of
  45. ``(key, current_value, description)`` tuples.
  46. """
  47. import re
  48. from pprint import pformat
  49. from os.path import join, dirname
  50. assignment_re = re.compile(r'\s*([A-Z_][A-Z0-9_]*)\s*=')
  51. # use items() here instead of iteritems so that if a different
  52. # thread somehow fiddles with the globals, we don't break
  53. items = dict((k, (pformat(v).decode('utf-8', 'replace'), u''))
  54. for (k, v) in globals().items() if k.isupper())
  55. with open(join(dirname(__file__), 'default_settings.cfg')) as f:
  56. comment_buf = []
  57. for line in f:
  58. line = line.rstrip().decode('utf-8')
  59. if line.startswith('#:'):
  60. comment_buf.append(line[2:].lstrip())
  61. else:
  62. match = assignment_re.match(line)
  63. if match is not None:
  64. key = match.group(1)
  65. tup = items.get(key)
  66. if tup is not None and comment_buf:
  67. items[key] = (tup[0], u'\n'.join(comment_buf))
  68. del comment_buf[:]
  69. return sorted([(k,) + v for k, v in items.items()])
  70. revert_to_default()
  71. autodiscover_settings()