config.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. """Cement core config module."""
  2. import os
  3. from ..core import interface, handler
  4. from ..utils.fs import abspath
  5. from ..utils.misc import minimal_logger
  6. LOG = minimal_logger(__name__)
  7. def config_validator(klass, obj):
  8. """Validates a handler implementation against the IConfig interface."""
  9. members = [
  10. '_setup',
  11. 'keys',
  12. 'get_sections',
  13. 'get_section_dict',
  14. 'get',
  15. 'set',
  16. 'parse_file',
  17. 'merge',
  18. 'add_section',
  19. 'has_section',
  20. ]
  21. interface.validate(IConfig, obj, members)
  22. class IConfig(interface.Interface):
  23. """
  24. This class defines the Config Handler Interface. Classes that
  25. implement this handler must provide the methods and attributes defined
  26. below.
  27. All implementations must provide sane 'default' functionality when
  28. instantiated with no arguments. Meaning, it can and should accept
  29. optional parameters that alter how it functions, but can not require
  30. any parameters. When the framework first initializes handlers it does
  31. not pass anything too them, though a handler can be instantiated first
  32. (with or without parameters) and then passed to 'CementApp()' already
  33. instantiated.
  34. Implementations do *not* subclass from interfaces.
  35. Usage:
  36. .. code-block:: python
  37. from cement.core import config
  38. class MyConfigHandler(config.CementConfigHandler):
  39. class Meta:
  40. interface = config.IConfig
  41. label = 'my_config_handler'
  42. ...
  43. """
  44. # pylint: disable=W0232, C0111, R0903
  45. class IMeta:
  46. """Interface meta-data."""
  47. label = 'config'
  48. """The string identifier of the interface."""
  49. validator = config_validator
  50. """The validator function."""
  51. # Must be provided by the implementation
  52. Meta = interface.Attribute('Handler Meta-data')
  53. def _setup(app_obj):
  54. """
  55. The _setup function is called during application initialization and
  56. must 'setup' the handler object making it ready for the framework
  57. or the application to make further calls to it.
  58. :param app_obj: The application object.
  59. :returns: None
  60. """
  61. def parse_file(file_path):
  62. """
  63. Parse config file settings from file_path. Returns True if the file
  64. existed, and was parsed successfully. Returns False otherwise.
  65. :param file_path: The path to the config file to parse.
  66. :returns: True if the file was parsed, False otherwise.
  67. :rtype: ``boolean``
  68. """
  69. def keys(section):
  70. """
  71. Return a list of configuration keys from `section`.
  72. :param section: The config [section] to pull keys from.
  73. :returns: A list of keys in `section`.
  74. :rtype: ``list``
  75. """
  76. def get_sections():
  77. """
  78. Return a list of configuration sections. These are designated by a
  79. [block] label in a config file.
  80. :returns: A list of config sections.
  81. :rtype: ``list``
  82. """
  83. def get_section_dict(section):
  84. """
  85. Return a dict of configuration parameters for [section].
  86. :param section: The config [section] to generate a dict from (using
  87. that section keys).
  88. :returns: A dictionary of the config section.
  89. :rtype: ``dict``
  90. """
  91. def add_section(section):
  92. """
  93. Add a new section if it doesn't exist.
  94. :param section: The [section] label to create.
  95. :returns: ``None``
  96. """
  97. def get(section, key):
  98. """
  99. Return a configuration value based on [section][key]. The return
  100. value type is unknown.
  101. :param section: The [section] of the configuration to pull key value
  102. from.
  103. :param key: The configuration key to get the value from.
  104. :returns: The value of the `key` in `section`.
  105. :rtype: ``Unknown``
  106. """
  107. def set(section, key, value):
  108. """
  109. Set a configuration value based at [section][key].
  110. :param section: The [section] of the configuration to pull key value
  111. from.
  112. :param key: The configuration key to set the value at.
  113. :param value: The value to set.
  114. :returns: ``None``
  115. """
  116. def merge(dict_obj, override=True):
  117. """
  118. Merges a dict object into the configuration.
  119. :param dict_obj: The dictionary to merge into the config
  120. :param override: Boolean. Whether to override existing values.
  121. Default: True
  122. :returns: ``None``
  123. """
  124. def has_section(section):
  125. """
  126. Returns whether or not the section exists.
  127. :param section: The section to test for.
  128. :returns: ``boolean``
  129. """
  130. class CementConfigHandler(handler.CementBaseHandler):
  131. """
  132. Base class that all Config Handlers should sub-class from.
  133. """
  134. class Meta:
  135. """
  136. Handler meta-data (can be passed as keyword arguments to the parent
  137. class).
  138. """
  139. label = None
  140. """The string identifier of the implementation."""
  141. interface = IConfig
  142. """The interface that this handler implements."""
  143. def __init__(self, *args, **kw):
  144. super(CementConfigHandler, self).__init__(*args, **kw)
  145. def _parse_file(self, file_path):
  146. """
  147. Parse a configuration file at `file_path` and store it. This function
  148. must be provided by the handler implementation (that is sub-classing
  149. this).
  150. :param file_path: The file system path to the configuration file.
  151. :returns: True if file was read properly, False otherwise
  152. :rtype: ``boolean``
  153. """
  154. raise NotImplementedError
  155. def parse_file(self, file_path):
  156. """
  157. Ensure we are using the absolute/expanded path to `file_path`, and
  158. then call `_parse_file` to parse config file settings from it,
  159. overwriting existing config settings. If the file does not exist,
  160. returns False.
  161. Developers sub-classing from here should generally override
  162. `_parse_file` which handles just the parsing of the file and leaving
  163. this function to wrap any checks/logging/etc.
  164. :param file_path: The file system path to the configuration file.
  165. :returns: ``boolean``
  166. """
  167. file_path = abspath(file_path)
  168. if os.path.exists(file_path):
  169. LOG.debug("config file '%s' exists, loading settings..." %
  170. file_path)
  171. return self._parse_file(file_path)
  172. else:
  173. LOG.debug("config file '%s' does not exist, skipping..." %
  174. file_path)
  175. return False