-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhonieboxConfigChanger.py
executable file
·135 lines (117 loc) · 4.22 KB
/
PhonieboxConfigChanger.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#import json
import os,sys,signal
#from mpd import MPDClient
import configparser
#from RawConfigParserExtended import RawConfigParserExtended
from Phoniebox import Phoniebox
# get absolute path of this script
dir_path = os.path.dirname(os.path.realpath(__file__))
defaultconfigFilePath = os.path.join(dir_path,'./phoniebox.conf')
def is_int(s):
""" return True if string is an int """
try:
int(s)
return True
except ValueError:
return False
def str2bool(s):
""" convert string to a python boolean """
return s.lower() in ("yes", "true", "t", "1")
def str2num(s):
""" convert string to an int or a float """
try:
return int(s)
except ValueError:
return float(s)
class PhonieboxConfigChanger(Phoniebox):
def __init__(self,configFilePath=defaultconfigFilePath):
Phoniebox.__init__(self,configFilePath)
def assigncard(self,cardid,uri):
section = cardid
# set uri and cardid for card (section = cardid)
if not section in self.cardAssignments.sections():
self.cardAssignments.add_section(section)
self.cardAssignments.set(section,"cardid",cardid)
self.cardAssignments.set(section,"uri",uri)
# write updated assignments to file
with open(self.config['card_assignments_file'], 'w') as cardAssignmentsFile:
self.cardAssignments.write(cardAssignmentsFile)
def removecard(self,cardid):
section = cardid
if section in self.cardAssignments.sections():
self.cardAssignments.remove_section(section)
# write updated assignments to file
with open(self.config['card_assignments_file'], 'w') as f:
self.cardAssignments.write(f)
def set(self,section,key,value):
try:
num = int(section)
parser = self.cardAssignments
config_file = self.config.get("phoniebox","card_assignments_file")
except ValueError:
parser = self.config
config_file = configFilePath
# update value
try:
parser.set(section,key,value)
self.debug("Set {} = {} in section {}".format(key,value,section))
except configparser.NoSectionError as e:
raise configparser.NoSectionError, e
# write to file
# with open(config_file, 'w') as f:
# parser.write(f)
def get(self,section,t="ini"):
try:
num = int(section)
parser = self.cardAssignments
except ValueError:
parser = self.config
if t == "json":
print(parser.as_json(section))
elif t == "dict":
print(parser.as_dict(section))
else:
print(parser.print_ini(section))
def print_usage(self):
print("Usage: {} set ".format(sys.argv[0]))
if __name__ == "__main__":
cmdlist = ["assigncard","removecard","set","get"]
if len(sys.argv) < 1:
sys.exit()
else:
if sys.argv[1] in cmdlist:
configFilePath = defaultconfigFilePath
cmd = sys.argv[1]
shift = 0
else:
configFilePath = sys.argv[1]
cmd = sys.argv[2]
shift = 1
ConfigChanger = PhonieboxConfigChanger(configFilePath)
try:
if cmd == "assigncard":
cardid = sys.argv[2+shift]
uri = sys.argv[3+shift]
ConfigChanger.assigncard(cardid,uri)
elif cmd == "removecard":
cardid = sys.argv[2+shift]
ConfigChanger.removecard(cardid)
elif cmd == "set":
section = sys.argv[2+shift]
key = sys.argv[3+shift]
value = sys.argv[4+shift]
ConfigChanger.set(section,key,value)
elif cmd == "get":
section = sys.argv[2+shift]
try:
t = sys.argv[3+shift]
except:
t = "ini"
ConfigChanger.get(section,t)
else:
# will never be reached
print("supported commands are {} and {}".format(", ".join(cmdlist[:-1]),cmdlist[-1]))
except:
self.print_usage()