note_splitter.note module

Manages info about the user’s files.

class note_splitter.note.Note(path: str, folder_path: Optional[str] = None, name: Optional[str] = None)

Bases: object

Info about one of the user’s note files.

Parameters
  • path (str) – The absolute path to the file. The file must already exist.

  • folder_path (str, optional) – The absolute path to the folder that the file is in. If not provided, it will be retrieved from the path.

  • name (str, optional) – The name of the file, including the file extension. If not provided, it will be retrieved from the path.

title

The title of the note. This is the body of the first header, or the first line of the file if there is no header, or an empty string if the file is empty.

Type

str

name

The name of the file, including the file extension.

Type

str

ext

The file extension, starting with a period.

Type

str

path

The absolute path to the file.

Type

str

folder_path

The absolute path to the folder that the file is in.

Type

str

delete() bool | None

Moves the note file to the trash or recycle bin.

Returns

True if the note was deleted successfully, None if the note does not exist.

Return type

bool | None

move(new_folder_path: str, all_notes: list['Note'], note_types: list[str]) bool | None

Moves the note file to a new folder and updates internal links to them.

Parameters
  • new_folder_path (str) – The absolute path to the new folder.

  • all_notes (list[Note]) – A list of all the notes in the user’s notes folder.

  • note_types (list[str]) – A list of all the file extensions note files can have. Each file extension includes the period.

Returns

True if the note was moved successfully, None if the note does not exist, False otherwise.

Return type

bool | None

open() bool | None

Opens the note in the device’s default editor.

Returns

True if the note was opened successfully, None if the note does not exist.

Return type

bool | None

show() bool | None

Shows the note in the file browser.

Returns

True if the note was shown successfully, None if the note does not exist.

Return type

bool | None

Changes the path to a file in all notes’ links.

Use this before moving a file to a different location.

Parameters
  • current_path_to_change (str) – Absolute path to the file.

  • new_path (str) – Absolute path the file will have after being moved.

  • all_notes (list[Note]) – A list of all notes in the source folder.

note_splitter.note.__create_file_name(file_ext: str, file_id_format: str, file_name_format: str, file_contents: str, dt: datetime.datetime) str

Creates a name for a new file.

Parameters
  • file_ext (str) – The file extension, including the leading period.

  • file_id_format (str) – The format of the file ID.

  • file_name_format (str) – The format of the file name.

  • file_contents (str) – The contents of the file to be named.

  • dt (datetime) – The date and time to use for the file name if the file name format contains any date and/or time variables.

note_splitter.note.__get_variables(file_contents: str, dt: datetime.datetime) list[tuple[str, str]]

Gets the variable names and values for file name and ID formats.

Parameters
  • file_contents (str) – The contents of the file being named or IDed.

  • dt (datetime) – The datetime to use in the file name or ID if the format contains any date and/or time variables.

note_splitter.note._make_file_paths_absolute(note_path: str) None

Makes all file paths in a note’s file links absolute.

Assumes that all the file paths that should be made absolute are valid. Invalid paths are ignored.

Parameters

note_path (str) – Absolute path to the note.

note_splitter.note.create_file_id(file_id_format: str, file_contents: str, dt: Optional[datetime.datetime] = None) str

Creates an ID for a file.

Parameters
  • file_id_format (str) – The format of the file ID.

  • file_contents (str) – The contents of the file to be IDed.

  • dt (datetime, optional) – The datetime to use in the file name. If not provided, the current time will be used.

note_splitter.note.create_file_names(file_ext: str, file_id_format: str, file_name_format: str, files_contents: list[str]) list[str]

Creates names for new files.

If more than one file name is created and file_name_format contains a time variable, the time will be incremented for each file name.

Parameters
  • file_ext (str) – The file extension, including the leading period.

  • file_id_format (str) – The format of the file ID.

  • file_name_format (str) – The format of the file name.

  • files_contents (list[str]) – The contents of the files to be named.

note_splitter.note.create_notes(file_paths: list[str]) list[note_splitter.note.Note]

Creates a list of notes from a list of file paths.

Parameters

file_paths (list[str]) – The absolute file paths of the notes. Folders and files of types that are not in the list of note types will be ignored.

note_splitter.note.ensure_file_path_uniqueness(file_path: str) str

Makes sure a file’s path is unique.

If a file with the same name already exists, a .1 is appended to the file name unless that is already there, in which case it is changed to .2, etc. This function assumes the file name does not have any invalid characters.

Parameters

file_path (str) – The absolute path for the file, including the planned file name.

Returns

file_path – The absolute path for the file, including the unique file name.

Return type

str

note_splitter.note.get_by_title(notes: list[note_splitter.note.Note], title: str) note_splitter.note.Note

Gets a note by its title.

Parameters
  • notes (list[Note]) – A list of all notes in the source folder.

  • title (str) – The title of the note.

Returns

The note with the given title.

Return type

Note

note_splitter.note.get_file_paths(note_content: str, note_folder_path: str) list[tuple[str, str]]

Gets the original and formatted file paths in links in a note.

Only paths to files that exist are returned.

Parameters
  • note_content (str) – The note’s content.

  • note_folder_path (str) – The absolute path to the note’s folder.

Returns

A list of tuples of the original, unmodified file path in the note and its absolute version. All the paths are valid. (Broken file links and links to websites are ignored.)

Return type

list[tuple[str, str]]

note_splitter.note.get_title(file_contents: str) str

Gets the title of the file.

The title is the body of the first header, or the first line if there is no header, or a random string if the file is empty.

Parameters

file_contents (str) – The contents of the file to get the title from.

note_splitter.note.make_file_paths_absolute(note_content: str, note_path: str) str

Makes all file paths in a note’s file links absolute.

Assumes that all the file paths that should be made absolute are valid. Invalid paths are ignored.

Parameters
  • note_content (str) – The note’s content.

  • note_path (str) – The absolute path to the note.

Returns

note_content – The note’s content with all file paths made absolute.

Return type

str

note_splitter.note.move_files(paths_of_files_to_move: list[str], destination_path: str, all_notes: list[note_splitter.note.Note], note_types: list[str]) None

Moves files and updates all relevant references everywhere.

Updates paths to these files in any of the notes in the source folder chosen in settings, and updates any relative paths in these files if they are of a note type.

Parameters
  • paths_of_files_to_move (list[str]) – A list of absolute paths of files to be moved. These can be files of any type.

  • destination_path (str) – Absolute path to the destination folder.

  • all_notes (list[Note]) – All the notes in the source folder.

  • note_types (list[str]) – A list of all the file extensions note files can have. Each file extension includes the period.

note_splitter.note.show_message(text: str) None

Shows the user a message dialog and waits for the user to close it.

note_splitter.note.validate_file_name(file_name: str, max_length: int = 30) str

Validates a file name’s characters and length.

This function does NOT ensure that a file with the same name does not already exist. Invalid characters are replaced with hyphens. If the file name has a length greater than max_length, it is truncated. If the file name starts or ends with certain characters, they are removed.

Parameters
  • file_name (str) – The file name to validate.

  • max_length (int, optional) – The maximum length of the file name.

Returns

file_name – The validated file name.

Return type

str