Show More
@@ -1,116 +1,116 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Configuration loader |
|
3 | 3 | |
|
4 | 4 | $Id: ConfigLoader.py 1005 2006-01-12 08:39:26Z fperez $""" |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | |
|
17 | 17 | import exceptions |
|
18 | 18 | import os |
|
19 | 19 | from pprint import pprint |
|
20 | 20 | |
|
21 | 21 | from IPython import ultraTB |
|
22 | 22 | from IPython.ipstruct import Struct |
|
23 | 23 | from IPython.genutils import * |
|
24 | 24 | |
|
25 | 25 | class ConfigLoaderError(exceptions.Exception): |
|
26 | 26 | """Exception for ConfigLoader class.""" |
|
27 | 27 | |
|
28 | 28 | def __init__(self,args=None): |
|
29 | 29 | self.args = args |
|
30 | 30 | |
|
31 | 31 | class ConfigLoader: |
|
32 | 32 | |
|
33 | 33 | """Configuration file loader capable of handling recursive inclusions and |
|
34 | 34 | with parametrized conflict resolution for multiply found keys.""" |
|
35 | 35 | |
|
36 | 36 | def __init__(self,conflict=None,field_sep=None,reclimit=15): |
|
37 | 37 | |
|
38 | 38 | """The reclimit parameter controls the number of recursive |
|
39 | 39 | configuration file inclusions. This way we can stop early on (before |
|
40 | 40 | python's own recursion limit is hit) if there is a circular |
|
41 | 41 | inclusion. |
|
42 | 42 | |
|
43 | 43 | - conflict: dictionary for conflict resolutions (see Struct.merge()) |
|
44 | 44 | |
|
45 | 45 | """ |
|
46 | 46 | self.conflict = conflict |
|
47 | 47 | self.field_sep = field_sep |
|
48 | 48 | self.reset(reclimit) |
|
49 | 49 | |
|
50 | 50 | def reset(self,reclimit=15): |
|
51 | 51 | self.reclimit = reclimit |
|
52 | 52 | self.recdepth = 0 |
|
53 | 53 | self.included = [] |
|
54 | 54 | |
|
55 | 55 | def load(self,fname,convert=None,recurse_key='',incpath = '.',**kw): |
|
56 | 56 | """Load a configuration file, return the resulting Struct. |
|
57 | 57 | |
|
58 | 58 | Call: load_config(fname,convert=None,conflict=None,recurse_key='') |
|
59 | 59 | |
|
60 | 60 | - fname: file to load from. |
|
61 | 61 | - convert: dictionary of type conversions (see read_dict()) |
|
62 | 62 | - recurse_key: keyword in dictionary to trigger recursive file |
|
63 | 63 | inclusions. |
|
64 | 64 | """ |
|
65 | 65 | |
|
66 | 66 | if self.recdepth > self.reclimit: |
|
67 | 67 | raise ConfigLoaderError, 'maximum recursive inclusion of rcfiles '+\ |
|
68 | 68 | 'exceeded: ' + `self.recdepth` + \ |
|
69 | 69 | '.\nMaybe you have a circular chain of inclusions?' |
|
70 | 70 | self.recdepth += 1 |
|
71 | 71 | fname = filefind(fname,incpath) |
|
72 | 72 | data = Struct() |
|
73 | 73 | # avoid including the same file more than once |
|
74 | 74 | if fname in self.included: |
|
75 | 75 | return data |
|
76 | Xinfo = ultraTB.AutoFormattedTB() | |
|
76 | Xinfo = ultraTB.AutoFormattedTB(color_scheme='NoColor') | |
|
77 | 77 | if convert==None and recurse_key : convert = {qwflat:recurse_key} |
|
78 | 78 | # for production, change warn to 0: |
|
79 | 79 | data.merge(read_dict(fname,convert,fs=self.field_sep,strip=1, |
|
80 | 80 | warn=0,no_empty=0,**kw)) |
|
81 | 81 | # keep track of successfully loaded files |
|
82 | 82 | self.included.append(fname) |
|
83 |
if recurse_key in data |
|
|
83 | if recurse_key in data: | |
|
84 | 84 | for incfilename in data[recurse_key]: |
|
85 | 85 | found=0 |
|
86 | 86 | try: |
|
87 | 87 | incfile = filefind(incfilename,incpath) |
|
88 | 88 | except IOError: |
|
89 | 89 | if os.name in ['nt','dos']: |
|
90 | 90 | try: |
|
91 | 91 | # Try again with '.ini' extension |
|
92 | 92 | incfilename += '.ini' |
|
93 | 93 | incfile = filefind(incfilename,incpath) |
|
94 | 94 | except IOError: |
|
95 | 95 | found = 0 |
|
96 | 96 | else: |
|
97 | 97 | found = 1 |
|
98 | 98 | else: |
|
99 | 99 | found = 0 |
|
100 | 100 | else: |
|
101 | 101 | found = 1 |
|
102 | 102 | if found: |
|
103 | 103 | try: |
|
104 | 104 | data.merge(self.load(incfile,convert,recurse_key, |
|
105 | 105 | incpath,**kw), |
|
106 | 106 | self.conflict) |
|
107 | 107 | except: |
|
108 | 108 | Xinfo() |
|
109 | 109 | warn('Problem loading included file: '+ |
|
110 | 110 | `incfilename` + '. Ignoring it...') |
|
111 | 111 | else: |
|
112 | 112 | warn('File `%s` not found. Included by %s' % (incfilename,fname)) |
|
113 | 113 | |
|
114 | 114 | return data |
|
115 | 115 | |
|
116 | 116 | # end ConfigLoader |
General Comments 0
You need to be logged in to leave comments.
Login now