-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.py
104 lines (93 loc) · 2.73 KB
/
session.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
import pickle
import time
import os
from .config import *
from .libjmf import *
from .api import *
class Session():
busy = False
config = os.environ['HOME'] + '/.config/jmf/'
def __init__(self):
self.last_change = self.get_last_change()
def save_session(self, file='session.pkl'):
print('Saving . . . ', end='')
session = self.matches
session_file = open(config+file, 'wb')
pickle.dump(session, session_file)
session_file.close()
print('done.')
self.set_last_change()
self.last_change = self.get_last_change()
def sure_save(self):
if self.last_change == self.get_last_change():
self.save_session()
return 0
else:
warning = True
prompt = 'Save file was altered by another process during this edit. Are you sure you would like to overwrite?'
if yn(prompt, 'n') == True:
self.save_session()
return 0
else:
prompt = 'Would you like to save this session as a new file?'
if yn(prompt, 'y') == True:
filename = 'session' + now() + '.pkl'
self.save_session(filename)
log_print('Session saved to ' + filename)
return 1
def load_session(self):
try:
session_file = open(config+'session.pkl', 'rb')
session = pickle.load(session_file)
session_file.close()
except FileNotFoundError:
session = None
return session
def retrieve_session(self):
session = self.load_session()
if session == None:
log_print('No session saved. Initializing empty session.')
else:
self.matches = session
self.user = session.user
self.last_change = self.get_last_change()
if self.user.logged_in():
username = self.user.identity()
self.prompt = c.bold + c.b + username + c.g + ' > ' + c.end
log_print('Logged in as ' + username)
else:
print('Type \'login\' to log in.')
def update_session(self):
session = self.load_session()
if session == None:
pass
else:
self.matches = session
self.last_change = self.get_last_change()
def set_last_change(self):
last_change_file = open(config+'last_change.pkl', 'wb')
pickle.dump(now(), last_change_file)
last_change_file.close()
def get_last_change(self):
try:
last_change_file = open(config+'last_change.pkl', 'rb')
last_change = pickle.load(last_change_file)
last_change_file.close()
return last_change
except (FileNotFoundError, EOFError) as e:
print('Error: ' + str(e) + ' (last_change.pkl)')
return None
def set_busy(self, busy):
busy_file = open(config+'busy.pkl', 'wb')
self.busy = busy
pickle.dump(busy, busy_file)
busy_file.close()
def check_busy(self):
try:
busy_file = open(config+'busy.pkl', 'rb')
busy = pickle.load(busy_file)
busy_file.close()
return busy
except (FileNotFoundError, EOFError) as e:
print('Warning: ' + str(e) + ' (busy.pkl)')
return False