Skip to content

Commit

Permalink
Suppressed Pylint import errors, added downloads directory argument s…
Browse files Browse the repository at this point in the history
…upport to flatc_downloader and correct handling for existing deserialized JSON files.
  • Loading branch information
Shararamosh committed Sep 12, 2024
1 parent d3d10f8 commit 0475bd6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 20 deletions.
3 changes: 2 additions & 1 deletion flatc_deserializer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""
Десериализация выбранных бинарных файлов Flatbuffers по выбранной схеме.
"""
# pylint: disable=import-error
import os
import sys
import argparse
from i18n import t

from main import prepare_app, get_flatc_path, execute_deserialize # pylint: disable=import-error
from main import prepare_app, get_flatc_path, execute_deserialize

if __name__ == "__main__":
prepare_app("images/flatbuffers-logo-clean.png")
Expand Down
4 changes: 2 additions & 2 deletions flatc_deserializer_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
Десериализация бинарных файлов Flatbuffers в выбранной директории по всем схемам в другой
выбранной директории.
"""
# pylint: disable=import-error
import os
import sys
import argparse
from i18n import t

from main import prepare_app, get_flatc_path, \
execute_deserialize_batch # pylint: disable=import-error
from main import prepare_app, get_flatc_path, execute_deserialize_batch

if __name__ == "__main__":
prepare_app("images/flatbuffers-batch-logo-clean.png")
Expand Down
7 changes: 4 additions & 3 deletions flatc_download_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ def download_flatc(root_path: str) -> str:
"""
Скачивание компилятора Flatbuffers в заданную папку.
:param root_path: Путь для скачивания компилятора.
:return: Путь к файлу компилятора.
:return: Путь к файлу компилятора, если архив с ним был успешно скачан и распакован.
"""
root_path = os.path.abspath(root_path)
if not os.path.isdir(root_path):
raise FileNotFoundError(t("main.directory_not_found"), root_path)
os.makedirs(root_path)
info(t("flatc_download_funcs.download_start"))
zip_path, _ = request.urlretrieve(get_flatc_url())
info(t("flatc_download_funcs.download_finish"), zip_path)
with ZipFile(zip_path, "r") as f:
f.extractall(root_path)
info(t("flatc_download_funcs.unpack_path"), zip_path, root_path)
info(t("flatc_download_funcs.unpack_path"), zip_path, root_path)
os.remove(zip_path)
info(t("main.file_removed"), zip_path)
flatc_path = which("flatc", path=root_path + os.sep)
Expand Down
12 changes: 10 additions & 2 deletions flatc_downloader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"""
Скачивание компилятора схемы Flatbuffers при его отсутствии в рабочей директории.
"""
# pylint: disable=import-error
import os
import sys
import argparse
from i18n import t

from main import prepare_app, execute_download # pylint: disable=import-error
from main import prepare_app, execute_download

