示例#1
0
 def __init__(self, bus):
     self.threads = {}
     SimplePlugin.__init__(self, bus)
     self.bus.listeners.setdefault('acquire_thread', set())
     self.bus.listeners.setdefault('start_thread', set())
     self.bus.listeners.setdefault('release_thread', set())
     self.bus.listeners.setdefault('stop_thread', set())
示例#2
0
 def __init__(self, bus):
     self.threads = {}
     SimplePlugin.__init__(self, bus)
     self.bus.listeners.setdefault('acquire_thread', set())
     self.bus.listeners.setdefault('start_thread', set())
     self.bus.listeners.setdefault('release_thread', set())
     self.bus.listeners.setdefault('stop_thread', set())
示例#3
0
def _engine_namespace_handler(k, v):
    engine = cherrypy.engine
    if k == 'autoreload_on':
        if v:
            engine.autoreload.subscribe()
        else:
            engine.autoreload.unsubscribe()
    elif k == 'autoreload_frequency':
        engine.autoreload.frequency = v
    elif k == 'autoreload_match':
        engine.autoreload.match = v
    elif k == 'reload_files':
        engine.autoreload.files = set(v)
    elif k == 'deadlock_poll_freq':
        engine.timeout_monitor.frequency = v
    elif k == 'SIGHUP':
        engine.listeners['SIGHUP'] = set([v])
    elif k == 'SIGTERM':
        engine.listeners['SIGTERM'] = set([v])
    elif '.' in k:
        plugin, attrname = k.split('.', 1)
        plugin = getattr(engine, plugin)
        if attrname == 'on':
            if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
                plugin.subscribe()
                return
            if not v and hasattr(getattr(plugin, 'unsubscribe', None), '__call__'):
                plugin.unsubscribe()
                return
        setattr(plugin, attrname, v)
    else:
        setattr(engine, k, v)
示例#4
0
def _engine_namespace_handler(k, v):
    """Backward compatibility handler for the "engine" namespace."""
    engine = cherrypy.engine
    if k == 'autoreload_on':
        if v:
            engine.autoreload.subscribe()
        else:
            engine.autoreload.unsubscribe()
    elif k == 'autoreload_frequency':
        engine.autoreload.frequency = v
    elif k == 'autoreload_match':
        engine.autoreload.match = v
    elif k == 'reload_files':
        engine.autoreload.files = set(v)
    elif k == 'deadlock_poll_freq':
        engine.timeout_monitor.frequency = v
    elif k == 'SIGHUP':
        engine.listeners['SIGHUP'] = set([v])
    elif k == 'SIGTERM':
        engine.listeners['SIGTERM'] = set([v])
    elif "." in k:
        plugin, attrname = k.split(".", 1)
        plugin = getattr(engine, plugin)
        if attrname == 'on':
            if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
                plugin.subscribe()
                return
            elif (not v) and hasattr(getattr(plugin, 'unsubscribe', None),
                                     '__call__'):
                plugin.unsubscribe()
                return
        setattr(plugin, attrname, v)
    else:
        setattr(engine, k, v)
