Example #1
0
    def get_reader(self, file_name):
        """See `IBackend.get_reader()`.

        If `file_name` matches `re_config_file` then the response is obtained
        from a server. Otherwise the filesystem is used to service the
        response.
        """
        config_file_match = self.re_config_file.match(file_name)
        if config_file_match is None:
            return super(TFTPBackend, self).get_reader(file_name)
        else:
            # Do not include any element that has not matched (ie. is None)
            params = {
                key: value
                for key, value in config_file_match.groupdict().items()
                if value is not None
                }
            # Send the local and remote endpoint addresses.
            local_host, local_port = get("local", (None, None))
            params["local"] = local_host
            remote_host, remote_port = get("remote", (None, None))
            params["remote"] = remote_host
            params["cluster_uuid"] = get_cluster_uuid()
            d = self.get_config_reader(params)
            d.addErrback(self.get_page_errback, file_name)
            return d
Example #2
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        For example::

        >>> log.msg('Hello, world.')

        In particular, you MUST avoid the forms::

        >>> log.msg(u'Hello, world.')
        >>> log.msg('Hello ', 'world.')

        These forms work (sometimes) by accident and will be disabled
        entirely in the future.
        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        for i in xrange(len(self.observers) - 1, -1, -1):
            try:
                self.observers[i](actualEventDict)
            except KeyboardInterrupt:
                # Don't swallow keyboard interrupt!
                raise
            except UnicodeEncodeError:
                raise
            except:
                o = self.observers.pop(i)
                err(failure.Failure(),
                    "Log observer %s failed, removing from observer list." % (o,))
Example #3
0
 def eval(self,stackvalue):
     context.get('ctx')['cache'] = False        
     stackvalue = stackvalue +1
     checkstack(stackvalue)
     if len(self.args) != 0:
         raise SSValueError("TODAY takes no arguments")
     return littools.datetimeToSerialDate(datetime.now(self.locale.tz))
Example #4
0
    def logPrefix():
        "logPrefix that adjust to current context"

        logCtx = context.get('system',"-")
        if logCtx is not "-":
            return "%s,%s"%(logCtx,prefix)
        return prefix
Example #5
0
def _contextualize(contextFactory, contextReceiver):
    """
    Invoke a callable with an argument derived from the current execution
    context (L{twisted.python.context}), or automatically created if none is
    yet present in the current context.

    This function, with a better name and documentation, should probably be
    somewhere in L{twisted.python.context}.  Calling context.get() and
    context.call() individually is perilous because you always have to handle
    the case where the value you're looking for isn't present; this idiom
    forces you to supply some behavior for that case.

    @param contextFactory: An object which is both a 0-arg callable and
    hashable; used to look up the value in the context, set the value in the
    context, and create the value (by being called).

    @param contextReceiver: A function that receives the value created or
    identified by contextFactory.  It is a 1-arg callable object, called with
    the result of calling the contextFactory, or retrieving the contextFactory
    from the context.
    """
    value = context.get(contextFactory, _NOT_SPECIFIED)
    if value is not _NOT_SPECIFIED:
        return contextReceiver(value)
    else:
        return context.call({contextFactory: contextFactory()},
                            _contextualize, contextFactory, contextReceiver)
 def test_unsetAfterCall(self):
     """
     After a L{twisted.python.context.call} completes, keys specified in the
     call are no longer associated with the values from that call.
     """
     context.call({"x": "y"}, lambda: None)
     self.assertIsNone(context.get("x"))
Example #7
0
 def dataReceived(self, bytes):
     self.system = context.get(ILogContext)["system"]
     self.transport.write("b")
     # Only close connection if both sides have received data, so
     # that both sides have system set.
     if "b" in bytes:
         self.transport.loseConnection()
 def test_setDefault(self):
     """
     A default value may be set for a key in the context using
     L{twisted.python.context.setDefault}.
     """
     key = object()
     self.addCleanup(context.defaultContextDict.pop, key, None)
     context.setDefault(key, "y")
     self.assertEqual("y", context.get(key))
Example #9
0
 def test_setDefault(self):
     """
     A default value may be set for a key in the context using
     L{twisted.python.context.setDefault}.
     """
     key = object()
     self.addCleanup(context.defaultContextDict.pop, key, None)
     context.setDefault(key, "y")
     self.assertEqual("y", context.get(key))
