2 #include <boost/python.hpp>
12 #include <boost/filesystem.hpp>
37 return getPathsImpl();
50 std::string
generate (
const std::string& iOutputFormatString,
55 return generateImpl (iNbOfDraws, lOutputFormatEnum);
61 std::string
search (
const std::string& iOutputFormatString,
62 const std::string& iTravelQuery) {
66 return searchImpl (iTravelQuery, lOutputFormatEnum);
73 std::string getPathsImpl() {
74 std::ostringstream oPythonLogStr;
77 if (_logOutputStream == NULL) {
78 oPythonLogStr <<
"The log filepath is not valid." << std::endl;
79 return oPythonLogStr.str();
81 assert (_logOutputStream != NULL);
86 *_logOutputStream <<
"Get the file-path details" << std::endl;
88 if (_opentrepService == NULL) {
89 oPythonLogStr <<
"The OpenTREP service has not been initialised, "
90 <<
"i.e., the init() method has not been called "
91 <<
"correctly on the OpenTrepSearcher object. Please "
92 <<
"check that all the parameters are not empty and "
93 <<
"point to actual files.";
94 *_logOutputStream << oPythonLogStr.str();
95 return oPythonLogStr.str();
97 assert (_opentrepService != NULL);
102 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
105 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
106 const SQLiteDBFilePath_T& lSQLiteDBFilePath = lDBFilePathPair.second;
109 oPythonLogStr << lPORFilePath <<
";" << lTravelDBFilePath
110 <<
";" << lSQLiteDBFilePath;
113 *_logOutputStream <<
"ORI-maintained list of POR: '" << lPORFilePath
115 *_logOutputStream <<
"Xapian travel database/index: '"
116 << lTravelDBFilePath <<
"'" << std::endl;
117 *_logOutputStream <<
"SQLite3 database: '"
118 << lSQLiteDBFilePath <<
"'" << std::endl;
120 }
catch (
const RootException& eOpenTrepError) {
121 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
124 }
catch (
const std::exception& eStdError) {
125 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
128 *_logOutputStream <<
"Unknown error" << std::endl;
132 return oPythonLogStr.str();
138 std::string indexImpl() {
139 std::ostringstream oPythonLogStr;
142 if (_logOutputStream == NULL) {
143 oPythonLogStr <<
"The log filepath is not valid." << std::endl;
144 return oPythonLogStr.str();
146 assert (_logOutputStream != NULL);
151 *_logOutputStream <<
"Indexation by Xapian" << std::endl;
153 if (_opentrepService == NULL) {
154 oPythonLogStr <<
"The OpenTREP service has not been initialised, "
155 <<
"i.e., the init() method has not been called "
156 <<
"correctly on the OpenTrepSearcher object. Please "
157 <<
"check that all the parameters are not empty and "
158 <<
"point to actual files.";
159 *_logOutputStream << oPythonLogStr.str();
160 return oPythonLogStr.str();
162 assert (_opentrepService != NULL);
167 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
170 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
171 const SQLiteDBFilePath_T& lSQLiteDBFilePath = lDBFilePathPair.second;
175 *_logOutputStream <<
"ORI-maintained list of POR: '" << lPORFilePath
177 *_logOutputStream <<
"Xapian travel database/index: '"
178 << lTravelDBFilePath <<
"'" << std::endl;
179 *_logOutputStream <<
"SQLite3 database: '"
180 << lSQLiteDBFilePath <<
"'" << std::endl;
186 oPythonLogStr << lNbOfEntries;
189 *_logOutputStream <<
"Xapian indexation yielded " << lNbOfEntries
190 <<
" POR (points of reference) entries." << std::endl;
192 }
catch (
const RootException& eOpenTrepError) {
193 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
196 }
catch (
const std::exception& eStdError) {
197 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
200 *_logOutputStream <<
"Unknown error" << std::endl;
204 return oPythonLogStr.str();
210 std::string searchImpl(
const std::string& iTravelQuery,
212 std::ostringstream oNoDetailedStr;
213 std::ostringstream oDetailedStr;
214 std::ostringstream oJSONStr;
215 std::ostringstream oProtobufStr;
218 if (_logOutputStream == NULL) {
219 oNoDetailedStr <<
"The log filepath is not valid." << std::endl;
220 return oNoDetailedStr.str();
222 assert (_logOutputStream != NULL);
227 *_logOutputStream <<
"Travel query ('" << iTravelQuery <<
"'"
228 <<
"') search" << std::endl;
230 if (_opentrepService == NULL) {
231 oNoDetailedStr <<
"The OpenTREP service has not been initialised, "
232 <<
"i.e., the init() method has not been called "
233 <<
"correctly on the OpenTrepSearcher object. Please "
234 <<
"check that all the parameters are not empty and "
235 <<
"point to actual files.";
236 *_logOutputStream << oNoDetailedStr.str();
237 return oNoDetailedStr.str();
239 assert (_opentrepService != NULL);
244 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
247 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
248 const SQLiteDBFilePath_T& lSQLiteDBFilePath = lDBFilePathPair.second;
251 *_logOutputStream <<
"Xapian travel database/index: '"
253 <<
"' - SQLite3 database: '"
255 <<
"' - ORI-maintained list of POR: '"
256 << lPORFilePath <<
"'" << std::endl;
263 lNonMatchedWordList);
266 *_logOutputStream <<
"Python search for '" << iTravelQuery <<
"' gave "
267 << nbOfMatches <<
" matches." << std::endl;
269 if (nbOfMatches != 0) {
272 for(LocationList_T::const_iterator itLocation = lLocationList.begin();
273 itLocation != lLocationList.end(); ++itLocation, ++idx) {
274 const Location& lLocation = *itLocation;
277 oNoDetailedStr <<
",";
281 oNoDetailedStr << lLocation.getIataCode();
282 oDetailedStr << idx+1 <<
". " << lLocation.toSingleLocationString()
288 lLocation.getExtraLocationList();
289 if (lExtraLocationList.empty() ==
false) {
290 oDetailedStr <<
" Extra matches: " << std::endl;
293 for (LocationList_T::const_iterator itLoc =
294 lExtraLocationList.begin();
295 itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
296 oNoDetailedStr <<
":";
297 oDetailedStr <<
" " << idx+1 <<
"." << idxExtra+1 <<
". ";
299 const Location& lExtraLocation = *itLoc;
300 oNoDetailedStr << lExtraLocation.getIataCode();
301 oDetailedStr << lExtraLocation << std::endl;
307 oNoDetailedStr <<
"/" << lLocation.getPercentage();
312 lLocation.getAlternateLocationList();
313 if (lAlternateLocationList.empty() ==
false) {
314 oDetailedStr <<
" Alternate matches: " << std::endl;
317 for (LocationList_T::const_iterator itLoc =
318 lAlternateLocationList.begin();
319 itLoc != lAlternateLocationList.end(); ++itLoc, ++idxAlter) {
320 oNoDetailedStr <<
"-";
321 oDetailedStr <<
" " << idx+1 <<
"." << idxAlter+1 <<
". ";
323 const Location& lAlternateLocation = *itLoc;
324 oNoDetailedStr << lAlternateLocation.getIataCode()
325 <<
"/" << lAlternateLocation.getPercentage();
326 oDetailedStr << lAlternateLocation << std::endl;
332 if (lNonMatchedWordList.empty() ==
false) {
333 oNoDetailedStr <<
";";
334 oDetailedStr <<
"Not recognised words:" << std::endl;
336 for (WordList_T::const_iterator itWord = lNonMatchedWordList.begin();
337 itWord != lNonMatchedWordList.end(); ++itWord, ++idx) {
338 const Word_T& lWord = *itWord;
340 oNoDetailedStr <<
",";
341 oDetailedStr << idx+1 <<
"." << std::endl;
343 oNoDetailedStr << lWord;
344 oDetailedStr << lWord;
349 *_logOutputStream <<
"Python search for '" << iTravelQuery
350 <<
"' yielded:" << std::endl;
357 lNonMatchedWordList);
360 *_logOutputStream <<
"Short version: "
361 << oNoDetailedStr.str() << std::endl;
362 *_logOutputStream <<
"Long version: "
363 << oDetailedStr.str() << std::endl;
364 *_logOutputStream <<
"JSON version: "
365 << oJSONStr.str() << std::endl;
366 *_logOutputStream <<
"Protobuf version: "
367 << oProtobufStr.str() << std::endl;
369 }
catch (
const RootException& eOpenTrepError) {
370 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
373 }
catch (
const std::exception& eStdError) {
374 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
377 *_logOutputStream <<
"Unknown error" << std::endl;
382 switch (iOutputFormat) {
384 return oNoDetailedStr.str();
388 return oDetailedStr.str();
392 return oJSONStr.str();
396 return oProtobufStr.str();
412 std::ostringstream oNoDetailedStr;
413 std::ostringstream oDetailedStr;
414 std::ostringstream oJSONStr;
415 std::ostringstream oProtobufStr;
418 if (_logOutputStream == NULL) {
419 oNoDetailedStr <<
"The log filepath is not valid." << std::endl;
420 return oNoDetailedStr.str();
422 assert (_logOutputStream != NULL);
427 *_logOutputStream <<
"Number of random draws: " << iNbOfDraws
430 if (_opentrepService == NULL) {
431 oNoDetailedStr <<
"The OpenTREP service has not been initialised, "
432 <<
"i.e., the init() method has not been called "
433 <<
"correctly on the OpenTrepSearcher object. Please "
434 <<
"check that all the parameters are not empty and "
435 <<
"point to actual files.";
436 *_logOutputStream << oNoDetailedStr.str();
437 return oNoDetailedStr.str();
439 assert (_opentrepService != NULL);
444 const PORFilePath_T& lPORFilePath = lFilePathSet.first;
447 const TravelDBFilePath_T& lTravelDBFilePath = lDBFilePathPair.first;
448 const SQLiteDBFilePath_T& lSQLiteDBFilePath = lDBFilePathPair.second;
451 *_logOutputStream <<
"Xapian travel database/index: '"
453 <<
"' - SQLite3 database: '"
455 <<
"' - ORI-maintained list of POR: '"
456 << lPORFilePath <<
"'" << std::endl;
464 *_logOutputStream <<
"Python generation of " << iNbOfDraws <<
" gave "
465 << nbOfMatches <<
" documents." << std::endl;
467 if (nbOfMatches != 0) {
470 for(LocationList_T::const_iterator itLocation = lLocationList.begin();
471 itLocation != lLocationList.end(); ++itLocation, ++idx) {
472 const Location& lLocation = *itLocation;
475 oNoDetailedStr <<
",";
479 oNoDetailedStr << lLocation.getIataCode();
480 oDetailedStr << idx+1 <<
". " << lLocation.toSingleLocationString()
486 lLocation.getExtraLocationList();
487 if (lExtraLocationList.empty() ==
false) {
488 oDetailedStr <<
" Extra matches: " << std::endl;
491 for (LocationList_T::const_iterator itLoc =
492 lExtraLocationList.begin();
493 itLoc != lExtraLocationList.end(); ++itLoc, ++idxExtra) {
494 oNoDetailedStr <<
":";
495 oDetailedStr <<
" " << idx+1 <<
"." << idxExtra+1 <<
". ";
497 const Location& lExtraLocation = *itLoc;
498 oNoDetailedStr << lExtraLocation.getIataCode();
499 oDetailedStr << lExtraLocation << std::endl;
505 oNoDetailedStr <<
"/" << lLocation.getPercentage();
510 lLocation.getAlternateLocationList();
511 if (lAlternateLocationList.empty() ==
false) {
512 oDetailedStr <<
" Alternate matches: " << std::endl;
515 for (LocationList_T::const_iterator itLoc =
516 lAlternateLocationList.begin();
517 itLoc != lAlternateLocationList.end(); ++itLoc, ++idxAlter) {
518 oNoDetailedStr <<
"-";
519 oDetailedStr <<
" " << idx+1 <<
"." << idxAlter+1 <<
". ";
521 const Location& lAlternateLocation = *itLoc;
522 oNoDetailedStr << lAlternateLocation.getIataCode()
523 <<
"/" << lAlternateLocation.getPercentage();
524 oDetailedStr << lAlternateLocation << std::endl;
531 *_logOutputStream <<
"Python generation of " << iNbOfDraws
532 <<
" yielded:" << std::endl;
540 lNonMatchedWordList);
543 *_logOutputStream <<
"Short version: "
544 << oNoDetailedStr.str() << std::endl;
545 *_logOutputStream <<
"Long version: "
546 << oDetailedStr.str() << std::endl;
547 *_logOutputStream <<
"JSON version: "
548 << oJSONStr.str() << std::endl;
549 *_logOutputStream <<
"Protobuf version: "
550 << oProtobufStr.str() << std::endl;
552 }
catch (
const RootException& eOpenTrepError) {
553 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.what()
556 }
catch (
const std::exception& eStdError) {
557 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
560 *_logOutputStream <<
"Unknown error" << std::endl;
565 switch (iOutputFormat) {
567 return oNoDetailedStr.str();
571 return oDetailedStr.str();
575 return oJSONStr.str();
579 return oProtobufStr.str();
601 : _opentrepService (iOpenTrepSearcher._opentrepService),
602 _logOutputStream (iOpenTrepSearcher._logOutputStream) {
609 _opentrepService = NULL;
610 _logOutputStream = NULL;
616 bool init (
const std::string& iTravelDBFilePath,
617 const std::string& iSQLiteDBFilePath,
618 const std::string& iLogFilePath) {
619 bool isEverythingOK =
true;
624 boost::filesystem::path lXapianFilePath (iTravelDBFilePath.begin(),
625 iTravelDBFilePath.end());
626 if (!(boost::filesystem::exists (lXapianFilePath)
627 && boost::filesystem::is_directory (lXapianFilePath))) {
628 isEverythingOK =
false;
629 return isEverythingOK;
633 _logOutputStream =
new std::ofstream;
634 assert (_logOutputStream != NULL);
637 _logOutputStream->open (iLogFilePath.c_str());
638 _logOutputStream->clear();
641 *_logOutputStream <<
"Python wrapper initialisation" << std::endl;
652 *_logOutputStream <<
"Python wrapper initialised" << std::endl;
655 *_logOutputStream <<
"OpenTrep error: " << eOpenTrepError.
what()
658 }
catch (
const std::exception& eStdError) {
659 *_logOutputStream <<
"Error: " << eStdError.what() << std::endl;
662 *_logOutputStream <<
"Unknown error" << std::endl;
665 return isEverythingOK;
672 bool isEverythingOK =
true;
677 if (_opentrepService != NULL) {
678 delete _opentrepService; _opentrepService = NULL;
682 if (_logOutputStream != NULL) {
684 *_logOutputStream <<
"Python wrapper finalization" << std::endl;
685 _logOutputStream->close();
686 delete _logOutputStream; _logOutputStream = NULL;
692 return isEverythingOK;
700 std::ofstream* _logOutputStream;
707 boost::python::class_<OPENTREP::OpenTrepSearcher> (
"OpenTrepSearcher")
std::pair< const TravelDBFilePath_T, const SQLiteDBFilePath_T > DBFilePathPair_T
bool init(const std::string &iTravelDBFilePath, const std::string &iSQLiteDBFilePath, const std::string &iLogFilePath)
std::vector< std::string > WordList_T
unsigned short NbOfMatches_T
OpenTrepSearcher(const OpenTrepSearcher &iOpenTrepSearcher)
unsigned int NbOfDBEntries_T
Interface for the OPENTREP Services.
BOOST_PYTHON_MODULE(libpyopentrep)
NbOfMatches_T drawRandomLocations(const NbOfMatches_T &iNbOfDraws, LocationList_T &)
Root of the OpenTREP exceptions.
std::string search(const std::string &iOutputFormatString, const std::string &iTravelQuery)
API wrapper around the OpenTREP C++ API, so that Python scripts can use it seamlessly.
std::list< Location > LocationList_T
NbOfDBEntries_T buildSearchIndex()
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
std::pair< const PORFilePath_T, const DBFilePathPair_T > FilePathSet_T
FilePathSet_T getFilePaths() const
static void jsonExportLocationList(std::ostream &, const LocationList_T &)
static void exportLocationList(std::ostream &, const LocationList_T &, const WordList_T &iNonMatchedWordList)
std::string generate(const std::string &iOutputFormatString, const NbOfMatches_T &iNbOfDraws)
const char * what() const