forked from digi-project/dspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
silveryfu
committed
Apr 12, 2021
1 parent
f7a9425
commit f756a61
Showing
10 changed files
with
398 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Event filters""" | ||
|
||
|
||
def always(*args, **kwargs): | ||
_, _ = args, kwargs | ||
return True | ||
|
||
|
||
def has_diff(_, diff, path, *args, **kwargs) -> bool: | ||
_, _ = args, kwargs | ||
changed_paths = {(".",): True} | ||
# TBD: add shared diff for all handlers | ||
# TBD: support incremental diff | ||
|
||
for op, path_, old, new in diff: | ||
# on create | ||
if old is None and len(path_) == 0: | ||
changed_paths.update(_from_model(new)) | ||
else: | ||
changed_paths.update(_from_path_tuple(path_)) | ||
|
||
if path in changed_paths or len(diff) == 0: | ||
return True | ||
return False | ||
|
||
|
||
def _from_model(d: dict): | ||
result = dict() | ||
to_visit = [[d.get("spec", {}), []]] | ||
for n, prefix in to_visit: | ||
result[tuple(prefix)] = True | ||
if type(n) is not dict: | ||
continue | ||
for _k, _v in n.items(): | ||
to_visit.append([_v, prefix + [_k]]) | ||
return result | ||
|
||
|
||
def _from_path_tuple(p: tuple): | ||
# expand a path tuple to dict of paths | ||
return { | ||
# skip "spec" | ||
p[1:_i + 1]: True | ||
for _i in range(len(p)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Logic and policy processors""" | ||
|
||
import pyjq | ||
import time | ||
from view import ModelView | ||
|
||
|
||
def jq(policy: str): | ||
# preproc macro | ||
_macros = { | ||
"$time": str(time.time()), | ||
# ... | ||
} | ||
|
||
for _m, _v in _macros.items(): | ||
policy = policy.replace(_m, _v) | ||
|
||
def fn(proc_view, *args, **kwargs): | ||
_, _ = args, kwargs | ||
with ModelView(proc_view) as mv: | ||
mv.update(pyjq.one(policy, mv)) | ||
|
||
return fn | ||
|
||
|
||
def py(policy: str): | ||
return NotImplemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#kopf==0.28.03 | ||
kopf | ||
kubernetes | ||
pyyaml | ||
inflection | ||
pyjq | ||
python-box |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import time | ||
import yaml | ||
import os | ||
import sys | ||
import pprint as pp | ||
|
||
_dir = os.path.dirname(os.path.realpath(__file__)) | ||
_parent_dir = os.path.dirname(_dir) | ||
sys.path.insert(0, _parent_dir) | ||
|
||
from processor import jq | ||
|
||
test_yaml = f""" | ||
control: | ||
brightness: | ||
intent: 0.8 | ||
status: 0 | ||
mode: | ||
intent: sleep | ||
status: sleep | ||
mount: | ||
mock.digi.dev/v1/lamps: | ||
default/lamp-test: | ||
spec: | ||
control: | ||
power: | ||
intent: "on" | ||
mock.digi.dev/v1/motionsensors: | ||
default/motionsensor-test: | ||
spec: | ||
obs: | ||
last_triggered_time: {time.time()} | ||
reflex: | ||
motion-mode: | ||
policy: 'if $time - ."motionsensor-test".obs.last_triggered_time <= 600 | ||
then .root.control.mode.intent = "work" else . end' | ||
priority: 1 | ||
""" | ||
|
||
if __name__ == '__main__': | ||
v = yaml.load(test_yaml, Loader=yaml.FullLoader) | ||
start = time.time() | ||
jq(v["reflex"]["motion-mode"]["policy"])(v) | ||
print(f"took {time.time() - start}s") | ||
pp.pprint(v) |
Oops, something went wrong.