/usr/lib/python3/dist-packages/cement/ext/ext_json_configobj.py is in python3-cement 2.10.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | """
The JSON ConfigObj Extension is a combination of the
:class:`JsonConfigHandler` and :class:`ConfigObjConfigHandler` which allows
the application to read JSON configuration files into a ConfigObj based
configuration handler.
Requirements
------------
* ConfigObj (``pip install configobj``)
Configuration
-------------
This extension does not support any configuration settings.
Usage
-----
**myapp.conf**
.. code-block:: json
{
"myapp": {
"foo": "bar"
}
}
**myapp.py**
.. code-block:: python
from cement.core.foundation import CementApp
class MyApp(CementApp):
class Meta:
label = 'myapp'
extensions = ['json_configobj']
config_handler = 'json_configobj'
with MyApp() as app:
app.run()
# get config settings
app.config['myapp']['foo']
# set config settings
app.config['myapp']['foo'] = 'bar2'
# etc...
"""
from ..utils.misc import minimal_logger
from ..ext.ext_configobj import ConfigObjConfigHandler
LOG = minimal_logger(__name__)
class JsonConfigObjConfigHandler(ConfigObjConfigHandler):
"""
This class implements the :ref:`IConfig <cement.core.config>`
interface, and provides the same functionality of
:ref:`ConfigObjConfigHandler <cement.ext.ext_configobj>`
but with JSON configuration files.
**Note** This extension has an external dependency on ``ConfigObj``. You
must include ``configobj`` in your application's dependencies as Cement
explicitly does *not* include external dependencies for optional
extensions.
"""
class Meta:
"""Handler meta-data."""
#: The string identifier of this handler.
label = 'json_configobj'
#: Backend JSON module to use (``json``, ``ujson``, etc)
json_module = 'json'
def __init__(self, *args, **kw):
super(JsonConfigObjConfigHandler, self).__init__(*args, **kw)
self._json = None
def _setup(self, app):
super(JsonConfigObjConfigHandler, self)._setup(app)
self._json = __import__(self._meta.json_module,
globals(), locals(), [], 0)
def _parse_file(self, file_path):
"""
Parse JSON configuration file settings from ``file_path``, overwriting
existing config settings. If the file does not exist, returns
``False``.
:param file_path: The file system path to the JSON configuration file.
:returns: boolean
"""
self.merge(self._json.load(open(file_path)))
# FIX ME: Should check that file was read properly, however if not it
# will likely raise an exception anyhow.
return True
def load(app):
app.handler.register(JsonConfigObjConfigHandler)
|