comparison cpack/dml/lib/backend_json.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(backend_json,
20 [ python_json/3
21 , python_apply/3
22 , python_call/3
23 , python_call/4
24 , python_call/5
25 ]).
26
27 :- use_module(library(http/json)).
28
29 python_apply(Module:Function,Args,Result) :- !,
30 Input = _{ module:Module, function:Function, arguments:Args },
31 python_json('json_wrapper.py', Input, Output),
32 ( Output=error{value:Msg} -> throw(python_json_error(Msg,Module:Function))
33 ; Output=ok{value:Result}
34 ).
35 python_apply(Function,Args,Result) :- atom(Function),
36 python_apply('__builtin__':Function,Args,Result).
37
38 python_call(ModFunc,A1,Result) :- python_apply(ModFunc,[A1],Result).
39 python_call(ModFunc,A1,A2,Result) :- python_apply(ModFunc,[A1,A2],Result).
40 python_call(ModFunc,A1,A2,A3,Result) :- python_apply(ModFunc,[A1,A2,A3],Result).
41
42 python_json(Script,Input,Output) :-
43 setup_call_cleanup(
44 process_create( dml(python/Script),[],
45 [ cwd(dml(python)), process(PID),
46 stdin(pipe(ToScript)), stdout(pipe(FromScript))]),
47 call_cleanup(
48 ( call_cleanup(json_write_dict(ToScript,Input,[width(0),tag(tag)]), close(ToScript)),
49 json_read_dict(FromScript,Output,[tag(tag)])
50 ), exception(_), process_kill(PID)),
51 (close(FromScript), process_wait(PID,_))
52 ).
53