From 57299b5873f1a61b3288987ab673611d6f7e562c Mon Sep 17 00:00:00 2001 From: Kesara Rathnayake Date: Mon, 30 Jan 2023 12:07:39 +1300 Subject: [PATCH] refactor: Drop dependency on six in log module (#29) --- requirements.txt | 1 - svgcheck/log.py | 60 ++++++++---------------------------------------- svgcheck/run.py | 4 ++-- 3 files changed, 11 insertions(+), 54 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2273594..3de2685 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ lxml>=4.1.1 -six xml2rfc>=3.16.0 diff --git a/svgcheck/log.py b/svgcheck/log.py index e11196c..15b57dd 100644 --- a/svgcheck/log.py +++ b/svgcheck/log.py @@ -11,56 +11,14 @@ import sys import os -import six import io quiet = False verbose = False debug = False -write_out = sys.stdout write_err = sys.stderr -logging_codePage = 'utf8' - -if not six.PY2 and os.name == 'nt' and os.isatty(2): - logging_codePage = sys.stdout.encoding - - -def write_to(file, unicodeString): - if os.name == 'nt': - if six.PY2: - if isinstance(file, io.StringIO): - file.write(unicodeString) - else: - file.write(unicodeString.encode(logging_codePage)) - else: - if isinstance(file, io.StringIO): - file.write(unicodeString) - else: - file.buffer.write(unicodeString.encode(logging_codePage)) - else: - if six.PY2: - if isinstance(file, io.StringIO): - file.write(unicodeString) - else: - file.write(unicodeString.encode(logging_codePage)) - else: - file.write(unicodeString) - - -def write_on_line(*args): - """ Writes a message without ending the line, i.e. in a loading bar """ - write_to(write_err, u' '.join(args)) - write_err.flush() - - -def write(*args): - """ Prints a message to write_out """ - # write_err.write(u' '.join(args)) - write_to(write_err, u' '.join(args)) - write_to(write_err, '\n') - def info(*args, **kwargs): """ Prints a warning message unless quiet """ @@ -77,15 +35,15 @@ def info(*args, **kwargs): else: fileName = os.path.relpath(fileName) prefix = "{0}:{1}: ".format(fileName, where.sourceline) - write_to(write_err, prefix + u' '.join(args)) - write_err.write(u'\n') + write_err.write(prefix + ' '.join(args)) + write_err.write('\n') write_err.flush() def note(*args): """ Call for being verbose only """ if verbose and not quiet: - write_to(write_err, u' '.join(args)) + write_err.write(' '.join(args)) write_err.write('\n') @@ -105,8 +63,8 @@ def warn(*args, **kwargs): else: fileName = os.path.relpath(fileName) prefix = "{0}:{1}: ".format(fileName, where.sourceline) - write_to(write_err, prefix + u' '.join(args)) - write_err.write(u'\n') + write_err.write(prefix + u' '.join(args)) + write_err.write('\n') write_err.flush() @@ -123,8 +81,8 @@ def error(*args, **kwargs): if 'additional' in kwargs: prefix = ' ' * kwargs['additional'] - write_to(write_err, (prefix + u' '.join(args))) - write_to(write_err, u'\n') + write_err.write(prefix + u' '.join(args)) + write_err.write('\n') write_err.flush() @@ -146,7 +104,7 @@ def exception(message, list): attr['filename'] = 'unknown' if 'line' not in attr: attr['line'] = -1 - write_to(write_err, " %(filename)s: Line %(line)s: %(message)s\n" % attr) + write_err.write(" %(filename)s: Line %(line)s: %(message)s\n" % attr) def exception_lines(message, list): @@ -158,7 +116,7 @@ def exception_lines(message, list): if attr["message"].endswith(", got "): attr["message"] += "nothing." attr["filename"] = make_relative(attr["filename"]) - write_to(write_err, " %(filename)s: Line %(line)s: %(message)s\n" % attr) + write_err.write(" %(filename)s: Line %(line)s: %(message)s\n" % attr) def make_relative(fileName): diff --git a/svgcheck/run.py b/svgcheck/run.py index f6c31ec..ad78926 100644 --- a/svgcheck/run.py +++ b/svgcheck/run.py @@ -143,8 +143,8 @@ def main(): if options.output_filename is None: file = sys.stdout else: - file = open(options.output_filename, 'w') - log.write_to(file, encodedBytes) + file = open(options.output_filename, 'w', encoding='utf-8') + file.write(encodedBytes) if ok: log.info("File conforms to SVG requirements.")