taskbridgeapp.notes.model.notefolder.NoteFolder

class taskbridgeapp.notes.model.notefolder.NoteFolder(local_folder=None, remote_folder=None, sync_direction=0)

Bases: object

Represents a folder containing notes. A link is established here between the local folder and the remote one. Also contains a list of notes in this folder.

Methods

assoc_local_remote

Associate local folders with remote folders.

assoc_remote_local

Associate remote folders with local folders.

create_linked_folders

Creates an association between local and remote folders.

delete_local_notes

Delete notes from local which were deleted remotely.

delete_remote_notes

Delete notes from remote which were deleted locally.

load_local_folders

Loads the list of local folders by calling an AppleScript script.

load_local_notes

Calls an AppleScript script to fetch the notes in the local folder.

load_remote_folders

Loads the list of remote folders by checking the filesystem.

load_remote_notes

Loads the Markdown notes from the remote notes folder.

persist_folders

Save the list of linked note folders to SQLite.

persist_notes

Stores a list of notes in SQLite.

reset_list

Reset the folder list to empty.

seed_folder_table

Creates the initial structure for the table storing folders in SQLite.

seed_note_table

Creates the initial structure for the table storing notes in SQLite.

sync_bidirectional_local_deletions

Sync folders that have been marked for bidirectional or local -> remote sync.

sync_folder_deletions

Synchronises deletions to folders.

sync_local_note_to_remote

Sync local notes to remote.

sync_local_to_remote

Sync all the local notes in this folder to remote.

sync_note_deletions

Synchronises deletions to notes.

sync_notes

Synchronises notes.

sync_remote_deletions

Sync folders that have been marked for local <- remote sync.

sync_remote_note_to_local

Sync remote notes to local.

sync_remote_to_local

Sync all remote notes in this folder to local.

Attributes

FOLDER_LIST

List of all found note folders

SYNC_BOTH

Synchronise changes from both folders.

SYNC_LOCAL_TO_REMOTE

Update the remote folder with the changes in the local one (One-way).

SYNC_NONE

Do not sync this folder

SYNC_REMOTE_TO_LOCAL

Update the local folder with the changes in the remote one (One-way).

FOLDER_LIST: List[NoteFolder] = []

List of all found note folders

SYNC_BOTH: int = 3

Synchronise changes from both folders.

SYNC_LOCAL_TO_REMOTE: int = 1

Update the remote folder with the changes in the local one (One-way).

SYNC_NONE: int = 0

Do not sync this folder

SYNC_REMOTE_TO_LOCAL: int = 2

Update the local folder with the changes in the remote one (One-way).

__init__(local_folder=None, remote_folder=None, sync_direction=0)

Create a new note folder.

Parameters:
  • local_folder (LocalNoteFolder | None) – the local folder containing the local notes.

  • remote_folder (RemoteNoteFolder | None) – the remote folder containing the remote notes.

  • sync_direction (int) – how to sync the folders.

sync_direction:

  • NoteFolder.NO_SYNC - these folders will not be synchronised.

  • NoteFolder.SYNC_LOCAL_TO_REMOTE - local notes will be pushed to remote.

  • NoteFolder.SYNC_REMOTE_TO_LOCAL - remote notes will be pulled to local.

  • NoteFolder.SYNC_BOTH - bidirectional sync based on the note’s modification date/time.

static assoc_local_remote(local_folders, remote_folders, remote_notes_path, associations)

Associate local folders with remote folders.

Parameters:
  • local_folders (List[LocalNoteFolder]) – the list of local note folders.

  • remote_folders (List[RemoteNoteFolder]) – the list of remote note folders.

  • remote_notes_path (Path) – path to the remote folders, i.e. where on the local filesystem the remote notes are stored.

  • associations (dict) – list of folder associations.

Return type:

None

static assoc_remote_local(local_folders, remote_folders, associations)

Associate remote folders with local folders.

Parameters:
  • local_folders (List[LocalNoteFolder]) – the list of local note folders.

  • remote_folders (List[RemoteNoteFolder]) – the list of remote note folders.

  • associations (dict) – list of folder associations.

