-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.py
95 lines (83 loc) · 2.83 KB
/
config.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
#
# This file is part of Mediary's Caspar Client.
# Copyright (C) 2018 Mediary Limited. All rights reserved.
#
'''
Helper class that wraps a ConfigParser
'''
from configparser import *
SERVER='server'
CHANNEL='channel'
class config(object):
'''
Proxy class to the config on disk.
Wraps a ConfigParser, but with syntactic sugar.
'''
def __init__(self, filename='config.ini'):
'''
The initialiser also loads the config from disk, assuming it can.
(If the file does not exist, you get an empty config.)
'''
self.filename = filename
self.reload()
def reload(self, filename=None):
if filename is None:
filename=self.filename
self.parser = ConfigParser()
self.parser.read(filename)
def write(self, filename=None):
''' Write to disk. Defaults to the filename we read from. '''
if filename is None:
filename=self.filename
with open(filename, 'w') as cf:
self.parser.write(cf)
def get(self, section, item, default=None):
'''
Config accessor.
Returns the default if the section or item do not exist.
'''
try:
return self.parser.get(section,item)
except:
return default
def get_int(self, section, item, default=0):
'''
Convenience wrapper to get() which coerces the result to an int.
'''
return int(self.get(section, item, default))
def get_bool(self, section, item, default=0):
'''
Convenience wrapper to get() which does the boolean interpretation thing
'''
try:
return self.parser.getboolean(section,item)
except:
return default
def put(self, section, item, value):
'''
Config accessor.
Creates the section if it doesn't exist.
Overwrites the item if it doesn't exist.
Does NOT write - call write() for that.
'''
if not self.parser.has_section(section):
self.parser.add_section(section)
self.parser.set(section, item, value)
def section(self, section):
'''
Bulk access to the dict for a config section.
Creates the section if it doesn't already exist.
(Note that keys and values are always Unicode strings.)
(Note that configparser matches case-insensitively!)
'''
if not self.parser.has_section(section):
self.parser.add_section(section)
return self.parser._sections[section]
def channel(self):
'''
Quick access to the Caspar channel this system is to use globally.
Configure as
[server]
channel=1
'''
return self.get_int(SERVER, CHANNEL, 1)