Skip to content

Commit 50c17fb

Browse files
committed
Added virtual env
0 parents  commit 50c17fb

File tree

467 files changed

+77062
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

467 files changed

+77062
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pyc
2+
.project
3+
.pydevproject

__init__.py

Whitespace-only changes.

app/RIPMatlab.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'''
2+
Created on 20/10/2015
3+
4+
@author: jcsombria
5+
'''
6+
import os.path
7+
8+
from jsonrpc.JsonRpcServer import JsonRpcServer
9+
from app.MatlabConnector import CommandBuilder, MatlabConnector
10+
from app.SimulinkConnector import SimulinkConnector
11+
12+
import matlab.engine
13+
14+
class RIPMatlab(JsonRpcServer):
15+
'''
16+
classdocs
17+
'''
18+
19+
def __init__(self):
20+
'''
21+
Constructor
22+
'''
23+
super().__init__()
24+
self.on('connect', 0, self._connect)
25+
self.on('disconnect', 0, self._disconnect)
26+
self.on('get', 1, self._get)
27+
self.on('set', 2, self._set)
28+
self.on('eval', 1, self._eval)
29+
self.on('open', 1, self._open)
30+
self.on('step', 1, self._step)
31+
self.commandBuilder = CommandBuilder()
32+
self.matlab = MatlabConnector()
33+
self.simulink = SimulinkConnector()
34+
35+
def _connect(self):
36+
self._matlab = matlab.engine.start_matlab('-desktop')
37+
self.simulink.set
38+
return True
39+
40+
41+
def _disconnect(self):
42+
self._matlab.quit()
43+
44+
45+
def _set(self, variables, values):
46+
self.matlab.set(variables, values)
47+
48+
49+
def _get(self, variables):
50+
return self.matlab.get(variables)
51+
52+
53+
54+
55+
def _eval(self, command):
56+
try:
57+
result = self._matlab.eval(command, nargout=0)
58+
except:
59+
pass
60+
return result
61+
62+
63+
def _open(self, path):
64+
try:
65+
#open
66+
dirname = os.path.dirname(path)
67+
cd = self.commandBuilder.cd(dirname)
68+
model = os.path.basename(path)
69+
load_system = self.commandBuilder.load_system(model)
70+
command = cd + load_system
71+
result = self._matlab.eval(command, nargout=0)
72+
#addEjsSubsystem
73+
_load_model(model)
74+
75+
except:
76+
return None
77+
return result
78+
79+
def _load_model(self, model):
80+
command = self.commandBuilder.addEjsSubblock(model) + \
81+
self.commandBuilder.addPauseBlock(model) + \
82+
self.commandBuilder.set_param(model, "StartTime", "0") + \
83+
self.commandBuilder.set_param(model, "StopTime", "inf")
84+
self._matlab.eval(command)
85+
86+
87+
def _step(self, command):
88+
try:
89+
result = self._matlab.eval(command, nargout=0)
90+
except:
91+
return None
92+
return result

app/__init__.py

Whitespace-only changes.

jsonrpc/JsonRpcBuilder.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# JSON-RPC Builder
2+
# author: Jesús Chacón <jcsombria@gmail.com>
3+
#
4+
# Copyright (C) 2013 Jesús Chacón
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
import ujson
19+
20+
class JsonRpcBuilder(object):
21+
"""A helper class to build JSON-RPC v2.0 objects."""
22+
def request(self, method, params=None, request_id=None):
23+
request = {
24+
'jsonrpc': '2.0',
25+
'method': method,
26+
}
27+
if(params != None):
28+
request['params'] = params
29+
if(request_id != None):
30+
request['id'] = request_id
31+
return request
32+
33+
def response(self, result, request_id=None):
34+
''' Build a jsonrpc response object
35+
'''
36+
if(request_id != None):
37+
return {
38+
'jsonrpc': '2.0',
39+
'result': result,
40+
'id': request_id
41+
}
42+
else:
43+
return {
44+
'jsonrpc': '2.0',
45+
'result': result,
46+
}
47+
48+
def error_response(self, error, request_id=None):
49+
''' Build a jsonrpc error response object
50+
'''
51+
return {
52+
'jsonrpc': '2.0',
53+
'error': error,
54+
'id': request_id
55+
}
56+
57+
def error(self, code, message, data=None):
58+
''' Build a jsonrpc error object
59+
'''
60+
if(data != None):
61+
return {
62+
'code': code,
63+
'message': message,
64+
'data': data
65+
}
66+
else:
67+
return {
68+
'code': code,
69+
'message': message,
70+
}
71+
72+
def parseResponse(self, message):
73+
response = None
74+
try:
75+
response = ujson.loads(message);
76+
except ValueError:
77+
print(error);
78+
return response.get('result') if response != None else None
79+
80+
#def _add_if_defined():
81+

