-
Notifications
You must be signed in to change notification settings - Fork 14
/
als_textconv.py
42 lines (32 loc) · 1.07 KB
/
als_textconv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Module to convert an ALS file to a textual representation that can then be diffed."""
import sys
import gzip
import argparse
def parse(path: str) -> str:
"""Parse a file and returns a textual representation of it."""
if is_gzipped(path):
with gzip.open(path, "rb") as file_obj:
data = file_obj.read()
return data.decode("ascii")
else:
with open(path, "rb") as file_obj:
data = file_obj.read()
return data.decode("ascii")
def is_gzipped(path: str) -> bool:
"""Check if a file is gzipped or not."""
with open(path, "rb") as file_obj:
return file_obj.read(2) == b"\x1f\x8b"
def main():
"""Entry point of the program."""
parser = argparse.ArgumentParser(
description="Convert ALS file to a textual representation."
)
parser.add_argument("file", type=str, help="Path to the file to convert")
args = parser.parse_args()
try:
result = parse(args.file)
print(result)
except RuntimeError:
sys.exit(2)
if __name__ == "__main__":
main()