def registerMethodCallback(path, callback, cacheable=False, hidden=True, secure=False, module=None, name=None, confirm=False, display_mode=WEB_METHOD_RAW): """ Installs a webservice method; at most one instance of ``path`` will be accepted. :param basestring path: The location at which the service may be called, like "/ca/uguu/puukusoft/staticDHCPd/extension/stats/histograph.csv". :param callable callback: Must accept the parameters `path`, `queryargs`, `mimetype`, `data`, and `headers`, with the possibility that `mimetype` and `data` may be None; `queryargs` is a dictionary of parsed query-string items, with values expressed as lists of strings; `headers` is a dictionary-like object. It must return a tuple of (mimetype, data, headers), with data being a string or bytes-like object. :param bool cacheable: Whether the client is allowed to cache the method's content. :param bool hidden: Whether to render a link in the side-bar. :param bool secure: Whether authentication will be required before this method can be called. :param basestring module: The name of the module to which this element belongs. :param basestring name: The name under which to display the element. :param bool confirm: Adds JavaScript validation to ask the user if they're sure they know what they're doing before the method will be invoked, if not `hidden`. :param display_mode: One of the WEB_METHOD_* constants. """ with _web_lock: if path in _web_methods: _logger.error("'%(path)s' is already registered" % { 'path': path, }) else: _web_methods[path] = method = _WebMethod( functions.sanitise(module), functions.sanitise(name), hidden, secure, confirm, display_mode, cacheable, callback) _logger.debug("Registered method %(method)r at %(path)s" % { 'method': method, 'path': path, })
def render(self, *args, **kwargs): """ Generates a view of the current log. :return str: An HTML fragment, containing the log. """ global _SEVERITY_MAP output = [] for (severity, line) in self._handler.readContents(): output.append( '<span class="%(severity)s">%(message)s</span>' % { 'severity': _SEVERITY_MAP[severity], 'message': functions.sanitise(line).replace('\n', '<br/>'), }) return """ <div style='overflow-y: auto;%(max-height)s'> %(lines)s </div>""" % { 'max-height': config.WEB_LOG_MAX_HEIGHT and ' max-height: %(max-height)ipx;' % { 'max-height': config.WEB_LOG_MAX_HEIGHT, }, 'lines': '<br/>\n'.join(output), }
def registerDashboardCallback(module, name, callback, ordering=None): """ Installs an element in the dashboard; at most one instance of any given ``callback`` will be accepted. :param basestring module: The name of the module to which this element belongs. :param basestring name: The name under which to display the element. :param callable callback: Must accept the parameters `path`, `queryargs`, `mimetype`, `data`, and `headers`, with the possibility that `mimetype` and `data` may be None; `queryargs` is a dictionary of parsed query-string items, with values expressed as lists of strings; `headers` is a dictionary-like object. It must return data as a string, formatted as XHTML, to be embedded inside of a <div/>, or None to suppress inclusion. :param int ordering: A number that controls where this element will appear in relation to others. If not specified, the value will be that of the highest number plus one, placing it at the end; negatives are valid. """ with _web_lock: for (i, element) in enumerate(_web_dashboard): if element.callback is callback: _logger.error("%(element)r is already registered" % { 'element': element, }) break else: if ordering is None: if _web_dashboard: ordering = _web_dashboard[-1].ordering + 1 else: ordering = 0 element = _WebDashboardElement(ordering, functions.sanitise(module), functions.sanitise(name), callback) _web_dashboard.append(element) _web_dashboard.sort() _logger.debug("Registered dashboard element %(element)r" % { 'element': element, })
def registerMethodCallback(path, callback, cacheable=False, hidden=True, secure=False, module=None, name=None, confirm=False, display_mode=WEB_METHOD_RAW): """ Installs a webservice method; at most one instance of ``path`` will be accepted. :param basestring path: The location at which the service may be called, like "/ca/uguu/puukusoft/staticDHCPd/extension/stats/histograph.csv". :param callable callback: Must accept the parameters `path`, `queryargs`, `mimetype`, `data`, and `headers`, with the possibility that `mimetype` and `data` may be None; `queryargs` is a dictionary of parsed query-string items, with values expressed as lists of strings; `headers` is a dictionary-like object. It must return a tuple of (mimetype, data, headers), with data being a string or bytes-like object. :param bool cacheable: Whether the client is allowed to cache the method's content. :param bool hidden: Whether to render a link in the side-bar. :param bool secure: Whether authentication will be required before this method can be called. :param basestring module: The name of the module to which this element belongs. :param basestring name: The name under which to display the element. :param bool confirm: Adds JavaScript validation to ask the user if they're sure they know what they're doing before the method will be invoked, if not `hidden`. :param display_mode: One of the WEB_METHOD_* constants. """ with _web_lock: if path in _web_methods: _logger.error("'%(path)s' is already registered" % {'path': path,}) else: _web_methods[path] = method = _WebMethod( functions.sanitise(module), functions.sanitise(name), hidden, secure, confirm, display_mode, cacheable, callback ) _logger.debug("Registered method %(method)r at %(path)s" % {'method': method, 'path': path,})
def registerDashboardCallback(module, name, callback, ordering=None): """ Installs an element in the dashboard; at most one instance of any given ``callback`` will be accepted. :param basestring module: The name of the module to which this element belongs. :param basestring name: The name under which to display the element. :param callable callback: Must accept the parameters `path`, `queryargs`, `mimetype`, `data`, and `headers`, with the possibility that `mimetype` and `data` may be None; `queryargs` is a dictionary of parsed query-string items, with values expressed as lists of strings; `headers` is a dictionary-like object. It must return data as a string, formatted as XHTML, to be embedded inside of a <div/>, or None to suppress inclusion. :param int ordering: A number that controls where this element will appear in relation to others. If not specified, the value will be that of the highest number plus one, placing it at the end; negatives are valid. """ with _web_lock: for (i, element) in enumerate(_web_dashboard): if element.callback is callback: _logger.error("%(element)r is already registered" % {'element': element,}) break else: if ordering is None: if _web_dashboard: ordering = _web_dashboard[-1].ordering + 1 else: ordering = 0 element = _WebDashboardElement(ordering, functions.sanitise(module), functions.sanitise(name), callback) _web_dashboard.append(element) _web_dashboard.sort() _logger.debug("Registered dashboard element %(element)r" % {'element': element,})
def render(self, *args, **kwargs): """ Generates a view of the current log. :return str: An HTML fragment, containing the log. """ global _SEVERITY_MAP output = [] for (severity, line) in self._handler.readContents(): output.append('<span class="%(severity)s">%(message)s</span>' % { 'severity': _SEVERITY_MAP[severity], 'message': functions.sanitise(line).replace('\n', '<br/>'), }) return """ <div style='overflow-y: auto;%(max-height)s'> %(lines)s </div>""" % { 'max-height': config.WEB_LOG_MAX_HEIGHT and ' max-height: %(max-height)ipx;' % { 'max-height': config.WEB_LOG_MAX_HEIGHT, }, 'lines': '<br/>\n'.join(output), }
import logging from .. import config import functions _logger = logging.getLogger('web.headers') def contentType(*args, **kwargs): """ Provides the default content-type HTML header. :return str: The content-type header. """ return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>' _TITLE = '<title>' + functions.sanitise(config.SYSTEM_NAME) + '</title>' #: The title of the web interface def title(*args, **kwargs): """ Provides the default title HTML header. :return str: The title header. """ return _TITLE def css(*args, **kwargs): """ Provides the default CSS HTML header. :return str: The CSS header. """ return '<link rel="stylesheet" type="text/css" href="/css"/>'
from .. import config import functions import staticdhcpdlib import libpydhcpserver _logger = logging.getLogger('web.server') from staticdhcpdlib.web import ( retrieveHeaderCallbacks, retrieveDashboardCallbacks, retrieveVisibleMethodCallbacks ) _SYSTEM_NAME = functions.sanitise(config.SYSTEM_NAME) #: The name of the system _FOOTER = """<div style="float: right;">If you benefit from this system, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=11056045">support it</a></div> <a href="%(staticdhcpd-url)s">staticDHCPd</a> v%(staticdhcpd-version)s | <a href="%(libpydhcpserver-url)s">libpydhcpserver</a> v%(libpydhcpserver-version)s""" % { 'staticdhcpd-url': functions.sanitise(staticdhcpdlib.URL), 'staticdhcpd-version': functions.sanitise(staticdhcpdlib.VERSION), 'libpydhcpserver-url': functions.sanitise(libpydhcpserver.URL), 'libpydhcpserver-version': functions.sanitise(libpydhcpserver.VERSION), } #: The footer's HTML fragment _BOOT_TIME = datetime.datetime.now().replace(microsecond=0) #: The time at which the system was started def _renderHeaders(path, queryargs, mimetype, data, headers): """ Renders all HTML headers. :param basestring path: The requested path.
from .. import config import functions _logger = logging.getLogger('web.headers') def contentType(*args, **kwargs): """ Provides the default content-type HTML header. :return str: The content-type header. """ return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>' _TITLE = '<title>' + functions.sanitise( config.SYSTEM_NAME) + '</title>' #: The title of the web interface def title(*args, **kwargs): """ Provides the default title HTML header. :return str: The title header. """ return _TITLE def css(*args, **kwargs): """ Provides the default CSS HTML header.