-
Notifications
You must be signed in to change notification settings - Fork 1
/
asztal.py
executable file
·174 lines (138 loc) · 5.06 KB
/
asztal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
import sys,json,os,datetime,subprocess,shutil,time
#to avoid asztal creating files from where its ran
curdir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0,os.path.join(curdir,'storage'))
debug = 1
#clear log file
open(os.path.join(curdir,'log'),'w').close
#====================functions=======================
#this refreshes the debug value for the main file i think?
def refresh():
global debug
from settings import debug
#======================init=========================
#import settings, create file if failed
try: from settings import subSorter
except Exception as e:
backups = os.listdir(curdir+'/backups/')
if 'settings_backup' in backups:
shutil.copyfile('backups/settings_default','storage/settings.py')
time.sleep(0.1)
#from settings import debug
elif 'settings_default' in backups:
shutil.copyfile('backups/settings_default','storage/settings.py')
time.sleep(0.1)
#from settings import debug
else:
print('No settings.py in directory and no default_settings found.\n Check log for info.')
with open(os.path.join(curdir,'log'),'a') as f:
f.write(f'no settings in directory ({curdir}).')
sys.exit()
#get version number
try:
vrs = float(
subprocess.Popen(
['cat' ,os.path.join(curdir,'changelog')],
stdout=subprocess.PIPE
)
.communicate()[0]
.decode('utf-8')
.split('\n')[0]
.replace(':','')
)
except:
vrs = 0.0
#======================main==========================
def start(args,_input=None):
import ui
from ui import createUser,editUser,listUsers,tWidth,tHeight,dbg,clr
from api import Student
clr(1)
print('\033[?25l')
##initialize student object based on offline mode
offline = False
offset = 0
if len(args) > 1:
for a in args:
if a == '-o':
offline = True
elif '--offset' in a:
off = a.replace('--offset=','')
if off.isdigit():
offset = int(off)
clr()
dbg('INIT :',time=0)
#get user data
try:
try:
from usercfg import users
except:
if 'usercfg_backup' in os.listdir(curdir):
shutil.copyfile(os.path.join(curdir,'usercfg_backup'),os.path.join(curdir,'usercfg.py'))
time.sleep(0.2)
from usercfg import users
else:
#if none is present create with createUser
users = createUser()
clr(f=1)
#add default attribute to users
change = False
for i,u in enumerate(users):
if not 'isDefault' in u.keys():
users[i]['isDefault'] = 'False'
change = True
#add change to usercfg, make backup
if change:
shutil.copyfile(os.path.join(curdir,'usercfg.py'),os.path.join(curdir,'usercfg_backup'))
with open(os.path.join(curdir,'usercfg.py'),'w') as f:
f.write('users = '+json.dumps(users,indent=4))
#if there's one user move forward
if len(users) == 1:
usr,pwd,ist = users[0]['usr'],users[0]['pwd'],users[0]['ist']
#if theres more than one try to choose default
if len(users) > 1:
found = False
for u in users:
if u['isDefault'] == 'True':
usr,pwd,ist = u['usr'],u['pwd'],u['ist']
break
#if theres no default use the first one in the list
else:
usr,pwd,ist = users[0]['usr'],users[0]['pwd'],users[0]['ist']
#print('\n\n')
#usr,pwd,ist,_ = [i for i in listUsers().values()]
clr()
##create student object
userIndex = [u['usr'] for u in users].index(usr)
user = Student(usr,pwd,ist,offline=offline,logger=dbg)
##detect and fix errors in user
print('\n\n')
err = user.start()
while err:
print('\n')
for u in users:
if u['isDefault'] == 'True':
user = u
usr,pwd,ist = editUser(user)
clr()
user = Student(usr,pwd,ist,offline=offline,logger=dbg)
print('\n\n')
err = user.start()
except Exception as e:
raise e
##start ui based on shortcut
shortcut = None
if len(args) > 1:
#shortcuts: [grades,recents,timetable,profiles,settings]
opt = args[1]
options = ['m','r','t','p','s']
shortcuts = [str(i) for i in range(len(options))]
for s,o in zip(shortcuts,options):
if '-'+o == opt:
shortcut = s
dbg('UI :',time=0)
mode = ('offline' if user.maci == 'offline' else 'online')
ui.start(shortcut=shortcut,_debug=debug,_mode=mode,_offset=offset,_input=_input)
if __name__ == "__main__":
start(sys.argv)