Return type:

None

static create_linked_folders(local_folders, remote_folders, remote_notes_path, associations)

Creates an association between local and remote folders. Missing folders are created; for example, if the folder foo is present locally, and set to SYNC_LOCAL_TO_REMOTE or SYNC_BOTH this method will check whether the remote folder foo exists and, if not, creates it.

The list of folders is saved to an SQLite database.

Parameters:
  • local_folders (List[LocalNoteFolder]) – the list of local note folders.

  • remote_folders (List[RemoteNoteFolder]) – the list of remote note folders.

  • remote_notes_path (Path) – path to the remote folders, i.e. where on the local filesystem the remote notes are stored.

  • associations (dict) – list of folder associations.

The associations dictionary must contain the following keys:

  • local_to_remote - notes to push from local to remote as :py:class`List[str]`.

  • remote_to_local - notes to pull from remote to local as :py:class`List[str]`.

  • bi_directional - notes to synchronise in both folders as :py:class`List[str]`.

Any other folders found which are not listed above are not synchronised.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if the folders are successfully linked.

-data (str) - error message on failure, or success message.

static delete_local_notes(folder, result)

Delete notes from local which were deleted remotely.

Parameters:
  • folder (NoteFolder) – the folder data.

  • result (dict) – dictionary where results are appended.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if local notes are successfully deleted.

-data (str) - error message on failure, or success message.

static delete_remote_notes(folder, remote_folder, result)

Delete notes from remote which were deleted locally.

Parameters:
  • folder (NoteFolder) – the folder data.

  • remote_folder (Path) – the remote folder.

  • result (dict) – dictionary where results are appended.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if remote notes are successfully deleted.

-data (str) - error message on failure, or success message.

static load_local_folders()

Loads the list of local folders by calling an AppleScript script.

Return type:

tuple[bool, str] | tuple[bool, List[LocalNoteFolder]]

Returns:

-success (bool) - true if folders are successfully loaded.

-data (str | List[LocalNoteFolder]) - error message on failure, list of folders on success.

load_local_notes()

Calls an AppleScript script to fetch the notes in the local folder. The script saves each note with a .staged file name in a temporary folder. Each file is then read, parsed and added as a Note instance in the in local_notes.

Return type:

tuple[bool, str] | tuple[bool, int]

Returns:

-success (bool) - true if notes are successfully loaded.

-data (str | int) - error message on failure, or number of notes loaded on success.

static load_remote_folders(remote_notes_path)

Loads the list of remote folders by checking the filesystem.

Return type:

tuple[bool, List[RemoteNoteFolder]]

Returns:

-success (bool) - true if folders are successfully loaded.

-data (str | List[RemoteNoteFolder]) - error message on failure, list of folders on success.

load_remote_notes()

Loads the Markdown notes from the remote notes folder. Each note is then parsed and added as a Note instance in remote_notes.

Return type:

tuple[bool, str] | tuple[bool, int]

Returns:

-success (bool) - true if notes are successfully loaded.

-data (str | int) - error message on failure, or number of notes loaded on success.

static persist_folders()

Save the list of linked note folders to SQLite.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if folders are successfully saved.

-data (str) - error message on failure, or success message.

static persist_notes()

Stores a list of notes in SQLite. Note that the only ‘sensitive’ part of the note which is stored is the note’s name. The database is stored locally.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if table notes are successfully saved to the database.

-data (str) - error message on failure, or success message.

static reset_list()

Reset the folder list to empty.

static seed_folder_table()

Creates the initial structure for the table storing folders in SQLite.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if table is successfully seeded.

-data (str) - error message on failure, or success message.

static seed_note_table()

Creates the initial structure for the table storing notes in SQLite.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if table is successfully created.

-data (str) - error message on failure, or success message.

static sync_bidirectional_local_deletions(discovered_local)

Sync folders that have been marked for bidirectional or local -> remote sync.

Parameters:

discovered_local (List[LocalNoteFolder]) – list of currently discovered local note folders.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if folder deletions are successfully synchronised.

-data (str) - error message on failure, or success message.

static sync_folder_deletions(discovered_local, discovered_remote)