if __name__ == "__main__":
prepare_app("images/flatbuffers-downloader-logo-clean.png")
sys.exit(execute_download(os.getcwd()))
parser = argparse.ArgumentParser(prog=t("main.flatc_downloader_name"),
description=t("main.flatc_downloader_desc"))
parser.add_argument("downloads_directory", type=str, default=os.getcwd(),
help=t("main.download_directory_arg"))
args = parser.parse_args()
sys.exit(execute_download(args.downloads_directory))
22 changes: 17 additions & 5 deletions flatc_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path
flatc_path = os.path.abspath(flatc_path)
schema_path = os.path.abspath(schema_path)
binary_path = os.path.abspath(binary_path)
binary_name = os.path.splitext(os.path.basename(binary_path))[0]
output_path = os.path.abspath(output_path)
json_path = os.path.abspath(output_path + os.sep + binary_name + ".json")
previous_json_contents = None
try:
with open(json_path, "rb") as json_file:
previous_json_contents = json_file.read()
except OSError:
pass
args = [flatc_path]
if output_path == "":
output_path = binary_path
Expand All @@ -41,11 +49,15 @@ def deserialize(flatc_path: str, schema_path: str, binary_path: str, output_path
if proc.stdout is not None and proc.stdout != "":
info(t("flatc_funcs.run_ok"), " ".join(args))
info(proc.stdout)
binary_name = os.path.splitext(os.path.basename(binary_path))[0]
json_path = os.path.abspath(output_path + os.sep + binary_name + ".json")
if not os.path.isfile(json_path):
info(t("flatc_funcs.json_error"), binary_path)
return {}
info(t("flatc_funcs.json_ok"), binary_path, json_path)
with open(json_path, "r", encoding="utf-8") as json_file:
return loads(json_file.read())
try:
with open(json_path, "rb") as json_file:
current_json_contents = json_file.read()
except OSError:
info(t("main.file_failed_to_open"), json_path)
return {}
if current_json_contents != previous_json_contents:
info(t("flatc_funcs.json_ok"), binary_path, json_path)
return loads(current_json_contents.decode("utf-8"))
8 changes: 6 additions & 2 deletions localization/en_US/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ executable_not_found: Executable %s not found.
file_not_found: File %s not found.
directory_not_found: Directory %s not found.
file_removed: File %s is removed.
flatc_already_exists: "Flabuffers schema compiler already exists in working directory: %s."
flatc_already_exists: "Flabuffers schema compiler already exists in directory: %s."
file_not_executable: File %s is not executable.
tkinter_fbs_directory_select: Select directory with schema files
tkinter_binary_directory_select: Select directory with binary files
Expand All @@ -23,4 +23,8 @@ schema_file_arg: Schema file
binary_files_arg: Binary files
flatc_path_arg: Path to schema compiler (flatc)
flatc_deserializer_name: Flatbuffers Batch Deserializer
flatc_deserializer_desc: Program for batch deserialization of Flatbuffers binary files.
flatc_deserializer_desc: Program for batch deserialization of Flatbuffers binary files.
file_failed_to_open: Failed to open file %s.
flatc_downloader_name: Flatbuffers Schema Compiler Downloader
flatc_downloader_desc: Program for downloading latest version of Flatbuffers schema compiler for this platform.
download_directory_arg: Directory for saving downloaded files
8 changes: 6 additions & 2 deletions localization/ru_RU/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ executable_not_found: Исполняемый файл %s не найден.
file_not_found: Файл %s не найден.
directory_not_found: Директория %s не найдена.
file_removed: Файл %s был удалён.
flatc_already_exists: "Компилятор схемы Flatbuffers уже существует в рабочей директории: %s"
flatc_already_exists: "Компилятор схемы Flatbuffers уже существует в директории: %s"
file_not_executable: Файл %s не является исполняемым.
tkinter_fbs_directory_select: Выбор директории с файлами схем
tkinter_binary_directory_select: Выбор директории с бинарными файлами
Expand All @@ -23,4 +23,8 @@ schema_file_arg: Файл схемы
binary_files_arg: Бинарные файлы
flatc_path_arg: Путь к компилятору схемы (flatc)
flatc_deserializer_name: Flatbuffers Batch Deserializer
flatc_deserializer_desc: Программа для пакетной десериализации бинарных файлов Flatbuffers.
flatc_deserializer_desc: Программа для пакетной десериализации бинарных файлов Flatbuffers.
file_failed_to_open: Не удалось открыть файл %s.
flatc_downloader_name: Flatbuffers Schema Compiler Downloader
flatc_downloader_desc: Программа для скачивания последней версии компилятора схемы Flatbuffers для данной платформы.
download_directory_arg: Директория для сохранения скачанных файлов
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Десериализация бинарных файлов Flatbuffers по заданной схеме.
"""
# pylint: disable=import-error, line-too-long, too-many-branches
import logging
import os
import sys
Expand All @@ -15,8 +16,8 @@
from tqdm import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm

from flatc_download_funcs import download_flatc # pylint: disable=import-error
from flatc_funcs import deserialize # pylint: disable=import-error
from flatc_download_funcs import download_flatc
from flatc_funcs import deserialize


def get_resource_path(filename: str) -> str:
Expand Down Expand Up @@ -88,7 +89,7 @@ def execute_download(root_path: str) -> int | str:
if flatc_path != "":
logging.info(i18n.t("main.flatc_already_exists"), flatc_path)
else:
download_flatc(os.getcwd())
download_flatc(root_path)
return os.EX_OK


Expand Down

0 comments on commit 0475bd6

Please sign in to comment.