This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
settings.py
93 lines (75 loc) · 2.79 KB
/
settings.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
# This file handles reading/writing settings from shotmanager.conf
#
import os
import threading
import shotLogger
import ConfigParser
logger = shotLogger.logger
settingsLock = threading.Lock()
if 'SOLOLINK_SANDBOX' in os.environ:
CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'sim/shotmanager.sandbox.conf')
CONFIG_FILE_BACKUP = os.path.join(os.path.dirname(__file__), 'sim/shotmanager.back')
else:
CONFIG_FILE = "/etc/shotmanager.conf"
CONFIG_FILE_BACKUP = "/etc/shotmanager.back"
CONFIG_FILE_EXT = "/usr/bin/extSettings.conf"
def writeSettingsThread(name, value):
settingsLock.acquire()
# write to the config file
config = ConfigParser.SafeConfigParser()
config.optionxform=str
config.read(CONFIG_FILE)
try:
config.set("shotManager", name, value)
except:
logger.log("Failed to write setting")
# back up the file
os.system("cp %s %s" % (CONFIG_FILE, CONFIG_FILE_BACKUP))
os.system("md5sum %s > %s.md5" % (CONFIG_FILE_BACKUP, CONFIG_FILE_BACKUP))
os.system("sync")
# modify config file and set md5
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
os.system("md5sum %s > %s.md5" % (CONFIG_FILE, CONFIG_FILE))
os.system("sync")
os.system("rm %s %s.md5" % (CONFIG_FILE_BACKUP, CONFIG_FILE_BACKUP))
os.system("sync")
logger.log("wrote setting: %s: %s"%(name, value))
settingsLock.release()
# reads and returns setting of the given name
# perhaps we shouldn't read the file for each setting, but that's something we can address
# when we have more settings
def readSetting(name):
# get our saved button mappings
config = ConfigParser.SafeConfigParser()
settingsLock.acquire()
# if the config file is not found, an empty list is returned and the "get"
# operations below fail
config.read(CONFIG_FILE)
settingsLock.release()
try:
return config.get("shotManager", name)
except:
logger.log("error reading %s"%(CONFIG_FILE,))
raise
return 0
def readSettingExt(name):
# get our saved button mappings for extended button functions
config = ConfigParser.SafeConfigParser()
settingsLock.acquire()
# if the config file is not found, an empty list is returned and the "get"
# operations below fail
config.read(CONFIG_FILE_EXT)
settingsLock.release()
try:
return config.get("extFunctions", name)
except:
logger.log("[Ext Func Settings]: Unable to read %s from %s"%(name, CONFIG_FILE_EXT,))
return 0
# starts our thread which writes out our setting
# note both name and value should be strings
def writeSetting(name, value):
thread = threading.Thread(name = "writeSettingsThread", target = writeSettingsThread, args = (name, value))
thread.daemon = True
thread.start()