3 ### Configuration handling
5 ### (c) 2013 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Chopwood: a password-changing service.
12 ### Chopwood is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU Affero General Public License as
14 ### published by the Free Software Foundation; either version 3 of the
15 ### License, or (at your option) any later version.
17 ### Chopwood is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU Affero General Public License for more details.
22 ### You should have received a copy of the GNU Affero General Public
23 ### License along with Chopwood; if not, see
24 ### <http://www.gnu.org/licenses/>.
26 from __future__
import with_statement
28 import os
as OS
; ENV
= OS
.environ
29 import output
as O
; OUT
= O
.OUT
33 from auto
import PACKAGE
, VERSION
35 ### Configuration is done by interpreting a file of Python code. We expect
36 ### the code to define a number of variables in its global scope. We import
37 ### a number of identifiers (named in the `EXPORT' list) to this module, and
38 ### also our entire parent module as `chpwd'.
40 ## Names which ought to be exported to the configuration module.
43 ## The configuration module.
44 CFG
= TY
.ModuleType('chpwd_config')
46 ## A list of hooks to call once configuration is complete.
49 """Decorator for post-configuration hooks."""
53 ## A suitable set of defaults.
56 def export(*names
, **kw
):
58 Export the names to the configuration module from the caller's environment.
61 ## Find the caller's global environment. Please don't try this at home.
63 except: tb
= SYS
.exc_info()[2]
64 env
= tb
.tb_frame
.f_back
.f_globals
68 _EXPORT
[name
] = env
[name
]
71 ## Some things to export for sure.
72 export('PACKAGE', 'VERSION', 'ENV', CONF
= SYS
.modules
[__name__
])
74 def loadconfig(config
):
76 Load the configuration, populating the `CFG' module with settings.
79 ## Make a new module for the configuration, and import ourselves into it.
84 ## And run the configuration code.
86 with
open(config
) as f
:
89 OUT
.warn("couldn't open configuration file `%s': %s" %
96 ###----- That's all, folks --------------------------------------------------