diff --git a/src/python/recoll/pyrecoll.cpp b/src/python/recoll/pyrecoll.cpp new file mode 100644 index 00000000..975d5d0d --- /dev/null +++ b/src/python/recoll/pyrecoll.cpp @@ -0,0 +1,114 @@ +#ifndef lint +static char rcsid[] = "@(#$Id: pyrecoll.cpp,v 1.1 2008-05-09 12:34:17 dockes Exp $ (C) 2007 J.F.Dockes"; +#endif + +#include + +#include +#include +using namespace std; + +#include "rclinit.h" +#include "rclconfig.h" +#include "idfile.h" +#include "rcldb.h" +#include "pathut.h" +#include "wasastringtoquery.h" +#include "wasatorcl.h" + +static RclConfig *config; + +static PyObject * +recollq_idfile(PyObject *self, PyObject *args) +{ + const char *filename; + + if (!PyArg_ParseTuple(args, "s", &filename)) + return NULL; + // string tp = "Newfule"; + string tp = idFile(filename); + return Py_BuildValue("s", tp.c_str()); +} + +static PyObject * +recollq_question(PyObject *self, PyObject *args) +{ + int limit = 100; + const char *qs; + if (!PyArg_ParseTuple(args, "s", &qs)) { + PyErr_SetString(PyExc_EnvironmentError, "Bad query syntax"); + return NULL; + } + + Rcl::Db rcldb; + string reason; + string dbdir = config->getDbDir(); + rcldb.open(dbdir, config->getStopfile(), + Rcl::Db::DbRO, Rcl::Db::QO_STEM); + + Rcl::SearchData *sd = wasaStringToRcl(qs, reason); + if (!sd) { + PyErr_SetString(PyExc_EnvironmentError, reason.c_str()); + return 0; + } + + RefCntr rq(sd); + rcldb.setQuery(rq, Rcl::Db::QO_STEM); + int cnt = rcldb.getResCnt(); + cout << "Recoll query: " << rq->getDescription() << endl; + if (cnt <= limit) + cout << cnt << " results" << endl; + else + cout << cnt << " results (printing " << limit << " max):" << endl; + + for (int i = 0; i < limit; i++) { + int pc; + Rcl::Doc doc; + if (!rcldb.getDoc(i, doc, &pc)) + break; + char cpc[20]; + sprintf(cpc, "%d", pc); + cout + << doc.mimetype.c_str() << "\t" + << "[" << doc.url.c_str() << "]" << "\t" + << "[" << doc.meta["title"].c_str() << "]" << "\t" + << doc.fbytes.c_str() << "\tbytes" << "\t" + << endl; + } + Py_INCREF(Py_None); + return Py_None; +} + + +static PyMethodDef recollqMethods[] = { + {"idfile", recollq_idfile, METH_VARARGS, "Identify file type."}, + {"question", recollq_question, METH_VARARGS, "Query language query."}, + + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + + +RclConfig *RclConfig::getMainConfig() +{ + return config; +} + +PyMODINIT_FUNC +initrecollq(void) +{ + (void) Py_InitModule("recollq", recollqMethods); + + string reason; + config = recollinit(0, 0, reason, 0); + if (config == 0) { + PyErr_SetString(PyExc_EnvironmentError, reason.c_str()); + return; + } + if (!config->ok()) { + PyErr_SetString(PyExc_EnvironmentError, + "Recoll init error: bad environment ?"); + return; + } + fprintf(stderr, "initrecollq ok\n"); +} + diff --git a/src/python/recoll/setup.py b/src/python/recoll/setup.py new file mode 100644 index 00000000..d9a8afe9 --- /dev/null +++ b/src/python/recoll/setup.py @@ -0,0 +1,55 @@ +from distutils.core import setup, Extension + +module1 = Extension('recollq', + define_macros = [('MAJOR_VERSION', '1'), + ('MINOR_VERSION', '0'), + ('HAVE_MKDTEMP', '1'), + ('HAVE_VASPRINTF', '1'), + ('UNAC_VERSION', '"1.0.7"'), + ('STATFS_INCLUDE', '"sys/mount.h"'), + ('RECOLL_DATADIR', + '"/usr/local/share/recoll"') + ], + include_dirs = ['/usr/local/include', + '../utils', + '../common', + '../rcldb', + '../query', + '../unac' + ], + libraries = ['xapian', 'iconv'], + library_dirs = ['/usr/local/lib'], + sources = ['recoll_query.cpp', + '../common/rclinit.cpp', + '../common/rclconfig.cpp', + '../common/textsplit.cpp', + '../common/unacpp.cpp', + '../query/wasastringtoquery.cpp', + '../query/wasatorcl.cpp', + '../rcldb/rcldb.cpp', + '../rcldb/searchdata.cpp', + '../rcldb/stemdb.cpp', + '../rcldb/pathhash.cpp', + '../rcldb/stoplist.cpp', + '../unac/unac.c', + '../utils/base64.cpp', + '../utils/conftree.cpp', + '../utils/debuglog.cpp', + '../utils/idfile.cpp', + '../utils/readfile.cpp', + '../utils/md5.cpp', + '../utils/pathut.cpp', + '../utils/wipedir.cpp', + '../utils/smallut.cpp' + ]) + + +setup (name = 'RecollQuery', + version = '1.0', + description = 'Enable querying a Recoll full text index', + author = 'J.F. Dockes', + author_email = 'jean-francois.dockes@wanadoo.fr', + long_description = ''' +This is really just a demo package for now. +''', + ext_modules = [module1])