twisted.python.util.untilConcludes

Here are the examples of the python api twisted.python.util.untilConcludes taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

26 Examples 7

Example 1

Project: greplin-exception-catcher Source File: twistedLog.py
Function: write
  def write(self, output):
    """Write a GEC error report, making sure we do not overwrite an existing one
    """
    while True:
      filename = os.path.join(self.__path, str(uuid.uuid4()) + '.gec.json')
      if not os.path.exists(filename):
        with open(filename, 'w') as f:
          util.untilConcludes(f.write, output)
          util.untilConcludes(f.flush)
        break

Example 2

Project: greplin-exception-catcher Source File: twistedLog.py
Function: write
  def write(self, output):
    """Write a gec error report, possibly overwriting a previous one."""
    self.errorId = (self.errorId + 1) % GentleGecLogObserver.MAX_ERRORS
    filename = os.path.join(self._GecHandler__path, '%d-%d.gec.json' % (self.baseName, self.errorId))
    with open(filename, 'w') as f:
      util.untilConcludes(f.write, output)
      util.untilConcludes(f.flush)

Example 3

Project: mythbox Source File: posixbase.py
Function: wake_up
    def wakeUp(self):
        """Send a byte to my connection.
        """
        try:
            util.untilConcludes(self.w.send, 'x')
        except socket.error, (err, msg):
            if err != errno.WSAEWOULDBLOCK:
                raise

Example 4

Project: mythbox Source File: posixbase.py
Function: wake_up
    def wakeUp(self):
        """Write one byte to the pipe, and flush it.
        """
        # We don't use fdesc.writeToFD since we need to distinguish
        # between EINTR (try again) and EAGAIN (do nothing).
        if self.o is not None:
            try:
                util.untilConcludes(os.write, self.o, 'x')
            except OSError, e:
                # XXX There is no unit test for raising the exception
                # for other errnos. See #4285.
                if e.errno != errno.EAGAIN:
                    raise

Example 5

Project: mythbox Source File: log.py
Function: emit
    def emit(self, eventDict):
        text = textFromEventDict(eventDict)
        if text is None:
            return

        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t")}
        msgStr = _safeFormat("[%(system)s] %(text)s\n", fmtDict)

        util.untilConcludes(self.write, timeStr + " " + msgStr)
        util.untilConcludes(self.flush)  # Hoorj!

Example 6

Project: mythbox Source File: test_util.py
Function: testuninterruptably
    def testUninterruptably(self):
        def f(a, b):
            self.calls += 1
            exc = self.exceptions.pop()
            if exc is not None:
                raise exc(errno.EINTR, "Interrupted system call!")
            return a + b

        self.exceptions = [None]
        self.calls = 0
        self.assertEquals(util.untilConcludes(f, 1, 2), 3)
        self.assertEquals(self.calls, 1)

        self.exceptions = [None, OSError, IOError]
        self.calls = 0
        self.assertEquals(util.untilConcludes(f, 2, 3), 5)
        self.assertEquals(self.calls, 3)

Example 7

Project: mythbox Source File: test_fdesc.py
    def _execWithFileDescriptor(self, fObj):
        pid = os.fork()
        if pid == 0:
            try:
                os.execv(sys.executable, [sys.executable, '-c', self.program % (fObj.fileno(),)])
            except:
                import traceback
                traceback.print_exc()
                os._exit(30)
        else:
            # On Linux wait(2) doesn't seem ever able to fail with EINTR but
            # POSIX seems to allow it and on OS X it happens quite a lot.
            return untilConcludes(os.waitpid, pid, 0)[1]

Example 8

Project: mythbox Source File: reporter.py
Function: write
    def _write(self, format, *args):
        """
        Safely write to the reporter's stream.

        @param format: A format string to write.
        @param *args: The arguments for the format string.
        """
        s = str(format)
        assert isinstance(s, type(''))
        if args:
            self._stream.write(s % args)
        else:
            self._stream.write(s)
        untilConcludes(self._stream.flush)

