mirror of
https://github.com/Paillat-dev/FABLE.git
synced 2026-01-02 09:16:20 +00:00
feat(main.py): move check_license_agreement function to a separate module for better organization and reusability
feat(utils/license.py): create a new module to handle license agreement checking and display
This commit is contained in:
50
main.py
50
main.py
@@ -2,64 +2,16 @@ import os
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import yaml
|
import yaml
|
||||||
import hashlib
|
|
||||||
|
|
||||||
from pydoc import pager
|
|
||||||
from sys import platform
|
|
||||||
from utils.settings import settings
|
from utils.settings import settings
|
||||||
from classes.channel import Channel
|
from classes.channel import Channel
|
||||||
from utils.config import loadingmessage, bcolors
|
from utils.config import loadingmessage, bcolors
|
||||||
from utils.misc import clear_screen, printm, getenv
|
from utils.misc import clear_screen, printm, getenv
|
||||||
from utils.openaicaller import openai
|
from utils.openaicaller import openai
|
||||||
|
from utils.license import check_license_agreement
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
async def check_license_agreement():
|
|
||||||
h = hashlib.sha256()
|
|
||||||
with open('LICENSE', 'rb') as file:
|
|
||||||
chunk = file.read(1024)
|
|
||||||
while chunk:
|
|
||||||
h.update(chunk)
|
|
||||||
chunk = file.read(1024)
|
|
||||||
license_hash = h.hexdigest()
|
|
||||||
if settings.license_agreement_accepted == False or settings.license_agreement_accepted == None:
|
|
||||||
printm("You have to accept the license agreement before using this program.")
|
|
||||||
elif settings.license_agreement_accepted == True:
|
|
||||||
if settings.license_agreement_hash != license_hash:
|
|
||||||
printm("The license agreement has been updated since you last accepted it. Please accept it again.")
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
printm("There was an error with the license agreement. Please accept it again.")
|
|
||||||
while True:
|
|
||||||
printm('\n\n')
|
|
||||||
inp = input("Type p to print the license agreement, a to accept it, q to quit the program or o to open the LICENSE in your default text editor: ")
|
|
||||||
if inp == "p":
|
|
||||||
input('You can use the "Enter" key to scroll down and the "q" key to quit the license agreement. Press "Enter" to continue.')
|
|
||||||
with open('LICENSE', 'r', encoding='utf-8') as f:
|
|
||||||
pager(f.read())
|
|
||||||
elif inp == "a":
|
|
||||||
settings.set_setting('license_agreement_accepted', True)
|
|
||||||
settings.set_setting('license_agreement_hash', license_hash)
|
|
||||||
if os.path.exists('LICENSE.txt'):
|
|
||||||
os.remove('LICENSE.txt')
|
|
||||||
printm("License agreement accepted.")
|
|
||||||
return True
|
|
||||||
elif inp == "q":
|
|
||||||
printm("Quitting the program...")
|
|
||||||
raise KeyboardInterrupt
|
|
||||||
elif inp == "o":
|
|
||||||
dict_os_commands = {
|
|
||||||
"linux": "xdg-open",
|
|
||||||
"win32": "start",
|
|
||||||
"darwin": "open"
|
|
||||||
}
|
|
||||||
#copy the license file to a temporary txt file
|
|
||||||
with open('LICENSE', 'r', encoding='utf-8') as f:
|
|
||||||
with open('LICENSE.txt', 'w', encoding='utf-8') as f2:
|
|
||||||
f2.write(f.read())
|
|
||||||
os.system(f"{dict_os_commands[platform]} LICENSE.txt")
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
clear_screen()
|
clear_screen()
|
||||||
await check_license_agreement()
|
await check_license_agreement()
|
||||||
|
|||||||
53
utils/license.py
Normal file
53
utils/license.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
|
||||||
|
from utils.settings import settings
|
||||||
|
from utils.misc import printm
|
||||||
|
from pydoc import pager
|
||||||
|
from sys import platform
|
||||||
|
|
||||||
|
async def check_license_agreement():
|
||||||
|
h = hashlib.sha256()
|
||||||
|
with open('LICENSE', 'rb') as file:
|
||||||
|
chunk = file.read(1024)
|
||||||
|
while chunk:
|
||||||
|
h.update(chunk)
|
||||||
|
chunk = file.read(1024)
|
||||||
|
license_hash = h.hexdigest()
|
||||||
|
if settings.license_agreement_accepted == False or settings.license_agreement_accepted == None:
|
||||||
|
printm("You have to accept the license agreement before using this program.")
|
||||||
|
elif settings.license_agreement_accepted == True:
|
||||||
|
if settings.license_agreement_hash != license_hash:
|
||||||
|
printm("The license agreement has been updated since you last accepted it. Please accept it again.")
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
printm("There was an error with the license agreement. Please accept it again.")
|
||||||
|
while True:
|
||||||
|
printm('\n\n')
|
||||||
|
inp = input("Type p to print the license agreement, a to accept it, q to quit the program or o to open the LICENSE in your default text editor: ")
|
||||||
|
if inp == "p":
|
||||||
|
input('You can use the "Enter" key to scroll down and the "q" key to quit the license agreement. Press "Enter" to continue.')
|
||||||
|
with open('LICENSE', 'r', encoding='utf-8') as f:
|
||||||
|
pager(f.read())
|
||||||
|
elif inp == "a":
|
||||||
|
settings.set_setting('license_agreement_accepted', True)
|
||||||
|
settings.set_setting('license_agreement_hash', license_hash)
|
||||||
|
if os.path.exists('LICENSE.txt'):
|
||||||
|
os.remove('LICENSE.txt')
|
||||||
|
printm("License agreement accepted.")
|
||||||
|
return True
|
||||||
|
elif inp == "q":
|
||||||
|
printm("Quitting the program...")
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
elif inp == "o":
|
||||||
|
dict_os_commands = {
|
||||||
|
"linux": "xdg-open",
|
||||||
|
"win32": "start",
|
||||||
|
"darwin": "open"
|
||||||
|
}
|
||||||
|
#copy the license file to a temporary txt file
|
||||||
|
with open('LICENSE', 'r', encoding='utf-8') as f:
|
||||||
|
with open('LICENSE.txt', 'w', encoding='utf-8') as f2:
|
||||||
|
f2.write(f.read())
|
||||||
|
os.system(f"{dict_os_commands[platform]} LICENSE.txt")
|
||||||
Reference in New Issue
Block a user