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

Adding hook feature #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion bin/terminal_velocity
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import os
import logging
import logging.handlers
import sys
import json

import terminal_velocity.urwid_ui as urwid_ui

Expand Down Expand Up @@ -60,6 +61,11 @@ the default default will be used"""
help="the filename extensions to recognize in the notes dir, a "
"comma-separated list (default: %(default)s)")

parser.add_argument("--hooks", dest="hooks", action="store",
default=defaults.get("hooks", "{}"),
help="the scripts which get executed when events occure such has \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos:

occure -> occur
such has -> such as

Also I don't think the \ is necessary because Python has implied line continuation inside parentheses, so you can do:

parser.add_argument(...
    help="the scripts which get executed when events occur such as "
         "saving notes (default: %(default)s)")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an error on python 2.7.9 when trying:
print("test test")

... Sorry can't get markdown to work but both test are on different line

saving notes (default: %(default)s)")

parser.add_argument("-d", "--debug", dest="debug", action="store_true",
default=defaults.get("debug", False),
help="debug logging on or off (default: off)")
Expand All @@ -78,6 +84,9 @@ the default default will be used"""

args = parser.parse_args()


args.hooks = json.loads(args.hooks)

extensions = []
for extension in args.extensions.split(","):
extensions.append(extension.strip())
Expand Down Expand Up @@ -107,7 +116,8 @@ the default default will be used"""

try:
urwid_ui.launch(notes_dir=args.notes_dir, editor=args.editor,
extension=args.extension, extensions=args.extensions)
extension=args.extension, extensions=args.extensions,
hooks=args.hooks)
except KeyboardInterrupt:
# Silence KeyboardInterrupt tracebacks on ctrl-c.
sys.exit()
Expand Down
26 changes: 20 additions & 6 deletions terminal_velocity/urwid_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import subprocess
import logging
import os
logger = logging.getLogger(__name__)

import urwid
Expand Down Expand Up @@ -238,8 +239,7 @@ def mouse_event(self, size, event, button, col, row, focus):
class MainFrame(urwid.Frame):
"""The topmost urwid widget."""

def __init__(self, notes_dir, editor, extension, extensions):

def __init__(self, notes_dir, editor, extension, extensions, hooks):
self.editor = editor
self.notebook = notebook.PlainTextNoteBook(notes_dir, extension,
extensions)
Expand All @@ -253,6 +253,8 @@ def __init__(self, notes_dir, editor, extension, extensions):

self._selected_note = None

self.hooks = hooks

self.search_box = AutocompleteWidget(wrap="clip")
self.list_box = NoteFilterListBox(on_changed=self.on_list_box_changed)

Expand Down Expand Up @@ -306,6 +308,15 @@ def quit(self):

raise urwid.ExitMainLoop()

def execute_hook(self, hook, param):
"""Execute the defined hook

For the time being only the post hook has been tested

"""
if hook in self.hooks != "" and os.path.isfile(self.hooks[hook]):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd. hook in self.hooks is a boolean expression, it will always equal True or False, never "", so if hook in self.hooks != "" will always be True.

I think this might do what you wanted: if self.hooks.get(hook) and ....

subprocess.call([self.hooks[hook], param])

def keypress(self, size, key):

maxcol, maxrow = size
Expand All @@ -325,17 +336,20 @@ def keypress(self, size, key):
elif key in ["enter"]:
if self.selected_note:
system(self.editor, [self.selected_note.abspath], self.loop)
self.execute_hook("post", self.selected_note.abspath)
else:
if self.search_box.edit_text:
try:
note = self.notebook.add_new(self.search_box.edit_text)
system(self.editor, [note.abspath], self.loop)
self.execute_hook("post", note.abspath)
except notebook.NoteAlreadyExistsError:
# Try to open the existing note instead.
system(self.editor,
[self.search_box.edit_text +
system(self.editor, [self.search_box.edit_text +
self.notebook.extension],
self.loop)
self.execute_hook("post", self.search_box.edit_text +
self.notebook.extension)
except notebook.InvalidNoteTitleError:
# TODO: Display error message to user.
pass
Expand Down Expand Up @@ -435,10 +449,10 @@ def on_list_box_changed(self, note):
self.selected_note = note


def launch(notes_dir, editor, extension, extensions):
def launch(notes_dir, editor, extension, extensions, hooks):
"""Launch the user interface."""

frame = MainFrame(notes_dir, editor, extension, extensions)
frame = MainFrame(notes_dir, editor, extension, extensions, hooks)
loop = urwid.MainLoop(frame, palette)
frame.loop = loop
loop.run()