Example 9

Project: vcdm Source File: utils.py
    def emit(self, eventDict):
        if eventDict.get('type') == 'accounting':
            acc_type = eventDict.get('acc_type')
            if acc_type != 'blob_total_size':
                self.aggregated_operations += eventDict.get('amount')
            else:
                timeStr = datetime.datetime.fromtimestamp(eventDict['time']).isoformat()
                avatar = eventDict.get('avatar')
                amount = eventDict.get('amount')
                acc_type = eventDict.get('acc_type')
                start_time = datetime.datetime.fromtimestamp(eventDict.get('start_time', 0)).isoformat()
                end_time = datetime.datetime.fromtimestamp(eventDict.get('end_time', 0)).isoformat()

                msg = "%s %s %s %s %s %s\n" % (timeStr, start_time, end_time, avatar, amount, self.aggregated_operations)
                if conf.getboolean('general', 'send_accounting_to_ur'):
                    send_ogf_ur_accounting(start_time, end_time, avatar, amount, self.aggregated_operations)
                util.untilConcludes(self.write, msg)
                util.untilConcludes(self.flush)
                self.aggregated_operations = 0

Example 10

Project: TwistedBot Source File: posixbase.py
Function: wake_up
    def wakeUp(self):
        """Send a byte to my connection.
        """
        try:
            util.untilConcludes(self.w.send, b'x')
        except socket.error as e:
            if e.args[0] != errno.WSAEWOULDBLOCK:
                raise

Example 11

Project: TwistedBot Source File: posixbase.py
Function: wake_up
    def wakeUp(self):
        """Write one byte to the pipe, and flush it.
        """
        # We don't use fdesc.writeToFD since we need to distinguish
        # between EINTR (try again) and EAGAIN (do nothing).
        if self.o is not None:
            try:
                util.untilConcludes(os.write, self.o, b'x')
            except OSError as e:
                # XXX There is no unit test for raising the exception
                # for other errnos. See #4285.
                if e.errno != errno.EAGAIN:
                    raise

Example 12

Project: TwistedBot Source File: logbot.py
Function: emit
    def emit(self, eventDict):
        if "isError" in eventDict and \
                eventDict["isError"] and \
                "header" not in eventDict:
            eventDict["header"] = "-"
        if "header" not in eventDict:
            return
        text = log.textFromEventDict(eventDict)
        if text is None:
            return
        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'header': eventDict['header'], 'text':
                   text.replace("\n", "\n\t")}
        msgStr = log._safeFormat("[%(header)s] %(text)s\n", fmtDict)
        util.untilConcludes(self.write, timeStr + " " + msgStr)
        util.untilConcludes(self.flush)

Example 13

Project: ccs-calendarserver Source File: xattrprops.py
Function: set
    def set(self, property, uid=None):
        """
        Store the given property as an extended attribute on the wrapped path.

        @param uid: The per-user identifier for per user properties.

        @param property: A L{WebDAVElement} to store.
        """
        key = self._encode(property.qname(), uid)
        value = compress(property.toxml(pretty=False))
        untilConcludes(setitem, self.attrs, key, value)

        # Update the resource because we've modified it
        self.resource.fp.restat()

Example 14

Project: pushproxy Source File: logger.py
Function: emit
    def emit(self, eventDict):
        text = log.textFromEventDict(eventDict)
        if text is None:
            text = '<no text>'

        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'time': timeStr, 'text': text}
        output = log._safeFormat('%(time)s %(text)s\n', fmtDict)
        util.untilConcludes(self.write, output)
        util.untilConcludes(self.flush)

Example 15

