Skip to content

Commit

Permalink
Update for Zim 0.67+
Browse files Browse the repository at this point in the history
Zim's internals (especially IPC) changed rather a lot for this release,
so the plugin's code needs to adapt.
The search provider part now kicks off Zim as a subprocess to handle
the user choosing results.

There may be some performance issues with this update,
while stuff shakes out.

Closes #11.

Sadly, clicking the application icon now does not launch a search of
notebooks (regression against ef691e2).
However it does cause Zim to show the "list of notebooks" window, which
might be useful.
  • Loading branch information
achadwick committed Oct 26, 2017
1 parent 7da6e72 commit 333ebf5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
6 changes: 4 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Dependencies

You need to install the `dbus` Python 2 module from your OS
distribution, and the standard Python package manager `pip`. You need
[Zim][] too.
distribution, and the standard Python package manager `pip`.

You need [Zim][] too.
This plugin is written for Zim 0.67 and later.

### Debian and derivatives

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def run(self):

setup(
name='zimsearch',
version='0.0.1',
version='0.0.2a', # SemVer, interfaces unstable [0.x] while Zim's are too.
license="GPL-2.0+",
description='GNOME integration for Zim',
description='GNOME integration for Zim 0.67+',
url='https://github.com/achadwick/zimsearch',
requires=["dbus"],
long_description=textwrap.dedent("""
Expand Down
130 changes: 67 additions & 63 deletions src/gnomeshellsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import logging
import textwrap
from gettext import gettext as _
import subprocess

import dbus.service
from zim.main import NotebookCommand
from zim.plugins import PluginClass
from zim.search import SearchSelection
from zim.search import Query

logger = logging.getLogger(__name__)
ZIM_COMMAND = "zim"


# Zim plugin inegration:
Expand All @@ -25,7 +29,13 @@ class GnomeShellSearchPluginCommand (NotebookCommand):

def run(self):
from zim.config import ConfigManager
notebook, _ = self.build_notebook()
import zim.notebook

notebook_info = self.get_default_or_only_notebook()
if not notebook_info:
logger.error("No notebooks?")
return
notebook, _junk = zim.notebook.build_notebook(notebook_info)
config_manager = ConfigManager()
config_dict = config_manager.get_config_dict('preferences.conf')
preferences = config_dict['GnomeShellSearch']
Expand Down Expand Up @@ -99,35 +109,16 @@ def quit(self):
async_callbacks=('reply_handler', 'error_handler'))
def GetInitialResultSet(self, terms, reply_handler, error_handler):
"""Handles the initial search."""
notebook_terms, normal_terms = self._process_terms(terms)
search_notebooks = self._get_search_notebooks(notebook_terms)
result = []
if search_notebooks:
for search_notebook in search_notebooks:
res = self._search_notebook(search_notebook, normal_terms)
result.extend(res)
self._process_results(result, terms)
reply_handler(result)
results = self._get_search_results(terms)
reply_handler(results)

@dbus.service.method(dbus_interface=SEARCH_IFACE,
in_signature='asas', out_signature='as',
async_callbacks=('reply_handler', 'error_handler'))
def GetSubsearchResultSet(self, prev_results, terms,
reply_handler, error_handler):
"""Handles searches within sets of previously returned results."""
notebook_terms, normal_terms = self._process_terms(terms)
results = []
for result_id in prev_results:
notebook_id, page_id, create = self._from_result_id(result_id)
if create:
continue
# It will be added later, on the end, by _process_results().
if notebook_terms:
if not self._contains_all_terms(notebook_id, notebook_terms):
continue
if self._contains_all_terms(page_id, normal_terms):
results.append(result_id)
self._process_results(results, terms)
results = self._get_search_results(terms)
reply_handler(results)

@dbus.service.method(dbus_interface=SEARCH_IFACE,
Expand Down Expand Up @@ -163,15 +154,19 @@ def GetResultMetas(self, identifiers):
@dbus.service.method(dbus_interface=SEARCH_IFACE,
in_signature='sasu', out_signature='')
def ActivateResult(self, identifier, terms, timestamp):
"""Handles the user choosing a search result from those offered."""
"""Handles the user choosing a search result from those offered."""
try:
notebook_id, page_id, create = self._from_result_id(identifier)
server = self._get_server()
notebook = self._load_notebook(notebook_id)
gui = server.get_notebook(notebook)
gui.present(page=page_id)
# for the create case,
# could also do
# gui.new_page_from_text(" ".join(terms), open_page=True)
logger.debug("ActivateResult: notebook: %r", notebook_id)
logger.debug("ActivateResult: page: %r", page_id)
proc = subprocess.Popen(
args=[ZIM_COMMAND, notebook_id, page_id],
close_fds=True,
cwd="/",
)
logger.debug("ActivateResult: done (rc=%r)", proc.returncode)
except:
logger.exception("ActivateResult failed")

@dbus.service.method(dbus_interface=SEARCH_IFACE,
in_signature='asu', out_signature='')
Expand All @@ -180,15 +175,42 @@ def LaunchSearch(self, terms, timestamp):
This is supposed to launch the application itself, with the
search terms already typed in.
After upstream's changes for 0.67, we cannot do this without
reimplementing as an GApplication and zim-plugin pair.
While we're in transition, just show the list.
"""
server = self._get_server()
gui = server.get_notebook(self.notebook)
gui.present()
gui.show_search(" ".join(terms))
# FIXME: is it possible to make the GUI search all notebooks too?

def _process_results(self, results, terms):
try:
proc = subprocess.Popen(
args=[ZIM_COMMAND, "--list"],
close_fds=True,
cwd="/",
)
logger.debug("LaunchSearch: done (rc=%r)", proc.returncode)
except:
logger.exception("LaunchSearch failed")

def _get_search_results(self, terms):
try:
notebook_terms, normal_terms = self._process_terms(terms)
notebooks = list(self._get_search_notebooks(notebook_terms))
results = []
query_str = u" ".join(normal_terms)
if not query_str.isspace():
query = Query(query_str)
for notebook in notebooks:
logger.debug('Searching %r for %r', notebook, query)
selection = SearchSelection(notebook)
selection.search(query)
for path in selection:
rid = self._to_result_id(notebook.name, path.name)
results.append(rid)
self._process_results(results, notebook_terms, normal_terms)
return results
except:
logger.exception("_get_search_results() failed")

def _process_results(self, results, notebook_terms, normal_terms):
"""Post-processing of the results set.
This adds extra "New page" results for all matching notebooks,
Expand All @@ -205,23 +227,22 @@ def _process_results(self, results, terms):
if not self.notebook:
return

notebook_terms, normal_terms = self._process_terms(terms)
page_id = " ".join(normal_terms)
page_name = " ".join(normal_terms)

if notebook_terms:
import zim.notebook
notebook_list = zim.notebook.get_notebook_list()
for notebook_info in notebook_list:
notebook_id = notebook_info.name
if self._contains_all_terms(notebook_id, notebook_terms):
result_id = self._to_result_id(notebook_id, page_id,
result_id = self._to_result_id(notebook_id, page_name,
create=True)
results.append(result_id)
return

notebook_id = self.notebook.name
result_id = self._to_result_id(notebook_id, page_id, create=True)
results.append(result_id)
else:
notebook_id = self.notebook.name
result_id = self._to_result_id(notebook_id, page_name,
create=True)
results.append(result_id)

def _process_terms(self, terms):
"""Pre-processing of search terms."""
Expand Down Expand Up @@ -254,11 +275,6 @@ def _get_search_notebooks(self, notebook_terms):
for notebook_info in search_notebooks_info:
yield self._load_notebook(notebook_info.name)

def _search_notebook(self, search_notebook, terms):
for page in search_notebook.index.walk():
if self._contains_all_terms(page.basename, terms):
yield self._to_result_id(search_notebook.name, page.name)

def _load_notebook(self, notebook_id):
if notebook_id in self.notebook_cache:
notebook = self.notebook_cache[notebook_id]
Expand All @@ -272,12 +288,6 @@ def _load_notebook(self, notebook_id):
self.notebook_cache[notebook_id] = notebook
return notebook

def _get_server(self):
import zim.ipc

zim.ipc.start_server_if_not_running()
return zim.ipc.ServerProxy()

def _to_result_id(self, notebook_id, page_id, create=False):
result_dict = {
"notebook": notebook_id,
Expand All @@ -300,9 +310,3 @@ def _contains_all_terms(self, contents, terms):
if unicode(term).lower() not in unicode(contents).lower():
return False
return True

def _contains_any_term(self, contents, terms):
for term in terms:
if unicode(term).lower() in unicode(contents).lower():
return True
return False

0 comments on commit 333ebf5

Please sign in to comment.