-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathhou_settings.py
More file actions
59 lines (40 loc) · 1.33 KB
/
hou_settings.py
File metadata and controls
59 lines (40 loc) · 1.33 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
"""
Simple JSON based settings manager
Stores the json files under Dcos/houdinixx.x/tool_name.json
Usage:
import hou_settings
settings = hou_settings.Settings("my_tool_name")
settings.set("my_setting", "my_value")
print settings.value("my_setting")
"""
import os
import json
class Settings(object):
def __init__(self, toolname=None, filepath=None):
self.filename = None
if filepath:
self.filename = filepath
if toolname:
self.filename = os.path.join(os.getenv("HOUDINI_USER_PREF_DIR"), toolname + ".json")
if not self.filename:
self.filename = os.path.join(os.getenv("HOUDINI_USER_PREF_DIR"), "user_settings.json")
self._settings_dic = {}
if os.path.exists(self.filename):
self._load()
def set(self, key, value):
self._settings_dic[key] = value
self._save()
def value(self, key):
if key in self._settings_dic:
return self._settings_dic[key]
else:
return None
def _load(self):
with open(self.filename, 'r') as fp:
try:
self._settings_dic = json.load(fp)
except:
pass
def _save(self):
with open(self.filename, 'w') as fp:
json.dump(self._settings_dic, fp)