__init__.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # See http://code.google.com/p/python-nose/issues/detail?id=373
  2. # The code below enables nosetests to work with i18n _() blocks
  3. import __builtin__
  4. import sys
  5. import os
  6. from ConfigParser import MissingSectionHeaderError
  7. from StringIO import StringIO
  8. from swift.common.utils import readconf
  9. setattr(__builtin__, '_', lambda x: x)
  10. # Work around what seems to be a Python bug.
  11. # c.f. https://bugs.launchpad.net/swift/+bug/820185.
  12. import logging
  13. logging.raiseExceptions = False
  14. def get_config(section_name=None, defaults=None):
  15. """
  16. Attempt to get a test config dictionary.
  17. :param section_name: the section to read (all sections if not defined)
  18. :param defaults: an optional dictionary namespace of defaults
  19. """
  20. config_file = os.environ.get('SWIFT_TEST_CONFIG_FILE',
  21. '/etc/swift/test.conf')
  22. config = {}
  23. if defaults is not None:
  24. config.update(defaults)
  25. try:
  26. config = readconf(config_file, section_name)
  27. except SystemExit:
  28. if not os.path.exists(config_file):
  29. print >>sys.stderr, \
  30. 'Unable to read test config %s - file not found' \
  31. % config_file
  32. elif not os.access(config_file, os.R_OK):
  33. print >>sys.stderr, \
  34. 'Unable to read test config %s - permission denied' \
  35. % config_file
  36. else:
  37. print >>sys.stderr, \
  38. 'Unable to read test config %s - section %s not found' \
  39. % (config_file, section_name)
  40. return config