|
| 1 | +""" |
| 2 | + Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +""" |
| 16 | +import re |
| 17 | +import types |
| 18 | +import inspect |
| 19 | +import logging |
| 20 | + |
| 21 | +from springpython.context import scope |
| 22 | +from decorator import decorator, partial |
| 23 | +from springpython.context import ApplicationContextAware |
| 24 | +from springpython.factory import PythonObjectFactory |
| 25 | +from springpython.factory import ReflectiveObjectFactory |
| 26 | +from springpython.container import InvalidObjectScope |
| 27 | + |
| 28 | +def get_string(value): |
| 29 | + """This function is used to parse text that could either be ASCII or unicode.""" |
| 30 | + try: |
| 31 | + return str(value) |
| 32 | + except UnicodeEncodeError: |
| 33 | + return unicode(value) |
| 34 | + |
| 35 | +class ObjectDef(object): |
| 36 | + """ |
| 37 | + ObjectDef is a format-neutral way of storing object definition information. It includes |
| 38 | + a handle for the actual ObjectFactory that should be used to utilize this information when |
| 39 | + creating an instance of a object. |
| 40 | + """ |
| 41 | + def __init__(self, id, props=None, factory=None, scope=scope.SINGLETON, |
| 42 | + lazy_init=False, abstract=False, parent=None): |
| 43 | + super(ObjectDef, self).__init__() |
| 44 | + self.id = id |
| 45 | + self.factory = factory |
| 46 | + if props is None: |
| 47 | + self.props = [] |
| 48 | + else: |
| 49 | + self.props = props |
| 50 | + self.scope = scope |
| 51 | + self.lazy_init = lazy_init |
| 52 | + self.abstract = abstract |
| 53 | + self.parent = parent |
| 54 | + self.pos_constr = [] |
| 55 | + self.named_constr = {} |
| 56 | + |
| 57 | + def __str__(self): |
| 58 | + return "id=%s props=%s scope=%s factory=%s" % (self.id, self.props, self.scope, self.factory) |
| 59 | + |
| 60 | +class ReferenceDef(object): |
| 61 | + """ |
| 62 | + This class represents a definition that is referencing another object. |
| 63 | + """ |
| 64 | + def __init__(self, name, ref): |
| 65 | + self.name = name |
| 66 | + self.ref = ref |
| 67 | + |
| 68 | + def prefetch(self, container): |
| 69 | + self.get_value(container) |
| 70 | + |
| 71 | + def get_value(self, container): |
| 72 | + return container.get_object(self.ref) |
| 73 | + |
| 74 | + def set_value(self, obj, container): |
| 75 | + setattr(obj, self.name, container.objects[self.ref]) |
| 76 | + |
| 77 | + def __str__(self): |
| 78 | + return "name=%s ref=%s" % (self.name, self.ref) |
| 79 | + |
| 80 | +class InnerObjectDef(object): |
| 81 | + """ |
| 82 | + This class represents an inner object. It is optional whether or not the object |
| 83 | + has its own name. |
| 84 | + """ |
| 85 | + def __init__(self, name, inner_comp): |
| 86 | + self.name = name |
| 87 | + self.inner_comp = inner_comp |
| 88 | + |
| 89 | + def prefetch(self, container): |
| 90 | + self.get_value(container) |
| 91 | + |
| 92 | + def get_value(self, container): |
| 93 | + return container.get_object(self.inner_comp.id) |
| 94 | + |
| 95 | + def set_value(self, obj, container): |
| 96 | + setattr(obj, self.name, self.get_value(container)) |
| 97 | + |
| 98 | + def __str__(self): |
| 99 | + return "name=%s inner_comp=%s" % (self.name, self.inner_comp) |
| 100 | + |
| 101 | +class ValueDef(object): |
| 102 | + """ |
| 103 | + This class represents a property that holds a value. The value can be simple value, or |
| 104 | + it can be a complex container which internally holds references, inner objects, or |
| 105 | + any other type. |
| 106 | + """ |
| 107 | + def __init__(self, name, value): |
| 108 | + self.name = name |
| 109 | + if value == "True": |
| 110 | + self.value = True |
| 111 | + elif value == "False": |
| 112 | + self.value= False |
| 113 | + else: |
| 114 | + self.value = value |
| 115 | + self.logger = logging.getLogger("springpython.config.ValueDef") |
| 116 | + |
| 117 | + def scan_value(self, container, value): |
| 118 | + if hasattr(value, "get_value"): |
| 119 | + return value.get_value(container) |
| 120 | + elif isinstance(value, tuple): |
| 121 | + new_list = [self.scan_value(container, item) for item in value] |
| 122 | + results = tuple(new_list) |
| 123 | + return results |
| 124 | + elif isinstance(value, list): |
| 125 | + new_list = [self.scan_value(container, item) for item in value] |
| 126 | + return new_list |
| 127 | + elif isinstance(value, set): |
| 128 | + results = set([self.scan_value(container, item) for item in value]) |
| 129 | + return results |
| 130 | + elif isinstance(value, frozenset): |
| 131 | + results = frozenset([self.scan_value(container, item) for item in value]) |
| 132 | + return results |
| 133 | + else: |
| 134 | + if value == "True": |
| 135 | + return True |
| 136 | + elif value == "False": |
| 137 | + return False |
| 138 | + else: |
| 139 | + return value |
| 140 | + |
| 141 | + def get_value(self, container): |
| 142 | + val = self._replace_refs_with_actuals(self.value, container) |
| 143 | + if val is None: |
| 144 | + return self.value |
| 145 | + else: |
| 146 | + return val |
| 147 | + |
| 148 | + def set_value(self, obj, container): |
| 149 | + setattr(obj, self.name, self.value) |
| 150 | + val = self._replace_refs_with_actuals(obj, container) |
| 151 | + |
| 152 | + def _replace_refs_with_actuals(self, obj, container): |
| 153 | + """Normal values do nothing for this step. However, sub-classes are defined for |
| 154 | + the various containers, like lists, set, dictionaries, etc., to handle iterating |
| 155 | + through and pre-fetching items.""" |
| 156 | + pass |
| 157 | + |
| 158 | + def __str__(self): |
| 159 | + return "name=%s value=%s" % (self.name, self.value) |
| 160 | + |
| 161 | +class DictDef(ValueDef): |
| 162 | + """Handles behavior for a dictionary-based value.""" |
| 163 | + def __init__(self, name, value): |
| 164 | + super(DictDef, self).__init__(name, value) |
| 165 | + |
| 166 | + def _replace_refs_with_actuals(self, obj, container): |
| 167 | + for key in self.value.keys(): |
| 168 | + if hasattr(self.value[key], "ref"): |
| 169 | + self.value[key] = container.get_object(self.value[key].ref) |
| 170 | + else: |
| 171 | + self.value[key] = self.scan_value(container, self.value[key]) |
| 172 | + |
| 173 | +class ListDef(ValueDef): |
| 174 | + """Handles behavior for a list-based value.""" |
| 175 | + def __init__(self, name, value): |
| 176 | + super(ListDef, self).__init__(name, value) |
| 177 | + self.logger = logging.getLogger("springpython.config.ListDef") |
| 178 | + |
| 179 | + def _replace_refs_with_actuals(self, obj, container): |
| 180 | + for i in range(0, len(self.value)): |
| 181 | + self.logger.debug("Checking out %s, wondering if I need to do any replacement..." % get_string(self.value[i])) |
| 182 | + if hasattr(self.value[i], "ref"): |
| 183 | + self.value[i] = container.get_object(self.value[i].ref) |
| 184 | + else: |
| 185 | + self.value[i] = self.scan_value(container, self.value[i]) |
| 186 | + |
| 187 | +class TupleDef(ValueDef): |
| 188 | + """Handles behavior for a tuple-based value.""" |
| 189 | + |
| 190 | + def __init__(self, name, value): |
| 191 | + super(TupleDef, self).__init__(name, value) |
| 192 | + |
| 193 | + def _replace_refs_with_actuals(self, obj, container): |
| 194 | + new_value = list(self.value) |
| 195 | + for i in range(0, len(new_value)): |
| 196 | + if hasattr(new_value[i], "ref"): |
| 197 | + new_value[i] = container.get_object(new_value[i].ref) |
| 198 | + else: |
| 199 | + new_value[i] = self.scan_value(container, new_value[i]) |
| 200 | + try: |
| 201 | + setattr(obj, self.name, tuple(new_value)) |
| 202 | + except AttributeError: |
| 203 | + pass |
| 204 | + return tuple(new_value) |
| 205 | + |
| 206 | +class SetDef(ValueDef): |
| 207 | + """Handles behavior for a set-based value.""" |
| 208 | + def __init__(self, name, value): |
| 209 | + super(SetDef, self).__init__(name, value) |
| 210 | + self.logger = logging.getLogger("springpython.config.SetDef") |
| 211 | + |
| 212 | + def _replace_refs_with_actuals(self, obj, container): |
| 213 | + self.logger.debug("Replacing refs with actuals...") |
| 214 | + self.logger.debug("set before changes = %s" % self.value) |
| 215 | + new_set = set() |
| 216 | + for item in self.value: |
| 217 | + if hasattr(item, "ref"): |
| 218 | + self.logger.debug("Item !!!%s!!! is a ref, trying to replace with actual object !!!%s!!!" % (item, item.ref)) |
| 219 | + #self.value.remove(item) |
| 220 | + #self.value.add(container.get_object(item.ref)) |
| 221 | + newly_fetched_value = container.get_object(item.ref) |
| 222 | + new_set.add(newly_fetched_value) |
| 223 | + self.logger.debug("Item !!!%s!!! was removed, and newly fetched value !!!%s!!! was added." % (item, newly_fetched_value)) |
| 224 | + #new_set.add(container.get_object(item.ref)) |
| 225 | + else: |
| 226 | + self.logger.debug("Item !!!%s!!! is NOT a ref, trying to replace with scanned value" % get_string(item)) |
| 227 | + #self.value.remove(item) |
| 228 | + #self.value.add(self.scan_value(container, item)) |
| 229 | + newly_scanned_value = self.scan_value(container, item) |
| 230 | + new_set.add(newly_scanned_value) |
| 231 | + self.logger.debug("Item !!!%s!!! was removed, and newly scanned value !!!%s!!! was added." % (item, newly_scanned_value)) |
| 232 | + #new_set.add(self.scan_value(container, item)) |
| 233 | + #self.value = new_set |
| 234 | + self.logger.debug("set after changes = %s" % new_set) |
| 235 | + #return self.value |
| 236 | + try: |
| 237 | + setattr(obj, self.name, new_set) |
| 238 | + except AttributeError: |
| 239 | + pass |
| 240 | + return new_set |
| 241 | + |
| 242 | +class FrozenSetDef(ValueDef): |
| 243 | + """Handles behavior for a frozen-set-based value.""" |
| 244 | + def __init__(self, name, value): |
| 245 | + super(FrozenSetDef, self).__init__(name, value) |
| 246 | + self.logger = logging.getLogger("springpython.config.FrozenSetDef") |
| 247 | + |
| 248 | + def _replace_refs_with_actuals(self, obj, container): |
| 249 | + self.logger.debug("Replacing refs with actuals...") |
| 250 | + self.logger.debug("set before changes = %s" % self.value) |
| 251 | + new_set = set() |
| 252 | + for item in self.value: |
| 253 | + if hasattr(item, "ref"): |
| 254 | + self.logger.debug("Item <<<%s>>> is a ref, trying to replace with actual object <<<%s>>>" % (item, item.ref)) |
| 255 | + #new_set.remove(item) |
| 256 | + #debug begin |
| 257 | + newly_fetched_value = container.get_object(item.ref) |
| 258 | + new_set.add(newly_fetched_value) |
| 259 | + self.logger.debug("Item <<<%s>>> was removed, and newly fetched value <<<%s>>> was added." % (item, newly_fetched_value)) |
| 260 | + #debug end |
| 261 | + #new_set.add(container.get_object(item.ref)) |
| 262 | + else: |
| 263 | + self.logger.debug("Item <<<%s>>> is NOT a ref, trying to replace with scanned value" % get_string(item)) |
| 264 | + #new_set.remove(item) |
| 265 | + #debug begin |
| 266 | + newly_scanned_value = self.scan_value(container, item) |
| 267 | + new_set.add(newly_scanned_value) |
| 268 | + self.logger.debug("Item <<<%s>>> was removed, and newly scanned value <<<%s>>> was added." % (item, newly_scanned_value)) |
| 269 | + #debug end |
| 270 | + #new_set.add(self.scan_value(container, item)) |
| 271 | + #self.logger.debug("Newly built set = %s" % new_set) |
| 272 | + #self.value = frozenset(new_set) |
| 273 | + new_frozen_set = frozenset(new_set) |
| 274 | + self.logger.debug("set after changes = %s" % new_frozen_set) |
| 275 | + #return self.value |
| 276 | + try: |
| 277 | + setattr(obj, self.name, new_frozen_set) |
| 278 | + except AttributeError: |
| 279 | + pass |
| 280 | + except TypeError: |
| 281 | + pass |
| 282 | + return new_frozen_set |
| 283 | + |
| 284 | +class Config(object): |
| 285 | + """ |
| 286 | + Config is an interface that defines how to read object definitions from an input source. |
| 287 | + """ |
| 288 | + def read_object_defs(self): |
| 289 | + """Abstract method definition - should return an array of Object objects""" |
| 290 | + raise NotImplementedError() |
| 291 | + |
0 commit comments