mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
Dont use exec() in plugin system
This commit is contained in:
parent
c0ea7ee298
commit
2d39638020
@ -42,17 +42,30 @@ def get_parts(obj, parts=None, *args, **kwargs):
|
|||||||
for v in fields:
|
for v in fields:
|
||||||
if not v in parts:
|
if not v in parts:
|
||||||
parts[v] = ''
|
parts[v] = ''
|
||||||
exec("""
|
|
||||||
try:
|
try:
|
||||||
if str(type(obj.%(v)s))=="<type 'instancemethod'>":
|
method = getattr(obj, v)
|
||||||
parts[v] += obj.%(v)s(*args, **kwargs)
|
if callable(method):
|
||||||
|
parts[v] = method(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
parts[v] += obj.%(v)s
|
parts[v] = method
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass""" % {'v':v})
|
pass
|
||||||
|
|
||||||
return parts
|
return parts
|
||||||
|
|
||||||
|
|
||||||
|
def _setattr_deep(obj, path, value):
|
||||||
|
"""If path is 'x.y.z' or ['x', 'y', 'z'] then perform obj.x.y.z = value"""
|
||||||
|
if isinstance(path, basestring):
|
||||||
|
path = path.split('.')
|
||||||
|
|
||||||
|
for part in path[:-1]:
|
||||||
|
obj = getattr(obj, part)
|
||||||
|
|
||||||
|
setattr(obj, path[-1], value)
|
||||||
|
|
||||||
|
|
||||||
class PagePlugin:
|
class PagePlugin:
|
||||||
"""
|
"""
|
||||||
Mount point for page plugins. Page plugins provide display pages
|
Mount point for page plugins. Page plugins provide display pages
|
||||||
@ -72,7 +85,8 @@ class PagePlugin:
|
|||||||
|
|
||||||
def register_page(self, url):
|
def register_page(self, url):
|
||||||
cfg.log.info("Registering page: %s" % url)
|
cfg.log.info("Registering page: %s" % url)
|
||||||
exec "cfg.html_root.%s = self" % (url)
|
_setattr_deep(cfg.html_root, url, self)
|
||||||
|
|
||||||
def fill_template(self, *args, **kwargs):
|
def fill_template(self, *args, **kwargs):
|
||||||
return u.page_template(*args, **kwargs)
|
return u.page_template(*args, **kwargs)
|
||||||
|
|
||||||
@ -128,8 +142,9 @@ class FormPlugin():
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
for u in self.url:
|
for u in self.url:
|
||||||
exec "cfg.html_root.%s = self" % "%s.%s" % ('.'.join(u.split("/")[1:]), self.__class__.__name__)
|
path = u.split("/")[1:] + [self.__class__.__name__]
|
||||||
cfg.log("Registered page: %s.%s" % ('.'.join(u.split("/")[1:]), self.__class__.__name__))
|
_setattr_deep(cfg.html_root, path, self)
|
||||||
|
cfg.log("Registered page: %s" % '.'.join(path))
|
||||||
|
|
||||||
def main(self, *args, **kwargs):
|
def main(self, *args, **kwargs):
|
||||||
return "<p>Override this method and replace it with a form.</p>"
|
return "<p>Override this method and replace it with a form.</p>"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user