Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do i load a file with custom TODO keys? #18

Closed
schlagenhauf opened this issue Apr 15, 2020 · 3 comments · Fixed by #19
Closed

How do i load a file with custom TODO keys? #18

schlagenhauf opened this issue Apr 15, 2020 · 3 comments · Fixed by #19

Comments

@schlagenhauf
Copy link

schlagenhauf commented Apr 15, 2020

Hi,
i was wondering how to correctly parse an org file that contains custom TODO keys (e.g. ['TODO', 'WAITING', 'DONE', 'CANCELLED']). It seems supported as indicated by the add_todo_keys() method but looking into the file loading methods, those keys have to be set before actually loading the content and the API does not support that.

Am i missing something?

Thanks!

@karlicoss
Copy link
Owner

Hi! Not familiar with this bit of API (I'm not the one who implemented it), but I did a quick check.

Looks like it supports per-file todo keywords.

import orgparse
data = """#+TODO: TODO WAITING | DONE CANCELED 
* WAITING do it later... 
"""
od = orgparse.loads(data).root.children[0]

>>> od.todo                                                                                                                                                                                                                                        
 'WAITING'

>>> od.heading                                                                                                                                                                                                                                     
 'do it later...'

However, you probably have these set in your emacs config and don't want to specify in each file.
Looks like it's indeed hidden in the OrgEnv class and the user has no control about it. I guess need to figure out what was the intended way of using this class.. Looks like it should be an env keyword argument that's passed all the way up from loads/load/loadi functions and defaults to OrgEnv().

But, in the meantime: there is a hacky, but working way to solve it, so you don't have to wait for the fix. You can override parse_lines function:

import orgparse
def parse_lines_patched(lines, filename): 
    from orgparse.node import OrgEnv, lines_to_chunks # added this line!
    env = OrgEnv(filename=filename) 
    env.add_todo_keys(['TODO', 'WAITING'], ['DONE', 'CANCELED'])  # added this line!
    # parse into node of list (environment will be parsed) 
    nodelist = list(env.from_chunks(lines_to_chunks(lines))) 
    # parse headings (level, TODO, TAGs, and heading) 
    nodelist[0]._index = 0 
    for (i, node) in enumerate(nodelist[1:], 1):   # nodes except root node 
        node._index = i 
        node._parse_pre() 
    env._nodes = nodelist 
    return nodelist[0]  # root
orgparse.parse_lines = parse_lines_patched # overrid

After that it should work!

> orgparse.loads('* WAITING whatever').root.children[0].todo
'WAITING'

Let me know if it helps!

@schlagenhauf
Copy link
Author

schlagenhauf commented Apr 16, 2020

Awesome, your workaround does the trick. Thanks!

I'd try my hands at a PR for that, anything to look out for?

@karlicoss
Copy link
Owner

@schlagenhauf glad I could help!

PR would be cool! It's all CI tested, so should be straightforward without fear to break anything. If you add also add tests for custom todo keywords and add_todo_keys logic here, I'd be grateful :) You can run tests locally via tox, although running pytest directly should work too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants