/usr/lib/python3/dist-packages/cement/ext/ext_reload_config.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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | """
WARNING: THIS EXTENSION IS EXPERIMENTAL
Experimental extension may (and probably will) change at any time. Please do
not rely on these features until they are more fully vetted.
The Reload Config Framework Extension enables applications Built on Cement
(tm) to easily reload configuration settings any time configuration files are
modified without stopping/restarting the process.
Requirements
------------
* Python 2.6+, Python 3+
* Python Modules: pyinotify
* Linux (Kernel 2.6.13+)
Features
--------
* Application configuration files (``CementApp.Meta.config_files``) are
reloaded if modified.
* Application plugin configuration files (Anything found in
(``CementApp.Meta.plugin_config_dirs``) are reloaded if modified.
* The framework calls ``CementApp.config.parse_file()`` on any watched files
once the kernel has signaled a modification.
* New configurations settings are accessible via ``CementApp.config`` nearly
immediately once the kernel (inotify) picks up the change.
* Provides a ``pre_reload_config`` and ``post_reload_config`` hook so that
applications can tie into the event and perform operations any time a
configuration file is modified.
* Asynchronously monitors configuration files for changes via inotify. Long
running processes are not blocked by the operations performed when files
are detected to be modified.
Limitations
-----------
* Currently this extension only re-parses configuration files into the
config handler. Some applications may need further work in-order to truly
honor those changes. For example, if a configuration settings toggles
something on or off, or triggers something else to happen (like making an
API connection, etc)... this extension does not currently handle that, and
it is left up to the application developer to tie into the events via the
provided hooks.
* Only available on Linux based systems.
Configuration
-------------
This extension does not currently honor any configuration settings.
Hooks
-----
This extension defines the following hooks:
pre_reload_config
^^^^^^^^^^^^^^^^^
Run right before any framework actions are performed once modifications to
any of the watched files are detected. Expects a single argument, which is
the ``app`` object, and does not expect anything in return.
.. code-block:: python
def my_pre_reload_config_hook(app):
# do something with app?
pass
post_reload_config
^^^^^^^^^^^^^^^^^^
Run right after any framework actions are performed once modifications to any
of the watched files are detected. Expects a single argument, which is the
``app`` object, and does not expect anything in return.
.. code-block:: python
def my_post_reload_config_hook(app):
# do something with app?
pass
Usage
-----
The following example shows how to add the reload_config extension, as well as
perform an arbitrary action any time configuration changes are detected.
.. code-block:: python
from time import sleep
from cement.core.exc import CaughtSignal
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose
def print_foo(app):
print "Foo => %s" % app.config.get('myapp', 'foo')
class Base(CementBaseController):
class Meta:
label = 'base'
@expose(hide=True)
def default(self):
print('Inside Base.default()')
# simulate a long running process
while True:
sleep(30)
class MyApp(CementApp):
class Meta:
label = 'myapp'
base_controller = Base
extensions = ['reload_config']
with MyApp() as app:
# run this anytime the configuration has changed
app.hook.register('post_reload_config', print_foo)
try:
app.run()
except CaughtSignal as e:
# maybe do something... but catch it regardless so app.close() is
# called when exiting `with` cleanly.
print(e)
The following would output something like the following when ``~/.myapp.conf``
or any other configuration file is modified (spaces added for clarity):
.. code-block:: console
Inside Base.default()
2015-05-05 03:00:32,023 (DEBUG) cement.ext.ext_reload_config : config
path modified: mask=IN_CLOSE_WRITE,
path=/home/vagrant/.myapp.conf
2015-05-05 03:00:32,023 (DEBUG) cement.core.config : config file
'/home/vagrant/.myapp.conf' exists,
loading settings...
2015-05-05 03:00:32,023 (DEBUG) cement.core.hook : running hook
'post_reload_config' (<function print_foo
at 0x7f1b52a5ab70>) from __main__
Foo => bar
2015-05-05 03:00:32,023 (DEBUG) cement.ext.ext_reload_config : config
path modified: mask=IN_CLOSE_WRITE,
path=/home/vagrant/.myapp.conf
2015-05-05 03:00:32,023 (DEBUG) cement.core.config : config file
'/home/vagrant/.myapp.conf' exists,
loading settings...
2015-05-05 03:00:32,023 (DEBUG) cement.core.hook : running hook
'post_reload_config' (<function print_foo
at 0x7f1b52a5ab70>) from __main__
Foo => bar2
2015-05-05 03:00:32,023 (DEBUG) cement.ext.ext_reload_config : config
path modified: mask=IN_CLOSE_WRITE,
path=/home/vagrant/.myapp.conf
2015-05-05 03:00:32,023 (DEBUG) cement.core.config : config file
'/home/vagrant/.myapp.conf' exists,
loading settings...
2015-05-05 03:00:32,023 (DEBUG) cement.core.hook : running hook
'post_reload_config' (<function print_foo
at 0x7f1b52a5ab70>) from __main__
Foo => bar3
"""
import os
import signal
import pyinotify
from ..utils.misc import minimal_logger
from ..utils import fs
LOG = minimal_logger(__name__)
MASK = pyinotify.IN_CLOSE_WRITE
NOTIFIER = None
class ConfigEventHandler(pyinotify.ProcessEvent):
def __init__(self, app, watched_files, **kw):
self.app = app
self.watched_files = watched_files
super(ConfigEventHandler, self).__init__()
def process_default(self, event):
if event.pathname in self.watched_files:
LOG.debug('config path modified: mask=%s, path=%s' %
(event.maskname, event.pathname))
for res in self.app.hook.run('pre_reload_config', self.app):
pass
self.app.config.parse_file(event.pathname)
for res in self.app.hook.run('post_reload_config', self.app):
pass
def spawn_watcher(app):
global NOTIFIER
# directories to tell inotify to watch
watched_dirs = []
# files to actual perform actions on
watched_files = []
# watch manager
wm = pyinotify.WatchManager()
# watch all config files, and plugin config files
watched_files = []
for plugin_dir in app._meta.plugin_config_dirs:
plugin_dir = fs.abspath(plugin_dir)
if not os.path.exists(plugin_dir):
continue
if plugin_dir not in watched_dirs:
watched_dirs.append(plugin_dir)
# just want the first one... looks wierd, but python 2/3 compat
# res = os.walk(plugin_dir) ### ?
for path, dirs, files in os.walk(plugin_dir):
plugin_config_files = files
break
for plugin_config in plugin_config_files:
full_plugin_path = os.path.join(plugin_dir, plugin_config)
if full_plugin_path not in watched_files:
watched_files.append(full_plugin_path)
for path in app._meta.config_files:
if os.path.exists(path):
if path not in watched_files:
watched_files.append(path)
this_dir = os.path.dirname(path)
if this_dir not in watched_dirs:
watched_dirs.append(this_dir)
for path in watched_dirs:
wm.add_watch(path, MASK, rec=True)
# event handler
eh = ConfigEventHandler(app, watched_files)
# notifier
NOTIFIER = pyinotify.ThreadedNotifier(wm, eh)
NOTIFIER.start()
def kill_watcher(app):
if NOTIFIER.isAlive():
NOTIFIER.stop()
def signal_handler(app, signum, frame):
if signum in [signal.SIGTERM, signal.SIGINT]:
if NOTIFIER.isAlive():
NOTIFIER.stop()
def load(app):
app.hook.define('pre_reload_config')
app.hook.define('post_reload_config')
app.hook.register('pre_run', spawn_watcher)
app.hook.register('pre_close', kill_watcher)
app.hook.register('signal', signal_handler)
|