Skip to content

Commit

Permalink
eeptool: Avoid printing trace on common error
Browse files Browse the repository at this point in the history
Avoid printing trace on common errors for better usability.
  • Loading branch information
Wer-Wolf committed Jan 13, 2021
1 parent 6c84bd0 commit 2c7d8de
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions tool/eeptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,42 @@
parser.add_argument("file", type=str, help="file to read to/write from")
parser.add_argument("-V", "--version", help="display version number", action="version", version=f"%(prog)s {__version__}")
args = parser.parse_args()

if args.write is None:
with open(args.file, "rb") as fd:
damaged_entries = 0
digits = 0
data = fd.read(MAX_DATA_LENGTH)
sys.stdout.write(f"Content of {args.file}: ")
for byte in data:
if byte == 0x00:
sys.stdout.write("\n")
break
char = chr(byte)
if char.isnumeric():
sys.stdout.write(char)
digits += 1
try:
if args.write is None:
with open(args.file, "rb") as fd:
damaged_entries = 0
digits = 0
data = fd.read(MAX_DATA_LENGTH)
sys.stdout.write(f"Content of {args.file}: ")
for byte in data:
if byte == 0x00:
sys.stdout.write("\n")
break
char = chr(byte)
if char.isnumeric():
sys.stdout.write(char)
digits += 1
else:
sys.stdout.write("?")
damaged_entries += 1
else:
sys.stdout.write("?")
damaged_entries += 1
else:
sys.stdout.write("\n")
sys.stderr.write("WARNING: Delimiter missing\n")
print(f"Digits: {digits}")
print(f"Damaged entries: {damaged_entries}")
if damaged_entries > 0 or b"\x00" not in data:
print("\nWARNING: The EEPROM image is damaged, please consider creating a new EEPROM image")
elif not args.write.isnumeric():
raise ValueError("Only numbers (0-9) are supported")
elif len(args.write) > MAX_DIGITS:
raise ValueError(f"Too many numbers (max is {MAX_DIGITS})")
else:
padding = MAX_DATA_LENGTH - len(args.write)
with open(args.file, "w") as fd:
fd.write(args.write + "\x00" * padding)
print("[" + "#" * len(args.write) + "=" * padding + "]")
print(f"{len(args.write)} numbers written")
sys.stdout.write("\n")
sys.stderr.write("WARNING: Delimiter missing\n")
print(f"Digits: {digits}")
print(f"Damaged entries: {damaged_entries}")
if damaged_entries > 0 or b"\x00" not in data:
print("\nWARNING: The EEPROM image is damaged, please consider creating a new EEPROM image")
elif not args.write.isnumeric():
sys.exit("Only numbers (0-9) are supported")
elif len(args.write) > MAX_DIGITS:
sys.exit(f"Too many numbers (max is {MAX_DIGITS})")
else:
padding = MAX_DATA_LENGTH - len(args.write)
with open(args.file, "w") as fd:
fd.write(args.write + "\x00" * padding)
print("[" + "#" * len(args.write) + "=" * padding + "]")
print(f"{len(args.write)} numbers written")
except FileNotFoundError as msg:
sys.exit(msg)

sys.exit(0)

0 comments on commit 2c7d8de

Please sign in to comment.