forked from 1812z/sleepy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
47 lines (38 loc) · 1.38 KB
/
data.py
File metadata and controls
47 lines (38 loc) · 1.38 KB
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
# coding: utf-8
import json
import os
import utils as u
from jsonc_parser.parser import JsoncParser as jsonp
def initJson():
try:
jsonData = jsonp.parse_file('example.jsonc', encoding='utf-8')
with open('data.json', 'w+', encoding='utf-8') as file:
json.dump(jsonData, file, indent=4, ensure_ascii=False)
except:
u.error('Create data.json failed')
raise
class data:
def __init__(self):
if not os.path.exists('data.json'):
u.warning('data.json not exist, creating')
initJson()
with open('data.json', 'r', encoding='utf-8') as file:
self.data = json.load(file)
def load(self):
with open('data.json', 'r', encoding='utf-8') as file:
self.data = json.load(file)
def save(self):
with open('data.json', 'w+', encoding='utf-8') as file:
json.dump(self.data, file, indent=4, ensure_ascii=False)
def dset(self, name, value):
self.data[name] = value
with open('data.json', 'w+', encoding='utf-8') as file:
json.dump(self.data, file, indent=4, ensure_ascii=False)
def dget(self, name):
with open('data.json', 'r', encoding='utf-8') as file:
self.data = json.load(file)
try:
gotdata = self.data[name]
except:
gotdata = None
return gotdata