[kupfer: 3/6] zim: fix problem with loading notebook list in zim 0.53
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer: 3/6] zim: fix problem with loading notebook list in zim 0.53
- Date: Tue, 13 Dec 2011 17:56:25 +0000 (UTC)
commit 3b9c018717f5dde811e8f22af98f01d19a42a608
Author: Karol BÄdkowski <karol bedkowski gmail com>
Date: Fri Dec 2 16:58:36 2011 +0100
zim: fix problem with loading notebook list in zim 0.53
Rewrite and simplify zim notebook list loader.
Support new file format that come with zim 0.53
kupfer/plugin/zim.py | 131 +++++++++++++++++++++++++++++++++++++------------
1 files changed, 99 insertions(+), 32 deletions(-)
---
diff --git a/kupfer/plugin/zim.py b/kupfer/plugin/zim.py
index 2b73bb1..d840c34 100644
--- a/kupfer/plugin/zim.py
+++ b/kupfer/plugin/zim.py
@@ -9,7 +9,7 @@ __kupfer_actions__ = (
)
__description__ = _("Access to Pages stored in Zim - "
"A Desktop Wiki and Outliner")
-__version__ = "2010-02-03"
+__version__ = "2011-12-02"
__author__ = "Karol BÄdkowski <karol bedkowski gmail com>"
import os
@@ -24,7 +24,7 @@ from kupfer import config, utils, pretty, icons, plugin_support
__kupfer_settings__ = plugin_support.PluginSettings(
{
- "key" : "page_name_starts_colon",
+ "key": "page_name_starts_colon",
"label": _("Page names start with :colon"),
"type": bool,
"value": False,
@@ -32,11 +32,15 @@ __kupfer_settings__ = plugin_support.PluginSettings(
)
'''
+Changes:
+ 2011-12-02 Karol BÄdkowski
+ fix loading notebook list from zim 0.53
TODO:
use FilesystemWatchMixin (?)
'''
+
def _start_zim(notebook, page):
''' Start zim and open given notebook and page. '''
utils.spawn_async(("zim", notebook, page.replace("'", "_")))
@@ -72,12 +76,14 @@ class CreateZimPage(Action):
def get_description(self):
return _("Create page in default notebook")
+
def get_icon_name(self):
return 'document-new'
def item_types(self):
yield TextLeaf
+
class CreateZimPageInNotebook(Action):
""" Create new page in default notebook """
def __init__(self):
@@ -94,11 +100,14 @@ class CreateZimPageInNotebook(Action):
def requires_object(self):
return True
+
def object_types(self):
yield ZimNotebook
+
def object_source(self, for_item=None):
return ZimNotebooksSource()
+
class OpenZimPage(Action):
""" Open Zim page """
rank_adjust = 10
@@ -135,10 +144,11 @@ class CreateZimSubPage(Action):
def object_types(self):
yield TextLeaf
-
+
def object_source(self, for_item=None):
return TextSource()
+
def _read_zim_notebooks_old(zim_notebooks_file):
''' Yield (notebook name, notebook path) from zim config
@@ -161,19 +171,42 @@ def _get_default_notebook():
if not zim_notebooks_file:
pretty.print_error(__name__, "Zim notebooks.list not found")
return None
+ lines = None
with open(zim_notebooks_file, 'r') as notebooks_file:
- for line in notebooks_file.readlines():
- if line.strip() == "[NotebookList]":
- # new file format == pyzim
- return ''
- if line.strip() != '_default_': # when no default notebook
- notebook_name, notebook_path = line.strip().split('\t', 2)
- if notebook_name == '_default_':
- # _default_ is pointing at name of the default notebook
- return notebook_path.decode("UTF-8", "replace")
- else:
- # assume first notebook as default
- return notebook_name.decode("UTF-8", "replace")
+ lines = notebooks_file.readlines()
+ if not lines:
+ return ''
+ if lines[0].strip() == '[NotebookList]':
+ # new version
+ # first section looks like:
+ # [NotebookList]
+ # Default=~/doc/zim
+ # ~/doc/zim
+ # ~/tmp/test
+ for line in lines[1:]:
+ if line.startswith('Default='):
+ _dummy, name = line.split('=', 1)
+ name = name.strip()
+ if name:
+ pretty.print_debug(__name__, '_get_default_notebook:', name)
+ return name
+ return line
+ return ''
+ # old version
+ # format '<notebook name | _default_>\t<path>'
+ name = ''
+ for line in lines:
+ if '\t' in line:
+ notebook_name, notebook_path = line.strip().split('\t', 1)
+ if notebook_name == '_default_':
+ # _default_ is pointing at name of the default notebook
+ name = notebook_path.decode("UTF-8", "replace")
+ else:
+ # assume first notebook as default
+ name = notebook_name.decode("UTF-8", "replace")
+ break
+ pretty.print_debug(__name__, '_get_default_notebook (old):', name)
+ return name
def _read_zim_notebook_name(notebook_path):
@@ -186,32 +219,63 @@ def _read_zim_notebook_name(notebook_path):
return us_name
return os.path.basename(notebook_path)
+
def _read_zim_notebooks_new(zim_notebooks_file):
''' Yield (notebook name, notebook path) from zim config
+ NOTE: we can't use ConfigParser - zim config file is not parsable
+
+ Sample file:
+ [NotebookList]
+ Default=~/doc/zim
+ ~/doc/zim
+ ~/tmp/test
+
+ [Notebook]
+ uri=~/doc/zim
+ name=doc
+ interwiki=None
+ icon=
+
+ [Notebook]
+ ....
+
@notebook_name: Unicode name
@notebook_path: Filesystem byte string
'''
- default_url = None
+ notebooks = []
+ last_section = None
with open(zim_notebooks_file, 'r') as notebooks_file:
for line in notebooks_file:
+ line = line.strip()
if line.startswith("["):
+ if line == '[Notebook]':
+ notebooks.append(dict())
+ last_section = line
continue
- if line.startswith("Default="):
- _ignored, notebook_url = line.split("=", 1)
- notebook_url = notebook_url.strip()
- default_url = notebook_url
- elif line.strip() != default_url:
- notebook_url = line.strip()
- else:
+ if not line:
+ last_section = None
continue
- notebook_path = gio.File(notebook_url).get_path()
+ if last_section == '[Notebook]':
+ if '=' in line:
+ key, val = line.split('=', 1)
+ notebooks[-1][key] = val
+ for notebook in notebooks:
+ uri = notebook.get('uri')
+ if not uri:
+ continue
+ notebook_path = gio.File(os.path.expanduser(uri)).get_path()
+ notebook_name = notebook.get('name')
+ if not notebook_name:
+ # old version: name don't present in config
try:
notebook_name = _read_zim_notebook_name(notebook_path)
except IOError:
pass
- else:
- yield (notebook_name, notebook_path)
+ if not notebook_name:
+ notebook_name = notebook_path.split('/')[-1]
+ yield (notebook_name, notebook_path)
+
def _get_zim_notebooks():
''' Yield (notebook name, notebook path) from zim config
@@ -225,19 +289,22 @@ def _get_zim_notebooks():
pretty.print_error(__name__, "Zim notebooks.list not found")
return []
try:
+ config_first_line = None
with open(zim_notebooks_file, 'r') as notebooks_file:
- for line in notebooks_file.readlines():
- if line.strip() == "[NotebookList]":
- return _read_zim_notebooks_new(zim_notebooks_file)
- else:
- return _read_zim_notebooks_old(zim_notebooks_file)
+ config_first_line = notebooks_file.readline().strip()
+ if config_first_line == "[NotebookList]":
+ return _read_zim_notebooks_new(zim_notebooks_file)
+ else:
+ return _read_zim_notebooks_old(zim_notebooks_file)
except IOError, err:
pretty.print_error(__name__, err)
+
class ZimNotebook (Leaf):
def get_gicon(self):
return icons.get_gicon_for_file(self.object)
+
class ZimNotebooksSource (Source):
def __init__(self):
Source.__init__(self, _("Zim Notebooks"))
@@ -252,6 +319,7 @@ class ZimNotebooksSource (Source):
def provides(self):
yield ZimNotebook
+
class ZimPagesSource(AppLeafContentMixin, Source):
''' Index pages in all Zim notebooks '''
appleaf_content_id = "zim"
@@ -291,4 +359,3 @@ class ZimPagesSource(AppLeafContentMixin, Source):
def provides(self):
yield ZimPage
-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]