Project: SubliminalCollaborator Source File: tcp.py
    def writeSomeData(self, data):
        """
        Write as much as possible of the given data to this TCP connection.

        This sends up to C{self.SEND_LIMIT} bytes from C{data}.  If the
        connection is lost, an exception is returned.  Otherwise, the number
        of bytes successfully written is returned.
        """
        # Limit length of buffer to try to send, because some OSes are too
        # stupid to do so themselves (ahem windows)
        limitedData = buffer(data, 0, self.SEND_LIMIT)

        try:
            return untilConcludes(self.socket.send, limitedData)
        except socket.error, se:
            if se.args[0] in (EWOULDBLOCK, ENOBUFS):
                return 0
            else:
                return main.CONNECTION_LOST

Example 16

Project: SubliminalCollaborator Source File: test_util.py
Function: testuninterruptably
    def testUninterruptably(self):
        def f(a, b):
            self.calls += 1
            exc = self.exceptions.pop()
            if exc is not None:
                raise exc(errno.EINTR, "Interrupted system call!")
            return a + b

        self.exceptions = [None]
        self.calls = 0
        self.assertEqual(util.untilConcludes(f, 1, 2), 3)
        self.assertEqual(self.calls, 1)

        self.exceptions = [None, OSError, IOError]
        self.calls = 0
        self.assertEqual(util.untilConcludes(f, 2, 3), 5)
        self.assertEqual(self.calls, 3)

Example 17

Project: droned Source File: __main__.py
Function: emit
    def emit(self, eventDict):
        """ah, logs should be pretty much as the app intended"""
        text = textFromEventDict(eventDict)
        if text is None: return
        untilConcludes(self.write, text)
        untilConcludes(self.flush)  # Hoorj!

Example 18

Project: hellanzb Source File: Logging.py
    def emit(self, eventDict):
        isFailure = False
        edm = eventDict['message']
        if not edm:
            if eventDict['isError'] and eventDict.has_key('failure'):
                isFailure = True
                text = ((eventDict.get('why') or 'Unhandled Error')
                        + '\n' + eventDict['failure'].getTraceback())
            elif eventDict.has_key('format'):
                text = self._safeFormat(eventDict['format'], eventDict)
            else:
                # we don't know how to log this
                return
        else:
            text = ' '.join(map(reflect.safe_str, edm))

        fmtDict = {'system': eventDict['system'], 'text': text}
        msgStr = self._safeFormat("[%(system)s] %(text)s\n", fmtDict)

        util.untilConcludes(self.debug, msgStr, appendLF=False)
        if isFailure:
            util.untilConcludes(self.error, msgStr, appendLF=False)

Example 19

Project: nodeset.core Source File: log.py
    def emit(self, eventDict):
        text = log.textFromEventDict(eventDict)
        
        if text is None:
            return
        
        if not eventDict.has_key('logLevel'):
            eventDict['logLevel'] = 'INFO'
        elif logLevel.has_key(eventDict['logLevel']):
            if eventDict['logLevel'] < self.level:
                return
            
            eventDict['logLevel'] = logLevel[eventDict['logLevel']]
            
        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t"),
                   'logLevel': eventDict['logLevel']}
        msgStr = log._safeFormat("[%(logLevel)s] [%(system)s] %(text)s\n", fmtDict)

        util.untilConcludes(self.write, timeStr + " " + msgStr)
        util.untilConcludes(self.flush)  # Hoorj!

Example 20

Project: GlobaLeaks Source File: utility.py
Function: emit
    def emit(self, eventDict):
        if 'failure' in eventDict:
            vf = eventDict['failure']
            e_t, e_v, e_tb = vf.type, vf.value, vf.getTracebackObject()
            sys.excepthook(e_t, e_v, e_tb)

        text = twlog.textFromEventDict(eventDict)
        if text is None:
            return

        timeStr = self.formatTime(eventDict['time'])
        fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t")}
        msgStr = twlog._safeFormat("[%(system)s] %(text)s\n", fmtDict)

        if GLLogObserver.suppressed == GLLogObserver.limit_suppressed:
            GLLogObserver.suppressed = 0
            GLLogObserver.limit_suppressed += 5
            GLLogObserver.last_exception_msg = ""

        try:
            # in addition to escape sequence removal on logfiles we also quote html chars
            util.untilConcludes(self.write, timeStr + " " + log_encode_html(msgStr))
            util.untilConcludes(self.flush) # Hoorj!
        except Exception as excep:
            GLLogObserver.suppressed += 1
            GLLogObserver.last_exception_msg = str(excep)

