-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconfig_file.py
executable file
·99 lines (87 loc) · 3.73 KB
/
config_file.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
# This file is part of pi-jukebox.
#
# pi-jukebox is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pi-jukebox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with pi-jukebox. If not, see < http://www.gnu.org/licenses/ >.
#
# (C) 2015- by Mark Zwart, <[email protected]>
import ConfigParser
import subprocess
class ConfigFile(object):
def __init__(self):
self.parser = ConfigParser.ConfigParser()
self.parser.optionxform = str
self.parser.read("pi-jukebox.conf")
# MPD configuration settings
self.settings = []
self.radio_stations = []
self.settings.append({'section': 'MPD Settings', 'key': 'host', 'value': 'localhost', 'first_time': False})
self.settings.append({'section': 'MPD Settings', 'key': 'port', 'value': 6600, 'first_time': False})
self.settings.append({'section': 'MPD Settings', 'key': 'music directory', 'value': None, 'first_time': True})
self.initialize()
def initialize(self):
for setting in self.settings:
if self.setting_exists(setting['section'], setting['key']):
setting['value'] = self.setting_get(setting['section'], setting['key'])
elif not setting['first_time']:
self.setting_set(setting['section'], setting['key'], setting['value'])
for setting in self.settings:
if setting['section'] == 'Radio stations':
self.radio_stations.append((setting['key'], setting['value']))
def setting_get(self, section, key):
if self.setting_exists(section, key):
value = self.parser.get(section, key)
return value
def setting_set(self, section, key, value):
""" Write a setting to the configuration file
"""
file = open("pi-jukebox.conf", 'w')
try:
self.parser.add_section(section)
except Exception:
pass
self.parser.set(section, key, value)
self.parser.write(file)
file.close()
def setting_remove(self, section, key):
""" Remove a setting to the configuration file
"""
file = open("pi-jukebox.conf", 'w')
try:
self.parser.remove_option(section, key)
except Exception:
pass
self.parser.write(file)
file.close()
def section_exists(self, section):
return self.parser.has_section(section)
def setting_exists(self, section, key):
return self.parser.has_option(section, key)
def radio_station_set(self, name, URL):
""" Edits or creates radio station entry """
self.setting_set('Radio stations', name, URL)
def radio_stations_get(self):
""" Get's radio stations from the configuration file and returns them in a list """
self.radio_stations = []
options = self.parser.options('Radio stations')
for option in options:
description = option
URL = self.setting_get('Radio stations', option)
self.radio_stations.append((description, URL))
return self.radio_stations
def section_get(self, section):
dict1 = {}
options = self.parser.options(section)
for option in options:
setting = self.parser.getboolean(section, option)
return dict1
config_file = ConfigFile()