Example #10
0
 def broadcast(self):
     """
     Don't really broadcast.  Add this event to the events which will be
     sent when the action (or whatever) execution transaction is committed
     successfully.
     """
     broadcaster = context.get(iimaginary.ITransactionalEventBroadcaster)
     if broadcaster is not None:
         broadcaster.addEvent(self.reify())
     else:
         self.reify()()
Example #11
0
 def broadcast(self):
     """
     Don't really broadcast.  Add this event to the events which will be
     sent when the action (or whatever) execution transaction is committed
     successfully.
     """
     broadcaster = context.get(iimaginary.ITransactionalEventBroadcaster)
     if broadcaster is not None:
         broadcaster.addEvent(self.reify())
     else:
         self.reify()()
Example #12
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        The message should be a native string, i.e. bytes on Python 2 and
        Unicode on Python 3. For compatibility with both use the native string
        syntax, for example::

        >>> from twisted.python import log
        >>> log.msg('Hello, world.')

        You MUST avoid passing in Unicode on Python 2, and the form::

        >>> log.msg('Hello ', 'world.')

        This form only works (sometimes) by accident.

        Keyword arguments will be converted into items in the event
        dict that is passed to L{ILogObserver} implementations.
        Each implementation, in turn, can define keys that are used
        by it specifically, in addition to common keys listed at
        L{ILogObserver.__call__}.

        For example, to set the C{system} parameter while logging
        a message::

        >>> log.msg('Started', system='Foo')

        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        for i in range(len(self.observers) - 1, -1, -1):
            try:
                self.observers[i](actualEventDict)
            except KeyboardInterrupt:
                # Don't swallow keyboard interrupt!
                raise
            except UnicodeEncodeError:
                raise
            except:
                observer = self.observers[i]
                self.observers[i] = lambda event: None
                try:
                    self._err(failure.Failure(),
                              "Log observer %s failed." % (observer, ))
                except:
                    # Sometimes err() will throw an exception,
                    # e.g. RuntimeError due to blowing the stack; if that
                    # happens, there's not much we can do...
                    pass
                self.observers[i] = observer
Example #13
0
File: tftp.py Project: zhangrb/maas
def get_remote_address():
    """Return the ``(host, port)`` for the remote side of a TFTP transfer.

    This is important on a cluster controller that manages multiple networks.

    This is populated by ``python-tx-tftp``, and is only available in
    ``IBackend.get_reader()`` and ``IBackend.get_writer()``

    :return: A 2-tuple containing the address and port for the remote side of
        the transfer.
    """
    return extract_address(get("remote"))
Example #14
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        The message should be a native string, i.e. bytes on Python 2 and
        Unicode on Python 3. For compatibility with both use the native string
        syntax, for example::

        >>> from twisted.python import log
        >>> log.msg('Hello, world.')

        You MUST avoid passing in Unicode on Python 2, and the form::

        >>> log.msg('Hello ', 'world.')

        This form only works (sometimes) by accident.

        Keyword arguments will be converted into items in the event
        dict that is passed to L{ILogObserver} implementations.
        Each implementation, in turn, can define keys that are used
        by it specifically, in addition to common keys listed at
        L{ILogObserver.__call__}.

        For example, to set the C{system} parameter while logging
        a message::

        >>> log.msg('Started', system='Foo')

        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        for i in range(len(self.observers) - 1, -1, -1):
            try:
                self.observers[i](actualEventDict)
            except KeyboardInterrupt:
                # Don't swallow keyboard interrupt!
                raise
            except UnicodeEncodeError:
                raise
            except:
                observer = self.observers[i]
                self.observers[i] = lambda event: None
                try:
                    self._err(failure.Failure(),
                        "Log observer %s failed." % (observer,))
                except:
                    # Sometimes err() will throw an exception,
                    # e.g. RuntimeError due to blowing the stack; if that
                    # happens, there's not much we can do...
                    pass
                self.observers[i] = observer
Example #15
0
def log(level, message, system=None):
    """
    Write a message to the Twisted log with an appropriate prefix, assuming it
    meets our verbosity criteria.
    """
    if not system:
        system = context.get(twisted_log.ILogContext)['system']
    if level >= LEVEL:
        if level >= Levels.WARNING:
            twisted_log.msg(message, system="WARNING %s" % (system, ))
        elif level >= Levels.INFO:
            twisted_log.msg(message, system="INFO %s" % (system, ))
        else:
            twisted_log.msg(message, system="DEBUG %s" % (system, ))
