| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | # SPDX-License-Identifier: AGPL-3.0-or-later# lint: pylint"""MongoDB engine (Offline)"""import refrom pymongo import MongoClient  # pylint: disable=import-errorengine_type = 'offline'# mongodb connection variableshost = '127.0.0.1'port = 27017username = ''password = ''database = Nonecollection = Nonekey = None# engine specific variablespaging = Trueresults_per_page = 20exact_match_only = Falseresult_template = 'key-value.html'_client = Nonedef init(_):    connect()def connect():    global _client  # pylint: disable=global-statement    kwargs = {'port': port}    if username:        kwargs['username'] = username    if password:        kwargs['password'] = password    _client = MongoClient(host, **kwargs)[database][collection]def search(query, params):    results = []    if exact_match_only:        q = {'$eq': query}    else:        _re = re.compile('.*{0}.*'.format(re.escape(query)), re.I | re.M)        q = {'$regex': _re}    query = _client.find({key: q}).skip((params['pageno'] - 1) * results_per_page).limit(results_per_page)    results.append({'number_of_results': query.count()})    for r in query:        del r['_id']        r = {str(k): str(v) for k, v in r.items()}        r['template'] = result_template        results.append(r)    return results
 |