Example 21

Project: mythbox Source File: test_epoll.py
    def test_controlAndWait(self):
        """
        Test waiting on an epoll object which has had some sockets added to
        it.
        """
        client, server = self._connectedPair()

        p = _epoll.epoll(16)
        p._control(_epoll.CTL_ADD, client.fileno(), _epoll.IN | _epoll.OUT |
                   _epoll.ET)
        p._control(_epoll.CTL_ADD, server.fileno(), _epoll.IN | _epoll.OUT |
                   _epoll.ET)

        now = time.time()
        events = untilConcludes(p.wait, 4, 1000)
        then = time.time()
        self.failIf(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), _epoll.OUT),
                    (server.fileno(), _epoll.OUT)]
        expected.sort()

        self.assertEquals(events, expected)

        now = time.time()
        events = untilConcludes(p.wait, 4, 200)
        then = time.time()
        self.failUnless(then - now > 0.1)
        self.failIf(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = untilConcludes(p.wait, 4, 1000)
        then = time.time()
        self.failIf(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), _epoll.IN | _epoll.OUT),
                    (server.fileno(), _epoll.IN | _epoll.OUT)]
        expected.sort()

        self.assertEquals(events, expected)

Example 22

Project: mythbox Source File: reporter.py
Function: write
    def write(self, *a, **kw):
        return untilConcludes(self.original.write, *a, **kw)

Example 23

Project: nagcat Source File: log.py
Function: emit
    def emit(self, event):
        """Twisted log observer event handler"""

        # All exceptions here will normally be lost. Attempt to log
        # any problems to the original stderr in hopes that it is visible
        try:
            if event.get('isError', False):
                level = 0 # ERROR

            # HACK! tcp.Port and udp.Port like to announce themselves
            # loudly but I don't want them to (well UDP at least). This
            # seemed like an easier option than re-implementing things.
            # Also catch all starting/stopping factory noise if it exists.
            elif ('log_level' not in event and event.get('message', None) and
                    (event['message'][0].startswith(
                        'nagcat.plugins.query_ntp.NTPProtocol starting on') or
                    (event['message'][0].startswith('(Port ') and
                     event['message'][0].endswith(' Closed)'))) or
                    event['message'][0].startswith('Starting factory') or
                    event['message'][0].startswith('Stopping factory')):
                level = 3 # DEBUG

            else:
                level = event.get('log_level', 2) # INFO

            if self.log_level < level:
                return

            text = log.textFromEventDict(event)
            text = text.replace("\n", "\n    ")
            date = time.strftime(self.time_format,
                    time.localtime(event.get('time', None)))
            line = "%s [%s] %s\n" % (date, LEVELS[level], text)
            util.untilConcludes(self.log_file.write, line)
            util.untilConcludes(self.log_file.flush)

            # During init stderr is used to provide loud errors to the
            # console in addition to the log file to make things obvious.
            if not self.stdio_stolen and level <= 1:
                util.untilConcludes(self.log_stderr.write, line)
                util.untilConcludes(self.log_stderr.flush)
        except:
            if not self.stdio_stolen:
                self.log_stderr.write("%s" % failure.Failure())

Example 24

