示例#1
0
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.
    
    `path` is the location at which the service may be called, like
    "/ca/uguu/puukusoft/staticDHCPd/extension/stats/histograph.csv".
    
    The `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
    `Python BasicHTTPServer` headers object.
    
    It must return a tuple of (mimetype, data, headers), with data being a
    string or bytes-like object.
    
    `cacheable` controls whether the client is allowed to cache the method's
    content.
    
    `hidden` methods are not rendered in the side-bar.
    
    `secure` controls whether DIGEST authentication will be required before this
    method can be called.
    
    `module` and `name` describe how it will be presented, both as
    human-readable strings. Only if `hidden` is False, though.
    
    `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`.
    
    `display_mode` is one of the WEB_METHOD_* constants, described at the start
    of this module.
    """
    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,
            })
示例#2
0
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.
    
    `path` is the location at which the service may be called, like
    "/ca/uguu/puukusoft/staticDHCPd/extension/stats/histograph.csv".
    
    The `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
    `Python BasicHTTPServer` headers object.
    
    It must return a tuple of (mimetype, data, headers), with data being a
    string or bytes-like object.
    
    `cacheable` controls whether the client is allowed to cache the method's
    content.
    
    `hidden` methods are not rendered in the side-bar.
    
    `secure` controls whether DIGEST authentication will be required before this
    method can be called.
    
    `module` and `name` describe how it will be presented, both as
    human-readable strings. Only if `hidden` is False, though.
    
    `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`.
    
    `display_mode` is one of the WEB_METHOD_* constants, described at the start
    of this module.
    """
    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,})
示例#3
0
def registerDashboardCallback(module, name, callback, ordering=None):
    """
    Installs an element in the dashboard; at most one instance of any given
    `callback` will be accepted.
    
    `module` and `name` describe how it will be presented, both as
    human-readable strings.
    
    The `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
    `Python BasicHTTPServer` headers object.
    
    It must return data as a string, formatted as XHTML, to be embedded inside
    of a <div/>, or None to suppress inclusion.
    
    To control where the element will appear, supply `ordering`. This is a
    number that controls where this element will appear in relation to
    others. If omitted, 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,
            })
示例#4
0
def registerDashboardCallback(module, name, callback, ordering=None):
    """
    Installs an element in the dashboard; at most one instance of any given
    `callback` will be accepted.
    
    `module` and `name` describe how it will be presented, both as
    human-readable strings.
    
    The `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
    `Python BasicHTTPServer` headers object.
    
    It must return data as a string, formatted as XHTML, to be embedded inside
    of a <div/>, or None to suppress inclusion.
    
    To control where the element will appear, supply `ordering`. This is a
    number that controls where this element will appear in relation to
    others. If omitted, 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,})
示例#5
0
 def render(self, *args, **kwargs):
     global _SEVERITY_MAP
     output = []
     for (severity, line) in self._logger.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),
     }
示例#6
0
    def render(self, *args, **kwargs):
        global _SEVERITY_MAP
        output = []
        for (severity, line) in self._logger.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),
        }
示例#7
0
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 (C) Neil Tallim, 2013 <*****@*****.**>
"""
import logging

from .. import config
import _functions

_logger = logging.getLogger('web.headers')

def contentType(*args, **kwargs):
    return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>'
    
_TITLE = '<title>' + _functions.sanitise(config.SYSTEM_NAME) + '</title>'
def title(*args, **kwargs):
    return _TITLE
    
def css(*args, **kwargs):
    return '<link rel="stylesheet" type="text/css" href="/css"/>'
    
def favicon(*args, **kwargs):
    return '<link rel="icon" type="image/x-icon" href="/favicon.ico"/>'
    
def javascript(*args, **kwargs):
    return '<script type="text/javascript" src="/javascript"></script>'
    
示例#8
0
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)
_FOOTER = """<div style="float: right;">If you benefit from this system, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;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),
}
_BOOT_TIME = datetime.datetime.now().replace(microsecond=0)

def _renderHeaders(path, queryargs, mimetype, data, headers):
    output = []
    for callback in retrieveHeaderCallbacks():
        try:
            content = callback(path, queryargs, mimetype, data, headers)
示例#9
0
 
 (C) Neil Tallim, 2013 <*****@*****.**>
"""
import logging

from .. import config
import _functions

_logger = logging.getLogger("web.headers")


def contentType(*args, **kwargs):
    return '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>'


_TITLE = "<title>" + _functions.sanitise(config.SYSTEM_NAME) + "</title>"


def title(*args, **kwargs):
    return _TITLE


def css(*args, **kwargs):
    return '<link rel="stylesheet" type="text/css" href="/css"/>'


def favicon(*args, **kwargs):
    return '<link rel="icon" type="image/x-icon" href="/favicon.ico"/>'


def javascript(*args, **kwargs):
示例#10
0
from .. import config
import _functions

import dhcpdlib
import libpydhcpserver

_logger = logging.getLogger('web.server')

from dhcpdlib.web import (
 retrieveHeaderCallbacks,
 retrieveDashboardCallbacks,
 retrieveVisibleMethodCallbacks
)

_SYSTEM_NAME = _functions.sanitise(config.SYSTEM_NAME)
_FOOTER = """<div style="float: right;">If you benefit from this system, please <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;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(dhcpdlib.URL),
 'staticdhcpd-version': _functions.sanitise(dhcpdlib.VERSION),
 'libpydhcpserver-url': _functions.sanitise(libpydhcpserver.URL),
 'libpydhcpserver-version': _functions.sanitise(libpydhcpserver.VERSION),
}
_BOOT_TIME = datetime.datetime.now().replace(microsecond=0)

def _renderHeaders(path, queryargs, mimetype, data, headers):
    output = []
    for callback in retrieveHeaderCallbacks():
        try:
            content = callback(path, queryargs, mimetype, data, headers)