Skip to content

Commit

Permalink
refactor: Drop dependency on six in log module (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesara authored Jan 29, 2023
1 parent ac1cc51 commit 57299b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 54 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
lxml>=4.1.1
six
xml2rfc>=3.16.0
60 changes: 9 additions & 51 deletions svgcheck/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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')


Expand All @@ -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()


Expand All @@ -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()


Expand All @@ -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):
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions svgcheck/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down

0 comments on commit 57299b5

Please sign in to comment.