-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegistry.py
58 lines (47 loc) · 1.38 KB
/
Registry.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
# -*- coding: utf-8 -*-
import os
import pickle
class Registry:
__committed = True
__path = None
__data = dict()
def __init__(self, path):
self.__path = path
if os.path.isfile(path):
with open(path, 'rb') as f:
self.__data = dict(pickle.load(f))
def get(self, key, default=None):
if key in self.__data:
return self.__data[key]
return default
def set(self, key, value, commit=True):
if key:
self.__data[key] = value
self.__committed = False
if commit:
self.commit()
return True
return False
def update(self, key, value, commit=True):
if hasattr(self.get(key), 'update'):
self.get(key).update(value)
self.__committed = False
if commit:
self.commit()
return True
return self.set(key, value, commit)
def commit(self):
if not self.__committed:
with open(self.__path, 'wb') as f:
pickle.dump(self.__data, f, 1)
self.__committed = True
return self.__committed
def hasKey(self, key):
return key in self.__data
def keys(self):
return self.__data.keys()
class Key:
CSRF_TOKEN = 'token'
COOKIES = 'cookies'
def __init__(self):
pass