-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add shell hooks #190
base: develop
Are you sure you want to change the base?
Add shell hooks #190
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -42,6 +42,7 @@ Equally, the only thing flatnotes caches is the search index and that's incremen | |||
* Light/dark themes. | ||||
* Multiple authentication options (none, read-only, username/password, 2FA). | ||||
* Restful API. | ||||
* Hook on create/update/delete. | ||||
|
||||
See [the wiki](https://github.com/dullage/flatnotes/wiki) for more details. | ||||
|
||||
|
@@ -118,3 +119,32 @@ A special thanks to 2 fantastic open-source projects that make flatnotes possibl | |||
|
||||
* [Whoosh](https://whoosh.readthedocs.io/en/latest/intro.html) - A fast, pure Python search engine library. | ||||
* [TOAST UI Editor](https://ui.toast.com/tui-editor) - A GFM Markdown and WYSIWYG editor for the browser. | ||||
|
||||
## Contributing | ||||
|
||||
Requirements: | ||||
|
||||
- python3, node, npm, pipenv | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 thanks for doc'ing this. I was thinking about experimenting with this so this will help! |
||||
|
||||
### Local run | ||||
|
||||
|
||||
1. Install deps: | ||||
|
||||
pipenv install | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly dumb question; Should this include Line 50 in 5b5369b
From https://pipenv.pypa.io/en/latest/commands.html#install
|
||||
npm install | ||||
|
||||
3. Build UI | ||||
|
||||
npm run build | ||||
|
||||
4. Switch to virtual environment | ||||
|
||||
pipenv shell | ||||
mkdir notes # for local test, it's git-ignored | ||||
|
||||
5. Run local server | ||||
|
||||
FLATNOTES_PATH=notes FLATNOTES_AUTH_TYPE=none uvicorn --app-dir server main:app --port 8080 | ||||
|
||||
Dev instance will be available on http://127.0.0.1:8080 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import re | ||
import shutil | ||
import time | ||
import subprocess | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pedantic suggestion; recommend moving this to the line above to have pep-8 sorting. I know the existing code isn't strictly adhering to this. https://pypi.org/project/isort/ can help with this. |
||
from datetime import datetime | ||
from typing import List, Literal, Set, Tuple | ||
|
||
|
@@ -58,6 +59,7 @@ def create(self, data: NoteCreate) -> Note: | |
"""Create a new note.""" | ||
filepath = self._path_from_title(data.title) | ||
self._write_file(filepath, data.content) | ||
self._exec_hook("create", filepath) | ||
return Note( | ||
title=data.title, | ||
content=data.content, | ||
|
@@ -93,6 +95,8 @@ def update(self, title: str, data: NoteUpdate) -> Note: | |
content = data.new_content | ||
else: | ||
content = self._read_file(filepath) | ||
|
||
self._exec_hook("update", filepath) | ||
return Note( | ||
title=title, | ||
content=content, | ||
|
@@ -104,6 +108,7 @@ def delete(self, title: str) -> None: | |
is_valid_filename(title) | ||
filepath = self._path_from_title(title) | ||
os.remove(filepath) | ||
self._exec_hook("delete", filepath) | ||
|
||
def search( | ||
self, | ||
|
@@ -372,6 +377,22 @@ def _fieldnames_for_term(self, term: str) -> List[str]: | |
fields.append("tags") | ||
return fields | ||
|
||
def _exec_hook(self, hook_name: Literal["create", "update", "delete"], path: str): | ||
"""Execute shell script for provided hook. | ||
The hook script will be from FLATNOTES_HOOK_<name>, where name is a hook_name in upper case. | ||
If there is no such env, hook will be ignored. Workdir is the storage path. | ||
Changed path will be substituted from %s. | ||
""" | ||
hook_env_name = f"FLATNOTES_HOOK_{hook_name.strip().upper()}" | ||
script = os.getenv(hook_env_name) | ||
if not script: | ||
return | ||
subprocess.check_call( | ||
script.replace("%s", path), | ||
shell=True, | ||
cwd=self.storage_path, | ||
) | ||
|
||
@staticmethod | ||
def _get_matched_fields(matched_terms): | ||
"""Return a set of matched fields from a set of ('field', 'term') " | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend a new section to provide a demo like you did in the PR.
Alternative suggestion for git, sample. 100% Untested :-) I used this technique in some of my (shell) scripts I removed the
git add -A
to also handle deletes and possible renames (rename detection can be a little hit and miss)