paul@1210 | 1 | #!/usr/bin/env python |
paul@1210 | 2 | |
paul@1210 | 3 | """ |
paul@1210 | 4 | Configuration management. |
paul@1210 | 5 | |
paul@1210 | 6 | Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk> |
paul@1210 | 7 | |
paul@1210 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1210 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1210 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1210 | 11 | version. |
paul@1210 | 12 | |
paul@1210 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1210 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1210 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1210 | 16 | details. |
paul@1210 | 17 | |
paul@1210 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1210 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1210 | 20 | """ |
paul@1210 | 21 | |
paul@1210 | 22 | from compiler import parseFile |
paul@1210 | 23 | from os.path import isfile, join, split |
paul@1210 | 24 | from compiler.ast import Assign, AssName, Const, Discard, Module, Name, Stmt |
paul@1210 | 25 | |
paul@1210 | 26 | config_path = [".", "/etc/imip-agent", split(__file__)[0]] |
paul@1210 | 27 | |
paul@1210 | 28 | builtins = { |
paul@1210 | 29 | "False" : False, |
paul@1210 | 30 | "None" : None, |
paul@1210 | 31 | "True" : True, |
paul@1210 | 32 | } |
paul@1210 | 33 | |
paul@1210 | 34 | def get_config(filename="config.txt", path=config_path): |
paul@1210 | 35 | |
paul@1210 | 36 | "Obtain the configuration from 'filename', searching 'path' for the file." |
paul@1210 | 37 | |
paul@1210 | 38 | for dirname in path: |
paul@1210 | 39 | pathname = join(dirname, filename) |
paul@1210 | 40 | if isfile(pathname): |
paul@1210 | 41 | module = parseFile(pathname) |
paul@1210 | 42 | break |
paul@1210 | 43 | else: |
paul@1210 | 44 | return {} |
paul@1210 | 45 | |
paul@1210 | 46 | return get_config_data(module, {}) |
paul@1210 | 47 | |
paul@1210 | 48 | def get_config_data(module, d): |
paul@1210 | 49 | |
paul@1210 | 50 | "Interpret the parsed 'module', storing mappings in 'd'." |
paul@1210 | 51 | |
paul@1210 | 52 | name = None |
paul@1210 | 53 | |
paul@1210 | 54 | for node in module.getChildNodes(): |
paul@1210 | 55 | if isinstance(node, (Module, Stmt)): |
paul@1210 | 56 | get_config_data(node, d) |
paul@1210 | 57 | elif isinstance(node, Discard): |
paul@1210 | 58 | pass |
paul@1210 | 59 | elif isinstance(node, Assign): |
paul@1210 | 60 | if len(node.nodes) == 1 and isinstance(node.nodes[0], AssName): |
paul@1210 | 61 | name = node.nodes[0].name |
paul@1210 | 62 | if isinstance(node.expr, Const): |
paul@1210 | 63 | d[name] = node.expr.value |
paul@1210 | 64 | elif isinstance(node.expr, Name): |
paul@1210 | 65 | d[name] = d.get(node.expr.name, builtins.get(node.expr.name)) |
paul@1210 | 66 | |
paul@1210 | 67 | return d |
paul@1210 | 68 | |
paul@1210 | 69 | # Expose settings via a module-level name. |
paul@1210 | 70 | |
paul@1210 | 71 | settings = get_config() |
paul@1210 | 72 | |
paul@1210 | 73 | # vim: tabstop=4 expandtab shiftwidth=4 |