|
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 | from ctypes import * |
| 4 | +from tempfile import NamedTemporaryFile |
4 | 5 |
|
5 | 6 | from epicsdbbuilder.recordset import recordset |
6 | 7 |
|
7 | | -from . import imports, device, builder |
| 8 | +from . import imports, device |
8 | 9 |
|
9 | 10 | __all__ = ['dbLoadDatabase', 'iocInit', 'interactive_ioc'] |
10 | 11 |
|
@@ -240,35 +241,48 @@ def __call__(self): |
240 | 241 | exit = Exiter() |
241 | 242 | command_names.append('exit') |
242 | 243 |
|
243 | | -# For backwards compatibility |
244 | | -dbLoadDatabase = builder.dbLoadDatabase |
245 | 244 |
|
| 245 | +def dbLoadDatabase(database, path = None, substitutions = None): |
| 246 | + '''Loads a database file and applies any given substitutions.''' |
| 247 | + imports.dbLoadDatabase(database, path, substitutions) |
246 | 248 |
|
247 | | -def _add_records_from_file(dir, file, macros): |
248 | | - # This is very naive, for instance macros are added to but never removed, |
249 | | - # but it works well enough for devIocStats |
250 | | - with open(os.path.join(dir, file)) as f: |
| 249 | + |
| 250 | +def _add_records_from_file(dirname, file, substitutions): |
| 251 | + # This is very naive, it loads all includes before their parents which |
| 252 | + # possibly can put them out of order, but it works well enough for |
| 253 | + # devIocStats |
| 254 | + with open(os.path.join(dirname, file)) as f: |
| 255 | + lines, include_subs = [], "" |
251 | 256 | for line in f.readlines(): |
252 | 257 | line = line.rstrip() |
253 | 258 | if line.startswith('substitute'): |
254 | | - # substitute "QUEUE=scanOnce, QUEUE_CAPS=SCANONCE |
255 | | - for sub in line.split('"')[1].split(','): |
256 | | - k, v = sub.split('=') |
257 | | - macros[k.strip()] = v.strip() |
| 259 | + # substitute "QUEUE=scanOnce, QUEUE_CAPS=SCANONCE" |
| 260 | + # keep hold of the substitutions |
| 261 | + include_subs = line.split('"')[1] |
258 | 262 | elif line.startswith('include'): |
259 | 263 | # include "iocQueue.db" |
260 | | - _add_records_from_file(dir, line.split('"')[1], macros) |
| 264 | + subs = substitutions |
| 265 | + if substitutions and include_subs: |
| 266 | + subs = substitutions + ", " + include_subs |
| 267 | + else: |
| 268 | + subs = substitutions + include_subs |
| 269 | + _add_records_from_file(dirname, line.split('"')[1], subs) |
261 | 270 | else: |
262 | 271 | # A record line |
263 | | - builder.AddDatabaseLine(line, macros) |
| 272 | + lines.append(line) |
| 273 | + # Write a tempfile and load it |
| 274 | + with NamedTemporaryFile(suffix='.db', delete=False) as f: |
| 275 | + f.write(os.linesep.join(lines).encode()) |
| 276 | + dbLoadDatabase(f.name, substitutions=substitutions) |
| 277 | + os.unlink(f.name) |
264 | 278 |
|
265 | 279 |
|
266 | 280 | def devIocStats(ioc_name): |
267 | 281 | '''This will load a template for the devIocStats library with the specified |
268 | 282 | IOC name. This should be called before `iocInit`''' |
269 | | - macros = dict(IOCNAME=ioc_name, TODFORMAT='%m/%d/%Y %H:%M:%S') |
| 283 | + substitutions = 'IOCNAME=' + ioc_name + ', TODFORMAT=%m/%d/%Y %H:%M:%S' |
270 | 284 | iocstats_dir = os.path.join(os.path.dirname(__file__), 'iocStatsDb') |
271 | | - _add_records_from_file(iocstats_dir, 'ioc.template', macros) |
| 285 | + _add_records_from_file(iocstats_dir, 'ioc.template', substitutions) |
272 | 286 |
|
273 | 287 |
|
274 | 288 | def interactive_ioc(context = {}, call_exit = True): |
|
0 commit comments