config.py 5.1 KB

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