Permalink
Browse files

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
  • Loading branch information...
1 parent 9f9f872 commit 3b9c018717f5dde811e8f22af98f01d19a42a608 Karol Będkowski committed Dec 2, 2011
Showing with 99 additions and 32 deletions.
  1. +99 −32 kupfer/plugin/zim.py
View
@@ -9,7 +9,7 @@
)
__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,19 +24,23 @@
__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,
},
)
'''
+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 @@ def activate(self, leaf):
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 @@ def item_types(self):
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 @@ def requires_object(self):
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 @@ def get_icon_name(self):
def provides(self):
yield ZimNotebook
+
class ZimPagesSource(AppLeafContentMixin, Source):
''' Index pages in all Zim notebooks '''
appleaf_content_id = "zim"
@@ -291,4 +359,3 @@ def get_icon_name(self):
def provides(self):
yield ZimPage
-

0 comments on commit 3b9c018

Please sign in to comment.