Open log file in text mode instead of bytes mode

pull/14/head
Dan Puttick 2017-03-22 21:49:48 -04:00
parent 52bb566cc3
commit ba407219d3
1 changed files with 16 additions and 11 deletions

View File

@ -312,19 +312,23 @@ class GroomerLogger(object):
def tree(self, base_dir, padding=' '): def tree(self, base_dir, padding=' '):
"""Write a graphical tree to the log for `base_dir`.""" """Write a graphical tree to the log for `base_dir`."""
with open(self.log_path, mode='ab') as lf: horizontal_divider = '#' * 80 + '\n'
lf.write(bytes('#' * 80 + '\n', 'UTF-8')) with open(self.log_path, mode='a', encoding='utf-8') as lf:
lf.write(bytes('{}+- {}/\n'.format(padding, os.path.basename(os.path.abspath(base_dir)).encode()), 'utf8')) lf.write(horizontal_divider)
base_dir_name = os.path.basename(os.path.abspath(base_dir))
lf.write('{}+- {}/\n'.format(padding, base_dir_name))
padding += '| ' padding += '| '
# TODO: make sure this gets all sub directories and use scandir()
files = sorted(os.listdir(base_dir)) files = sorted(os.listdir(base_dir))
for f in files: for f in files:
curpath = os.path.join(base_dir, f) curpath = os.path.join(base_dir, f)
if os.path.islink(curpath): if os.path.islink(curpath):
lf.write('{}+-- {}\t- Symbolic link to {}\n'.format(padding, f, os.readlink(curpath)).encode(errors='ignore')) lf.write('{}+-- {}\t- Symbolic link to {}\n'.format(padding, f, os.readlink(curpath)))
elif os.path.isdir(curpath): elif os.path.isdir(curpath):
self.tree(curpath, padding) self.tree(curpath, padding)
elif os.path.isfile(curpath): elif os.path.isfile(curpath):
lf.write('{}+-- {}\t- {}\n'.format(padding, f, self._computehash(curpath)).encode(errors='ignore')) lf.write('{}+-- {}\t- {}\n'.format(padding, f, self._computehash(curpath)))
lf.write(horizontal_divider)
def _computehash(self, path): def _computehash(self, path):
"""Return a sha256 hash of a file at a given path.""" """Return a sha256 hash of a file at a given path."""
@ -345,12 +349,13 @@ class GroomerLogger(object):
"""Add a file to the log. Takes a dict of file properties.""" """Add a file to the log. Takes a dict of file properties."""
props = file_props props = file_props
description_string = ', '.join(props['description_string']) description_string = ', '.join(props['description_string'])
file_string = "{}: {}/{}, {}: {}\n".format(props['filename'], file_string = " * {}: {}/{}, {}: {}\n".format(
props['maintype'], props['filename'],
props['subtype'], props['maintype'],
props['safety_category'], props['subtype'],
description_string props['safety_category'],
) description_string
)
self._write_file_to_disk(file_string) self._write_file_to_disk(file_string)