Skip to content

Commit a491646

Browse files
committed
Merge pull request #2 from bpedman/master
Allow spring python YamlConfig to use values as defined in config file rather than forcing coersion to str.
2 parents 363555d + b659a4a commit a491646

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ target/
33
.DS_Store
44
.coverage
55
pip-log.txt
6+
temp/
7+
*.zip
8+

src/springpython/config/_yaml_config.py

+24-32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import types
1818
import inspect
1919
import logging
20+
import collections
2021

2122
from _config_base import *
2223
from springpython.context import scope
@@ -65,23 +66,12 @@ def read_object_defs(self):
6566
# A dictionary of abstract objects, keyed by their IDs, used in
6667
# traversing the hierarchies of parents; built upfront here for
6768
# convenience.
68-
abstract_objects = {}
69+
self.abstract_objects = {}
6970
for object in doc["objects"]:
7071
if "abstract" in object:
71-
abstract_objects[object["object"]] = object
72+
self.abstract_objects[object["object"]] = object
7273

7374
for object in doc["objects"]:
74-
if not "class" in object and not "parent" in object:
75-
self._map_custom_class(object, yaml_mappings)
76-
77-
elif "parent" in object:
78-
# Children are added to self.objects during the children->abstract parents traversal.
79-
pos_constr = self._get_pos_constr(object)
80-
named_constr = self._get_named_constr(object)
81-
props = self._get_props(object)
82-
self._traverse_parents(object, object, pos_constr, named_constr, props, abstract_objects)
83-
continue
84-
8575
self._print_obj(object)
8676
self.objects.append(self._convert_object(object))
8777

@@ -104,10 +94,10 @@ def _map_custom_class(self, obj, mappings):
10494
else:
10595
self.logger.warning("No matching type found for object %s" % obj)
10696

107-
def _traverse_parents(self, leaf, child, pos_constr,
108-
named_constr, props, abstract_objects):
97+
def _convert_child_object(self, leaf, child, pos_constr,
98+
named_constr, props):
10999

110-
parent = abstract_objects[child["parent"]]
100+
parent = self.abstract_objects[child["parent"]]
111101

112102
# At this point we only build up the lists of parameters but we don't create
113103
# the object yet because the current parent object may still have its
@@ -147,20 +137,19 @@ def _traverse_parents(self, leaf, child, pos_constr,
147137
props.append(parent_prop)
148138

149139
if "parent" in parent:
150-
self._traverse_parents(leaf, parent, pos_constr, named_constr, props, abstract_objects)
140+
# Continue traversing up the parent objects
141+
return self._convert_child_object(leaf, parent, pos_constr, named_constr, props)
151142
else:
152143
# Now we know we can create an object out of all the accumulated values.
153-
144+
154145
# The object's class is its topmost parent's class.
155146
class_ = parent["class"]
156147
id, factory, lazy_init, abstract, parent, scope_ = self._get_basic_object_data(leaf, class_)
157148

158149
c = self._create_object(id, factory, lazy_init, abstract, parent,
159150
scope_, pos_constr, named_constr, props)
160151

161-
self.objects.append(c)
162-
163-
return parent
152+
return c
164153

165154
def _get_pos_constr(self, object):
166155
""" Returns a list of all positional constructor arguments of an object.
@@ -224,20 +213,25 @@ def _convert_object(self, object, prefix=""):
224213
object["object"] = prefix + "." + object["object"]
225214
else:
226215
object["object"] = prefix + ".<anonymous>"
227-
228-
id, factory, lazy_init, abstract, parent, scope_ = self._get_basic_object_data(object, object.get("class"))
229-
216+
217+
if not "class" in object and "parent" not in object:
218+
self._map_custom_class(object, yaml_mappings)
219+
230220
pos_constr = self._get_pos_constr(object)
231221
named_constr = self._get_named_constr(object)
232222
props = self._get_props(object)
223+
224+
if "parent" in object:
225+
return self._convert_child_object(object, object, pos_constr, named_constr, props)
226+
else:
227+
id, factory, lazy_init, abstract, parent, scope_ = self._get_basic_object_data(object, object.get("class"))
233228

234-
return self._create_object(id, factory, lazy_init, abstract, parent,
235-
scope_, pos_constr, named_constr, props)
229+
return self._create_object(id, factory, lazy_init, abstract, parent,
230+
scope_, pos_constr, named_constr, props)
236231

237232
def _print_obj(self, obj, level=0):
238233
self.logger.debug("%sobject = %s" % ("\t"*level, obj["object"]))
239-
self.logger.debug("%sobject id = %s" % ("\t"*level, obj["object"]))
240-
self.logger.debug("%sclass = %s" % ("\t"*(level+1), obj["class"]))
234+
self.logger.debug("%sclass = %s" % ("\t"*(level+1), obj.get("class")))
241235

242236
if "scope" in obj:
243237
self.logger.debug("%sscope = %s" % ("\t"*(level+1), obj["scope"]))
@@ -247,7 +241,7 @@ def _print_obj(self, obj, level=0):
247241
if "properties" in obj:
248242
self.logger.debug("%sproperties:" % ("\t"*(level+1)))
249243
for prop in obj["properties"].keys():
250-
if "object" in obj["properties"][prop]:
244+
if isinstance(obj["properties"][prop], collections.Iterable) and "object" in obj["properties"][prop]:
251245
self.logger.debug("%s%s = ..." % ("\t"*(level+2), prop))
252246
self._print_obj(obj["properties"][prop], level+3)
253247
else:
@@ -398,10 +392,8 @@ def _convert_prop_def(self, comp, p, name):
398392
return self._convert_dict(p, comp["object"], name)
399393
elif isinstance(p, list):
400394
return self._convert_list(p, comp["object"], name)
401-
elif isinstance(p, unicode):
402-
return ValueDef(name, unicode(p))
403395
else:
404-
return ValueDef(name, str(p))
396+
return ValueDef(name, p)
405397
return None
406398

407399
if hasattr(p, "ref"):

0 commit comments

Comments
 (0)