Example #16
0
def log(level, message, system=None):
    """
    Write a message to the Twisted log with an appropriate prefix, assuming it
    meets our verbosity criteria.
    """
    if not system:
        system = context.get(twisted_log.ILogContext)['system']
    if level >= LEVEL:
        if level >= Levels.WARNING:
            twisted_log.msg(message, system="WARNING %s" % (system,))
        elif level >= Levels.INFO:
            twisted_log.msg(message, system="INFO %s" % (system,))
        else:
            twisted_log.msg(message, system="DEBUG %s" % (system,))
Example #17
0
    def eval(self,stackvalue):
        stackvalue = stackvalue +1
        checkstack(stackvalue)
        if len(self.args) != 0:
            raise BadArgumentsError()            

        # implement the randomness designed to pass the diehard test
        # (what excel does in modern versions)
        # see http://support.microsoft.com/default.aspx?scid=kb;en-us;828795&Product=xl2003

        # create an array of 3 items and sum them and take the fractional value.
        # we cast to a float because of type checking by other methods

        # never cache this value
        context.get('ctx')['cache'] = False
        return float(numpy.modf(numpy.random.rand(3).sum())[0])
Example #18
0
    def legacyEvent(self, *message: str, **values: object) -> legacyLog.EventDict:
        """
        Return a basic old-style event as would be created by L{legacyLog.msg}.

        @param message: a message event value in the legacy event format
        @param values: additional event values in the legacy event format

        @return: a legacy event
        """
        event = (context.get(legacyLog.ILogContext) or {}).copy()
        event.update(values)
        event["message"] = message
        event["time"] = time()
        if "isError" not in event:
            event["isError"] = 0
        return event
Example #19
0
    def legacyEvent(self, *message, **values):
        """
        Return a basic old-style event as would be created by L{legacyLog.msg}.

        @param message: a message event value in the legacy event format
        @type message: L{tuple} of L{bytes}

        @param values: additional event values in the legacy event format
        @type event: L{dict}

        @return: a legacy event
        """
        event = (context.get(legacyLog.ILogContext) or {}).copy()
        event.update(values)
        event["message"] = message
        event["time"] = time()
        return event
Example #20
0
    def legacyEvent(self, *message, **values):
        """
        Return a basic old-style event as would be created by L{legacyLog.msg}.

        @param message: a message event value in the legacy event format
        @type message: L{tuple} of L{bytes}

        @param values: additional event values in the legacy event format
        @type event: L{dict}

        @return: a legacy event
        """
        event = (context.get(legacyLog.ILogContext) or {}).copy()
        event.update(values)
        event["message"] = message
        event["time"] = time()
        if "isError" not in event:
            event["isError"] = 0
        return event
Example #21
0
    def eval(self,stackvalue):
        stackvalue = stackvalue +1
        checkstack(stackvalue)        

        arglen = len(self.args)
        if arglen > 1:
            raise WrongNumArgumentsError('COLUMN')


        if arglen == 0:
            cellI = context.get('ctx')['cell']
            return int(cellI.cellHandle.col)
        else:
            check = self.args[0]
            if isinstance(check,CellRef):
                return int(check.cellHandle.col)
            elif isinstance(check,RangeMixin):
                return int(check.getCellRange()._ulCol)
            else:
                raise BadArgumentsError()
Example #22
0
    def _newOperation(self, label, deferred):
        """
        Helper to emit a log event when a new operation is started and
        another one when it completes.
        """
        # If this is a scheduled request, record the lag in the
        # scheduling now so it can be reported when the response is
        # received.
        lag = context.get('lag', None)

        before = self._reactor.seconds()
        msg(
            type="operation",
            phase="start",
            user=self._client.record.uid,
            client_type=self._client.title,
            client_id=self._client._client_id,
            label=label,
            lag=lag,
        )

        def finished(passthrough):
            success = not isinstance(passthrough, Failure)
            if not success:
                passthrough.trap(IncorrectResponseCode)
                passthrough = passthrough.value.response
            after = self._reactor.seconds()
            msg(
                type="operation",
                phase="end",
                duration=after - before,
                user=self._client.record.uid,
                client_type=self._client.title,
                client_id=self._client._client_id,
                label=label,
                success=success,
            )
            return passthrough

        deferred.addBoth(finished)
        return deferred
