-
Notifications
You must be signed in to change notification settings - Fork 8
/
file_tokens.py
39 lines (33 loc) · 1.02 KB
/
file_tokens.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
import json
class FileTokens:
def __init__(self, filename):
self.filename = filename
self.__load(filename)
def __load(self, filename):
try:
open(filename, 'r')
except IOError:
self.tokens = {}
else:
json_str = open(filename, 'r').read()
if json_str:
self.tokens = json.loads(open(filename, 'r').read())
else:
self.tokens = {}
def __save(self):
with open(self.filename, 'w') as f:
json.dump(self.tokens, f, sort_keys=True, indent=4)
def add(self, _id, _type, token):
if self.tokens.get(_id):
self.tokens[_id].update({_type: token})
else:
self.tokens[_id] = {_type: token}
self.__save()
def get(self, _id, _type):
if self.tokens.get(_id):
if self.tokens[_id].get(_type):
return self.tokens[_id][_type]
else:
return None
else:
return None