def _print_message(string, custom_context, debug_only, prefix, color, logging_category, start_time=time.time()): context = frame_info(2) # strip the package name if context.count(".") > 1: context = context.split(".", 1)[-1] if custom_context: context = "%s(%r)" % (context, custom_context) timestr = ("%2.3f" % (time.time() - start_time))[-6:] info = "%s: %s: %s:" % (getattr(Colorise, color)(prefix), Colorise.magenta(timestr), Colorise.blue(context)) lines = string.splitlines() if len(lines) > 1: string = os.linesep.join([info] + [" " * 4 + l for l in lines]) else: string = info + " " + lines[0] if (not debug_only or const.DEBUG) and not is_py2exe_window(): file_ = sys.stderr if not file_.isatty(): string = strip_color(string) print_(string, file=file_) logging.log(strip_color(string), logging_category)
def _print_message(string, custom_context, debug_only, prefix, color, logging_category, start_time=time.time()): context = frame_info(2) # strip the package name if context.count(".") > 1: context = context.split(".", 1)[-1] if custom_context: context = "%s(%r)" % (context, custom_context) timestr = ("%2.3f" % (time.time() - start_time))[-6:] info = "%s: %s: %s:" % ( getattr(Colorise, color)(prefix), Colorise.magenta(timestr), Colorise.blue(context)) lines = string.splitlines() if len(lines) > 1: string = os.linesep.join([info] + [" " * 4 + l for l in lines]) else: string = info + " " + lines[0] if not debug_only or const.DEBUG: file_ = sys.stderr if _should_write_to_file(file_): if not _supports_ansi_escapes(file_): string = strip_color(string) print_(string, file=file_, flush=True) ql_logging.log(strip_color(string), logging_category)
def test_print_defaults_none(): # python 3 print takes None as default, try to do the same with capture_output() as (out, err): print_("foo", "bar") first = out.getvalue() with capture_output() as (out, err): print_("foo", "bar", end=None, sep=None, file=None) assert out.getvalue() == first
def test_print_ansi(): for i in range(1, 110): print_("\033[%dm" % i, end="") other = [ "\033[A", "\033[B", "\033[C", "\033[D", "\033[s", "\033[u", "\033[H", "\033[f" ] for c in other: print_(c, end="")
def test_print_windows(): f = BytesIO() print_(u"öäü\ud83d", file=f) out = f.getvalue() assert isinstance(out, bytes) assert out == b"\xc3\xb6\xc3\xa4\xc3\xbc\xed\xa0\xbd" + linesepb f = TextIO() print_(u"öäü\ud83d", file=f) out = f.getvalue() assert isinstance(out, text_type) assert out == u"öäü\ud83d" + linesepu
def test_print_strict_strio(): f = StringIO() real_write = f.write def strict_write(data): if not isinstance(data, text_type): raise TypeError real_write(data.encode("utf-8").decode("utf-8")) f.write = strict_write print_(b"\xff\xfe".decode(_encoding, "surrogateescape"), file=f) assert f.getvalue() == u"\ufffd\ufffd\n"
def _print_message(string, custom_context, debug_only, prefix, color, logging_category, start_time=time.time()): if not isinstance(string, (text_type, fsnative)): string = text_type(string) context = frame_info(2) # strip the package name if context.count(".") > 1: context = context.split(".", 1)[-1] if custom_context: context = "%s(%r)" % (context, custom_context) timestr = ("%2.3f" % (time.time() - start_time))[-6:] info = "%s: %s: %s:" % (getattr(Colorise, color)(prefix), Colorise.magenta(timestr), Colorise.blue(context)) lines = string.splitlines() if len(lines) > 1: string = os.linesep.join([info] + [" " * 4 + l for l in lines]) else: string = info + " " + lines[0] if not debug_only or const.DEBUG: file_ = sys.stderr if _should_write_to_file(file_): if not _supports_ansi_escape_codes(file_): string = strip_color(string) try: print_(string, file=file_, flush=True) except (IOError, OSError) as e: if e.errno == errno.EIO: # When we are started in a terminal with --debug and the # terminal gets closed we lose stdio/err before we stop # printing debug message, resulting in EIO and aborting the # exit process -> Just ignore it. pass else: raise ql_logging.log(strip_color(string), logging_category)
def _print_message(string, custom_context, debug_only, prefix, color, logging_category, start_time=time.time()): if not isinstance(string, (text_type, fsnative)): string = text_type(string) context = frame_info(2) # strip the package name if context.count(".") > 1: context = context.split(".", 1)[-1] if custom_context: context = "%s(%r)" % (context, custom_context) timestr = ("%2.3f" % (time.time() - start_time))[-6:] info = "%s: %s: %s:" % ( getattr(Colorise, color)(prefix), Colorise.magenta(timestr), Colorise.blue(context)) lines = string.splitlines() if len(lines) > 1: string = os.linesep.join([info] + [" " * 4 + l for l in lines]) else: string = info + " " + lines[0] if not debug_only or const.DEBUG: file_ = sys.stderr if _should_write_to_file(file_): if not _supports_ansi_escapes(file_): string = strip_color(string) try: print_(string, file=file_, flush=True) except (IOError, OSError) as e: if e.errno == errno.EIO: # When we are started in a terminal with --debug and the # terminal gets closed we lose stdio/err before we stop # printing debug message, resulting in EIO and aborting the # exit process -> Just ignore it. pass else: raise ql_logging.log(strip_color(string), logging_category)
def main(argv): dir_ = argv[1] for entry in sorted(os.listdir(dir_)): path = os.path.join(dir_, entry) size = os.path.getsize(path) mtime = os.path.getmtime(path) mtime_format = time.strftime("%b %d %H:%M", time.localtime(mtime)) reset = '\033[0m' if os.path.isdir(path): color = '\033[1;94m' elif os.access(path, os.X_OK): color = '\033[1;92m' else: color = '' senf.print_("%6d %13s %s%s%s" % (size, mtime_format, color, entry, reset))
def main(argv): assert len(argv) <= 2 if len(argv) == 2: key = argv[-1] if key not in senf.environ: return 1 senf.print_(senf.environ[key]) return for key, value in sorted(senf.environ.items()): if not senf.supports_ansi_escape_codes(sys.stdout.fileno()): reset = color1 = color2 = "" else: reset = "\033[0m" color1 = "\033[1;91m" color2 = "\033[1;94m" senf.print_("%s%s%s=%s%s" % (color1, key, color2, reset, value))
def main(): N = ["black", "blue", "magenta", "red", "yellow", "green", "cyan", "white"] FG = [ANSI.FG_DEFAULT] for n in N: FG.append(getattr(ANSI, "FG_" + n.upper())) FG.append(getattr(ANSI, "FG_LIGHT_" + n.upper())) BG = [ANSI.BG_DEFAULT] for n in N: BG.append(getattr(ANSI, "BG_" + n.upper())) BG.append(getattr(ANSI, "BG_LIGHT_" + n.upper())) def print_ansi(*args, **kwargs): if supports_ansi_escape_codes(sys.stdout.fileno()): print_(*args, **kwargs) else: print_(**kwargs) i = 0x180 for background in BG: print_ansi(background, end="") for foreground in FG: print_ansi(foreground, end="") print_ansi(ANSI.SET_UNDERLINE, end="") print_(unichr_(i), end="") print_ansi(ANSI.RESET_UNDERLINE, end=" ") print_ansi(ANSI.SET_BOLD, end="") print_(unichr_(i), end="") print_ansi(ANSI.RESET_BOLD, end=" ") print_ansi(ANSI.FG_DEFAULT, end="") i += 1 print_ansi(ANSI.BG_DEFAULT) print_()
def _print_message(string, custom_context, debug_only, prefix, color, logging_category, start_time=time.time()): context = frame_info(2) # strip the package name if context.count(".") > 1: context = context.split(".", 1)[-1] if custom_context: context = "%s(%r)" % (context, custom_context) timestr = ("%2.3f" % (time.time() - start_time))[-6:] info = "%s: %s: %s:" % (getattr(Colorise, color)(prefix), Colorise.magenta(timestr), Colorise.blue(context)) lines = string.splitlines() if len(lines) > 1: string = os.linesep.join([info] + [" " * 4 + l for l in lines]) else: string = info + " " + lines[0] if not debug_only or const.DEBUG: file_ = sys.stderr if _should_write_to_file(file_): if not _supports_ansi_escapes(file_): string = strip_color(string) print_(string, file=file_, flush=True) ql_logging.log(strip_color(string), logging_category)
def test_print_anything(): with capture_output(): print_(u"\u1234") with capture_output() as (out, err): print_(5, end="") assert out.getvalue() == b"5" with capture_output() as (out, err): print_([], end="") assert out.getvalue() == b"[]"
def test_print_capture(): with capture_output() as (out, err): print_(u"bla") assert out.getvalue() == b"bla" + linesepb assert err.getvalue() == b"" with capture_output() as (out, err): print_(u"bla", end="\n") assert out.getvalue() == b"bla" + linesepb with capture_output() as (out, err): print_() assert out.getvalue() == linesepb
def test_print_error(): with pytest.raises(TypeError): print_(end=4) with pytest.raises(TypeError): print_(sep=4)
def print_ansi(*args, **kwargs): if supports_ansi_escape_codes(sys.stdout.fileno()): print_(*args, **kwargs) else: print_(**kwargs)
# -*- coding: utf-8 -*- # Copyright 2016 Christoph Reiter # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. from senf import input_, print_ if __name__ == "__main__": print_(input_())
def main(): N = ["black", "blue", "magenta", "red", "yellow", "green", "cyan", "white"] FG = [ANSI.FG_DEFAULT] for n in N: FG.append(getattr(ANSI, "FG_" + n.upper())) FG.append(getattr(ANSI, "FG_LIGHT_" + n.upper())) BG = [ANSI.BG_DEFAULT] for n in N: BG.append(getattr(ANSI, "BG_" + n.upper())) BG.append(getattr(ANSI, "BG_LIGHT_" + n.upper())) i = 0x180 for background in BG: print_(background, end="") for foreground in FG: print_(foreground, end="") print_(ANSI.SET_UNDERLINE + unichr_(i) + ANSI.RESET_UNDERLINE + " ", end="") print_(ANSI.SET_BOLD + unichr_(i) + ANSI.RESET_BOLD + " ", end="") i += 1 print_() print_()
def test_print_bytes(objects, sep, end, flush): h = StringIO() print_(*objects, sep=sep, end=end, flush=flush, file=h) h.getvalue()
def test_print_py3_stringio(): if os.name != "nt" and PY3: f = StringIO() print_(b"\xff\xfe", file=f) assert f.getvalue() == \ b"\xff\xfe\n".decode(_encoding, "surrogateescape")
def test_print_real(): print_(u"foo") print_(u"foo", file=sys.stderr) print_(u"\033[94mfoo", u"\033[0m") print_(b"foo") print_(u"foo", flush=True) print_(u"\ud83d")
def test_print(): f = BytesIO() print_(u"foo", file=f) out = f.getvalue() assert isinstance(out, bytes) assert out == b"foo" + linesepb f = StringIO() print_(u"foo", file=f) out = f.getvalue() assert isinstance(out, str) assert out == "foo" + os.linesep print_(u"foo", file=f, flush=True) f = TextIO() print_(u"foo", file=f) out = f.getvalue() assert isinstance(out, text_type) assert out == u"foo" + linesepu f = TextIO() print_(u"", file=f, end=b"\n") out = f.getvalue() assert isinstance(out, text_type) assert out == linesepu f = BytesIO() print_("", file=f, end=b"\n") out = f.getvalue() assert isinstance(out, bytes) assert out == linesepb