Example #23
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        The message should be a native string, i.e. bytes on Python 2 and
        Unicode on Python 3. For compatibility with both use the native string
        syntax, for example::

        >>> log.msg('Hello, world.')

        You MUST avoid passing in Unicode on Python 2, and the form::

        >>> log.msg('Hello ', 'world.')

        This form only works (sometimes) by accident.
        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        for i in range(len(self.observers) - 1, -1, -1):
            try:
                self.observers[i](actualEventDict)
            except KeyboardInterrupt:
                # Don't swallow keyboard interrupt!
                raise
            except UnicodeEncodeError:
                raise
            except:
                observer = self.observers[i]
                self.observers[i] = lambda event: None
                try:
                    self._err(failure.Failure(),
                        "Log observer %s failed." % (observer,))
                except:
                    # Sometimes err() will throw an exception,
                    # e.g. RuntimeError due to blowing the stack; if that
                    # happens, there's not much we can do...
                    pass
                self.observers[i] = observer
Example #24
0
    def _newOperation(self, label, deferred):
        """
        Helper to emit a log event when a new operation is started and
        another one when it completes.
        """
        # If this is a scheduled request, record the lag in the
        # scheduling now so it can be reported when the response is
        # received.
        lag = context.get('lag', None)

        before = self._reactor.seconds()
        msg(
            type="operation",
            phase="start",
            user=self._client.record.uid,
            client_type=self._client.title,
            client_id=self._client._client_id,
            label=label,
            lag=lag,
        )

        def finished(passthrough):
            success = not isinstance(passthrough, Failure)
            if not success:
                passthrough.trap(IncorrectResponseCode)
                passthrough = passthrough.value.response
            after = self._reactor.seconds()
            msg(
                type="operation",
                phase="end",
                duration=after - before,
                user=self._client.record.uid,
                client_type=self._client.title,
                client_id=self._client._client_id,
                label=label,
                success=success,
            )
            return passthrough
        deferred.addBoth(finished)
        return deferred
Example #25
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        For example::

        >>> log.msg('Hello, world.')

        In particular, you MUST avoid the forms::

        >>> log.msg(u'Hello, world.')
        >>> log.msg('Hello ', 'world.')

        These forms work (sometimes) by accident and will be disabled
        entirely in the future.
        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        for i in xrange(len(self.observers) - 1, -1, -1):
            try:
                self.observers[i](actualEventDict)
            except KeyboardInterrupt:
                # Don't swallow keyboard interrupt!
                raise
            except UnicodeEncodeError:
                raise
            except:
                observer = self.observers[i]
                self.observers[i] = lambda event: None
                try:
                    self._err(failure.Failure(),
                        "Log observer %s failed." % (observer,))
                except:
                    # Sometimes err() will throw an exception,
                    # e.g. RuntimeError due to blowing the stack; if that
                    # happens, there's not much we can do...
                    pass
                self.observers[i] = observer
Example #26
0
    def msg(self, *message, **kw):
        """
        Log a new message.

        The message should be a native string, i.e. bytes on Python 2 and
        Unicode on Python 3. For compatibility with both use the native string
        syntax, for example::

            >>> log.msg('Hello, world.')

        You MUST avoid passing in Unicode on Python 2, and the form::

            >>> log.msg('Hello ', 'world.')

        This form only works (sometimes) by accident.

        Keyword arguments will be converted into items in the event
        dict that is passed to L{ILogObserver} implementations.
        Each implementation, in turn, can define keys that are used
        by it specifically, in addition to common keys listed at
        L{ILogObserver.__call__}.

        For example, to set the C{system} parameter while logging
        a message::

        >>> log.msg('Started', system='Foo')

        """
        actualEventDict = cast(EventDict, (context.get(ILogContext)
                                           or {}).copy())
        actualEventDict.update(kw)
        actualEventDict["message"] = message
        actualEventDict["time"] = time.time()
        if "isError" not in actualEventDict:
            actualEventDict["isError"] = 0

        _publishNew(self._publishPublisher, actualEventDict, textFromEventDict)
    def msg(self, *message, **kw):
        """
        Log a new message.

        The message should be a native string, i.e. bytes on Python 2 and
        Unicode on Python 3. For compatibility with both use the native string
        syntax, for example::

            >>> log.msg('Hello, world.')

        You MUST avoid passing in Unicode on Python 2, and the form::

            >>> log.msg('Hello ', 'world.')

        This form only works (sometimes) by accident.

        Keyword arguments will be converted into items in the event
        dict that is passed to L{ILogObserver} implementations.
        Each implementation, in turn, can define keys that are used
        by it specifically, in addition to common keys listed at
        L{ILogObserver.__call__}.

        For example, to set the C{system} parameter while logging
        a message::

        >>> log.msg('Started', system='Foo')

        """
        actualEventDict = (context.get(ILogContext) or {}).copy()
        actualEventDict.update(kw)
        actualEventDict['message'] = message
        actualEventDict['time'] = time.time()
        if "isError" not in actualEventDict:
            actualEventDict["isError"] = 0

        _publishNew(self._publishPublisher, actualEventDict, textFromEventDict)
