Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cc-node
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
cc-node
Commits
9c1807b2
Commit
9c1807b2
authored
12 years ago
by
Anael Beutot
Browse files
Options
Downloads
Patches
Plain Diff
Change tag calculation.
Use a timer watcher to refresh the value on the node side.
parent
13bd01f0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ccnode/tags.py
+37
-14
37 additions, 14 deletions
ccnode/tags.py
with
37 additions
and
14 deletions
ccnode/tags.py
+
37
−
14
View file @
9c1807b2
...
...
@@ -10,7 +10,7 @@ logger = logging.getLogger(__name__)
class
Tag
(
object
):
"""
``class`` that abstract tags and act as a simple container.
"""
def
__init__
(
self
,
name
,
valuable
,
ttl
=-
1
,
parent
=
None
):
def
__init__
(
self
,
name
,
valuable
,
ttl
=-
1
,
refresh
=
None
,
parent
=
None
):
"""
:param string name: tag name
:param valuable: something that gives tag value, string or callable
...
...
@@ -19,35 +19,58 @@ class Tag(object):
* None (means no ttl)
* -1 (means infinite ttl)
* positive integer in seconds
Note: this has absolutely not any importance for the cc-node, only
for cc-server
:param None,int refresh: period used to refresh tag value on the node
:param object obj: parent object the tag is attached to, it may be
needed for the tag callable to process the tag
"""
self
.
name
=
name
self
.
value
=
None
self
.
is_function
=
False
self
.
ttl
=
ttl
self
.
refresh
=
refresh
self
.
parent
=
parent
if
parent
is
None
else
weakref
.
proxy
(
parent
)
if
inspect
.
isfunction
(
valuable
):
self
.
is_function
=
True
args_count
=
len
(
inspect
.
getargspec
(
valuable
).
args
)
if
args_count
>
1
:
raise
Exception
(
'
Invalid number of argument for tag function
'
)
raise
TypeError
(
'
Tag function take at most 1 argument
'
)
elif
args_count
==
1
:
self
.
_value
=
partial
(
valuable
,
self
.
parent
)
self
.
_
calculate_
value
=
partial
(
valuable
,
self
.
parent
)
else
:
self
.
_value
=
valuable
self
.
_
calculate_
value
=
valuable
else
:
self
.
_
value
=
lambda
:
valuable
self
.
value
=
valuable
@property
def
value
(
self
):
"""
Property that return tag value. Just return the provided value or
call the function.
self
.
watcher
=
None
def
calculate_value
(
self
):
self
.
_value
=
self
.
_calculate_value
()
# logger.debug('Calculate Tag(%s) = %s', self.name, self._value)
def
start
(
self
,
loop
):
"""
:param loop: pyev loop
"""
try
:
return
self
.
_value
()
except
Exception
:
logger
.
exception
(
'
Calculating tag %s failed:
'
,
self
.
name
)
return
None
if
not
self
.
is_function
:
return
if
self
.
refresh
is
None
:
self
.
_value
=
self
.
_calculate_value
()
return
# TODO more sophisticated calculation with event propagation
self
.
watcher
=
loop
.
timer
(.
0
,
float
(
self
.
refresh
),
lambda
*
args
:
self
.
calculate_value
())
self
.
watcher
.
start
()
def
stop
(
self
):
if
watcher
is
not
None
:
self
.
watcher
.
stop
()
def
tag_inspector
(
mod
,
parent
=
None
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment