Пример #1
0
    def flush(self):
        """

        :type self: StringIONotifying or StringIO
        """
        StringIO.flush(self)
        self.listener()
Пример #2
0
class LogCapture(ContextDecorator):
    def __enter__(self):
        self.buf = StringIO()

        self.oldLogLevel = log.getEffectiveLevel()
        log.setLevel(logging.INFO)

        self.oldLogger = log.handlers[0]
        log.removeHandler(self.oldLogger)

        self.logHandler = logging.StreamHandler(self.buf)
        formatter = logging.Formatter("[%(levelname)s] %(message)s")
        self.logHandler.setFormatter(formatter)

        log.addHandler(self.logHandler)
        return self

    def __exit__(self, *args):
        # Restore logging level
        log.setLevel(self.oldLogLevel)
        log.removeHandler(self.logHandler)
        log.addHandler(self.oldLogger)

        return False

    def getOutput(self):
        self.logHandler.flush()
        self.buf.flush()

        output = re.sub(r'^\[\w+\]\s+', '', self.buf.getvalue(), flags=re.M)

        return output
Пример #3
0
 def stringify(self, header=None):
     contents = ''
     outputstream = StringIO()
     self.write(outputstream)
     outputstream.flush()
     contents = outputstream.getvalue()
     if header:
         contents = '\n'.join([header, contents, ''])
     return contents
Пример #4
0
 def stringify(self, header=None):
     contents = ''
     outputstream = StringIO()
     self.write(outputstream)
     outputstream.flush()
     contents = outputstream.getvalue()
     if header:
         contents = '\n'.join([header, contents, ''])
     return contents
Пример #5
0
 def cli():
     stdout_io = StringIO()
     spinner = click_spinner.Spinner(force=True, stream=stdout_io)
     spinner.start()
     time.sleep(1)  # allow time for a few spins
     spinner.stop()
     stdout_io.flush()
     stdout_str = stdout_io.getvalue()
     assert len(stdout_str) > 0
Пример #6
0
    def from_directories(cls, directories, pattern=None, ignore=(), write=None, relative_to=None):
        """
        convert directories to a simple manifest; returns ManifestParser instance

        pattern -- shell pattern (glob) or patterns of filenames to match
        ignore -- directory names to ignore
        write -- filename or file-like object of manifests to write;
                 if `None` then a StringIO instance will be created
        relative_to -- write paths relative to this path;
                       if false then the paths are absolute
        """

        # determine output
        opened_manifest_file = None  # name of opened manifest file
        absolute = not relative_to  # whether to output absolute path names as names
        if isinstance(write, string_types):
            opened_manifest_file = write
            write = open(write, 'w')
        if write is None:
            write = StringIO()

        # walk the directories, generating manifests
        def callback(directory, dirpath, dirnames, filenames):

            # absolute paths
            filenames = [os.path.join(dirpath, filename)
                         for filename in filenames]
            # ensure new manifest isn't added
            filenames = [filename for filename in filenames
                         if filename != opened_manifest_file]
            # normalize paths
            if not absolute and relative_to:
                filenames = [relpath(filename, relative_to)
                             for filename in filenames]

            # write to manifest
            write_content = '\n'.join([
                '[{}]'.format(denormalize_path(filename)) for filename in filenames
            ])
            print(write_content, file=write)

        cls._walk_directories(directories, callback, pattern=pattern, ignore=ignore)

        if opened_manifest_file:
            # close file
            write.close()
            manifests = [opened_manifest_file]
        else:
            # manifests/write is a file-like object;
            # rewind buffer
            write.flush()
            write.seek(0)
            manifests = [write]

        # make a ManifestParser instance
        return cls(manifests=manifests)
