Commit 0362f3bc authored by Jose Tiago Macara Coutinho's avatar Jose Tiago Macara Coutinho
Browse files

common: implement config_property descriptor tool

parent 9e42263c
......@@ -160,3 +160,43 @@ def set_custom_members(src_obj, target_obj, pre_call=None):
except AttributeError:
pass
class config_property(object):
"""
Config descriptor to allow easy access to static configuration options
Only works on classes which have a `config` member that has `get()` and
`set()` methods (for example:
:class:`~bliss.config.motors.beacon_backend.StaticConfig`).
Example::
from bliss.comm.tcp import Tcp
from bliss.common.utils import config_property
from bliss.controllers.motor import Controller
class MyMotorController(Controller):
host_name = config_property('host')
port = config_property('port', converter=int, default=5000)
def initialize(self):
url = '{0}:{1}'.format(self.host_name, self.port)
self.tcp = Tcp(url=url)
"""
def __init__(self, name, converter=str, default=None):
self.__name = name
self.__converter = converter
self.__default = default
def __get__(self, obj, objtype):
if obj is None:
return self
return obj.config.get(self.__name, converter=self.__converter,
default=self.__default)
def __set__(self, obj, value):
obj.config.set(self.__name, value)
......@@ -16,6 +16,7 @@ from bliss.common import event
from bliss.common.utils import object_method
from bliss.common.utils import object_attribute_get, object_attribute_set
from bliss.common.utils import config_property
"""
......@@ -46,6 +47,9 @@ config_xml = """
class Mockup(Controller):
host = config_property('host')
def __init__(self, name, config, axes, encoders):
Controller.__init__(self, name, config, axes, encoders)
......@@ -64,8 +68,8 @@ class Mockup(Controller):
# Access to the config.
try:
self.host = self.config.get("host")
except:
self.host
except KeyError:
elog.debug("no 'host' defined in config for %s" % name)
# Adds Mockup-specific settings.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment