openmedialibrary/oml/fulltext.py
2019-01-29 19:11:51 +05:30

44 lines
1.2 KiB
Python

import logging
import os
import subprocess
import sys
from sqlalchemy.sql import operators
logger = logging.getLogger(__name__)
def get_prefix():
import settings
return os.path.join(os.path.expanduser(settings.preferences['libraryPath']), 'Books/')
def get_ids(books):
from item.models import File
ids = [b[0] for b in File.query.filter(operators.in_op(File.path, books)).values('sha1')]
return ids
def find_fulltext_macos(query):
prefix = get_prefix()
cmd = ["mdfind", "-onlyin", prefix, query]
books = subprocess.check_output(cmd).decode().strip().split('\n')
books = [path[len(prefix):] for path in books]
return get_ids(books)
def find_fulltext_windows(query):
prefix = get_prefix()
prefix = os.path.normpath(prefix)
cmd = ['findstr', '/i', '/c:' + query, '/d:' + prefix]
books = subprocess.check_output(cmd).decode().strip().split('\n')
books = [b.split(':')[0] for b in books]
return get_ids(books, prefix)
def find_fulltext(query):
ids = []
if sys.platform == 'darwin':
ids = find_fulltext_macos(query)
else:
logger.debug('missing fulltext search implementation for %s', sys.platform)
return ids
def platform_supported():
return sys.platform == 'darwin'