2021-01-21 14:00:58 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import pip
|
2021-04-24 03:08:43 +03:00
|
|
|
import time
|
2021-01-21 14:00:58 +02:00
|
|
|
import json
|
|
|
|
import os
|
2021-04-24 03:08:43 +03:00
|
|
|
import shutil
|
|
|
|
import gzip
|
|
|
|
from datetime import datetime
|
2021-01-21 14:00:58 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
path = Path(__file__).resolve().parent
|
|
|
|
sounds_folder = str(Path(str(path)+"/sounds/")) + os.sep
|
|
|
|
files_folder = str(Path(str(path)+"/files/")) + os.sep
|
2021-04-24 03:08:43 +03:00
|
|
|
logs_folder = str(Path(str(path)+"/logs/")) + os.sep
|
|
|
|
|
|
|
|
|
|
|
|
default_config = {
|
|
|
|
"debug": False,
|
|
|
|
"shutdown_timeout": 30,
|
|
|
|
"shutdown_enabled": True,
|
|
|
|
"start": "shift+f7",
|
|
|
|
"stop": "shift+f8",
|
|
|
|
"telegram_enabled": False,
|
|
|
|
"use_colors": True,
|
|
|
|
"run_fullscreen": False,
|
|
|
|
"use_rpc": True,
|
|
|
|
"sounds": True,
|
|
|
|
"end_mode": "shutdown",
|
|
|
|
"obs_exe": None,
|
|
|
|
"obs_core": None,
|
|
|
|
"update_check": True,
|
|
|
|
"write_logs": True,
|
|
|
|
"log_size": 512
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Функция проверки размера файла
|
|
|
|
def checkSize():
|
|
|
|
global logs_folder
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
while i < 2:
|
|
|
|
try:
|
|
|
|
log = os.stat(logs_folder + 'latest.log')
|
|
|
|
|
|
|
|
if (log.st_size / 1024) > getConfig("log_size"):
|
|
|
|
with open(logs_folder + 'latest.log', 'rb') as f_in:
|
|
|
|
with gzip.open(f'{logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip', 'wb') as f_out:
|
|
|
|
shutil.copyfileobj(f_in, f_out)
|
|
|
|
|
|
|
|
if getConfig("debug"):
|
|
|
|
print(f'Copied {logs_folder}{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.zip')
|
|
|
|
|
|
|
|
open(logs_folder + 'latest.log', 'w').close()
|
|
|
|
|
|
|
|
i = 2
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
if getConfig("debug"):
|
|
|
|
print('Log file not found')
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
log = open(logs_folder + 'latest.log', 'a')
|
|
|
|
open(logs_folder + 'latest.log', 'a').close()
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
os.mkdir(logs_folder)
|
|
|
|
log = open(logs_folder + 'latest.log', 'a')
|
|
|
|
open(logs_folder + 'latest.log', 'a').close()
|
|
|
|
except:
|
|
|
|
if getConfig("debug"):
|
|
|
|
time.sleep(2)
|
|
|
|
print('Log file could not be created')
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
# Функция добавления в лог
|
|
|
|
def appendLog(message):
|
|
|
|
|
|
|
|
if getConfig("write_logs"):
|
|
|
|
|
|
|
|
global logs_folder
|
|
|
|
|
|
|
|
checkSize()
|
|
|
|
|
|
|
|
try:
|
|
|
|
log = open(logs_folder + 'latest.log', 'a')
|
|
|
|
open(logs_folder + 'latest.log', 'a').close()
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
os.mkdir(logs_folder)
|
|
|
|
log = open(logs_folder + 'latest.log', 'a')
|
|
|
|
open(logs_folder + 'latest.log', 'a').close()
|
|
|
|
except:
|
|
|
|
time.sleep(2)
|
|
|
|
print('Log file could not be created')
|
|
|
|
|
|
|
|
log.write(f'[{datetime.now().strftime("%H:%M:%S | %d.%m.%Y")}] {message}\n')
|
|
|
|
log.close()
|
|
|
|
|
|
|
|
|
|
|
|
# Функция добавления переменных, если их нет
|
|
|
|
def repairConfig(some_dic):
|
|
|
|
global files_folder
|
|
|
|
global default_config
|
|
|
|
|
|
|
|
for key in some_dic:
|
|
|
|
try:
|
|
|
|
some_dic[key]
|
|
|
|
except NameError:
|
|
|
|
some_dic[key] = default_config[key]
|
|
|
|
|
|
|
|
saveJson(files_folder+'config.json', some_dic)
|
|
|
|
|
2021-01-21 14:00:58 +02:00
|
|
|
|
|
|
|
def getConfig(some_var):
|
|
|
|
global files_folder
|
2021-04-24 03:08:43 +03:00
|
|
|
global default_config
|
2021-01-21 14:00:58 +02:00
|
|
|
|
|
|
|
if os.path.exists(files_folder):
|
|
|
|
if not os.path.exists(files_folder+'config.json'):
|
2021-04-24 03:08:43 +03:00
|
|
|
temp_config_list = default_config
|
|
|
|
|
2021-01-21 14:00:58 +02:00
|
|
|
saveJson(files_folder+'config.json', temp_config_list)
|
2021-01-22 14:53:33 +02:00
|
|
|
return temp_config_list[some_var]
|
2021-01-21 14:00:58 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
|
|
|
|
config_list = json.load(json_file)
|
2021-04-06 01:32:55 +03:00
|
|
|
json_file.close()
|
2021-04-24 03:08:43 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
return config_list[some_var]
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
repairConfig(config_list)
|
|
|
|
config_list = json.load(json_file)
|
|
|
|
json_file.close()
|
|
|
|
return config_list[some_var]
|
|
|
|
except:
|
|
|
|
return default_config[some_var]
|
2021-01-21 14:00:58 +02:00
|
|
|
except:
|
|
|
|
return "Error"
|
|
|
|
else:
|
|
|
|
os.mkdir(files_folder)
|
2021-01-22 14:53:33 +02:00
|
|
|
if not os.path.exists(files_folder+'config.json'):
|
2021-04-24 03:08:43 +03:00
|
|
|
temp_config_list = default_config
|
|
|
|
|
2021-01-22 14:53:33 +02:00
|
|
|
saveJson(files_folder+'config.json', temp_config_list)
|
|
|
|
return temp_config_list[some_var]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with open(f"{files_folder}config.json", encoding="utf-8") as json_file:
|
|
|
|
config_list = json.load(json_file)
|
2021-04-06 01:32:55 +03:00
|
|
|
json_file.close()
|
2021-04-24 03:08:43 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
return config_list[some_var]
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
repairConfig(config_list)
|
|
|
|
config_list = json.load(json_file)
|
|
|
|
json_file.close()
|
|
|
|
return config_list[some_var]
|
|
|
|
except:
|
|
|
|
return default_config[some_var]
|
|
|
|
|
2021-01-22 14:53:33 +02:00
|
|
|
except:
|
|
|
|
return "Error"
|
2021-01-21 14:00:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def saveJson(filename, value):
|
|
|
|
with open(filename, 'w', encoding="utf-8") as f:
|
2021-04-06 01:32:55 +03:00
|
|
|
json.dump(value, f, indent=4, ensure_ascii=False)
|
|
|
|
f.close()
|