Synchronises deletions to folders.

The list of folders found during the last sync is loaded from SQLite and compared to the local and remote folders found now. Any folders in the database which are no longer present will then have their counterpart deleted. For example, if the local folder foo is deleted, the remote folder foo will be deleted during sync.

Note that folder deletions only apply in the sync direction. In the example above, foo will only be deleted if sync_direction is set to SYNC_LOCAL_TO_REMOTE or SYNC_BOTH.

Parameters:
  • discovered_local (List[LocalNoteFolder]) – List of currently discovered local note folders.

  • discovered_remote (List[RemoteNoteFolder]) – List of currently discovered remote note folders.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if folder deletions are successfully synchronised.

-data (str) - error message on failure, or success message.

sync_local_note_to_remote(local, remote, result)

Sync local notes to remote. This performs an update or an insert.

Parameters:
  • local (Note) – the local note.

  • remote (Note | None) – the remote note.

  • result (dict) – a dictionary where results of sync will be saved.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if notes are successfully synchronised.

-data (str) - error message on failure, or dictionary of changes.

sync_local_to_remote(result)

Sync all the local notes in this folder to remote.

Parameters:

result (dict) – dictionary where results will be saved.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if notes are successfully synchronised.

-data (str) - error message on failure, or success message.

static sync_note_deletions(remote_folder)

Synchronises deletions to notes.

The list of notes found during the last sync is loaded from SQLite and compared to the local and remote notes found now. Any notes in the database which are no longer present will then have their counterpart deleted. For example, if the local note foo is deleted, the remote note foo will be deleted during sync.

Note that note deletions only apply in the sync direction. In the example above, foo will only be deleted if sync_direction is set to SYNC_LOCAL_TO_REMOTE or SYNC_BOTH.

On success, this method returns a dictionary with changes, containing the following keys:

  • remote_deleted - name of deleted remote notes as List[str].

  • local_deleted - name of deleted local notes as List[str].

  • remote_not_found - name of notes marked for remote deletion which were not found as List[str].

  • local_not_found - name of notes marked for local deletion which were not found as List[str].

A note not being found is not considered an error, as the user may have deleted the note manually prior to the sync running.

Parameters:

remote_folder (Path) – Path to the folder on the filesystem containing the remote notes.

Return type:

tuple[bool, str] | tuple[bool, dict]

Returns:

-success (bool) - true if note deletions are successfully synchronised.

-data (str | :py:class`dict`) - error message on failure, or result as above on success.

sync_notes()

Synchronises notes. This method checks the sync_direction of this folder to determine what to do. On success, it returns a dictionary with the following keys:

  • remote_added - name of notes added to the remote folder as List[str].

  • remote_updated - name of notes updated in the remote folder as List[str].

  • local_added - name of notes added to the local folder as List[str].

  • local_updated - name of notes updated in the local folder as List[str].

Any of the above may be empty if no such changes were made.

Return type:

tuple[bool, dict] | tuple[bool, str]

Returns:

-success (bool) - true if notes are successfully synchronised.

-data (str | dict) - error message on failure, or dict with results as above.

static sync_remote_deletions(discovered_remote)

Sync folders that have been marked for local <- remote sync.

Parameters:

discovered_remote (List[RemoteNoteFolder]) – list of currently discovered remote note folders.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if folder deletions are successfully synchronised.

-data (str) - error message on failure, or success message.

sync_remote_note_to_local(local, remote, result)

Sync remote notes to local. This performs an update or an insert. Since the Apple Notes modified date is read only, we have to update the modified date of the remote note as well, otherwise the remote note will be overwritten on the next sync.

Parameters:
  • local (Note) – the local note.

  • remote (Note) – the remote note.

  • result (dict) – a dictionary where results of sync will be saved.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if notes are successfully synchronised.

-data (str) - error message on failure, or success message.

sync_remote_to_local(result)

Sync all remote notes in this folder to local.

Parameters:

result – dictionary where results will be saved.

Return type:

tuple[bool, str]

Returns:

-success (bool) - true if notes are successfully synchronised.

-data (str) - error message on failure, or success message.