I imagine that the hook code would be something like this:
INJECTED_WARNING = """
#------------------------------------------------------------------------------
# The following is the import code for the settings directory injected by Juju
#------------------------------------------------------------------------------
"""
if not os.path.exists(PKG_DIR): os.mkdir(PKG_DIR)
with open(path_to_rb_settings_py, 'a') as settings_py: settings_py.write(INJECTED_WARNING) settings_py.write("from {} import *".format(PKG))
extfile = open(filename, 'w+')
with open(os.path.join(PKG_DIR, '__init__.py'), 'a') as initfile: initfile.write('from .{} import *\n'.format(extname))
return extfile
def set_settings(extname, settings):
# inspired by what the django charm does.
lines = []
for name, value in settings.items():
line = '{} = {!r}\n'.format(name, value)
try: ast.literal_eval(line)
except SyntaxError: hookenv.log('bad setting: {!r}'.format(line))
return # fail? lines.append(line)
with open_settings(extname) as extfile: extfile.writelines(lines) extfile.truncate()
# Force RB to reload settings.py.i
touch(path_to_rb_settings_py)
def clear_settings(extname=None):
if extname is not None:
filename = os.path.join(PKG_DIR, extname + '_settings.py')
if os.path.exists(filename): os.remove(filename)
extline = 'from .{} import *\n'.format(extname)
with open(os.path.join(PKG_DIR, '__init__.py'), 'r+') as initfile: last = 0
for line in initfile: if line == extline: break last += len(line) lines = []
for line in initfile: lines.append(line) initfile.seek(last) initfile.writelines(lines) initfile.truncate()
else:
if os.path.exists(PKG_DIR): os.rmdir(PKG_DIR)
# remove import from path_to_rb_settings_py?
def related_name():
"""Return the name of the extension in the current relation."""
# We just use the service name.
unit = hookenv.related_units()[0]
service = unit.partition('/')[0]
return service
def _handle_relation_settings():
requested = hookenv.relation_get('settings')
if not requested:
# no settings to add
return
name = related_name()
settings = hookenv.relation_get('_all_settings') or {}
settings.update(requested)
try: set_settings(name, settings)
except Exception as e: hookenv.log("could not update reviewboard settings: {}".format(e))
# hooks
@hooks.hook('reviewboard-extensions-relation-joined')
def reviewboard_extensions_relation_joined():
# Force reviewboard to reload, thus recognizing the new extension.
touch(path_to_rb_settings_py)
I imagine that the hook code would be something like this:
INJECTED_WARNING = """ ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- --
#------
# The following is the import code for the settings directory injected by Juju
#------
"""
PKG = '_juju_ extensions_ settings' join(os. path.dirname( path_to_ rb_settings_ py), PKG)
PKG_DIR = os.path.
def open_settings( extname) : join(PKG_ DIR, extname + '_settings.py') exists( filename) :
filename = os.path.
try:
return open(filename, 'r+')
except IOError:
if os.path.
raise
if not os.path. exists( PKG_DIR) :
os.mkdir( PKG_DIR) to_rb_settings_ py, 'a') as settings_py:
settings_ py.write( INJECTED_ WARNING)
settings_ py.write( "from {} import *".format(PKG))
with open(path_
extfile = open(filename, 'w+')
with open(os. path.join( PKG_DIR, '__init__.py'), 'a') as initfile:
initfile. write(' from .{} import *\n'.format( extname) )
return extfile
def set_settings( extname, settings):
# inspired by what the django charm does.
lines = [] .format( name, value)
ast. literal_ eval(line)
hookenv. log('bad setting: {!r}'.format(line))
lines. append( line)
for name, value in settings.items():
line = '{} = {!r}\n'
try:
except SyntaxError:
return # fail?
with open_settings( extname) as extfile:
extfile. writelines( lines)
extfile. truncate( )
# Force RB to reload settings.py.i path_to_ rb_settings_ py)
touch(
def clear_settings( extname= None): join(PKG_ DIR, extname + '_settings.py') exists( filename) :
os. remove( filename) extname) path.join( PKG_DIR, '__init__.py'), 'r+') as initfile:
last = 0
if line == extline:
break
last += len(line)
lines = []
lines. append( line)
initfile. seek(last)
initfile. writelines( lines)
initfile. truncate( ) exists( PKG_DIR) :
os. rmdir(PKG_ DIR) rb_settings_ py?
if extname is not None:
filename = os.path.
if os.path.
extline = 'from .{} import *\n'.format(
with open(os.
for line in initfile:
for line in initfile:
else:
if os.path.
# remove import from path_to_
def related_name(): related_ units() [0] '/')[0]
"""Return the name of the extension in the current relation."""
# We just use the service name.
unit = hookenv.
service = unit.partition(
return service
def _handle_ relation_ settings( ): relation_ get('settings' )
requested = hookenv.
if not requested:
# no settings to add
return
name = related_name() relation_ get('_all_ settings' ) or {} update( requested)
set_settings( name, settings)
hookenv. log("could not update reviewboard settings: {}".format(e))
settings = hookenv.
settings.
try:
except Exception as e:
# hooks
@hooks. hook('reviewboa rd-extensions- relation- joined' ) extensions_ relation_ joined( ): path_to_ rb_settings_ py)
def reviewboard_
# Force reviewboard to reload, thus recognizing the new extension.
touch(
@hooks. hook('reviewboa rd-extensions- relation- changed' ) extensions_ relation_ changed( ): relation_ settings( )
def reviewboard_
_handle_
@hooks. hook('reviewboa rd-extensions- relation- departed' ) extensions_ relation_ departed( ): settings( related_ name())
def reviewboard_
clear_
@hooks. hook('reviewboa rd-extensions- relation- broken' ) extensions_ relation_ broken( ): settings( related_ name())
def reviewboard_
clear_
@hooks.hook('stop') settings( )
def stop():
...
clear_