Files
solon/pc_linux/src/main.py
T
2024-01-30 18:32:31 +01:00

123 lines
5.3 KiB
Python
Executable File

#!/bin/python3
from solenlib import grades as gr
from solenlib import networking as nw
from solenlib import schedule_week as sw
from solenlib import schedule_tdy_tmr as st
import sys
import getpass
import argparse
import os
from datetime import datetime
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="solen", description="solen, an enhanced version of the ŠkolaOnLine online school system")
parser.add_argument("-t", "--today-tomorrow", action="store_true", help="show the schedule for today and tomorrow")
parser.add_argument("-w", "--week", action="store_true", help="show the schedule for this week")
parser.add_argument("-g", "--grades", action="store_true", help="show the grades")
parser.add_argument("-u", "--username", action="store", default="", help="provide the username for login")
parser.add_argument("-p", "--password", action="store", default="", help="provide the password for login")
parser.add_argument("-l", "--local", action="store_true", help="only fetch local files, don't connect to the internet")
parser.add_argument("-d", "--delete-local-files", action="store_true", help="delete all local user data, including schedules, grades and user credentials")
parser.add_argument("-b", "--backup", action="store_true", help="back up all user data and store them in a .tar.gz archive")
parser.add_argument("-R", "--backup-restore", action="store", nargs=1, metavar="PATH", help="specify the path to a .tar.gz archive that was previously created with Solen")
args = parser.parse_args()
if args.delete_local_files:
try:
print("Removing all schedules and grades ...", end=" ")
os.system(f"rm -rf {os.environ['HOME']}/.local/lib/solendata/*")
print("done")
print("Removing all user credentials ...", end=" ")
os.system(f"rm -rf {os.environ['HOME']}/.local/lib/solendata/shadow")
print("done")
print("All user data removed successfully")
except:
print("Could not remove user data")
elif args.backup:
try:
nowtime = str(datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
try:
print("Creating the backup archive ...", end=" ")
os.system(f"tar cf solen-backup-{nowtime}.tar {os.getcwd()}")
print("done")
print("Compressing the archive ...", end=" ")
os.system(f"gzip {os.getcwd()}/solen-backup-{nowtime}.tar")
print("done")
print("Backup archive created successfully")
except:
print("The 'tar' utility or the 'gzip' utility is not installed")
except:
print("Could not backup user data")
elif args.backup_restore:
try:
backup_path = args.backup_restore
print("Preparing ...", end=" ")
os.system(f"mkdir {os.getcwd()}/.solentmp")
print("done")
print("Uncompressing the archive ...", end=" ")
os.system(f"gunzip {backup_path}")
print("done")
print("Unpacking the archive ...", end=" ")
os.system(f"tar xzjf {backup_path} {os.getcwd()}/.solentmp")
print("done")
print("Moving the files ...", end=" ")
os.system(f"mv {os.getcwd()}/.solentmp/* {os.environ['HOME']}/.local/lib/solendata")
print("done")
print("Cleaning up ...", end=" ")
os.system(f"rm -rf {os.getcwd()}/.solentmp")
print("done")
print("Backup restored successfully")
except:
print("Could not restore the backup")
else:
if args.username:
user = str(args.username)
else:
user = str(input("Zadejte přihlašovací jméno: "))
if args.password:
pwd = str(args.password)
else:
try:
pwd = str(getpass.getpass("Zadejte heslo: "))
except:
pwd = str(input("Zadejte heslo: "))
if not nw.login(user, pwd):
print("Login unsuccessful")
sys.exit(1)
else:
try:
if args.week:
if args.local:
sw.show_schedule(pwd, user, local=True)
else:
sw.show_schedule(pwd, user)
if args.today_tomorrow:
if args.local:
st.show_schedule(pwd, user, local=True)
else:
st.show_schedule(pwd, user)
if args.grades:
if args.local:
gr.show_grades(pwd, user, local=True)
else:
gr.show_grades(pwd, user)
if not args.grades and not args.today_tomorrow and not args.week:
if args.local:
sw.show_schedule(pwd, user, local=True)
st.show_schedule(pwd, user, local=True)
gr.show_grades(pwd, user, local=True)
else:
sw.show_schedule(pwd, user)
st.show_schedule(pwd, user)
gr.show_grades(pwd, user)
except:
print("Could not execute")