Skip to content

Commit 2688941

Browse files
committed
Modified deserialization functions for the future GUI app.
1 parent 1962551 commit 2688941

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

flatc_funcs.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
from i18n import t
1010

1111

12-
def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path: str = "") -> dict:
12+
def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path: str = "", return_dict = True) -> dict:
1313
"""
1414
Десериализация бинарного файла, используя схему Flatbuffers.
1515
:param flatc_path: Путь к компилятору схемы.
1616
:param schema_path: Путь к файлу схемы.
1717
:param binary_path: Путь к бинарному файлу.
1818
:param output_path: Путь к директории вывода.
19+
:param return_dict: Если True, возвращать словарь из прочитанного файла. Иначе - путь к файлу.
1920
:return: Десериализованный бинарный файл в виде словаря.
2021
"""
2122
flatc_path = os.path.abspath(flatc_path)
@@ -32,7 +33,7 @@ def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path
3233
pass
3334
args = [flatc_path]
3435
if output_path == "":
35-
output_path = binary_path
36+
output_path = os.path.dirname(binary_path)
3637
output_path += os.sep
3738
args += ["--raw-binary"]
3839
args += ["-o", output_path]
@@ -45,19 +46,27 @@ def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path
4546
info(t("flatc_funcs.run_error"), " ".join(cpe.cmd), cpe.returncode)
4647
if cpe.stderr is not None and cpe.stderr != "":
4748
info(cpe.stderr)
48-
return {}
49+
if return_dict:
50+
return {}
51+
return ""
4952
if proc.stdout is not None and proc.stdout != "":
5053
info(t("flatc_funcs.run_ok"), " ".join(args))
5154
info(proc.stdout)
5255
if not os.path.isfile(json_path):
5356
info(t("flatc_funcs.json_error"), binary_path)
54-
return {}
57+
if return_dict:
58+
return {}
59+
return ""
5560
try:
5661
with open(json_path, "rb") as json_file:
5762
current_json_contents = json_file.read()
5863
except OSError:
5964
info(t("main.file_failed_to_open"), json_path)
60-
return {}
65+
if return_dict:
66+
return {}
67+
return ""
6168
if current_json_contents != previous_json_contents:
6269
info(t("flatc_funcs.json_ok"), binary_path, json_path)
63-
return loads(current_json_contents.decode("utf-8"))
70+
if return_dict:
71+
return loads(current_json_contents.decode("utf-8"))
72+
return json_path

main.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,35 @@ def get_schema_paths(root_path: str) -> list[str]:
194194
return schema_paths
195195

196196

197-
def get_binary_tuples(binaries_path: str, schema_paths: list[str]) -> list[tuple[str, str]]:
197+
def get_binary_tuples(binary_paths: list[str], schema_paths: list[str]) -> list[tuple[str, str]]:
198198
"""
199199
Получение списка кортежей из двух элементов: (путь к бинарному файлу, путь к соответствующему ему файлу схемы)
200-
:param binaries_path: Список путей к бинарным файлам.
200+
:param binary_paths: Список путей к бинарным файлам или директориям с ними.
201201
:param schema_paths: Список путей к файлам схем.
202202
:return: Кортеж из двух строковых элементов.
203203
"""
204204
binary_tuples = []
205-
for subdir, _, files in os.walk(binaries_path):
206-
for file in files:
207-
file_path = os.path.abspath(os.path.join(subdir, file))
205+
for _, binary_path in enumerate(binary_paths):
206+
if os.path.isfile(binary_path):
207+
file_path = os.path.abspath(binary_path)
208208
for schema_path in schema_paths:
209209
schema_ext = os.path.splitext(os.path.basename(schema_path))[0]
210-
file_ext = os.path.splitext(file)[1][1:]
210+
file_ext = os.path.splitext(file_path)[1][1:]
211211
if schema_ext.casefold() == file_ext.casefold():
212212
binary_tuples.append((file_path, schema_path))
213213
break
214+
continue
215+
if not os.path.isdir(binary_path):
216+
continue
217+
for subdir, _, files in os.walk(binary_path):
218+
for file in files:
219+
file_path = os.path.abspath(os.path.join(subdir, file))
220+
for schema_path in schema_paths:
221+
schema_ext = os.path.splitext(os.path.basename(schema_path))[0]
222+
file_ext = os.path.splitext(file_path)[1][1:]
223+
if schema_ext.casefold() == file_ext.casefold():
224+
binary_tuples.append((file_path, schema_path))
225+
break
214226
return binary_tuples
215227

216228

@@ -252,7 +264,7 @@ def execute_deserialize_batch(flatc_path: str, schemas_path: str, binaries_path:
252264
if len(schema_paths) < 1:
253265
logging.info(i18n.t("main.no_schema_files_found"), binaries_path)
254266
return os.EX_OK
255-
binary_tuples = get_binary_tuples(binaries_path, schema_paths)
267+
binary_tuples = get_binary_tuples([binaries_path], schema_paths)
256268
with logging_redirect_tqdm():
257269
pbar = tqdm(total=len(binary_tuples), desc=i18n.t("main.files"))
258270
with ThreadPoolExecutor() as executor:

0 commit comments

Comments
 (0)