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)).

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