Пример #7
0
    def connect(self):
        """
        打开websocket连接,通过前端传入的参数尝试连接ssh主机
        :return:
        """
        self.accept()
        query_string = self.scope.get('query_string')
        ssh_args = QueryDict(query_string=query_string, encoding='utf-8')

        width = ssh_args.get('width')
        height = ssh_args.get('height')
        port = ssh_args.get('port')

        width = int(width)
        height = int(height)
        port = int(port)

        auth = ssh_args.get('auth')
        ssh_key_name = ssh_args.get('ssh_key')
        password = ssh_args.get('password')

        host = ssh_args.get('host')
        user = ssh_args.get('user')

        if password:
            password = base64.b64decode(password).decode('utf-8')
        else:
            password = None

        ssh_connect_dict = {
            'host': host,
            'user': user,
            'port': port,
            'timeout': 30,
            'pty_width': width,
            'pty_height': height,
            'password': password
        }

        if auth == 'key':
            ssh_key_file = os.path.join(TMP_DIR, ssh_key_name)
            try:
                with open(ssh_key_file, 'r') as f:
                    ssh_key = f.read()
                string_io = StringIO()
                string_io.write(ssh_key)
                string_io.flush()
                string_io.seek(0)
                ssh_connect_dict['ssh_key'] = string_io

                os.remove(ssh_key_file)
            except Exception:
                self.disconnect(self)

        self.ssh.connect(**ssh_connect_dict)
Пример #8
0
class message(object):
    """
    Generic pretty printer with redirection.
    It also suports buffering using bufferize() and flush().
    """
    def __init__(self):
        self.out = sys.stdout
        self.buffering = 0

    def bufferize(self, f=None):
        """Activate message's bufferization, can also be used as a decorater."""

        if f is not None:

            @functools.wraps(f)
            def wrapper(*args, **kwargs):
                self.bufferize()
                try:
                    f(*args, **kwargs)
                finally:
                    self.flush()

            return wrapper

        # If we are still using stdio we need to change it.
        if self.buffering == 0:
            self.out = StringIO()
        self.buffering += 1

    def flush(self):
        if self.buffering == 0:
            raise ValueError(
                "Tried to flush a message that is not bufferising.")
        self.buffering -= 1

        # We only need to flush if this is the lowest recursion level.
        if self.buffering == 0:
            self.out.flush()
            sys.stdout.write(self.out.getvalue())
            self.out = sys.stdout

    def __call__(self, text, color=None, attrib=None, teefd=None):
        if not teefd:
            teefd = config.Option.get("_teefd")

        if isinstance(text, six.string_types) and "\x00" not in text:
            print(colorize(text, color, attrib), file=self.out)
            if teefd:
                print(colorize(text, color, attrib), file=teefd)
        else:
            pprint.pprint(text, self.out)
            if teefd:
                pprint.pprint(text, teefd)
Пример #9
0
 def cli():
     stdout_io = StringIO()
     saved_stdout = sys.stdout
     sys.stdout = stdout_io  # redirect stdout to a string buffer
     spinner = click_spinner.Spinner(disable=True)
     spinner.start()
     time.sleep(1)  # allow time for doing nothing
     spinner.stop()
     sys.stdout = saved_stdout
     stdout_io.flush()
     stdout_str = stdout_io.getvalue()
     assert len(stdout_str) == 0
Пример #10
0
class message(object):
    """
    Generic pretty printer with redirection.
    It also suports buffering using bufferize() and flush().
    """

    def __init__(self):
        self.out = sys.stdout
        self.buffering = 0

    def bufferize(self, f=None):
        """Activate message's bufferization, can also be used as a decorater."""

        if f is not None:
            @functools.wraps(f)
            def wrapper(*args, **kwargs):
                self.bufferize()
                f(*args, **kwargs)
                self.flush()

            return wrapper

        # If we are still using stdio we need to change it.
        if not self.buffering:
            self.out = StringIO()
        self.buffering += 1

    def flush(self):
        if not self.buffering:
            raise ValueError("Tried to flush a message that is not bufferising.")
        self.buffering -= 1

        # We only need to flush if this is the lowest recursion level.
        if not self.buffering:
            self.out.flush()
            sys.stdout.write(self.out.getvalue())
            self.out = sys.stdout

    def __call__(self, text, color=None, attrib=None, teefd=None):
        if not teefd:
            teefd = config.Option.get("_teefd")

        if isinstance(text, six.string_types) and "\x00" not in text:
            print(colorize(text, color, attrib), file=self.out)
            if teefd:
                print(colorize(text, color, attrib), file=teefd)
        else:
            pprint.pprint(text, self.out)
            if teefd:
                pprint.pprint(text, teefd)
Пример #11
0
def captured_logging(name=None):
    buffer = StringIO()
    logger = logging.getLogger(name)
    handlers = logger.handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)
    handler = logging.StreamHandler(buffer)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    yield buffer
    buffer.flush()
    logger.removeHandler(handler)
    for handler in handlers:
        logger.addHandler(handler)
