-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.py
49 lines (36 loc) · 1.24 KB
/
state.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
import json
import os
import time
data_folder = "users"
if not os.path.exists(data_folder):
os.makedirs(data_folder)
class State:
def __init__(self, _session):
self.userId = _session.user.userId or None
def load_user_data(self):
user_data = {}
try:
with open(self.data_file()) as data_file:
user_data = json.load(data_file)
except IOError as e:
pass
if len(user_data) == 0:
user_data = {'createdAt': time.strftime("%c")}
return user_data
def update_busstop(self, naptan_code):
user_data = self.load_user_data()
user_data['mystop'] = naptan_code
self.save_user_data(user_data)
def get_busstop(self):
return self.load_user_data().get('mystop', None)
def save_user_data(self, user_data):
with open(self.data_file(), 'w') as outfile:
json.dump(user_data, outfile)
def data_file(self):
return "{}/{}".format(data_folder, self.userId)
def get_current(self):
return self.load_user_data().get('current', None)
def set_current(self, state):
user_data = self.load_user_data()
user_data['current'] = state
self.save_user_data()