示例#5
0
    def test_graceful(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('graceful', self.get_listener('graceful', index))

        b.graceful()
        self.assertEqual(set(self.responses), set([ msg % (i, 'graceful', None) for i in range(num) ]))
        self.assertLog(['Bus graceful'])
示例#6
0
    def test_graceful(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('graceful', self.get_listener('graceful', index))

        b.graceful()
        self.assertEqual(set(self.responses), set([ msg % (i, 'graceful', None) for i in range(num) ]))
        self.assertLog(['Bus graceful'])
示例#7
0
    def test_stop(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('stop', self.get_listener('stop', index))

        b.stop()
        self.assertEqual(set(self.responses), set([ msg % (i, 'stop', None) for i in range(num) ]))
        self.assertEqual(b.state, b.states.STOPPED)
        self.assertLog(['Bus STOPPING', 'Bus STOPPED'])
示例#8
0
    def test_stop(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('stop', self.get_listener('stop', index))

        b.stop()
        self.assertEqual(set(self.responses), set([ msg % (i, 'stop', None) for i in range(num) ]))
        self.assertEqual(b.state, b.states.STOPPED)
        self.assertLog(['Bus STOPPING', 'Bus STOPPED'])
示例#9
0
def _engine_namespace_handler(k, v):
    """Backward compatibility handler for the "engine" namespace."""
    engine = cherrypy.engine

    deprecated = {
        'autoreload_on': 'autoreload.on',
        'autoreload_frequency': 'autoreload.frequency',
        'autoreload_match': 'autoreload.match',
        'reload_files': 'autoreload.files',
        'deadlock_poll_freq': 'timeout_monitor.frequency'
    }

    if k in deprecated:
        engine.log(
            'WARNING: Use of engine.%s is deprecated and will be removed in a '
            'future version. Use engine.%s instead.' % (k, deprecated[k]))

    if k == 'autoreload_on':
        if v:
            engine.autoreload.subscribe()
        else:
            engine.autoreload.unsubscribe()
    elif k == 'autoreload_frequency':
        engine.autoreload.frequency = v
    elif k == 'autoreload_match':
        engine.autoreload.match = v
    elif k == 'reload_files':
        engine.autoreload.files = set(v)
    elif k == 'deadlock_poll_freq':
        engine.timeout_monitor.frequency = v
    elif k == 'SIGHUP':
        engine.listeners['SIGHUP'] = set([v])
    elif k == 'SIGTERM':
        engine.listeners['SIGTERM'] = set([v])
    elif "." in k:
        plugin, attrname = k.split(".", 1)
        plugin = getattr(engine, plugin)
        if attrname == 'on':
            if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
                plugin.subscribe()
                return
            elif (
                (not v) and
                hasattr(getattr(plugin, 'unsubscribe', None), '__call__')
            ):
                plugin.unsubscribe()
                return
        setattr(plugin, attrname, v)
    else:
        setattr(engine, k, v)
示例#10
0
    def test_start(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('start', self.get_listener('start', index))

        b.start()
        try:
            self.assertEqual(set(self.responses), set([ msg % (i, 'start', None) for i in range(num) ]))
            self.assertEqual(b.state, b.states.STARTED)
            self.assertLog(['Bus STARTING', 'Bus STARTED'])
        finally:
            b.exit()
示例#11
0
    def test_start(self):
        b = wspbus.Bus()
        self.log(b)
        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('start', self.get_listener('start', index))

        b.start()
        try:
            self.assertEqual(set(self.responses), set([ msg % (i, 'start', None) for i in range(num) ]))
            self.assertEqual(b.state, b.states.STARTED)
            self.assertLog(['Bus STARTING', 'Bus STARTED'])
        finally:
            b.exit()
示例#12
0
    def test_graceful(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe("graceful", self.get_listener("graceful", index))

        b.graceful()

        # The graceful method MUST call all 'graceful' listeners.
        self.assertEqual(set(self.responses), set([msg % (i, "graceful", None) for i in range(num)]))
        # The graceful method MUST log its states.
        self.assertLog(["Bus graceful"])
示例#13
0
def _engine_namespace_handler(k, v):
    """Backward compatibility handler for the "engine" namespace."""
    engine = cherrypy.engine

    deprecated = {
        'autoreload_on': 'autoreload.on',
        'autoreload_frequency': 'autoreload.frequency',
        'autoreload_match': 'autoreload.match',
        'reload_files': 'autoreload.files',
        'deadlock_poll_freq': 'timeout_monitor.frequency'
    }

    if k in deprecated:
        engine.log(
            'WARNING: Use of engine.%s is deprecated and will be removed in '
            'a future version. Use engine.%s instead.' % (k, deprecated[k]))

    if k == 'autoreload_on':
        if v:
            engine.autoreload.subscribe()
        else:
            engine.autoreload.unsubscribe()
    elif k == 'autoreload_frequency':
        engine.autoreload.frequency = v
    elif k == 'autoreload_match':
        engine.autoreload.match = v
    elif k == 'reload_files':
        engine.autoreload.files = set(v)
    elif k == 'deadlock_poll_freq':
        engine.timeout_monitor.frequency = v
    elif k == 'SIGHUP':
        engine.listeners['SIGHUP'] = set([v])
    elif k == 'SIGTERM':
        engine.listeners['SIGTERM'] = set([v])
    elif "." in k:
        plugin, attrname = k.split(".", 1)
        plugin = getattr(engine, plugin)
        if attrname == 'on':
            if v and hasattr(getattr(plugin, 'subscribe', None), '__call__'):
                plugin.subscribe()
                return
            elif (not v) and hasattr(getattr(plugin, 'unsubscribe', None),
                                     '__call__'):
                plugin.unsubscribe()
                return
        setattr(plugin, attrname, v)
    else:
        setattr(engine, k, v)
示例#14
0
 def subscribe(self, channel, callback, priority=None):
     if channel not in self.listeners:
         self.listeners[channel] = set()
     self.listeners[channel].add(callback)
     if priority is None:
         priority = getattr(callback, 'priority', 50)
     self._priorities[channel, callback] = priority
示例#15
0
文件: wspbus.py 项目: Pluckyduck/eve
 def subscribe(self, channel, callback, priority = None):
     if channel not in self.listeners:
         self.listeners[channel] = set()
     self.listeners[channel].add(callback)
     if priority is None:
         priority = getattr(callback, 'priority', 50)
     self._priorities[channel, callback] = priority
示例#16
0
 def __init__(self):
     self.execv = False
     self.state = states.STOPPED
     self.listeners = dict([(channel, set())
                            for channel in ('start', 'stop', 'exit',
                                            'graceful', 'log', 'main')])
     self._priorities = {}
示例#17
0
文件: wspbus.py 项目: ducin/cherrypy
 def __init__(self):
     self.execv = False
     self.state = states.STOPPED
     self.listeners = dict(
         [(channel, set()) for channel
          in ('start', 'stop', 'exit', 'graceful', 'log', 'main')])
     self._priorities = {}
示例#18
0
 def subscribe(self, channel, callback, priority=None):
     """Add the given callback at the given channel (if not present)."""
     if channel not in self.listeners:
         self.listeners[channel] = set()
     self.listeners[channel].add(callback)
     if priority is None:
         priority = getattr(callback, 'priority', 50)
     self._priorities[channel, callback] = priority
示例#19
0
    def subscribe_params(self, channel:str, callback:Function(DynParameters, Void)):
        """Add the given callback at the given channel (if not present)."""
        if channel not in self.listeners:
            self.listeners[channel] = set()
        self.listeners[channel].add(callback)

        priority = getattr(callback, 'priority', 50)
        self._priorities[(channel, callback)] = priority
示例#20
0
    def test_stop(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe("stop", self.get_listener("stop", index))

        b.stop()

        # The stop method MUST call all 'stop' listeners.
        self.assertEqual(set(self.responses), set([msg % (i, "stop", None) for i in range(num)]))
        # The stop method MUST move the state to STOPPED
        self.assertEqual(b.state, b.states.STOPPED)
        # The stop method MUST log its states.
        self.assertLog(["Bus STOPPING", "Bus STOPPED"])
示例#21
0
文件: wspbus.py 项目: ducin/cherrypy
    def subscribe(self, channel, callback, priority=None):
        """Add the given callback at the given channel (if not present)."""
        if channel not in self.listeners:
            self.listeners[channel] = set()
        self.listeners[channel].add(callback)

        if priority is None:
            priority = getattr(callback, 'priority', 50)
        self._priorities[(channel, callback)] = priority
示例#22
0
    def test_stop(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('stop', self.get_listener('stop', index))

        b.stop()

        # The stop method MUST call all 'stop' listeners.
        self.assertEqual(set(self.responses),
                         set([msg % (i, 'stop', None) for i in range(num)]))
        # The stop method MUST move the state to STOPPED
        self.assertEqual(b.state, b.states.STOPPED)
        # The stop method MUST log its states.
        self.assertLog(['Bus STOPPING', 'Bus STOPPED'])
示例#23
0
 def set_response_header():
     resp_h = cherrypy.serving.response.headers
     v = set([ e.value for e in resp_h.elements('Vary') ])
     if debug:
         cherrypy.log('Accessed headers: %s' % request.headers.accessed_headers, 'TOOLS.AUTOVARY')
     v = v.union(request.headers.accessed_headers)
     v = v.difference(ignore)
     v = list(v)
     v.sort()
     resp_h['Vary'] = ', '.join(v)
示例#24
0
 def set_response_header():
     resp_h = cherrypy.serving.response.headers
     v = set([e.value for e in resp_h.elements("Vary")])
     if debug:
         cherrypy.log("Accessed headers: %s" % request.headers.accessed_headers, "TOOLS.AUTOVARY")
     v = v.union(request.headers.accessed_headers)
     v = v.difference(ignore)
     v = list(v)
     v.sort()
     resp_h["Vary"] = ", ".join(v)
示例#25
0
 def set_response_header():
     resp_h = cherrypy.serving.response.headers
     v = set([ e.value for e in resp_h.elements('Vary') ])
     if debug:
         cherrypy.log('Accessed headers: %s' % request.headers.accessed_headers, 'TOOLS.AUTOVARY')
     v = v.union(request.headers.accessed_headers)
     v = v.difference(ignore)
     v = list(v)
     v.sort()
     resp_h['Vary'] = ', '.join(v)
示例#26
0
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

        self.attempted_charsets = set()
        request = cherrypy.serving.request
        if request.handler is not None:
            if self.debug:
                cherrypy.log('Replacing request.handler', 'TOOLS.ENCODE')
            self.oldhandler = request.handler
            request.handler = self
示例#27
0
    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)

        self.attempted_charsets = set()
        request = cherrypy.serving.request
        if request.handler is not None:
            if self.debug:
                cherrypy.log('Replacing request.handler', 'TOOLS.ENCODE')
            self.oldhandler = request.handler
            request.handler = self
示例#28
0
    def test_exit(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('stop', self.get_listener('stop', index))
            b.subscribe('exit', self.get_listener('exit', index))

        b.exit()

        # The exit method MUST call all 'stop' listeners,
        # and then all 'exit' listeners.
        self.assertEqual(set(self.responses),
                         set([msg % (i, 'stop', None) for i in range(num)] + 
                             [msg % (i, 'exit', None) for i in range(num)]))
        # The exit method MUST move the state to EXITING
        self.assertEqual(b.state, b.states.EXITING)
        # The exit method MUST log its states.
        self.assertLog(['Bus STOPPING', 'Bus STOPPED', 'Bus EXITING', 'Bus EXITED'])
示例#29
0
    def test_start(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe("start", self.get_listener("start", index))

        b.start()
        try:
            # The start method MUST call all 'start' listeners.
            self.assertEqual(set(self.responses), set([msg % (i, "start", None) for i in range(num)]))
            # The start method MUST move the state to STARTED
            # (or EXITING, if errors occur)
            self.assertEqual(b.state, b.states.STARTED)
            # The start method MUST log its states.
            self.assertLog(["Bus STARTING", "Bus STARTED"])
        finally:
            # Exit so the atexit handler doesn't complain.
            b.exit()
示例#30
0
文件: test_bus.py 项目: avsyap/fitbit
    def test_exit(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('stop', self.get_listener('stop', index))
            b.subscribe('exit', self.get_listener('exit', index))

        b.exit()

        # The exit method MUST call all 'stop' listeners,
        # and then all 'exit' listeners.
        self.assertEqual(set(self.responses),
                         set([msg % (i, 'stop', None) for i in range(num)] +
                             [msg % (i, 'exit', None) for i in range(num)]))
        # The exit method MUST move the state to EXITING
        self.assertEqual(b.state, b.states.EXITING)
        # The exit method MUST log its states.
        self.assertLog(
            ['Bus STOPPING', 'Bus STOPPED', 'Bus EXITING', 'Bus EXITED'])
示例#31
0
文件: plugins.py 项目: Pluckyduck/eve
    def sysfiles(self):
        files = set()
        for k, m in sys.modules.items():
            if re.match(self.match, k):
                if hasattr(m, '__loader__') and hasattr(m.__loader__, 'archive'):
                    f = m.__loader__.archive
                else:
                    f = getattr(m, '__file__', None)
                    if f is not None and not os.path.isabs(f):
                        f = os.path.normpath(os.path.join(_module__file__base, f))
                files.add(f)

        return files
示例#32
0
文件: plugins.py 项目: xyb/0bin
 def sysfiles(self):
     """Return a Set of sys.modules filenames to monitor."""
     files = set()
     for k, m in sys.modules.items():
         if re.match(self.match, k):
             if hasattr(m, '__loader__') and hasattr(m.__loader__, 'archive'):
                 f = m.__loader__.archive
             else:
                 f = getattr(m, '__file__', None)
                 if f is not None and not os.path.isabs(f):
                     # ensure absolute paths so a os.chdir() in the app doesn't break me
                     f = os.path.normpath(os.path.join(_module__file__base, f))
             files.add(f)
     return files
示例#33
0
    def test_start(self):
        b = wspbus.Bus()
        self.log(b)

        self.responses = []
        num = 3
        for index in range(num):
            b.subscribe('start', self.get_listener('start', index))

        b.start()
        try:
            # The start method MUST call all 'start' listeners.
            self.assertEqual(
                set(self.responses),
                set([msg % (i, 'start', None) for i in range(num)]))
            # The start method MUST move the state to STARTED
            # (or EXITING, if errors occur)
            self.assertEqual(b.state, b.states.STARTED)
            # The start method MUST log its states.
            self.assertLog(['Bus STARTING', 'Bus STARTED'])
        finally:
            # Exit so the atexit handler doesn't complain.
            b.exit()
示例#34
0
 def sysfiles(self):
     """Return a Set of sys.modules filenames to monitor."""
     files = set()
     for k, m in sys.modules.items():
         if re.match(self.match, k):
             if hasattr(m, '__loader__') and hasattr(m.__loader__, 'archive'):
                 f = m.__loader__.archive
             else:
                 f = getattr(m, '__file__', None)
                 if f is not None and not os.path.isabs(f):
                     # ensure absolute paths so a os.chdir() in the app doesn't break me
                     f = os.path.normpath(os.path.join(_module__file__base, f))
             files.add(f)
     return files
示例#35
0
    def sysfiles(self):
        files = set()
        for k, m in sys.modules.items():
            if re.match(self.match, k):
                if hasattr(m, '__loader__') and hasattr(
                        m.__loader__, 'archive'):
                    f = m.__loader__.archive
                else:
                    f = getattr(m, '__file__', None)
                    if f is not None and not os.path.isabs(f):
                        f = os.path.normpath(
                            os.path.join(_module__file__base, f))
                files.add(f)

        return files
示例#36
0
def autovary(ignore = None, debug = False):
    request = cherrypy.serving.request
    req_h = request.headers
    request.headers = MonitoredHeaderMap()
    request.headers.update(req_h)
    if ignore is None:
        ignore = set(['Content-Disposition', 'Content-Length', 'Content-Type'])

    def set_response_header():
        resp_h = cherrypy.serving.response.headers
        v = set([ e.value for e in resp_h.elements('Vary') ])
        if debug:
            cherrypy.log('Accessed headers: %s' % request.headers.accessed_headers, 'TOOLS.AUTOVARY')
        v = v.union(request.headers.accessed_headers)
        v = v.difference(ignore)
        v = list(v)
        v.sort()
        resp_h['Vary'] = ', '.join(v)

    request.hooks.attach('before_finalize', set_response_header, 95)
示例#37
0
def autovary(ignore = None, debug = False):
    request = cherrypy.serving.request
    req_h = request.headers
    request.headers = MonitoredHeaderMap()
    request.headers.update(req_h)
    if ignore is None:
        ignore = set(['Content-Disposition', 'Content-Length', 'Content-Type'])

    def set_response_header():
        resp_h = cherrypy.serving.response.headers
        v = set([ e.value for e in resp_h.elements('Vary') ])
        if debug:
            cherrypy.log('Accessed headers: %s' % request.headers.accessed_headers, 'TOOLS.AUTOVARY')
        v = v.union(request.headers.accessed_headers)
        v = v.difference(ignore)
        v = list(v)
        v.sort()
        resp_h['Vary'] = ', '.join(v)

    request.hooks.attach('before_finalize', set_response_header, 95)
示例#38
0
def autovary(ignore=None, debug=False):
    """Auto-populate the Vary response header based on request.header access."""
    request = cherrypy.serving.request

    req_h = request.headers
    request.headers = MonitoredHeaderMap()
    request.headers.update(req_h)
    if ignore is None:
        ignore = set(["Content-Disposition", "Content-Length", "Content-Type"])

    def set_response_header():
        resp_h = cherrypy.serving.response.headers
        v = set([e.value for e in resp_h.elements("Vary")])
        if debug:
            cherrypy.log("Accessed headers: %s" % request.headers.accessed_headers, "TOOLS.AUTOVARY")
        v = v.union(request.headers.accessed_headers)
        v = v.difference(ignore)
        v = list(v)
        v.sort()
        resp_h["Vary"] = ", ".join(v)

    request.hooks.attach("before_finalize", set_response_header, 95)
示例#39
0
def test_callable_spec(callable, callable_args, callable_kwargs):
    """
    Inspect callable and test to see if the given args are suitable for it.

    When an error occurs during the handler's invoking stage there are 2
    erroneous cases:
    1.  Too many parameters passed to a function which doesn't define
        one of *args or **kwargs.
    2.  Too little parameters are passed to the function.

    There are 3 sources of parameters to a cherrypy handler.
    1.  query string parameters are passed as keyword parameters to the handler.
    2.  body parameters are also passed as keyword parameters.
    3.  when partial matching occurs, the final path atoms are passed as
        positional args.
    Both the query string and path atoms are part of the URI.  If they are
    incorrect, then a 404 Not Found should be raised. Conversely the body
    parameters are part of the request; if they are invalid a 400 Bad Request.
    """
    show_mismatched_params = getattr(
        cherrypy.serving.request, 'show_mismatched_params', False)
    try:
        (args, varargs, varkw, defaults) = inspect.getargspec(callable)
    except TypeError:
        if isinstance(callable, object) and hasattr(callable, '__call__'):
            (args, varargs, varkw, defaults) = inspect.getargspec(callable.__call__)
        else:
            # If it wasn't one of our own types, re-raise
            # the original error
            raise

    if args and args[0] == 'self':
        args = args[1:]

    arg_usage = dict([(arg, 0,) for arg in args])
    vararg_usage = 0
    varkw_usage = 0
    extra_kwargs = set()

    for i, value in enumerate(callable_args):
        try:
            arg_usage[args[i]] += 1
        except IndexError:
            vararg_usage += 1

    for key in callable_kwargs.keys():
        try:
            arg_usage[key] += 1
        except KeyError:
            varkw_usage += 1
            extra_kwargs.add(key)

    # figure out which args have defaults.
    args_with_defaults = args[-len(defaults or []):]
    for i, val in enumerate(defaults or []):
        # Defaults take effect only when the arg hasn't been used yet.
        if arg_usage[args_with_defaults[i]] == 0:
            arg_usage[args_with_defaults[i]] += 1

    missing_args = []
    multiple_args = []
    for key, usage in arg_usage.items():
        if usage == 0:
            missing_args.append(key)
        elif usage > 1:
            multiple_args.append(key)

    if missing_args:
        # In the case where the method allows body arguments
        # there are 3 potential errors:
        # 1. not enough query string parameters -> 404
        # 2. not enough body parameters -> 400
        # 3. not enough path parts (partial matches) -> 404
        #
        # We can't actually tell which case it is,
        # so I'm raising a 404 because that covers 2/3 of the
        # possibilities
        #
        # In the case where the method does not allow body
        # arguments it's definitely a 404.
        message = None
        if show_mismatched_params:
            message="Missing parameters: %s" % ",".join(missing_args)
        raise cherrypy.HTTPError(404, message=message)

    # the extra positional arguments come from the path - 404 Not Found
    if not varargs and vararg_usage > 0:
        raise cherrypy.HTTPError(404)

    body_params = cherrypy.serving.request.body.params or {}
    body_params = set(body_params.keys())
    qs_params = set(callable_kwargs.keys()) - body_params

    if multiple_args:
        if qs_params.intersection(set(multiple_args)):
            # If any of the multiple parameters came from the query string then
            # it's a 404 Not Found
            error = 404
        else:
            # Otherwise it's a 400 Bad Request
            error = 400

        message = None
        if show_mismatched_params:
            message="Multiple values for parameters: "\
                    "%s" % ",".join(multiple_args)
        raise cherrypy.HTTPError(error, message=message)

    if not varkw and varkw_usage > 0:

        # If there were extra query string parameters, it's a 404 Not Found
        extra_qs_params = set(qs_params).intersection(extra_kwargs)
        if extra_qs_params:
            message = None
            if show_mismatched_params:
                message="Unexpected query string "\
                        "parameters: %s" % ", ".join(extra_qs_params)
            raise cherrypy.HTTPError(404, message=message)

        # If there were any extra body parameters, it's a 400 Not Found
        extra_body_params = set(body_params).intersection(extra_kwargs)
        if extra_body_params:
            message = None
            if show_mismatched_params:
                message="Unexpected body parameters: "\
                        "%s" % ", ".join(extra_body_params)
            raise cherrypy.HTTPError(400, message=message)
示例#40
0
def test_callable_spec(callable, callable_args, callable_kwargs):
    """
    Inspect callable and test to see if the given args are suitable for it.

    When an error occurs during the handler's invoking stage there are 2
    erroneous cases:
    1.  Too many parameters passed to a function which doesn't define
        one of *args or **kwargs.
    2.  Too little parameters are passed to the function.

    There are 3 sources of parameters to a cherrypy handler.
    1.  query string parameters are passed as keyword parameters to the
        handler.
    2.  body parameters are also passed as keyword parameters.
    3.  when partial matching occurs, the final path atoms are passed as
        positional args.
    Both the query string and path atoms are part of the URI.  If they are
    incorrect, then a 404 Not Found should be raised. Conversely the body
    parameters are part of the request; if they are invalid a 400 Bad Request.
    """
    show_mismatched_params = getattr(cherrypy.serving.request,
                                     'show_mismatched_params', False)
    try:
        (args, varargs, varkw, defaults) = getargspec(callable)
    except TypeError:
        if isinstance(callable, object) and hasattr(callable, '__call__'):
            (args, varargs, varkw, defaults) = getargspec(callable.__call__)
        else:
            # If it wasn't one of our own types, re-raise
            # the original error
            raise

    if args and args[0] == 'self':
        args = args[1:]

    arg_usage = dict([(
        arg,
        0,
    ) for arg in args])
    vararg_usage = 0
    varkw_usage = 0
    extra_kwargs = set()

    for i, value in enumerate(callable_args):
        try:
            arg_usage[args[i]] += 1
        except IndexError:
            vararg_usage += 1

    for key in callable_kwargs.keys():
        try:
            arg_usage[key] += 1
        except KeyError:
            varkw_usage += 1
            extra_kwargs.add(key)

    # figure out which args have defaults.
    args_with_defaults = args[-len(defaults or []):]
    for i, val in enumerate(defaults or []):
        # Defaults take effect only when the arg hasn't been used yet.
        if arg_usage[args_with_defaults[i]] == 0:
            arg_usage[args_with_defaults[i]] += 1

    missing_args = []
    multiple_args = []
    for key, usage in arg_usage.items():
        if usage == 0:
            missing_args.append(key)
        elif usage > 1:
            multiple_args.append(key)

    if missing_args:
        # In the case where the method allows body arguments
        # there are 3 potential errors:
        # 1. not enough query string parameters -> 404
        # 2. not enough body parameters -> 400
        # 3. not enough path parts (partial matches) -> 404
        #
        # We can't actually tell which case it is,
        # so I'm raising a 404 because that covers 2/3 of the
        # possibilities
        #
        # In the case where the method does not allow body
        # arguments it's definitely a 404.
        message = None
        if show_mismatched_params:
            message = "Missing parameters: %s" % ",".join(missing_args)
        raise cherrypy.HTTPError(404, message=message)

    # the extra positional arguments come from the path - 404 Not Found
    if not varargs and vararg_usage > 0:
        raise cherrypy.HTTPError(404)

    body_params = cherrypy.serving.request.body.params or {}
    body_params = set(body_params.keys())
    qs_params = set(callable_kwargs.keys()) - body_params

    if multiple_args:
        if qs_params.intersection(set(multiple_args)):
            # If any of the multiple parameters came from the query string then
            # it's a 404 Not Found
            error = 404
        else:
            # Otherwise it's a 400 Bad Request
            error = 400

        message = None
        if show_mismatched_params:
            message = "Multiple values for parameters: "\
                "%s" % ",".join(multiple_args)
        raise cherrypy.HTTPError(error, message=message)

    if not varkw and varkw_usage > 0:

        # If there were extra query string parameters, it's a 404 Not Found
        extra_qs_params = set(qs_params).intersection(extra_kwargs)
        if extra_qs_params:
            message = None
            if show_mismatched_params:
                message = "Unexpected query string "\
                    "parameters: %s" % ", ".join(extra_qs_params)
            raise cherrypy.HTTPError(404, message=message)

        # If there were any extra body parameters, it's a 400 Not Found
        extra_body_params = set(body_params).intersection(extra_kwargs)
        if extra_body_params:
            message = None
            if show_mismatched_params:
                message = "Unexpected body parameters: "\
                    "%s" % ", ".join(extra_body_params)
            raise cherrypy.HTTPError(400, message=message)
示例#41
0
文件: cptools.py 项目: ADA110/Cibus
 def __init__(self):
     self.accessed_headers = set()
示例#42
0
 def __init__(self):
     self.accessed_headers = set()
示例#43
0
tree = _cptree.Tree()
from cherrypy._cptree import Application
from cherrypy import _cpwsgi as wsgi

from cherrypy import process
try:
    from cherrypy.process import win32
    engine = win32.Win32Bus()
    engine.console_control_handler = win32.ConsoleCtrlHandler(engine)
    del win32
except ImportError:
    engine = process.bus

# Timeout monitor. We add two channels to the engine
# to which cherrypy.Application will publish.
engine.listeners['before_request'] = set()
engine.listeners['after_request'] = set()


class _TimeoutMonitor(process.plugins.Monitor):
    def __init__(self, bus):
        self.servings = []
        process.plugins.Monitor.__init__(self, bus, self.run)

    def before_request(self):
        self.servings.append((serving.request, serving.response))

    def after_request(self):
        try:
            self.servings.remove((serving.request, serving.response))
        except ValueError:
示例#44
0
 def __init__(self, bus, frequency=1, match='.*'):
     self.mtimes = {}
     self.files = set()
     self.match = match
     Monitor.__init__(self, bus, self.run, frequency)
示例#45
0
from cherrypy import process

try:
    from cherrypy.process import win32

    engine = win32.Win32Bus()
    engine.console_control_handler = win32.ConsoleCtrlHandler(engine)
    del win32
except ImportError:
    engine = process.bus


# Timeout monitor. We add two channels to the engine
# to which cherrypy.Application will publish.
engine.listeners["before_request"] = set()
engine.listeners["after_request"] = set()


class _TimeoutMonitor(process.plugins.Monitor):
    def __init__(self, bus):
        self.servings = []
        process.plugins.Monitor.__init__(self, bus, self.run)

    def before_request(self):
        self.servings.append((serving.request, serving.response))

    def after_request(self):
        try:
            self.servings.remove((serving.request, serving.response))
        except ValueError:
示例#46
0
from cherrypy._cptree import Application
from cherrypy import _cpwsgi as wsgi

from cherrypy import process
try:
    from cherrypy.process import win32
    engine = win32.Win32Bus()
    engine.console_control_handler = win32.ConsoleCtrlHandler(engine)
    del win32
except ImportError:
    engine = process.bus


# Timeout monitor. We add two channels to the engine
# to which cherrypy.Application will publish.
engine.listeners['before_request'] = set()
engine.listeners['after_request'] = set()


class _TimeoutMonitor(process.plugins.Monitor):

    def __init__(self, bus):
        self.servings = []
        process.plugins.Monitor.__init__(self, bus, self.run)

    def before_request(self):
        self.servings.append((serving.request, serving.response))

    def after_request(self):
        try:
            self.servings.remove((serving.request, serving.response))
示例#47
0
 def __init__(self, bus, frequency=1, match='.*'):
     self.mtimes = {}
     self.files = set()
     self.match = match
     Monitor.__init__(self, bus, self.run, frequency)