Пример #12
0
def captured_logging(name=None):
    buffer = StringIO()
    logger = logging.getLogger(name)
    handlers = logger.handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)
    handler = logging.StreamHandler(buffer)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    yield buffer
    buffer.flush()
    logger.removeHandler(handler)
    for handler in handlers:
        logger.addHandler(handler)
Пример #13
0
class Stream(object):

    def __init__(self):
        self._stringio = StringIO()
        self._stringio.isatty = sys.stdout.isatty

    def write(self, msg, color='default', newline=True):
        if newline:
            msg += '\n'
        color_print(msg, color, file=self._stringio)
        self._stringio.flush()

    def readout(self):
        self._stringio.seek(0)
        return self._stringio.read()
Пример #14
0
def make_audio(tensor, sample_rate, length_frames, num_channels):
    """Convert an numpy representation audio to Audio protobuf"""
    output = StringIO()
    wav_out = wave.open(output, "w")
    wav_out.setframerate(float(sample_rate))
    wav_out.setsampwidth(2)
    wav_out.setcomptype('NONE', 'not compressed')
    wav_out.setnchannels(num_channels)
    wav_out.writeframes(tensor.astype("int16").tostring())
    wav_out.close()
    output.flush()
    audio_string = output.getvalue()

    return Summary.Audio(sample_rate=float(sample_rate),
                         num_channels=num_channels,
                         length_frames=length_frames,
                         encoded_audio_string=audio_string,
                         content_type="audio/wav")
Пример #15
0
    def render(self, mode='human'):
        outfile = StringIO() if mode == 'ansi' else sys.stdout

        for i in range(0, 3):
            # Extract line from state
            l = self.state[(3 * i):(3 * (i + 1))]

            # For each box on this line, replace numbers with characters
            c = [' ', ' ', ' ']
            for j in range(0, 3):
                c[j] = 'X' if l[j] == 1.0 else 'O' if l[j] == -1.0 else ' '

            outfile.write('{:s}|{:s}|{:s}\n'.format(c[0], c[1], c[2]))
            if i < 2:
                outfile.write('-----\n')

        outfile.write('\n')
        outfile.flush()

        if mode != 'human':
            return outfile
Пример #16
0
class RedirectStdStreams(object):
    def __init__(self):
        self.enabled = True

    def __enter__(self):
        if self.enabled:
            self.strio = StringIO()
            self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
            self.old_stdout.flush()
            self.old_stderr.flush()
            sys.stdout, sys.stderr = self.strio, self.strio

    def __exit__(self, exc_type, exc_value, traceback):
        if self.enabled:
            self.strio.flush()
            if exc_type is not None:
                print(self.strio.getvalue())

            sys.stdout = self.old_stdout
            sys.stderr = self.old_stderr
            self.strio.close()
            del self.strio
Пример #17
0
class LogBuffer(object):

    def __init__(self, logger, logLevel=None):
        if isinstance(logger, logging.Logger):
            self.logger = logger
        else:
            self.logger = logging.getLogger(logger)
        self.buffer = StringIO()
        if logLevel is None:
            self.old_level = None
        else:
            self.old_level = self.logger.getEffectiveLevel()
            self.logger.setLevel(logLevel)

        self.logHandler = logging.StreamHandler(self.buffer)
        self.logger.addHandler(self.logHandler)

    def __del__(self):
        self.close()

    def close(self):
        if self.logger is None:
            return
        if self.old_level is not None:
            self.logger.setLevel(self.old_level)

        self.logger.removeHandler(self.logHandler)
        self.logHandler.flush()
        self.buffer.flush()
        self.logHandler = None
        self.logger = None

    def value(self):
        if self.logHandler is not None:
            self.logHandler.flush();
        self.buffer.flush()
        return self.buffer.getvalue()
Пример #18
0
class LogBuffer(object):

    def __init__(self, logger, logLevel=None):
        if isinstance(logger, logging.Logger):
            self.logger = logger
        else:
            self.logger = logging.getLogger(logger)
        self.buffer = StringIO()
        if logLevel is None:
            self.old_level = None
        else:
            self.old_level = self.logger.getEffectiveLevel()
            self.logger.setLevel(logLevel)

        self.logHandler = logging.StreamHandler(self.buffer)
        self.logger.addHandler(self.logHandler)

    def __del__(self):
        self.close()

    def close(self):
        if self.logger is None:
            return
        if self.old_level is not None:
            self.logger.setLevel(self.old_level)

        self.logger.removeHandler(self.logHandler)
        self.logHandler.flush()
        self.buffer.flush()
        self.logHandler = None
        self.logger = None

    def value(self):
        if self.logHandler is not None:
            self.logHandler.flush();
        self.buffer.flush()
        return self.buffer.getvalue()
Пример #19
0
    def connect(self):
        """
        建立WebSocket连接,并实例化SSHBridge类,在这个对象中建立SSH连接,放在 self.ssh_channel 通道中
        :return:
        """
        # pri00nt('【Web  --websocket-->  WS】建立WebSocket通道,当前连接用户:', self.simple_user)

        self.accept()

        # WebSocket连接成功后,连接ssh
        query_string = self.scope.get('query_string')
        ws_args = QueryDict(query_string=query_string, encoding='utf-8')
        # # pri00nt(ws_args)
        # <QueryDict: {'user': ['admin'], 'host': ['192.168.96.20'], 'port': ['22'], 'auth': ['pwd'], 'pwd': ['ZGphbmdvYWRtaW4='], 'key': [''], 'width': ['113'], 'height': ['43']}>
        # 根据参数判断是否是协作
        team = ws_args.get('team')
        if team:
            self.is_team = True
            self.team_name = "team_{}".format(self.host_id)  # 加到这个通道组
            async_to_sync(self.channel_layer.group_add)(
                self.team_name,
                self.channel_name
            )
            # 用户连接时,同一群组发送消息
            self.send_message_or_team(json.dumps({'flag': 'user', 'message': '用户 {} 已连接本终端'.format(self.simple_user)}))

        width = ws_args.get('width')
        height = ws_args.get('height')
        width = int(500)
        height = int(500)  # ssh连接要求int类型:required argument is an integer

        ssh_connect_dict = {}

        if self.host_id:
            # 指定连接
            # pri00nt('连接的服务器id:', self.host_id)
            if int(self.host_id) == 1:
                ssh_connect_dict = {
                    'host': '120.26.175.79',
                    'user': '******',
                    'port': 22,
                    'timeout': 30,
                    'pty_width': width,
                    'pty_height': height,
                    'pwd': 'AAAAaaaa0'
                }
            else:
                self.close()
                return

        else:
            user = ws_args.get('user')
            host = ws_args.get('host')
            port = ws_args.get('port')
            port = int(port)
            auth = ws_args.get('auth')
            pwd = ws_args.get('pwd')
            if pwd:
                pwd = base64.b64decode(pwd).decode('utf-8')
            sshkey_filename = ws_args.get('sshkey_filename')

            ssh_connect_dict = {
                'host': host,
                'user': user,
                'port': port,
                'timeout': 30,
                'pty_width': width,
                'pty_height': height,
                'pwd': pwd
            }

            if auth == 'key':
                sshkey_file = os.path.join(settings.MEDIA_ROOT, 'sshkey', sshkey_filename)
                if not os.path.exists(sshkey_file):
                    self.send(json.dumps({'flag': 'error', 'message': '密钥文件不存在'}))

                else:
                    try:
                        f = open(sshkey_file, 'r', encoding='utf-8')
                        key = f.read()
                        string_io = StringIO()
                        string_io.write(key)
                        string_io.flush()
                        string_io.seek(0)
                        ssh_connect_dict['key'] = string_io

                        os.remove(sshkey_file)  # 用完之后删除key文件
                    except BaseException as e:
                        # pri00nt('打开密钥文件出错', e)
                        pass

        # 建立SSH连接
        self.ssh = SSHBridge(websocket=self, simpleuser=self.simple_user)
        # pri00nt('【WS  --SSHBridge-->  SSH】连接SSH参数:', ssh_connect_dict)
        self.ssh.connect(**ssh_connect_dict)
Пример #20
0
class OutputCapture(object):
    def __init__(self):
        self.saved_outputs = dict()
        self._log_level = logging.INFO
        self._logs = None
        self._logs_handler = None
        self._logger = None
        self._orig_log_level = None

    def set_log_level(self, log_level):
        self._log_level = log_level
        if self._logs_handler is not None:
            self._logs_handler.setLevel(self._log_level)

    def _capture_output_with_name(self, output_name):
        stream = getattr(sys, output_name)
        captured_output = StringIO()
        self.saved_outputs[output_name] = stream
        setattr(sys, output_name, captured_output)
        return captured_output

    def _restore_output_with_name(self, output_name):
        captured_output = getattr(sys, output_name).getvalue()
        setattr(sys, output_name, self.saved_outputs[output_name])
        del self.saved_outputs[output_name]
        return captured_output

    def capture_output(self):
        self._logs = StringIO()
        self._logs_handler = logging.StreamHandler(self._logs)
        self._logs_handler.setLevel(self._log_level)
        self._logger = logging.getLogger()
        self._orig_log_level = self._logger.level
        self._logger.addHandler(self._logs_handler)
        self._logger.setLevel(min(self._log_level, self._orig_log_level))
        return (self._capture_output_with_name('stdout'),
                self._capture_output_with_name('stderr'))

    def restore_output(self):
        self._logger.removeHandler(self._logs_handler)
        self._logger.setLevel(self._orig_log_level)
        self._logs_handler.flush()
        self._logs.flush()
        logs_string = self._logs.getvalue()
        delattr(self, '_logs_handler')
        delattr(self, '_logs')
        return (self._restore_output_with_name('stdout'),
                self._restore_output_with_name('stderr'), logs_string)

    def assert_outputs(self,
                       testcase,
                       function,
                       args=None,
                       kwargs=None,
                       expected_stdout='',
                       expected_stderr='',
                       expected_exception=None,
                       expected_logs=None):
        args = args or []
        kwargs = kwargs or {}
        self.capture_output()
        try:
            if expected_exception:
                return_value = testcase.assertRaises(expected_exception,
                                                     function, *args, **kwargs)
            else:
                return_value = function(*args, **kwargs)
        finally:
            (stdout_string, stderr_string, logs_string) = self.restore_output()

        if hasattr(testcase, 'assertMultiLineEqual'):
            testassert = testcase.assertMultiLineEqual
        else:
            testassert = testcase.assertEqual

        testassert(stdout_string, expected_stdout)
        testassert(stderr_string, expected_stderr)
        if expected_logs is not None:
            testassert(logs_string, expected_logs)
        # This is a little strange, but I don't know where else to return this information.
        return return_value
Пример #21
0
def booking_bpoint_settlement_report(_date):
    try:
        bpoint, bpay, cash = [], [], []
        bpoint.extend([x for x in BpointTransaction.objects.filter(created__date=_date, response_code=0, crn1__startswith=WC_PAYMENT_SYSTEM_PREFIX).exclude(crn1__endswith='_test')])
        bpay.extend([x for x in BpayTransaction.objects.filter(p_date__date=_date, crn__startswith=WC_PAYMENT_SYSTEM_PREFIX).exclude(crn__endswith='_test')])
        cash = CashTransaction.objects.filter(created__date=_date, invoice__reference__startswith=WC_PAYMENT_SYSTEM_PREFIX).exclude(type__in=['move_out','move_in'])

        strIO = StringIO()
        fieldnames = ['Payment Date','Settlement Date','Confirmation Number','Name','Type','Amount','Invoice']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        # BookingInvoice ==> InfringementPenaltyInvoice
        # Booking ==> SanctionOutcome

        for b in bpoint:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)
                try:
                    # booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
                    ip_invoice = InfringementPenaltyInvoice.objects.get(invoice_reference=invoice.reference)
                    sanction_outcome = ip_invoice.infringement_penalty.sanction_outcome
                # except BookingInvoice.DoesNotExist:
                except InfringementPenaltyInvoice.DoesNotExist:
                    pass

                # try:
                #     application_fee = ApplicationFeeInvoice.objects.get(invoice_reference=invoice.reference).application_fee
                # except ApplicationFeeInvoice.DoesNotExist:
                #     pass


                # if booking:
                #     b_name = u'{}'.format(booking.proposal.applicant)
                #     created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
                #     settlement_date = invoice.settlement_date.strftime('%d/%m/%Y') if invoice.settlement_date else ''
                #     writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date, booking.admission_number,
                #                  b_name.encode('utf-8'), invoice.get_payment_method_display(), invoice.amount,
                #                  invoice.reference])
                if sanction_outcome:
                    # b_name = u'{}'.format(booking.proposal.applicant)
                    b_name = u'{}'.format(sanction_outcome.get_offender()[0].get_full_name())
                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
                    settlement_date = invoice.settlement_date.strftime('%d/%m/%Y') if invoice.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'),
                        settlement_date,
                        # booking.admission_number,
                        sanction_outcome.lodgement_number,
                        # b_name.encode('utf-8'),
                        b_name,
                        invoice.get_payment_method_display(),
                        invoice.amount,
                        invoice.reference
                    ])
                # elif application_fee:
                #     b_name = u'{}'.format(application_fee.proposal.applicant)
                #     created = timezone.localtime(application_fee.created, pytz.timezone('Australia/Perth'))
                #     settlement_date = invoice.settlement_date.strftime('%d/%m/%Y') if invoice.settlement_date else ''
                #     writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),settlement_date, application_fee.proposal.lodgement_number, b_name.encode('utf-8'),invoice.get_payment_method_display(),invoice.amount,invoice.reference])
                else:
                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.settlement_date.strftime('%d/%m/%Y'),'','',str(b.action),b.amount,invoice.reference])
            except Invoice.DoesNotExist:
                pass
