Update filesio.py, main.py

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2022-09-08 09:00:29 +07:00
parent a3031f78cd
commit e866ff0b91
2 changed files with 26 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from json import load, dump
from json import load, dump, JSONDecodeError
from os.path import exists
from os import remove
@@ -18,12 +18,22 @@ class FilesIO:
with open(self.file_name, encoding="utf-8", mode="w") as f:
dump(self.default_values, f, indent=4, separators=(",", " : "))
def check_file_structure(self, file_data):
if self.default_values is not None:
return file_data.keys() == self.default_values.keys()
def get_data(self):
ret_data, err_msg = False, ""
try:
with open(self.file_name, encoding="utf-8", mode="r") as f:
ret_data = load(f)
return ret_data
if not self.check_file_structure(ret_data):
err_msg = "В файле настроек не хватает данных. Пересоздайте файл с настройками."
ret_data = False
except FileNotFoundError:
err_msg = "Файл не найден. Создан новый шаблонный файл."
self.create_default_file()
# ret_data = self.default_values
return False
except JSONDecodeError:
err_msg = "Ошибка загрузки файла, проверьте на наличие ошибок."
finally:
return ret_data, err_msg

21
main.py
View File

@@ -2,21 +2,22 @@
# -*- coding: utf-8 -*-
from sys import exit, argv
from os import system
from os import system, getcwd
from subprocess import Popen
from filesio import FilesIO
from m_match_name.main import MatchIO
from time import sleep
default_values = {
"restart_time": 60,
"restart_message": "Плановая перезагрузка компьютера через 1 минуту!",
"pc": {
"2": [ # Пятидневка
"1": [ # Пятидневка
"IT01",
"IT02"
],
"7": [ # Цех
"2": [ # Цех
"PIS09",
"PIS10"
]
@@ -37,6 +38,7 @@ def main(comp_key, is_debug):
if is_debug is False:
# system("shutdown /m \\\{} /r /f /t 60 /c \"Плановая перезагрузка компьютера через 1 минуту!\"".format(formatted_pc_name))
Popen("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message)).wait()
sleep(2)
else:
print("shutdown /m \\\{} /r /f /t {} /c {}".format(formatted_pc_name, restart_time, restart_message))
else:
@@ -44,18 +46,19 @@ def main(comp_key, is_debug):
if __name__ == "__main__":
if len(argv) < 3:
print("Укажите номер запускаемой области!\n2 - Кабинеты; 7 - Цех")
print(argv)
if len(argv) < 2:
print("Укажите номер запускаемой области!\n1 - Кабинеты; 2 - Цех")
exit()
try:
filesio = FilesIO(default_values=default_values)
loaded_file = filesio.get_data()
loaded_file, err_msg = filesio.get_data()
if loaded_file is False:
raise OSError
raise Exception(err_msg)
matchio = MatchIO()
main(argv[1], argv[2].strip() == "debug")
except OSError:
print("Файл с настройками не найден. Создан файл со стандартными настройками.")
except Exception as exc:
print(exc)
exit()
except KeyboardInterrupt:
exit()