forked from iBaa/PlexConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.py
executable file
·102 lines (75 loc) · 2.57 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
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
import sys
from os import sep
import ConfigParser
from Debug import * # dprint()
"""
Global Settings...
PMS: plexgdm, ip_pms, port_pms
DNS: ip_dnsmaster - IP of Router, ISP's DNS, ... [dflt: google public DNS]
HTTP: ip_httpforward, port_httpforward
"""
g_settings = { \
'enable_plexgdm' :('True', 'False'), \
'ip_pms' :('192.168.178.10',), \
'port_pms' :('32400',), \
\
'enable_dnsserver':('True', 'False'), \
'ip_dnsmaster' :('8.8.8.8',), \
\
'ip_webserver' :('0.0.0.0',), \
'port_webserver' :('80',), \
\
'loglevel' :('Normal', 'High') \
}
class CSettings():
def __init__(self):
dprint(__name__, 1, "init class CSettings")
self.cfg = None
self.section = 'PlexConnect'
self.loadSettings()
self.checkSection()
# load/save config
def loadSettings(self):
dprint(__name__, 1, "load settings")
# options -> default
dflt = {}
for opt in g_settings:
dflt[opt] = g_settings[opt][0]
# load settings
self.cfg = ConfigParser.SafeConfigParser()
self.cfg.read(self.getSettingsFile())
def saveSettings(self):
dprint(__name__, 1, "save settings")
f = open(self.getSettingsFile(), 'wb')
self.cfg.write(f)
f.close()
def getSettingsFile(self):
return sys.path[0] + sep + "Settings.cfg"
def checkSection(self):
modify = False
# check for existing section
if not self.cfg.has_section(self.section):
modify = True
self.cfg.add_section(self.section)
dprint(__name__, 0, "add section {0}", self.section)
for opt in g_settings:
if not self.cfg.has_option(self.section, opt):
modify = True
self.cfg.set(self.section, opt, g_settings[opt][0])
dprint(__name__, 0, "add option {0}={1}", opt, g_settings[opt][0])
# save if changed
if modify:
self.saveSettings()
# access/modify PlexConnect settings
def getSetting(self, option):
dprint(__name__, 1, "getsetting {0}={1}", option, self.cfg.get(self.section, option))
return self.cfg.get(self.section, option)
if __name__=="__main__":
Settings = CSettings()
option = 'enable_plexgdm'
print Settings.getSetting(option)
option = 'enable_dnsserver'
print Settings.getSetting(option)
Settings.saveSettings()
del Settings