Replace logging with prints to stderr
This commit is contained in:
parent
86993f0b15
commit
364f348e40
1 changed files with 18 additions and 22 deletions
40
pass2csv
40
pass2csv
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
import csv
|
import csv
|
||||||
import logging
|
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
@ -10,7 +9,9 @@ import gnupg
|
||||||
|
|
||||||
__version__ = '1.0.0'
|
__version__ = '1.0.0'
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
||||||
|
def stderr(s, *args, **kwargs):
|
||||||
|
print(s, *args, file=sys.stderr, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def set_meta(entry, path, grouping_base):
|
def set_meta(entry, path, grouping_base):
|
||||||
|
@ -79,7 +80,7 @@ def write(file, entries, get_fields, get_lines):
|
||||||
field_names = get_field_names | get_line_names
|
field_names = get_field_names | get_line_names
|
||||||
header = ["Group(/)", "Title", "Password", *field_names, "Notes"]
|
header = ["Group(/)", "Title", "Password", *field_names, "Notes"]
|
||||||
csvw = csv.writer(file, dialect='unix')
|
csvw = csv.writer(file, dialect='unix')
|
||||||
logging.info("\nWriting data to %s\n", file.name)
|
stderr(f"\nWriting data to {file.name}\n")
|
||||||
csvw.writerow(header)
|
csvw.writerow(header)
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
fields = [entry['fields'].get(name) for name in field_names]
|
fields = [entry['fields'].get(name) for name in field_names]
|
||||||
|
@ -103,16 +104,15 @@ def main(store_path, outfile, grouping_base, gpgbinary, use_agent, encodings,
|
||||||
if path.is_file():
|
if path.is_file():
|
||||||
files = [path]
|
files = [path]
|
||||||
else:
|
else:
|
||||||
err = "No such file or directory: {}".format(path)
|
stderr(f"No such file or directory: {path}")
|
||||||
logging.error(err)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
for file in files:
|
for file in files:
|
||||||
logging.info("Processing %s", file)
|
stderr(f"Processing {file}")
|
||||||
with open(file, 'rb') as fp:
|
with open(file, 'rb') as fp:
|
||||||
decrypted = gpg.decrypt_file(fp)
|
decrypted = gpg.decrypt_file(fp)
|
||||||
if not decrypted.ok:
|
if not decrypted.ok:
|
||||||
err = "Could not decrypt {}: {}".format(file, decrypted.status)
|
err = f"Could not decrypt {file}: {decrypted.status}"
|
||||||
logging.error(err)
|
stderr(err)
|
||||||
failures.append(err)
|
failures.append(err)
|
||||||
continue
|
continue
|
||||||
for i, encoding in enumerate(encodings):
|
for i, encoding in enumerate(encodings):
|
||||||
|
@ -120,14 +120,11 @@ def main(store_path, outfile, grouping_base, gpgbinary, use_agent, encodings,
|
||||||
# decrypted.data is bytes
|
# decrypted.data is bytes
|
||||||
decrypted_data = decrypted.data.decode(encoding)
|
decrypted_data = decrypted.data.decode(encoding)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(
|
stderr(f"Could not decode {file} with encoding {encoding}: {e}")
|
||||||
"Could not decode {} with encoding {}: {}"
|
|
||||||
.format(file, encoding, e)
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
if i > 0:
|
if i > 0:
|
||||||
# don't log if the first encoding worked
|
# don't log if the first encoding worked
|
||||||
logging.warning("Decoded {} with encoding {}".format(file, encoding))
|
stderr(f"Decoded {file} with encoding {encoding}")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
err = "Could not decode {}, see messages above for more info.".format(file)
|
err = "Could not decode {}, see messages above for more info.".format(file)
|
||||||
|
@ -138,9 +135,12 @@ def main(store_path, outfile, grouping_base, gpgbinary, use_agent, encodings,
|
||||||
set_data(entry, decrypted_data, exclude, get_fields, get_lines)
|
set_data(entry, decrypted_data, exclude, get_fields, get_lines)
|
||||||
entries.append(entry)
|
entries.append(entry)
|
||||||
if failures:
|
if failures:
|
||||||
logging.warning("\nGot errors while processing files:")
|
stderr("\nGot errors while processing files:")
|
||||||
for err in failures:
|
for err in failures:
|
||||||
logging.warning(err)
|
stderr(err)
|
||||||
|
if not entries:
|
||||||
|
stderr("\nNothing to write.")
|
||||||
|
sys.exit(1)
|
||||||
write(outfile, entries, get_fields, get_lines)
|
write(outfile, entries, get_fields, get_lines)
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,10 +253,8 @@ def compile_regexp(pattern):
|
||||||
try:
|
try:
|
||||||
regexp = re.compile(pattern, re.I)
|
regexp = re.compile(pattern, re.I)
|
||||||
except re.error as e:
|
except re.error as e:
|
||||||
logging.error(
|
escaped = pattern.replace("'", "\\'")
|
||||||
"Could not compile pattern '%s', %s at position %s",
|
stderr(f"Could not compile pattern '{escaped}', {e.msg} at position {e.pos}")
|
||||||
pattern.replace("'", "\\'"), e.msg, e.pos
|
|
||||||
)
|
|
||||||
return None
|
return None
|
||||||
return regexp
|
return regexp
|
||||||
|
|
||||||
|
@ -296,9 +294,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
encodings = [e for e in parsed.encodings.split(',') if e]
|
encodings = [e for e in parsed.encodings.split(',') if e]
|
||||||
if not encodings:
|
if not encodings:
|
||||||
logging.error(
|
stderr(f"Did not understand '--encodings {parsed.encoding}'")
|
||||||
"Did not understand '--encodings {}'".format(parsed.encoding)
|
|
||||||
)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
|
|
Loading…
Reference in a new issue