Zim

External python access to task list

Asked by Morten Nielsen on 2013-04-29

I would like to use my todo lists in zim for other purposes also.

Is there a way for me to get access to the task list from my own python program?
As I have read it, it is located in a Sqlite database, but it would be nice if I didn't have to build the SQL my self.

And thanks for a good program.
morten

Question information

Language:
English Edit question
Status:
Answered
For:
Zim Edit question
Assignee:
No assignee Edit question
Last query:
2013-05-01
Last reply:
2013-05-02

This question was reopened

Same question came up on the mailing list recently: https://lists.launchpad.net/zim-wiki/msg02385.html

Minimal script could look something like:

#############
import logging

logging.basicConfig(level=logging.INFO) # Use "DEBUG" for more output or "WARN" for less

from zim import NotebookInterface
from zim.fs import Dir
from zim.notebook import get_notebook

dir = Dir('/path/to/notebook')
notebook = get_notebook(dir)
app = NotebookInterface(notebook)
app.load_plugin('tasklist')
tasklist = app.get_plugin('tasklist')
# notebook.index.update() ## uncomment to enforce index in sync (takes some time)
for task in tasklist.list_tasks():
 print task
#############

( Do note however that I'm in the process of refactoring this part of the code - so intialization of these objects will change a bit starting release 0.61 or 0.62. )

Hope this helps,

Jaap

Morten Nielsen (mbn) said : #2

Good answer, it works nicly for me.

As a hint for others who want to use this - the values in each task can be accessed using the columns names
    # entries in row from the Sqlite table creation
    # id INTEGER PRIMARY KEY,
    # source INTEGER,
    # parent INTEGER,
    # haschildren BOOLEAN,
    # open BOOLEAN,
    # actionable BOOLEAN,
    # prio INTEGER,
    # due TEXT,
    # description TEXT
e.g. task['due'] holds the due date (as text)

Morten Nielsen (mbn) said : #3

Ok, a quick update.

The solution work for a "normal" terminal, but when I use it in a cron job, I get stuff like

INFO:zim.config:Environment variable $USER was not set
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
  warnings.warn(str(e), _gtk.Warning)
/usr/lib/python2.7/dist-packages/zim/gui/clipboard.py:400: GtkWarning: IA__gtk_clipboard_get_for_display: assertion `display != NULL' failed
  self.clipboard = gtk.Clipboard(selection=atom)

and some more failures on the line with
    app = NotebookInterface(notebook)

Any suggestions on how to use it without GUI elements?

Would need to check which module imports those ui elements and either
refactor those modules to avoid the import, or wrap the import in a
try .. except.

Regards,

Jaap

On Wed, May 1, 2013 at 10:36 AM, Morten Nielsen
<email address hidden> wrote:
> Question #227797 on Zim changed:
> https://answers.launchpad.net/zim/+question/227797
>
> Status: Solved => Open
>
> Morten Nielsen is still having a problem:
> Ok, a quick update.
>
> The solution work for a "normal" terminal, but when I use it in a cron
> job, I get stuff like
>
> INFO:zim.config:Environment variable $USER was not set
> /usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
> warnings.warn(str(e), _gtk.Warning)
> /usr/lib/python2.7/dist-packages/zim/gui/clipboard.py:400: GtkWarning: IA__gtk_clipboard_get_for_display: assertion `display != NULL' failed
> self.clipboard = gtk.Clipboard(selection=atom)
>
> and some more failures on the line with
> app = NotebookInterface(notebook)
>
> Any suggestions on how to use it without GUI elements?
>
> --
> You received this question notification because you are an answer
> contact for Zim.

Svenn (svenn-bjerkem) said : #5

I happened to pull in zim 0.60 before trying out the script.
First I had no success, but then I opened the notebook in zim and discovered that I needed to reindex.
After reindexing, I could use the boilerplate code.
Is it so that a bump in version number requires a reindex?
How could this be tested in the boilerplate code and performed if needed?

Yes, after updating to a new version you need to re-index. (Because
the SQL format may have changed etc. etc.)

You can do something like:

if not tasklist.db_initialized:
    notebook.index.flush()
notebook.index.update()

The flush + update will drop all SQL tables and rebuild -- will take
more time than regular update, which only checks for deltas in
filesystem timestamps.

Regards,

Jaap

On Wed, May 1, 2013 at 5:51 PM, Svenn
<email address hidden> wrote:
> Question #227797 on Zim changed:
> https://answers.launchpad.net/zim/+question/227797
>
> Svenn posted a new comment:
> I happened to pull in zim 0.60 before trying out the script.
> First I had no success, but then I opened the notebook in zim and discovered that I needed to reindex.
> After reindexing, I could use the boilerplate code.
> Is it so that a bump in version number requires a reindex?
> How could this be tested in the boilerplate code and performed if needed?
>
> --
> You received this question notification because you are an answer
> contact for Zim.

Morten Nielsen (mbn) said : #7

I see that my thought are running in circles....
2½ years ago I posted this questions - and now I am back at wanting this feature :-)

Could I request an updated minimal script?

Morten Nielsen (mbn) said : #8

Ok, I hacked something together :-)

I created a script that fetches the task data from the index sqlite database and creates a calendar file.
It is here:
https://github.com/moozer/zimToCal

As mentioned I access the sqlite3 database directly, a better solution is to ask zim for the task information.

Can you help with this problem?

Provide an answer of your own, or ask Morten Nielsen for more information if necessary.

To post a message you must log in.