-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsettings.py
More file actions
67 lines (61 loc) · 1.94 KB
/
settings.py
File metadata and controls
67 lines (61 loc) · 1.94 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import os
# Configuration file path, depends on WORKDIR environment variable
configsrc = os.path.join(os.environ.get("WORKDIR", "."), "config", "config.json")
def getConfigRaw():
"""
Returns a file object opened in read mode for the raw config JSON file.
Caller is responsible for closing the file.
"""
return open(configsrc, 'r')
def loadConfig():
"""
Loads the configuration JSON from file.
If the config file does not exist, creates it with default values.
Returns the configuration as a Python dictionary.
"""
if not os.path.exists(configsrc):
default_config = """
{
"port": 80,
"debug": "off",
"lang": "eng",
"updatetime": 1800,
"append_date": true,
"append_random": true,
"enable_update_flag": true,
"force_ocr": false,
"openai_api_key":"",
"imap": {
"enabled": false,
"host": "",
"port": 993,
"ssl": true,
"username": "",
"password": "",
"folder": "INBOX",
"processed_folder": "Processed",
"delete_after_import": false,
"scan_interval": 300
},
"index": {
"Foldername/Filename": ["Keyword1"],
"Foldername2/Filename": ["Keyword1", "Keyword2", "Keyword3"]
}
}
"""
writeConfig(default_config)
with open(configsrc, 'r') as fp:
return json.load(fp)
def writeJsonConfig(config_dict):
"""
Accepts a dictionary, serializes it to JSON string, and writes to config file.
"""
json_str = json.dumps(config_dict, indent=4)
writeConfig(json_str)
def writeConfig(config_str):
"""
Writes a JSON string directly to the config file.
"""
with open(configsrc, "w") as f:
f.write(config_str)