From d67567d304035b705f9abb4dd5d515502e89151e Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 24 Mar 2024 14:19:31 +0200 Subject: [PATCH 1/4] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B0=D0=B2=20=D1=81?= =?UTF-8?q?=D0=BA=D1=80=D0=B8=D0=BF=D1=82=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D1=83=D0=B2=D0=B0=D0=BD=D0=BD=D1=8F=20=D1=82=D0=B0=D0=B1?= =?UTF-8?q?=D0=BB=D0=B8=D1=86=D1=8C=20=D0=B4=D0=BB=D1=8F=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/table-generator/convert.py | 140 +++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 utils/table-generator/convert.py diff --git a/utils/table-generator/convert.py b/utils/table-generator/convert.py new file mode 100755 index 0000000..ad46394 --- /dev/null +++ b/utils/table-generator/convert.py @@ -0,0 +1,140 @@ +#!/bin/python3 + +import sys + +# magic values +NO = 0 +AUTO = 1 +YES = 2 + +def convert_generic_v1(data): + split_raw_lines = [i.strip().split("|") for i in data.split("\n") if i] + + return generate_table(split_raw_lines) + +def convert_usecase_v1(filename, data): + split_raw_lines_from_file = [i.split(" | ") for i in data.split("\n") if i] + split_raw_lines = [["ID", filename.upper()]] + split_raw_lines_from_file + + merged_lines = [] + for i in split_raw_lines: + if i[0] == "": + merged_lines[-1][1] += "
" + i[1] + else: + merged_lines.append(i) + + return generate_table(merged_lines) + +def generate_table(raw_table_data): + table_lines = raw_table_data + transposed_table = list(zip(*table_lines)) + + field_sizes = [max([max([len(k)+2 for k in j.split("\n")]) for j in i]) for i in transposed_table] + + formatted_table_lines = [] + + l = "|" + for i, field in enumerate(table_lines[0]): + l += field.center(field_sizes[i]) + l += "|" + + formatted_table_lines.append(l) + formatted_table_lines.append(f"|{'|'.join([':'+'-'*(i-2)+':' for i in field_sizes])}|") + + for line in table_lines[1:]: + l = "|" + for i, field in enumerate(line): + l += field.center(field_sizes[i]) + l += "|" + + formatted_table_lines.append(l) + + return "\n".join(formatted_table_lines) + + +if __name__=="__main__": + # parse args + files = [] + write_to_file = AUTO + write_to_stdout = AUTO + usecase_formatting = AUTO + verbose = AUTO + + for i in sys.argv[1:]: + if i.startswith("-"): + # записувати таблицю в файл + if i in ["-f", "--file"]: + write_to_file = YES + elif i in ["-nf", "--no-file"]: + write_to_file = NO + + # виводити таблицю у стандартний вивід + elif i in ["-o", "--stdout"]: + write_to_stdout = YES + elif i in ["-no", "--no-stdout"]: + write_to_stdout = NO + + # форматувати таблицю як use-case + elif i in ["-u", "--usecase", "--use-case"]: + usecase_formatting = YES + elif i in ["-nu", "--no-usecase", "--no-use-case"]: + usecase_formatting = NO + + # виводити на екран додаткову інформацію + elif i in ["-v", "--verbose"]: + verbose = YES + elif i in ["-nv", "--no-verbose"]: + verbose = NO + + else: + files.append(i) + + if len(files) < 1: + print("You need to pass at least one file as CLI argument", file=sys.stderr) + print("Exiting...", file=sys.stderr) + exit(1) + + if len(files) == 1: + name = files[0] + data = open(name).read() + + if usecase_formatting == YES: + if verbose == YES: + print(f"Force-formatting {name} as a use-case\n") + formatted_table_data = convert_usecase_v1(name.rsplit(".", 1)[0], data) + elif (name.endswith(".usecase") or name.endswith(".uc")) and usecase_formatting >= AUTO: + if verbose == YES: + print(f"Auto-detected use-case in file {name}\n") + formatted_table_data = convert_usecase_v1(name.rsplit(".", 1)[0], data) + else: + formatted_table_data = convert_generic_v1(data) + + if write_to_stdout >= AUTO: + print(formatted_table_data) + + if write_to_file > AUTO: + open(name + ".table", 'w').write(data+"\n") + + exit(0) + + for no, name in enumerate(sys.argv[1:]): + print(f"Converting {no+1}/{len(sys.argv)}") + + data = open(name).read() + + if usecase_formatting == YES: + if verbose >= AUTO: + print(f"Force-formatting {name} as a use-case") + formatted_table_data = convert_usecase_v1(name.rsplit(".", 1)[0], data) + elif (name.endswith(".usecase") or name.endswith(".uc")) and usecase_formatting >= AUTO: + if verbose >= AUTO: + print(f"Auto-detected use-case in file {name}") + formatted_table_data = convert_usecase_v1(name.rsplit(".", 1)[0], data) + else: + formatted_table_data = convert_generic_v1(data) + + if write_to_stdout > AUTO: + print(formatted_table_data) + + if write_to_file >= AUTO: + open(name + ".table", 'w').write(data+"\n") -- 2.40.1 From 06b52b1efc7aa29aee57fb9fa62640c49ff06c41 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 24 Mar 2024 15:08:03 +0200 Subject: [PATCH 2/4] =?UTF-8?q?utils/table-generator/convert.py:=20=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=D0=B7=D0=B0=D0=B2=20=D0=BA=D0=BE=D0=B4=D1=83=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8F=20UTF-8=20=D0=B4=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=BE=D1=82=D0=B8=20=D0=B7=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/table-generator/convert.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/table-generator/convert.py b/utils/table-generator/convert.py index ad46394..c74402f 100755 --- a/utils/table-generator/convert.py +++ b/utils/table-generator/convert.py @@ -96,7 +96,7 @@ if __name__=="__main__": if len(files) == 1: name = files[0] - data = open(name).read() + data = open(name, encoding = "utf-8").read() if usecase_formatting == YES: if verbose == YES: @@ -113,14 +113,14 @@ if __name__=="__main__": print(formatted_table_data) if write_to_file > AUTO: - open(name + ".table", 'w').write(data+"\n") + open(name + ".table", 'w', encoding = "utf-8").write(data+"\n") exit(0) for no, name in enumerate(sys.argv[1:]): print(f"Converting {no+1}/{len(sys.argv)}") - data = open(name).read() + data = open(name, encoding = "utf-8").read() if usecase_formatting == YES: if verbose >= AUTO: @@ -137,4 +137,4 @@ if __name__=="__main__": print(formatted_table_data) if write_to_file >= AUTO: - open(name + ".table", 'w').write(data+"\n") + open(name + ".table", 'w', encoding = "utf-8").write(data+"\n") -- 2.40.1 From b8f1e590e73f27896a307e8fd8d8b3665941d935 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 24 Mar 2024 15:11:53 +0200 Subject: [PATCH 3/4] =?UTF-8?q?utils/table-generator/convert.py:=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=D0=B2=20=D0=B0=D0=B2=D1=82=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=BD=D0=B5=20=D0=BE=D1=87=D0=B8=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=8F=20ID=20use-case-?= =?UTF-8?q?=D1=96=D0=B2=20=D0=B2=D1=96=D0=B4=20=D1=81=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=BD=D0=BD=D1=96=D1=85=20=D1=88=D0=BB=D1=8F=D1=85=D1=96?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/table-generator/convert.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/utils/table-generator/convert.py b/utils/table-generator/convert.py index c74402f..1bec61d 100755 --- a/utils/table-generator/convert.py +++ b/utils/table-generator/convert.py @@ -14,7 +14,15 @@ def convert_generic_v1(data): def convert_usecase_v1(filename, data): split_raw_lines_from_file = [i.split(" | ") for i in data.split("\n") if i] - split_raw_lines = [["ID", filename.upper()]] + split_raw_lines_from_file + + if '/' in filename: + use_case_name = filename.rsplit("/", 1)[1].upper() + elif '\\' in filename: + use_case_name = filename.rsplit("\\", 1)[1].upper() + else: + use_case_name = filename.upper() + + split_raw_lines = [["ID", use_case_name]] + split_raw_lines_from_file merged_lines = [] for i in split_raw_lines: -- 2.40.1 From e9d104f53a8b1782d01efcb7280593b21ef1b8fb Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 24 Mar 2024 16:35:21 +0200 Subject: [PATCH 4/4] =?UTF-8?q?utils/table-generator/convert.py:=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=D0=B2=20=D0=B7=D0=B0=D1=85=D0=B8=D1=81=D1=82?= =?UTF-8?q?=20=D0=B2=D1=96=D0=B4=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D1=97=20=D0=BE=D0=B1=D1=80=D0=BE=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D1=96=D0=B2=20=D1=82=D0=B0=D0=B1?= =?UTF-8?q?=D0=BB=D0=B8=D1=86=D1=8C=20=D1=82=D0=B0=20=D0=B2=D0=B8=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B8=D0=B2=20=D0=B4=D0=B5=D0=BA=D1=96=D0=BB?= =?UTF-8?q?=D1=8C=D0=BA=D0=B0=20=D0=BF=D0=BE=D0=BC=D0=B8=D0=BB=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/table-generator/convert.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/utils/table-generator/convert.py b/utils/table-generator/convert.py index 1bec61d..edb798d 100755 --- a/utils/table-generator/convert.py +++ b/utils/table-generator/convert.py @@ -67,7 +67,9 @@ if __name__=="__main__": write_to_stdout = AUTO usecase_formatting = AUTO verbose = AUTO + process_table_files = AUTO + # 1 pass (argument harvest) for i in sys.argv[1:]: if i.startswith("-"): # записувати таблицю в файл @@ -94,8 +96,18 @@ if __name__=="__main__": elif i in ["-nv", "--no-verbose"]: verbose = NO - else: - files.append(i) + elif i in ["-t", "--process-table"]: + process_table_files = YES + elif i in ["-nt", "--no-process-table"]: + process_table_files = NO + + # 2 pass (filename harvest) + for i in sys.argv[1:]: + if not i.startswith("-"): + if i.endswith(".table") and process_table_files <= AUTO: + print(f"[Warning]: Excluding {i} to prevent processing of an already processed file (pass --process-table to override this behaviour)") + else: + files.append(i) if len(files) < 1: print("You need to pass at least one file as CLI argument", file=sys.stderr) @@ -121,12 +133,12 @@ if __name__=="__main__": print(formatted_table_data) if write_to_file > AUTO: - open(name + ".table", 'w', encoding = "utf-8").write(data+"\n") + open(name + ".table", 'w', encoding = "utf-8").write(formatted_table_data+"\n") exit(0) - for no, name in enumerate(sys.argv[1:]): - print(f"Converting {no+1}/{len(sys.argv)}") + for no, name in enumerate(files): + print(f"Converting {no+1}/{len(files)}") data = open(name, encoding = "utf-8").read() @@ -145,4 +157,4 @@ if __name__=="__main__": print(formatted_table_data) if write_to_file >= AUTO: - open(name + ".table", 'w', encoding = "utf-8").write(data+"\n") + open(name + ".table", 'w', encoding = "utf-8").write(formatted_table_data+"\n") -- 2.40.1