Gentoo Archives: gentoo-commits

From: "André Erdmann" <dywi@×××××××.de>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/
Date: Fri, 01 Jun 2012 16:19:45
Message-Id: 1338567543.d0fa945b8c81fedbcc02dc3f1e3e824a5b7170cc.dywi@gentoo
1 commit: d0fa945b8c81fedbcc02dc3f1e3e824a5b7170cc
2 Author: André Erdmann <dywi <AT> mailerd <DOT> de>
3 AuthorDate: Fri Jun 1 16:19:03 2012 +0000
4 Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
5 CommitDate: Fri Jun 1 16:19:03 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=d0fa945b
7
8 config: faster lookup
9
10 ---
11 roverlay/config.py | 74 ++++++++++++++++++++++++++++++++++++++++++---------
12 roverlay/const.py | 15 +++-------
13 2 files changed, 66 insertions(+), 23 deletions(-)
14
15 diff --git a/roverlay/config.py b/roverlay/config.py
16 index b0bf7a3..41a98bf 100644
17 --- a/roverlay/config.py
18 +++ b/roverlay/config.py
19 @@ -4,6 +4,7 @@
20
21 import sys
22 import shlex
23 +import copy
24
25 try:
26 import configparser
27 @@ -81,13 +82,36 @@ class ConfigTree:
28
29 self.parser = dict()
30
31 - self._config = const.clone() if import_const else None
32 + self._config = const.clone() if import_const else dict ()
33 self._const_imported = import_const
34 self._field_definitions = None
35
36 # --- end of __init__ (...) ---
37
38
39 + def _findpath ( self, path, root=None, create=False ):
40 + if path is None:
41 + return root
42 + elif isinstance ( path, str ):
43 + path = path.split ( '.' ) if key else []
44 +
45 + config_position = self._config if root is None else root
46 +
47 + for k in path:
48 + if not k in config_position:
49 + if create:
50 + config_position [k] = dict ()
51 +
52 + else:
53 + return None
54 +
55 + config_position = config_position [k]
56 +
57 + return config_position
58 +
59 + # --- end of _findpath (...) ---
60 +
61 +
62 def get ( self, key, fallback_value=None ):
63 """Searches for key in the ConfigTree and returns its value.
64 Searches in const if ConfigTree does not contain the requested key and
65 @@ -98,19 +122,10 @@ class ConfigTree:
66 * fallback_value --
67 """
68 if self._config:
69 - config_path = key.split ( '.' )
70 - config_path.reverse ()
71 -
72 - config_position = self._config
73 - while len ( config_path ) and config_position:
74 - next_key = config_path.pop ()
75 - if next_key in config_position:
76 - config_position = config_position [next_key]
77 - else:
78 - config_position = None
79 + config_value = self._findpath ( key )
80
81 - if config_position:
82 - return config_position
83 + if config_value:
84 + return config_value
85
86 if self._const_imported:
87 return fallback_value
88 @@ -119,6 +134,39 @@ class ConfigTree:
89
90 # --- end of get (...) ---
91
92 + def load_config ( self, config_file, start_section='' ):
93 + """Loads a config file and integrates its content into the config tree.
94 + Older config entries may be overwritten.
95 +
96 + arguments:
97 + config_file -- path to the file that should be read
98 + start_section -- relative root in the config tree as str or ref
99 + """
100 +
101 + config_root = None
102 + if start_section:
103 + if isinstance ( start_section, str ):
104 + config_root = self._findpath ( start_section, None, True )
105 + elif isinstance ( start_section, dict ):
106 + config_root = start_section
107 + else
108 + raise Exception ("bad usage")
109 +
110 + # load file
111 +
112 + try:
113 + fh = open ( config_file, 'r' )
114 + reader = shlex.shlex ( fh )
115 + if fh:
116 + fh.close ()
117 +
118 + # <TODO>
119 +
120 + except IOError as ioerr:
121 + raise
122 +
123 + # --- end of load_config (...) ---
124 +
125 def load_field_definition ( self, def_file, lenient=False ):
126 """Loads a field definition file. Please see the example file for format
127 details.
128
129 diff --git a/roverlay/const.py b/roverlay/const.py
130 index 32630cf..527939b 100644
131 --- a/roverlay/const.py
132 +++ b/roverlay/const.py
133 @@ -31,21 +31,16 @@ _CONSTANTS = dict (
134
135 def lookup ( key, fallback_value=None ):
136 path = key.split ( '.' )
137 - path.reverse ()
138
139 const_position = _CONSTANTS
140
141 - while len ( path ) and const_position:
142 - next_key = path.pop ()
143 - if next_key in const_position:
144 - const_position = const_position [next_key]
145 + for k in path:
146 + if k in const_position:
147 + const_position = const_position [k]
148 else:
149 - const_position = None
150 + return fallback_value
151
152 - if const_position:
153 - return const_position
154 - else:
155 - return fallback_value
156 + return const_position
157
158 def clone ( ):
159 return copy.deepcopy ( _CONSTANTS )