Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 094e485

Browse files
committed
Add python module (python wrapper for libcppl)
1 parent f78e9df commit 094e485

File tree

8 files changed

+671
-0
lines changed

8 files changed

+671
-0
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ performance.o: performance.c performance.h
6868
policy-decision-point:
6969
$(MAKE) -C policy-decision-point
7070

71+
.PHONY: python-module
72+
python-module:
73+
$(MAKE) -C python-module
74+
7175
test:
7276
make -C examples/test_cases/
7377
make -C examples/test_cases/ clean
@@ -81,3 +85,4 @@ clean:
8185
#delete output from reason printer
8286
$(RM) *.json
8387
$(MAKE) clean -C policy-decision-point
88+
$(MAKE) clean -C python-module

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ make
2626
make test
2727
```
2828

29+
#### Integrating CPPL in your python project
30+
31+
If you want to use CPPL from a python project you may use the available python module, which is built with
32+
```bash
33+
make python-module
34+
```
35+
This will produce `cppl_cpp_python_bridge.so` and `cppl.py` in `python-module/build/lib.<architecture>`.
36+
You should only use `cppl.py` in your python project, but still need to make `cppl_cpp_python_bridge.so` and `policy-decision-point/libcppl.so` available for the python interpreter, e.g., by setting `LD_LIBRARY_PATH` accordingly.
37+
2938
#### Troubeshooting
3039

3140
If your system provides the jsoncpp library in version smaller 0.7.0 the compiling process will encounter errors such as ```undefined reference to `Json::Value::isInt64() const'```. In such a case, please set `USE_SYSTEM_JSONCPP` in Makefile and policy-decision-point/Makefile to 0 to use the jsoncpp version included in this repository.

python-module/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
cppl.so
3+

python-module/Makefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
SOURCES = cpplmodule.cc cppl.py
3+
OBJFILES =
4+
5+
MODULES =
6+
7+
cppl.so: build
8+
9+
# TODO clean dependency ensures that always the newest ../../libprivercloud.a is used
10+
build: clean setup.py $(SOURCES) $(OBJFILES) $(MODULES) libcppl
11+
python setup.py build
12+
13+
sdist: $(SOURCES) setup.py $(OBJFILES) libcppl
14+
@echo "WARNING: untested makefile target"
15+
python setup.py sdist
16+
17+
install: $(SOURCES) setup.py $(OBJFILES) libcppl
18+
@echo "WARNING: untested makefile target"
19+
python setup.py install
20+
21+
.PHONY: libcppl
22+
libcppl:
23+
make -C ../policy-decision-point/ lib
24+
25+
tidy:
26+
rm -fr build
27+
28+
clean: tidy
29+

python-module/cppl.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import cppl_cpp_python_bridge as cppl_bridge
2+
3+
error = cppl_bridge.error
4+
5+
def init():
6+
cppl_bridge.init()
7+
8+
def cleanup():
9+
cppl_bridge.cleanup()
10+
11+
class PolicyDefinition(object):
12+
"""docstring for PolicyDefinition"""
13+
def __init__(self, policyDefinitionPath, functionHandlerPath):
14+
super(PolicyDefinition, self).__init__()
15+
self.pd_id = cppl_bridge.read_policy_definition_from_file( policyDefinitionPath, functionHandlerPath)
16+
17+
def get_id(self):
18+
return self.pd_id
19+
20+
def __del__(self):
21+
cppl_bridge.policy_definition_free( self.get_id() )
22+
23+
class NodeParameters(object):
24+
"""docstring for NodeParameters"""
25+
def __init__(self, nodeParametersPath, nodeRuntimeParametersPath, policyDefinition):
26+
# sanity checks
27+
if type(policyDefinition) != PolicyDefinition:
28+
raise TypeError
29+
30+
super(NodeParameters, self).__init__()
31+
self.np_id = cppl_bridge.read_node_parameters_from_file( nodeParametersPath, nodeRuntimeParametersPath, policyDefinition.get_id() )
32+
33+
def get_id(self):
34+
return self.np_id
35+
36+
def __del__(self):
37+
cppl_bridge.node_parameters_free( self.get_id() )
38+
39+
class Policy(object):
40+
"""docstring for Policy"""
41+
def __init__(self, policyPath):
42+
super(Policy, self).__init__()
43+
self.string = cppl_bridge.read_policy_from_file( policyPath )
44+
45+
def get_string(self):
46+
return self.string
47+
48+
def compress_to_file(self, destPath, policyDefinition):
49+
# sanity checks
50+
if type(policyDefinition) != PolicyDefinition:
51+
raise TypeError
52+
53+
ret = cppl_bridge.compress_policy_to_file(self.get_string(), policyDefinition.get_id(), destPath)
54+
55+
# def __del__(self):
56+
# # Nothing todo as only a string is passed from C++ to Python
57+
# cppl_bridge.compressed_cppl_free( self.get_id() )
58+
59+
class CompressedPolicy(object):
60+
"""docstring for CompressedPolicy"""
61+
def __init__(self, compressedPolicyPath, policyDefinition):
62+
# sanity checks
63+
if type(policyDefinition) != PolicyDefinition:
64+
raise TypeError
65+
66+
super(CompressedPolicy, self).__init__()
67+
self.ccppl_id = cppl_bridge.read_compressed_cppl_from_file( compressedPolicyPath, policyDefinition.get_id() )
68+
self.policyDefinition = policyDefinition
69+
70+
def get_id(self):
71+
return self.ccppl_id
72+
73+
def __del__(self):
74+
cppl_bridge.compressed_cppl_free( self.get_id() )
75+
76+
def evaluate(self, nodeParameters, with_reason=True):
77+
# sanity checks
78+
if type( nodeParameters ) != NodeParameters:
79+
raise TypeError
80+
81+
ret_eval = cppl_bridge.evaluate(self.get_id(), nodeParameters.get_id() )
82+
83+
reason = None
84+
if with_reason:
85+
reason = cppl_bridge.get_reason( self.get_id(), self.policyDefinition.get_id() )
86+
87+
return ret_eval, reason

0 commit comments

Comments
 (0)