#
#        for b in bpay:
#            booking, invoice = None, None
#            try:
#                invoice = Invoice.objects.get(reference=b.crn)
#                try:
#                    booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
#                except BookingInvoice.DoesNotExist:
#                    pass
#
#                if booking:
#                    b_name = u'{}'.format(booking.proposal.applicant)
#                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
#                    settlement_date = b.p_date.strftime('%d/%m/%Y')
#                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),settlement_date,booking.admission_number,b_name.encode('utf-8'),invoice.get_payment_method_display(),invoice.amount,invoice.reference])
#                else:
#                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.settlement_date.strftime('%d/%m/%Y'),'','',str(b.action),b.amount,invoice.reference])
#            except Invoice.DoesNotExist:
#                pass
#
#
#        for b in cash:
#            booking, invoice = None, None
#            try:
#                invoice = b.invoice
#                try:
#                    booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
#                except BookingInvoice.DoesNotExist:
#                    pass
#
#                if booking:
#                    b_name = u'{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
#                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
#                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),booking.confirmation_number,b_name.encode('utf-8'),str(b.type),b.amount,invoice.reference])
#                else:
#                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),'','',str(b.type),b.amount,invoice.reference])
#            except Invoice.DoesNotExist:
#                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Пример #22
0
class BufferedLogCapture(object):
    """Context manager to buffer logging messages to an internal buffer. Logging to the buffer can be achieved
    either by calling start() and stop(), or using an instances as a context manager.
    If `new_log_level` is passed to __init__, the root logger will be updated to that level while the buffer
    log is active. Please note that this may result in unwanted messages being logged to other handlers while active.
    This buffers data in memory, please be cognizant of logging output volume."""

    capturing = False
    new_log_level = None
    old_log_level = None
    log_handler = None
    buffer = None
    _context_nesting = 0

    def __init__(self,
                 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                 new_log_level=None):
        self.new_log_level = new_log_level
        self.format = format
        self.reset()

    def reset(self):
        """Empty the internal log buffer"""
        self.buffer = StringIO()

    def start(self):
        if self.capturing:
            return

        self.capturing = True
        root_logger = logging.getLogger()
        if self.new_log_level is not None:
            self.old_log_level = root_logger.getEffectiveLevel()
            root_logger.setLevel(self.new_log_level)

        self.log_handler = logging.StreamHandler(self.buffer)
        formatter = logging.Formatter(self.format)
        self.log_handler.setFormatter(formatter)
        root_logger.addHandler(self.log_handler)

    def stop(self):
        if not self.capturing:
            raise NotCapturing()

        self.capturing = False
        root_logger = logging.getLogger()
        if self.old_log_level is not None:
            root_logger.setLevel(self.old_log_level)
            self.old_log_level = None

        root_logger.removeHandler(self.log_handler)
        self.log_handler.flush()
        self.buffer.flush()

    def __enter__(self):
        self.start()
        self._context_nesting += 1

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._context_nesting -= 1
        if self._context_nesting == 0:
            self.stop()

    def getvalue(self):
        return self.buffer.getvalue()