Project: SubliminalCollaborator Source File: unix.py
    def writeSomeData(self, data):
        """
        Send as much of C{data} as possible.  Also send any pending file
        descriptors.
        """
        # Make it a programming error to send more file descriptors than you
        # send regular bytes.  Otherwise, due to the limitation mentioned below,
        # we could end up with file descriptors left, but no bytes to send with
        # them, therefore no way to send those file descriptors.
        if len(self._sendmsgQueue) > len(data):
            return error.FileDescriptorOverrun()

        # If there are file descriptors to send, try sending them first, using a
        # little bit of data from the stream-oriented write buffer too.  It is
        # not possible to send a file descriptor without sending some regular
        # data.
        index = 0
        try:
            while index < len(self._sendmsgQueue):
                fd = self._sendmsgQueue[index]
                try:
                    untilConcludes(
                        sendmsg.send1msg, self.socket.fileno(), data[index], 0,
                        _ancillaryDescriptor(fd))
                except socket.error, se:
                    if se.args[0] in (EWOULDBLOCK, ENOBUFS):
                        return index
                    else:
                        return main.CONNECTION_LOST
                else:
                    index += 1
        finally:
            del self._sendmsgQueue[:index]

        # Hand the remaining data to the base implementation.  Avoid slicing in
        # favor of a buffer, in case that happens to be any faster.
        limitedData = buffer(data, index)
        result = self._writeSomeDataBase.writeSomeData(self, limitedData)
        try:
            return index + result
        except TypeError:
            return result

Example 25

Project: SubliminalCollaborator Source File: unix.py
    def doRead(self):
        """
        Calls L{IFileDescriptorReceiver.fileDescriptorReceived} and
        L{IProtocol.dataReceived} with all available data.

        This reads up to C{self.bufferSize} bytes of data from its socket, then
        dispatches the data to protocol callbacks to be handled.  If the
        connection is not lost through an error in the underlying recvmsg(),
        this function will return the result of the dataReceived call.
        """
        try:
            data, flags, ancillary = untilConcludes(
                sendmsg.recv1msg, self.socket.fileno(), 0, self.bufferSize)
        except socket.error, se:
            if se.args[0] == EWOULDBLOCK:
                return
            else:
                return main.CONNECTION_LOST

        if ancillary:
            fd = struct.unpack('i', ancillary[0][2])[0]
            if interfaces.IFileDescriptorReceiver.providedBy(self.protocol):
                self.protocol.fileDescriptorReceived(fd)
            else:
                log.msg(
                    format=(
                        "%(protocolName)s (on %(hostAddress)r) does not "
                        "provide IFileDescriptorReceiver; closing file "
                        "descriptor received (from %(peerAddress)r)."),
                    hostAddress=self.getHost(), peerAddress=self.getPeer(),
                    protocolName=self._getLogPrefix(self.protocol),
                    )
                os.close(fd)

        return self._dataReceived(data)

Example 26

Project: SubliminalCollaborator Source File: test_epoll.py
    def test_controlAndWait(self):
        """
        Test waiting on an epoll object which has had some sockets added to
        it.
        """
        client, server = self._connectedPair()

        p = _epoll.epoll(16)
        p._control(_epoll.CTL_ADD, client.fileno(), _epoll.IN | _epoll.OUT |
                   _epoll.ET)
        p._control(_epoll.CTL_ADD, server.fileno(), _epoll.IN | _epoll.OUT |
                   _epoll.ET)

        now = time.time()
        events = untilConcludes(p.wait, 4, 1000)
        then = time.time()
        self.failIf(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), _epoll.OUT),
                    (server.fileno(), _epoll.OUT)]
        expected.sort()

        self.assertEqual(events, expected)

        now = time.time()
        events = untilConcludes(p.wait, 4, 200)
        then = time.time()
        self.failUnless(then - now > 0.1)
        self.failIf(events)

        client.send("Hello!")
        server.send("world!!!")

        now = time.time()
        events = untilConcludes(p.wait, 4, 1000)
        then = time.time()
        self.failIf(then - now > 0.01)

        events.sort()
        expected = [(client.fileno(), _epoll.IN | _epoll.OUT),
                    (server.fileno(), _epoll.IN | _epoll.OUT)]
        expected.sort()

        self.assertEqual(events, expected)