Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Wout De Nolf
bliss
Commits
0362f3bc
Commit
0362f3bc
authored
Aug 18, 2016
by
Jose Tiago Macara Coutinho
Browse files
common: implement config_property descriptor tool
parent
9e42263c
Changes
2
Hide whitespace changes
Inline
Side-by-side
bliss/common/utils.py
View file @
0362f3bc
...
...
@@ -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
)
bliss/controllers/motors/mockup.py
View file @
0362f3bc
...
...
@@ -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.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment