Skip to content

Commit

Permalink
Added correct exception messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shararamosh committed Sep 18, 2024
1 parent 6d2e0d1 commit 08efa24
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
5 changes: 3 additions & 2 deletions flatc_download_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import platform
import sys
import errno
from shutil import which
from zipfile import ZipFile
from logging import info
Expand Down Expand Up @@ -35,7 +36,7 @@ def get_flatc_url() -> str:
if platform.processor() == "i386": # Intel macOS.
return root_url + "/MacIntel.flatc.binary.zip"
return root_url + "/Mac.flatc.binary.zip"
raise OSError(t("main.unsupported_platform").format(platform.platform(True)))
raise OSError(errno.ENOSYS, t("main.unsupported_platform").format(platform.platform(True)))


def download_flatc(root_path: str) -> str:
Expand All @@ -57,5 +58,5 @@ def download_flatc(root_path: str) -> str:
info(t("main.file_removed"), zip_path)
flatc_path = which("flatc", path=root_path + os.sep)
if flatc_path is None:
raise FileNotFoundError(t("main.executable_not_found") % "flatc")
raise FileNotFoundError(errno.ENOENT, t("main.executable_not_found"), "flatc")
return os.path.abspath(flatc_path)
13 changes: 8 additions & 5 deletions localization/en_US/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ flatc_binary_filetype: Binary Flatbuffers file
tkinter_output_select: Select output directory
files: Files
unsupported_platform: "Can't run on unsupported platform: %s."
executable_not_found: Executable %s not found.
file_not_found: File %s not found.
directory_not_found: Directory %s not found.
executable_not_found: Executable not found
file_not_found: File not found
directory_not_found: Directory not found
file_removed: File %s is removed.
flatc_already_exists: "Flabuffers schema compiler already exists in directory: %s."
file_not_executable: File %s is not executable.
file_not_executable: File is not executable
tkinter_fbs_directory_select: Select directory with schema files
tkinter_binary_directory_select: Select directory with binary files
no_schema_files_found: No schema files found in directory %s.
Expand All @@ -27,4 +27,7 @@ flatc_deserializer_desc: Program for batch deserialization of Flatbuffers binary
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
download_directory_arg: Directory for saving downloaded files
no_files_selected: No files were selected.
no_directory_selected: No directory was selected.
no_file_selected: No file was selected.
13 changes: 8 additions & 5 deletions localization/ru_RU/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ flatc_binary_filetype: Бинарный файл Flatbuffers
tkinter_output_select: Выбор директории вывода
files: Файлы
unsupported_platform: "Запуск невозможен на неподдерживаемой платформе: %s."
executable_not_found: Исполняемый файл %s не найден.
file_not_found: Файл %s не найден.
directory_not_found: Директория %s не найдена.
executable_not_found: Исполняемый файл не найден
file_not_found: Файл не найден
directory_not_found: Директория не найдена
file_removed: Файл %s был удалён.
flatc_already_exists: "Компилятор схемы Flatbuffers уже существует в директории: %s"
file_not_executable: Файл %s не является исполняемым.
file_not_executable: Файл не является исполняемым
tkinter_fbs_directory_select: Выбор директории с файлами схем
tkinter_binary_directory_select: Выбор директории с бинарными файлами
no_schema_files_found: Файлы схем не были найдены в директории %s.
Expand All @@ -27,4 +27,7 @@ flatc_deserializer_desc: Программа для пакетной десери
file_failed_to_open: Не удалось открыть файл %s.
flatc_downloader_name: Flatbuffers Schema Compiler Downloader
flatc_downloader_desc: Программа для скачивания последней версии компилятора схемы Flatbuffers для данной платформы.
download_directory_arg: Директория для сохранения скачанных файлов
download_directory_arg: Директория для сохранения скачанных файлов
no_files_selected: Файлы не были выбраны.
no_directory_selected: Директория не была выбрана.
no_file_selected: Файл не был выбран.
45 changes: 25 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import sys
import errno
from locale import getdefaultlocale
from shutil import which
from tkinter import Tk
Expand Down Expand Up @@ -66,7 +67,7 @@ def get_flatc_path(root_path: str, allow_ask: bool, suppress_error: bool) -> str
if not allow_ask:
if suppress_error:
return ""
raise FileNotFoundError(i18n.t("main.executable_not_found") % "flatc")
raise FileNotFoundError(errno.ENOENT, i18n.t("main.executable_not_found"), "flatc")
if sys.platform == "win32":
filetypes = [(i18n.t("main.exe_filetype"), "*.exe")]
else:
Expand All @@ -75,12 +76,12 @@ def get_flatc_path(root_path: str, allow_ask: bool, suppress_error: bool) -> str
if flatc_path == "":
if suppress_error:
return ""
raise FileNotFoundError(i18n.t("main.executable_not_found") % "flatc")
raise FileNotFoundError(errno.ENOENT, i18n.t("main.executable_not_found"), "flatc")
flatc_path = which(os.path.split(flatc_path)[1], path=os.path.split(flatc_path)[0])
if flatc_path is None:
if suppress_error:
return ""
raise FileNotFoundError(i18n.t("main.executable_not_found") % "flatc")
raise FileNotFoundError(errno.ENOENT, i18n.t("main.executable_not_found"), "flatc")
return os.path.abspath(flatc_path)


Expand Down Expand Up @@ -109,28 +110,30 @@ def execute_deserialize(flatc_path: str, schema_path: str, binary_paths: list[st
:return: Код ошибки или строка об ошибке.
"""
if not os.path.isfile(flatc_path):
raise FileNotFoundError(i18n.t("main.file_not_found") % flatc_path)
raise FileNotFoundError(errno.ENOENT, i18n.t("main.file_not_found"), flatc_path)
if which(os.path.split(flatc_path)[1], path=os.path.split(flatc_path)[0]) is None:
raise FileNotFoundError(i18n.t("main.file_not_executable") % flatc_path)
raise FileNotFoundError(errno.ENOENT, i18n.t("main.file_not_executable"), flatc_path)
if schema_path == "":
schema_path = askopenfilename(title=i18n.t("main.tkinter_fbs_select"),
filetypes=[(i18n.t("main.fbs_filetype"), "*.fbs")])
if schema_path == "":
return os.EX_OK
elif not os.path.isfile(schema_path):
raise FileNotFoundError(i18n.t("main.file_not_found") % schema_path)
raise IOError(errno.EIO, i18n.t("main.no_file_selected"))
if not os.path.isfile(schema_path):
raise FileNotFoundError(errno.ENOENT, i18n.t("main.file_not_found"), schema_path)
schema_name = os.path.splitext(os.path.basename(schema_path))[0]
if len(binary_paths) < 1:
binary_paths = askopenfilenames(title=i18n.t("main.tkinter_binaries_select"), filetypes=[
(i18n.t("main.flatc_binary_filetype"), "*." + schema_name)])
if len(binary_paths) < 1:
return os.EX_OK
raise IOError(errno.EIO, i18n.t("main.no_files_selected"))
else:
binary_paths = [binary_path for binary_path in binary_paths if os.path.isfile(binary_path)]
if output_path == "":
output_path = askdirectory(title=i18n.t("main.tkinter_output_select"))
if output_path == "":
raise IOError(errno.EIO, i18n.t("main.no_directory_selected"))
elif not os.path.isdir(output_path):
raise FileNotFoundError(i18n.t("main.directory_not_found") % output_path)
raise FileNotFoundError(errno.ENOENT, i18n.t("main.directory_not_found"), output_path)
full_size = sum(os.stat(binary_path).st_size for binary_path in binary_paths)
with logging_redirect_tqdm():
pbar = tqdm(total=full_size, position=0, unit="B", unit_scale=True,
Expand Down Expand Up @@ -199,27 +202,29 @@ def execute_deserialize_batch(flatc_path: str, schemas_path: str, binaries_path:
:return: Код ошибки или строка об ошибке.
"""
if not os.path.isfile(flatc_path):
raise FileNotFoundError(i18n.t("main.file_not_found") % flatc_path)
raise FileNotFoundError(errno.ENOENT, i18n.t("main.file_not_found"), flatc_path)
if which(os.path.split(flatc_path)[1], path=os.path.split(flatc_path)[0]) is None:
raise FileNotFoundError(i18n.t("main.file_not_executable") % flatc_path)
raise FileNotFoundError(errno.ENOENT, i18n.t("main.file_not_executable"), flatc_path)
if schemas_path == "":
schemas_path = askdirectory(title=i18n.t("main.tkinter_fbs_directory_select"))
if schemas_path == "":
return os.EX_OK
elif not os.path.isdir(schemas_path):
raise FileNotFoundError(i18n.t("main.directory_not_found") % schemas_path)
raise IOError(errno.EIO, i18n.t("main.no_directory_selected"))
if not os.path.isdir(schemas_path):
raise FileNotFoundError(errno.ENOENT, i18n.t("main.directory_not_found"), schemas_path)
if binaries_path == "":
binaries_path = askdirectory(title=i18n.t("main.tkinter_binary_directory_select"))
if binaries_path == "":
return os.EX_OK
elif not os.path.isdir(binaries_path):
raise FileNotFoundError(i18n.t("main.directory_not_found") % binaries_path)
raise IOError(errno.EIO, i18n.t("main.no_directory_selected"))
if not os.path.isdir(binaries_path):
raise FileNotFoundError(errno.ENOENT, i18n.t("main.directory_not_found"), binaries_path)
if output_path == "":
output_path = askdirectory(title=i18n.t("main.tkinter_output_select"))
if output_path == "":
output_path = os.path.split(flatc_path)[0]
elif not os.path.isdir(output_path):
raise FileNotFoundError(i18n.t("main.directory_not_found") % output_path)
if output_path == "":
raise IOError(errno.EIO, i18n.t("main.no_directory_selected"))
if not os.path.isdir(output_path):
raise FileNotFoundError(errno.ENOENT, i18n.t("main.directory_not_found"), output_path)
schema_paths = get_schema_paths(schemas_path)
if len(schema_paths) < 1:
logging.info(i18n.t("main.no_schema_files_found"), binaries_path)
Expand Down

0 comments on commit 08efa24

Please sign in to comment.