Example #28
0
def callWithContext(ctx, func, *args, **kw):
    newCtx = context.get(ILogContext).copy()
    newCtx.update(ctx)
    return context.call({ILogContext: newCtx}, func, *args, **kw)
Example #29
0
 def __init__(self, args, names):
     super(CapturedContext, self).__init__(*args)
     self.context = dict((name, context.get(name)) for name in names)
Example #30
0
 def __init__(self, args, names):
     super(CapturedContext, self).__init__(*args)
     self.context = {name: context.get(name) for name in names}
Example #31
0
def _cssRewriter(topLevelBF, path, registry):
	"""
	C{path} is a C{str} representing the absolute path of the .css file.
	"""
	request = context.get('_BetterFile_last_request')
	return CSSResource(topLevelBF, request, path)
Example #32
0
def _cssRewriter(topLevelBF, path, registry):
    """
	C{path} is a C{str} representing the absolute path of the .css file.
	"""
    request = context.get('_BetterFile_last_request')
    return CSSResource(topLevelBF, request, path)
Example #33
0
def callWithContext(ctx, func, *args, **kw):
    newCtx = context.get(ILogContext).copy()
    newCtx.update(ctx)
    return context.call({ILogContext: newCtx}, func, *args, **kw)
Example #34
0
 def dataReceived(self, bytes):
     self.system.callback(context.get(ILogContext)["system"])
Example #35
0
 def decorate(*args, **kw):
     registry = context.get(IAdapterRegistry)
     if registry is not None:
         return getattr(registry, callable.__name__)(*args, **kw)
     return callable(*args, **kw)
Example #36
0
 def datagramReceived(self, bytes, addr):
     if self.system is not None:
         system = self.system
         self.system = None
         system.callback(context.get(ILogContext)["system"])
 def testBasicContext(self):
     self.assertEquals(context.get("x"), None)
     self.assertEquals(context.call({"x": "y"}, context.get, "x"), "y")
     self.assertEquals(context.get("x"), None)
 def datagramReceived(self, bytes, addr):
     if self.system is not None:
         system = self.system
         self.system = None
         system.callback(context.get(ILogContext)["system"])
Example #39
0
 def test_notPresentIfNotSet(self):
     """
     Arbitrary keys which have not been set in the context have an associated
     value of L{None}.
     """
     self.assertIsNone(context.get("x"))
Example #40
0
File: lwc.py Project: braams/shtoom
 def decorate(*args, **kw):
     registry = context.get(IAdapterRegistry)
     if registry is not None:
         return getattr(registry, callable.__name__)(*args, **kw)
     return callable(*args, **kw)
Example #41
0
 def func(self):
     # never cache the value of NOW, it is always 
     context.get('ctx')['cache'] = False
     if len(self.args) != 0:
         raise SSValueError("NOW takes no arguments")
     return littools.datetimeToSerialDate(datetime.now(self.locale.tz))
Example #42
0
 def something():
     something.who = get("worker")
Example #43
0
 def test_notPresentIfNotSet(self):
     """
     Arbitrary keys which have not been set in the context have an associated
     value of L{None}.
     """
     self.assertIsNone(context.get("x"))
Example #44
0
def gettext(string):
    return context.get(
        'translations', default=support.NullTranslations()).gettext(string)
Example #45
0
 def something():
     something.who = get("worker")
Example #46
0
def gettext(string):
    return context.get('translations',
                       default=support.NullTranslations()).gettext(string)
Example #47
0
 def testBasicContext(self):
     self.assertEquals(context.get("x"), None)
     self.assertEquals(context.call({"x": "y"}, context.get, "x"), "y")
     self.assertEquals(context.get("x"), None)