def _write_line(self, line): """Process individual line morphing, and write it""" # Process per platform newline transformation if line.endswith('\r\n'): line = line[:-2] + os.linesep elif line.endswith('\n'): line = line[:-1] + os.linesep # special case: debug tag is only printed if VERBOSITY is True # NOTE: considering that the python print() function does another # write() to add line separator, we need a self._write_lock # canary to block it if the previous message display aborted. from core import session if line.startswith("[v] ") and not session.Conf.VERBOSITY(): self._write_lock = True return if self._write_lock: self._write_lock = False if line == os.linesep: return line = self.process_tags(line) # handle tagged lines coloration # Write line to stdout, and it's decolorized version on backlog # if standard output is not a tty, decolorize anything. if self._has_backlog: self._backlog.write(decolorize(line)) if not self._has_colors: line = decolorize(line) try: self.outfile.write(line) except UnicodeEncodeError: buf = encoding.encode(line) self.outfile.buffer.write(buf)
def _write_line(self, line): """Process individual line morphing, and write it""" # Process per platform newline transformation if line.endswith('\r\n'): line = line[:-2] + os.linesep elif line.endswith('\n'): line = line[:-1] + os.linesep # special case: debug tag is only printed if VERBOSITY is True # NOTE: considering that the python print() function does another # write() to add line separator, we need a self._write_lock # canary to block it if the previous message display aborted. from core import session if line.startswith("[#] ") and not session.Conf.VERBOSITY(): self._write_lock = True return if self._write_lock: self._write_lock = False if line == os.linesep: return line = self.process_tags(line) # handle tagged lines coloration # Write line to stdout, and it's decolorized version on backlog # if standard output is not a tty, decolorize anything. if self._has_backlog: self._backlog.write(decolorize(line)) if not self._has_colors: line = decolorize(line) try: self.outfile.write(line) except UnicodeEncodeError: buf = encoding.encode(line) self.outfile.buffer.write(buf)
def write(self, data, bin_mode=False): """Write `data` to the file path. If bin_mode is True or data is a bytes() object, data is rawly written to the file in binary mode. Otherwise, data must be of type str(), and a pre treatmen is applied to the buffer, replacing line separators with system specific newline char(s). Note that newlines are automatically replaced by system specific newline char(s) is data is a string. Otherwise, is data is a bytes() buffer, data is rawly written. """ if isinstance(data, bytes): bin_mode = True if not bin_mode: try: lines = data.splitlines() data = os.linesep.join(lines) with open(self, 'w') as file: file.write(data) return except UnicodeDecodeError: bin_mode = True if bin_mode: # if bin_mode, convert str() to bytes() if isinstance(data, str): data = encoding.encode(data) # otherwise, try to convert to bytes() elif not isinstance(data, bytes): data = bytes(data) with open(self, 'wb') as file: file.write(data) return