1 ''' Read-only configuration database. ''' 49 "Cannot import the yaml library. Please install the python3 50 yaml package, on many distributions known as python3-PyYAML. It is also 51 available as a pypi package at https://pypi.python.org/pypi/PyYAML.''' 59 ''' Return path added to current dir for __file__. ''' 60 return os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
63 def _load_kerneldrivers(configdir):
64 ''' Parse the kerneldrivers.yaml file, discard unavailable 68 with open(os.path.join(configdir,
"kernel-drivers.yaml"))
as f:
69 cf = yaml.load(f.read())
70 drivers = cf[
'drivers'].copy()
71 for driver
in cf[
'drivers']:
72 if driver ==
'default':
74 with open(
'/dev/null',
'w')
as f:
76 subprocess.check_output([config.MODINFO, driver],
78 except subprocess.CalledProcessError:
83 class ItemLookupError(Exception):
84 """A lookup failed, either too namy or no matches found. """ 89 ''' The configuration selected, and it's sources. ''' 92 def __init__(self, cf=None):
102 for key, value
in cf.items():
103 setattr(self, key, value)
107 ''' The possible note to display when selected. ''' 109 return self.
config[
'note']
115 ''' Reflects the *.yaml files in the configs/ directory. ''' 117 def __init__(self, path=None, yamlpath=None):
119 if path
and os.path.exists(path):
122 raise FileNotFoundError(path)
123 elif os.path.exists(_here(
'configs')):
124 configdir = _here(
'configs')
125 elif os.path.exists(_here(
'../configs')):
126 configdir = _here(
'../configs')
127 elif os.path.exists(_here(
'../../configs')):
128 configdir = _here(
'../../configs')
130 where =
'configs:../configs' 133 raise FileNotFoundError(where)
137 with open(os.path.join(yamlpath,
"confs_by_driver.yaml"))
as f:
138 cf = yaml.load(f.read())
139 db[
'lircd_by_driver'] = cf[
'lircd_by_driver'].copy()
140 db[
'lircmd_by_driver'] = cf[
'lircmd_by_driver'].copy()
142 db[
'kernel-drivers'] = _load_kerneldrivers(configdir)
143 db[
'drivers'] = db[
'kernel-drivers'].copy()
144 with open(os.path.join(yamlpath,
"drivers.yaml"))
as f:
145 cf = yaml.load(f.read())
146 db[
'drivers'].update(cf[
'drivers'].copy())
147 for key, d
in db[
'drivers'].items():
149 hint = d[
'device_hint']
153 if hint.startswith(
'"')
and hint.endswith(
'"'):
155 hint = hint.replace(
r'\"',
"@$#!")
156 hint = hint.replace(
'"',
'')
157 hint = hint.replace(
"@$#!",
'"')
158 hint = hint.replace(
"\\\\",
"\\")
159 d[
'device_hint'] = hint
162 for path
in glob.glob(configdir +
'/*.conf'):
163 with open(path)
as f:
164 cf = yaml.load(f.read())
165 configs[cf[
'config'][
'id']] = cf[
'config']
166 db[
'configs'] = configs
171 ''' The kernel-drivers dictionary, drivers.yaml + kernel-drivers.yaml. 173 return self.
db[
'kernel-drivers']
177 ''' The drivers dictionary, drivers.yaml + kernel-drivers.yaml. ''' 178 return self.
db[
'drivers']
182 ''' Return dict of parsed config/*.conf files, keyd by id. ''' 183 return self.
db[
'configs']
186 ''' Return the list of remotes suggested for a given driver. ''' 187 if isinstance(driver, dict):
188 driver = driver[
'id']
190 return self.
db[
'lircd_by_driver'][driver]
195 ''' Return list of lircmd.conf file for given driver or None. ''' 196 if isinstance(driver, dict):
197 driver = driver[
'id']
199 return self.
db[
'lircmd_by_driver'][driver]
204 ''' Return the driver (possibly None) suggested for a remote. ''' 205 for driver, files
in self.
db[
'lircd_by_driver'].items():
207 return self.
db[
'drivers'][driver]
211 ''' Return item (a config) in configs where config[key] == value. ''' 212 found = [c
for c
in self.
db[
'configs'].values()
213 if key
in c
and c[key] == value]
216 "find_config: Too many matches for %s, %s): " % (key, value)
217 +
', '.join([c[
'id']
for c
in found]))
220 "find_config: Nothing found for %s, %s): " % (key, value))
221 found = dict(found[0])
222 if 'device_hint' not in found:
224 found[
'device_hint'] = \
225 self.
db[
'drivers'][found[
'driver']][
'device_hint']
227 found[
'device_hint'] = \
228 self.
db[
'kernel-drivers'][found[
'driver']][
'device_hint']
lircd_conf
Substituted data from driver.
def driver_by_remote(self, remote)
Return the driver (possibly None) suggested for a remote.
def kernel_drivers(self)
The kernel-drivers dictionary, drivers.yaml + kernel-drivers.yaml.
def drivers(self)
The drivers dictionary, drivers.yaml + kernel-drivers.yaml.
A lookup failed, either too namy or no matches found.
modprobe
Substituted data from driver.
def find_config(self, key, value)
Return item (a config) in configs where config[key] == value.
label
Name of lircmd.conf file.
def note(self)
The possible note to display when selected.
Reflects the *.yaml files in the configs/ directory.
lircmd_conf
Name of lircd.conf file.
def remotes_by_driver(self, driver)
Return the list of remotes suggested for a given driver.
def configs(self)
Return dict of parsed config/*.conf files, keyd by id.
def lircmd_by_driver(self, driver)
Return list of lircmd.conf file for given driver or None.
modinit
Read-only config dict in db.
config
Read-only driver dict in db.