Child: [98a4b6] (diff)

Download this file

pyrecoll.cpp    115 lines (95 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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 <Python.h>
#include <string>
#include <iostream>
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<Rcl::SearchData> 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");
}