Skip to content

Commit

Permalink
Refactoring of code, avoiding a specific handler for HiddenProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
tcalmant committed Apr 28, 2016
1 parent 461429b commit 0234897
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 188 deletions.
1 change: 0 additions & 1 deletion pelix/ipopo/core.py
Expand Up @@ -61,7 +61,6 @@

# Built-in handlers, automatically installed
BUILTIN_HANDLERS = ('pelix.ipopo.handlers.properties',
'pelix.ipopo.handlers.hidden_properties',
'pelix.ipopo.handlers.provides',
'pelix.ipopo.handlers.requires',
'pelix.ipopo.handlers.requiresbest',
Expand Down
3 changes: 0 additions & 3 deletions pelix/ipopo/decorators.py
Expand Up @@ -607,9 +607,6 @@ class HiddenProperty(Property):
Defines a component property.
"""
HANDLER_ID = constants.HANDLER_HIDDEN_PROPERTY
""" ID of the handler configured by this decorator """

def __call__(self, clazz):
"""
Adds the property to the class iPOPO properties field.
Expand Down
171 changes: 0 additions & 171 deletions pelix/ipopo/handlers/hidden_properties.py

This file was deleted.

62 changes: 49 additions & 13 deletions pelix/ipopo/handlers/properties.py
Expand Up @@ -105,13 +105,25 @@ def __init__(self):
"""
self._ipopo_instance = None

def _field_property_generator(self):
def _field_property_generator(self, public_properties):
"""
Generates the methods called by the injected class properties
:param public_properties: If True, create a public property accessor,
else an hidden property accessor
:return: getter and setter methods
"""
# Local variable, to avoid messing with "self"
stored_instance = self._ipopo_instance
properties = stored_instance.context.properties

# Choose public or hidden properties
# and select the method to call to notify about the property update
if public_properties:
properties = stored_instance.context.properties
update_notifier = stored_instance.update_property
else:
properties = stored_instance.context.hidden_properties
update_notifier = stored_instance.update_hidden_property

def get_value(_, name):
"""
Expand All @@ -138,12 +150,30 @@ def set_value(_, name, new_value):
properties[name] = new_value

# New value is different of the old one, trigger an event
stored_instance.update_property(name, old_value, new_value)
update_notifier(name, old_value, new_value)

return new_value

return get_value, set_value

@staticmethod
def get_methods_names(public_properties):
"""
Generates the names of the fields where to inject the getter and setter
methods
:param public_properties: If True, returns the names of public property
accessors, else of hidden property ones
:return: getter and a setter field names
"""
if public_properties:
prefix = ipopo_constants.IPOPO_PROPERTY_PREFIX
else:
prefix = ipopo_constants.IPOPO_HIDDEN_PROPERTY_PREFIX

return "{0}{1}".format(prefix, ipopo_constants.IPOPO_GETTER_SUFFIX), \
"{0}{1}".format(prefix, ipopo_constants.IPOPO_SETTER_SUFFIX),

def manipulate(self, stored_instance, component_instance):
"""
Manipulates the component instance
Expand All @@ -154,15 +184,21 @@ def manipulate(self, stored_instance, component_instance):
# Store the stored instance
self._ipopo_instance = stored_instance

# Inject properties
getter, setter = self._field_property_generator()
# Public flags to generate (True for public accessors)
flags_to_generate = set()
if stored_instance.context.properties:
flags_to_generate.add(True)

# (False for hidden ones)
if stored_instance.context.hidden_properties:
flags_to_generate.add(False)

# Prepare the methods names
getter_name = "{0}{1}".format(ipopo_constants.IPOPO_PROPERTY_PREFIX,
ipopo_constants.IPOPO_GETTER_SUFFIX)
setter_name = "{0}{1}".format(ipopo_constants.IPOPO_PROPERTY_PREFIX,
ipopo_constants.IPOPO_SETTER_SUFFIX)
# Inject properties getters and setters
for public_flag in flags_to_generate:
# Prepare methods
getter, setter = self._field_property_generator(public_flag)

# Inject the getter and setter at the instance level
setattr(component_instance, getter_name, getter)
setattr(component_instance, setter_name, setter)
# Inject the getter and setter at the instance level
getter_name, setter_name = self.get_methods_names(public_flag)
setattr(component_instance, getter_name, getter)
setattr(component_instance, setter_name, setter)

0 comments on commit 0234897

Please sign in to comment.