View source with raw comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2016, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(swish_csv, []).   36:- use_module(library(pengines), []).   37:- use_module(library(pairs)).   38:- use_module(library(csv), [csv_write_stream/3]).   39:- use_module(library(apply)).   40:- use_module(library(pprint)).   41:- use_module(library(option)).   42:- use_module(library(http/http_cors)).

Support CSV output from a Pengines server

This module defines the result-format csv for Pengines. It allows SWISH users to post a query against the core Prolog system or a saved SWISH program and obtain the results using a simple web client such as curl. An example shell script is provided in client/swish-ask.sh.

To be done
- Provide streaming output */
   54:- multifile
   55	pengines:write_result/3,
   56	write_answers/2,		% Answers, Bindings
   57	write_answers/3.		% Answers, Bindings, Options
 pengines:write_result(+Format, +Event, +VarNames) is semidet
Hook into library(pengines) that makes pengines support CSV output.
   64pengines:write_result(csv, Event, OptionDict) :-
   65	(   Disposition = OptionDict.get(disposition)
   66	->  Options = [disposition(Disposition)]
   67	;   Options = []
   68	),
   69	csv(Event, Options).
   70
   71csv(create(_Id, Features), Options) :- !,
   72	memberchk(answer(Answer), Features),
   73	csv(Answer, Options).
   74csv(destroy(_Id, Wrapped), Options) :- !,
   75	csv(Wrapped, Options).
   76csv(success(_Id, Answers, Projection, _Time, More), Options) :- !,
   77	VarTerm =.. [row|Projection],
   78	success(Answers, VarTerm, [more(More)|Options]).
   79csv(error(_Id, Error), _Options) :- !,
   80	message_to_string(Error, Msg),
   81	format('Status: 400 Bad request~n'),
   82	format('Content-type: text/plain~n~n'),
   83	format('ERROR: ~w~n', [Msg]).
   84csv(output(_Id, message(_Term, _Class, HTML, _Where)), _Opts) :- !,
   85	format('Status: 400 Bad request~n'),
   86	format('Content-type: text/html~n~n'),
   87	format('<html>~n~s~n</html>~n', [HTML]).
   88csv(page(Page, Event), Options) :-
   89	csv(Event, [page(Page)|Options]).
   90csv(failure(_Id, _Time), Options) :- !,
   91	success([], -, [more(false)|Options]).
   92csv(Event, _) :-
   93	print_term(Event, [output(user_error)]).
   94
   95success(Answers, VarTerm, Options) :-
   96	write_answers(Answers, VarTerm, Options), !.
   97success(Answers, VarTerm, Options) :-
   98	write_answers(Answers, VarTerm), !,
   99	assertion(\+option(page(_), Options)).
  100success(Answers, _VarTerm, Options) :-
  101	option(page(Page), Options),
  102	Page > 1, !,
  103	maplist(csv_answer, Answers, Rows),
  104	forall(paginate(100, OutPage, Rows),
  105	       csv_write_stream(current_output, OutPage, [])).
  106success(Answers, VarTerm, Options) :-
  107	option(disposition(Disposition), Options, 'swish-result.csv'),
  108	maplist(csv_answer, Answers, Rows),
  109	cors_enable,
  110	format('Content-encoding: chunked~n'),
  111	format('Content-disposition: attachment; filename="~w"~n', [Disposition]),
  112	format('Content-type: text/csv~n~n'),
  113	projection_row(VarTerm),
  114	forall(paginate(100, Page, Rows),
  115	       csv_write_stream(current_output, Page, [])).
  116
  117projection_row(-) :- !.
  118projection_row(row) :- !.
  119projection_row(VarTerm) :-
  120	csv_write_stream(current_output, [VarTerm], []).
  121
  122paginate(Len, Page, List) :-
  123	length(Page0, Len),
  124	(   append(Page0, Rest, List)
  125	->  (   Page = Page0
  126	    ;	paginate(Len, Page, Rest)
  127	    )
  128	;   Page = List
  129	).
  130
  131
  132csv_answer(JSON, Row) :-
  133	is_dict(JSON), !,
  134	dict_pairs(JSON, _, Pairs),
  135	pairs_values(Pairs, Values),
  136	maplist(csv_value, Values, CVSValues),
  137	Row =.. [row|CVSValues].
  138csv_answer(RowIn, Row) :-
  139	compound(RowIn), !,
  140	RowIn =.. [_|Values],
  141	maplist(csv_value, Values, CVSValues),
  142	Row =.. [row|CVSValues].
  143
  144csv_value(Var, '') :-
  145	var(Var), !.
  146csv_value(Number, Number) :-
  147	number(Number), !.
  148csv_value(Atom, Atom) :-
  149	atom(Atom), !.
  150csv_value(String, String) :-
  151	string(String), !.
  152csv_value(Term, Value) :-
  153	term_string(Term, Value)