GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing lirc.org — LIRC libraries: /home/columbus/software/lirc-doc/python-pkg/lirc/database.py (ba530e4 2017-01-22 19:05:53 +0100) Source File
AllClassesFilesFunctionsVariablesTypedefsEnumerationsMacrosModulesPages
database.py
Go to the documentation of this file.
1 ''' Read-only configuration database. '''
2 ##
3 # @file database.py
4 # @author Alec Leamas
5 # @brief python read-only view on configuration data.
6 # @ingroup database
7
8 ##
9 #
10 # @defgroup database python configuration database
11 #
12 # Python access to the configuration data.
13 #
14 # The database is loaded from some YAML files:
15 #
16 # - kernel-drivers.yaml: Static Info on the kernel drivers. Availability
17 # of these drivers is checked in runtime before being imported into db.
18 # - drivers.yaml, Info on the userspace drivers, collected from their
19 # compiled information by configs/Makefile using lirc-lsplugins(1).
20 # - confs_by_driver.yaml: Mapping of drivers -> suggested remote files,
21 # created by configs/Makefile using irdb-get.
22 #
23 # The directory used to load these files is (first match used):
24 # - Current directory
25 # - The 'configs' dir.
26 # - The ../../configs
27 #
28 # Although python cannot guarantee this, the database is designed as a
29 # read-only structure.
30 #
31 # Simple usage examples lives in doc/: data2hwdb and data2table. The
32 # lirc-setup script provides a more elaborated example. Data structures
33 # are basically documented in the yaml files.
34
35 ## @addtogroup database
36 # @{
37
38
39 import glob
40 import os
41 import os.path
42 import subprocess
43 import sys
44
45 try:
46 import yaml
47 except ImportError:
48 _YAML_MSG = '''
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.'''
52 print(_YAML_MSG)
53 sys.exit(1)
54
55 import config
56
57
58 def _here(path):
59 ''' Return path added to current dir for __file__. '''
60 return os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
61
62
63 def _load_kerneldrivers(configdir):
64 ''' Parse the kerneldrivers.yaml file, discard unavailable
65 drivers.
66 '''
67
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':
73 continue
74 with open('/dev/null', 'w') as f:
75 try:
76 subprocess.check_output([config.MODINFO, driver],
77 stderr=f)
78 except subprocess.CalledProcessError:
79 del drivers[driver]
80 return drivers
81
82
83 class ItemLookupError(Exception):
84 """A lookup failed, either too namy or no matches found. """
85 pass
86
87
88 class Config(object):
89 ''' The configuration selected, and it's sources. '''
90 # pylint: disable=too-many-instance-attributes
92 def __init__(self, cf=None):
93 self.device = None ## selected device.
94 self.driver = {} ## Read-only driver dict in db
95 self.config = {} ## Read-only config dict in db
101 if cf:
103 setattr(self, key, value)
105 @property
107 ''' The possible note to display when selected. '''
110 else:
111 return None
112
113
115 ''' Reflects the *.yaml files in the configs/ directory. '''
116
117 def __init__(self, path=None, yamlpath=None):
118
119 if path and os.path.exists(path):
120 configdir = path
121 elif 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')
129 else:
130 where = 'configs:../configs'
131 if path:
132 where += ':' + path
133 raise FileNotFoundError(where)
134 if not yamlpath:
135 yamlpath = configdir
136 db = {}
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()
141
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():
148 d['id'] = key
149 hint = d['device_hint']
150 if not hint:
151 continue
152 hint = hint.strip()
153 if hint.startswith('"') and hint.endswith('"'):
154 hint = hint[1:-1]
155 hint = hint.replace(r'\"', "@$#!")
156 hint = hint.replace('"', '')
157 hint = hint.replace("@$#!", '"')
158 hint = hint.replace("\\\\", "\\")
159 d['device_hint'] = hint
160
161 configs = {}
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
167 self.db = db
168
169 @property
171 ''' The kernel-drivers dictionary, drivers.yaml + kernel-drivers.yaml.
172 '''
174
175 @property
177 ''' The drivers dictionary, drivers.yaml + kernel-drivers.yaml. '''
180 @property
182 ''' Return dict of parsed config/*.conf files, keyd by id. '''
186 ''' Return the list of remotes suggested for a given driver. '''
187 if isinstance(driver, dict):
188 driver = driver['id']
189 try:
191 except KeyError:
192 return []
195 ''' Return list of lircmd.conf file for given driver or None. '''
196 if isinstance(driver, dict):
197 driver = driver['id']
198 try:
200 except KeyError:
201 return []
204 ''' Return the driver (possibly None) suggested for a remote. '''
206 if remote in files:
208 return None
209
211 ''' Return item (a config) in configs where config[key] == value. '''
213 if key in c and c[key] == value]
214 if len(found) > 1:
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:
223 try:
224 found['device_hint'] = \
226 except KeyError:
227 found['device_hint'] = \
229 return found
230
231 ## @}
232 # database
233
234 # vim: set expandtab ts=4 sw=4:
A lookup failed, either too namy or no matches found. Definition: database.py:91 def find_config Return item (a config) in configs where config[key] == value. Definition: database.py:218 def lircmd_by_driver Return list of lircmd.conf file for given driver or None. Definition: database.py:202 Reflects the *.yaml files in the configs/ directory. Definition: database.py:122 def remotes_by_driver Return the list of remotes suggested for a given driver. Definition: database.py:193 def kernel_drivers The kernel-drivers dictionary, drivers.yaml + kernel-drivers.yaml. Definition: database.py:179 def driver_by_remote Return the driver (possibly None) suggested for a remote. Definition: database.py:211
| ||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |