Exemplo n.º 1
0
    def get_content(self):
        '''
            Getter for the current content.

            Examples:

            >>> Buffer().write('test').content
            'test'

            >>> Buffer(queue=True).write('test').content
            'test'
        '''
        with self._lock:
            if self.file is not None:
                self._content = self.file.content
            elif self.queue:
                self._content = ''
                temp_buffer = []
                while not self.queue.empty():
# # python3.5
# #                     temp_buffer.append(self.queue.get())
                    temp_buffer.append(convert_to_unicode(
                        self.queue.get()))
# #
                    self._content += temp_buffer[-1]
                for content in temp_buffer:
                    self.queue.put(content)
# # python3.5
# #         pass
        if self.force_string and builtins.isinstance(
            self._content, builtins.unicode
        ):
            self._content = convert_to_string(self._content)
# #
        return self._content
Exemplo n.º 2
0
    def write(self, content):
        '''
            Writes content to the current output buffer file. If the current \
            given file "Buffer.file" doesn't exists it will be created.

            **content** - content to write into current buffer instance

            Examples:

            >>> buffer = Buffer(file=__test_folder__.path + 'write')
            >>> buffer.clear() # doctest: +ELLIPSIS
            '...'
            >>> buffer.write('hans') # doctest: +ELLIPSIS
            Object of "Buffer" (file buffered with "...write...nt "hans".
            >>> buffer.content
            'hans'

            >>> buffer = Buffer()
            >>> buffer.write('hans')
            Object of "Buffer" (memory buffered) with content "hans".
            >>> buffer.content
            'hans'
        '''
# # python3.5
# #         pass
        if self.force_string and builtins.isinstance(
            content, builtins.unicode
        ):
            content = convert_to_string(content)
# #
        with self._lock:
            self.last_written = content
            if self.file is not None:
                self.file.content += self.last_written
            elif self.queue:
                self.queue.put(self.last_written)
            else:
                self._content += self.last_written
        return self
Exemplo n.º 3
0
    def __init__(self, *output, **codewords):
# #
        '''
            Writes something to the output buffer or prints to standard \
            output.

            Examples:

            >>> buffer = Buffer()

            >>> Print(native_queue.Queue(), buffer=buffer) # doctest: +ELLIPSIS
            Object of "Print" with "...

            >>> queue1 = native_queue.Queue()
            >>> queue2 = native_queue.Queue()
            >>> queue1.put('hans')
            >>> queue2.put('hans')
            >>> Print(
            ...     queue1, queue2, buffer=buffer, flush=True
            ... ) # doctest: +ELLIPSIS
            Object of "Print" with "...

            >>> Print.default_buffer = Buffer()
            >>> Print('hans', 'hans again') # doctest: +ELLIPSIS
            Object of "Print" with "Object of "Buffer" (mem... "hans hans again
            ".".

            >>> buffer = Buffer()
            >>> Print(
            ...     'hans,', 'peter', end=' and klaus', sep=' ', buffer=buffer
            ... ) # doctest: +ELLIPSIS
            Object of "Print" with "Object of "Buffer" (memory buffered...".".

            >>> buffer # doctest: +ELLIPSIS
            Object ... (memory buffered) with content "hans, peter and klaus".
        '''
        keywords = {
            'replace': self.__class__.replace,
            'start': self.__class__.start,
            'separator': self.__class__.separator,
            'end': self.__class__.end,
            'buffer': self.__class__.default_buffer,
            'flush': codewords.get('replace', False)}
        keywords.update(codewords)
        '''Redirect print output to this buffer.'''
        self.buffer = keywords['buffer']
        output = builtins.list(output)
        for index, out in builtins.enumerate(output):
            if builtins.isinstance(out, native_queue.Queue):
                result = ''
                while not out.empty():
                    if index != 0 and keywords['separator']:
# # python3.5
# #                         result += builtins.str(keywords['separator'])
# #                     result += out.get()
                        result += convert_to_unicode(
                            keywords['separator'])
                    result += convert_to_unicode(out.get())
# #
                output[index] = result
            elif index == 0:
# # python3.5                 output[index] = builtins.str(out)
                output[index] = convert_to_unicode(out)
            else:
# # python3.5
# #                 output[index] = '%s%s' % (builtins.str(
# #                     keywords['separator']
# #                 ), builtins.str(out))
                output[index] = '%s%s' % (convert_to_unicode(
                    keywords['separator']
                ), convert_to_unicode(out))
# #
        line_replace = '\033[1A\r\033[2K' if keywords['replace'] else ''
        output = [keywords['start'], line_replace] + output + [keywords['end']]
# # python3.5
# #         builtins.print(
# #             *output, sep='', end='', file=keywords['buffer'],
# #             flush=keywords['flush'])
        builtins.print(*filter(lambda content: convert_to_string(
            content
        ), output), sep='', end='', file=keywords['buffer'])
        if keywords['flush']:
            sys.stdout.flush()
Exemplo n.º 4
0
    def clear(self, delete=True):
        '''
            Removes the current output buffer content.

            **delete** - indicates whether a file buffer should be deleted or \
                         truncated

            Examples:

            >>> buffer = Buffer(file=__test_folder__.path + 'clear')

            >>> buffer.clear() # doctest: +ELLIPSIS
            '...'
            >>> buffer.write('hans') # doctest: +ELLIPSIS
            Objec...(file buffered with "...clear...with content "hans".

            >>> buffer.clear(False)
            'hans'
            >>> buffer.content
            ''

            >>> buffer = Buffer()
            >>> buffer.write('hans')
            Object of "Buffer" (memory buffered) with content "hans".
            >>> buffer.clear()
            'hans'
            >>> buffer.content
            ''

            >>> buffer = Buffer(queue=True)

            >>> buffer.clear()
            ''

            >>> buffer.write('hans')
            Object of "Buffer" (queue buffered) with content "hans".
            >>> buffer.write('hans')
            Object of "Buffer" (queue buffered) with content "hanshans".
            >>> buffer.clear()
            'hanshans'
            >>> buffer.content
            ''
        '''
        with self._lock:
            if self.file is not None:
                content = self.file.content
                if delete:
                    self.file.remove_file()
                else:
                    self.file.content = ''
            elif self.queue:
                content = ''
                while not self.queue.empty():
                    content += self.queue.get()
            else:
                content = self._content
                self._content = ''
# # python3.5
# #         pass
        if self.force_string:
            self._content = builtins.str()
            content = convert_to_string(content)
# #
        return content