jsonrpc/JsonRpcServer.py

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# JSON-RPC Server
2+
# author: Jesús Chacón <jcsombria@gmail.com>
3+
#
4+
# Copyright (C) 2013 Jesús Chacón
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
import ujson
19+
from jsonrpc.JsonRpcBuilder import JsonRpcBuilder
20+
21+
class JsonRpcServer(object):
22+
"""Base class for defining a JSON-RPC v2.0 server."""
23+
24+
PARSE_ERROR = {
25+
'code': -32700,
26+
'message': 'Invalid JSON was received by the server.',
27+
}
28+
INVALID_REQUEST = {
29+
'code': -32600,
30+
'message': 'The JSON sent is not a valid Request object.',
31+
}
32+
METHOD_NOT_FOUND = {
33+
'code': -32601,
34+
'message': 'Method not found. The method does not exist / is not available.',
35+
}
36+
INVALID_PARAMS = {
37+
'code': -32602,
38+
'message': 'Invalid params',
39+
}
40+
INTERNAL_ERROR = {
41+
'code': -32603,
42+
'message':'Internal JSON-RPC error.',
43+
}
44+
ERRORS = {
45+
'PARSE_ERROR': PARSE_ERROR,
46+
'INVALID_REQUEST': INVALID_REQUEST,
47+
'METHOD_NOT_FOUND': METHOD_NOT_FOUND,
48+
'INVALID_PARAMS': INVALID_PARAMS,
49+
'INTERNAL_ERROR': INTERNAL_ERROR,
50+
# -32000 to -32099 server error:
51+
# Reserved for implementation-defined server-errors.
52+
}
53+
54+
def __init__(self):
55+
self.builder = JsonRpcBuilder()
56+
self.methods = {}
57+
58+
def parse(self, jsonrpc):
59+
try:
60+
methodCall = ujson.loads(jsonrpc)
61+
except ValueError:
62+
return self._response_with_error('PARSE_ERROR')
63+
isBatchMode = isinstance(methodCall, list)
64+
if(isBatchMode):
65+
if(len(methodCall) == 0):
66+
return self._response_with_error('INVALID_REQUEST')
67+
result = []
68+
for request in methodCall:
69+
response = self.process(request)
70+
if(response):
71+
result.append(response)
72+
else:
73+
result = self.process(methodCall)
74+
return result
75+
76+
def process(self, request):
77+
""" Process a JSON-RPC request:
78+
if the method exists in the server, invoke and return the result.
79+
if not, an error is returned.
80+
"""
81+
request_id = None
82+
try:
83+
(method, params, request_id) = self._check_fields(request)
84+
server_method = self.methods.get(method)
85+
if not server_method:
86+
raise InvocationException('METHOD_NOT_FOUND')
87+
result = self._invoke(method, params)
88+
if request_id:
89+
return self.builder.response(result, request_id)
90+
except InvocationException as error:
91+
return self._response_with_error(error.code, request_id)
92+
93+
def _check_fields(self, request):
94+
# jsonrpc must be 2.0, method must always exists. 'id' and 'params' may be omitted
95+
try:
96+
jsonrpc = request['jsonrpc']
97+
method = request['method']
98+
request_id = request.get('id')
99+
params = request.get('params')
100+
except:
101+
raise InvocationException('INVALID_REQUEST')
102+
103+
return (method, params, request_id)
104+
105+
def _invoke(self, method, params):
106+
theMethod = self.methods.get(method)
107+
if not theMethod:
108+
raise InvocationException('METHOD_NOT_FOUND')
109+
return theMethod.invoke(params)
110+
111+
def _response_with_error(self, error_name, request_id=None):
112+
error_info = self.ERRORS[error_name]
113+
error = self.builder.error(
114+
error_info['code'],
115+
error_info['message'],
116+
)
117+
return self.builder.error_response(error, request_id)
118+
119+
def on(self, method, expected, handler):
120+
if not isinstance(expected, (int, set, list)):
121+
return False
122+
self.methods[method] = Method(handler, expected)
123+
124+
125+
class InvocationException(Exception):
126+
"""Exception raised for an error occurred during the invocation of a method
127+
Attributes:
128+
code -- an code describing the error
129+
"""
130+
def __init__(self, code):
131+
self.code = code
132+
133+
134+
class Method(object):
135+
"""Helper class to invoke a method and check the params
136+
"""
137+
def __init__(self, handler, params):
138+
self._handler = handler
139+
self._params = params
140+
141+
def invoke(self, params):
142+
if not self._expect_params():
143+
return self._handler()
144+
elif self._check_params_by_position(params):
145+
return self._handler(*params)
146+
elif self._check_params_by_name(params):
147+
return self._handler(**params)
148+
raise InvocationException('INVALID_PARAMS')
149+
150+
def _expect_params(self):
151+
try:
152+
return self._params != None and not self._params == 0
153+
except:
154+
return False
155+
156+
157+
def _check_params_by_position(self, params):
158+
try:
159+
expectedByPosition = len(self._params)
160+
except:
161+
expectedByPosition = self._params
162+
return expectedByPosition == 0 or (isinstance(params, list) and len(params) == expectedByPosition)
163+
164+
def _check_params_by_name(self, params):
165+
for name in params:
166+
if name not in self._params:
167+
return False
168+
return True

jsonrpc/__init__.py

Whitespace-only changes.

test.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import unittest
2+
from test.testJsonRpcBuilder import JsonRpcBuilderTest
3+
from test.testJsonRpcServer import JsonRpcServerTest
4+
#from test.testRIPMatlab import RIPMatlabTest
5+
#from test.testCommandBuilder import SimulinkConnectorTest
6+
7+
if __name__ == '__main__':
8+
unittest.main()

test/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)