Last active
October 21, 2019 22:08
-
-
Save maqp/aeb33bd7c6bb7b8939d328ce46d8395a to your computer and use it in GitHub Desktop.
Upnote tool for QOwnnotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.7 | |
# -*- coding: utf-8 -*- | |
import shutil | |
import subprocess | |
import os | |
from typing import List, NoReturn, Tuple | |
debug = True | |
SYMLINK_PREFIX = '↑ ' | |
def get_path_from_user() -> Tuple[str, str, str]: | |
while True: | |
file_path = input("Give path to file: ") | |
if not os.path.isfile(file_path): | |
print(f"Error: No such file '{file_path}'.") | |
continue | |
path, file_name = os.path.split(file_path) | |
return path, file_name, file_path | |
def create_symlink(file_name: str, file_path: str) -> str: | |
symlink_file_name = SYMLINK_PREFIX + file_name | |
if os.path.isfile(symlink_file_name): | |
os.remove(symlink_file_name) | |
os.symlink(file_path, symlink_file_name) | |
return symlink_file_name | |
def get_list_of_subdirectories(path: str) -> List[str]: | |
return [data[0] for data in os.walk(path)][1:] | |
def copy_symlink_to_subdirs(symlink_file_name: str, path: str) -> None: | |
subdirectory_list = get_list_of_subdirectories(path) | |
for subdirectory in subdirectory_list: | |
try: | |
shutil.copy(symlink_file_name, subdirectory, follow_symlinks=False) | |
except shutil.SameFileError: | |
pass | |
os.remove(symlink_file_name) | |
def remove_symlink_notes(path: str, file_name: str, full_path: str) -> None: | |
os.chdir(path) | |
if input(f"Removing ↑-notes that point to '{full_path}' recursively from path '{path}'. Type YES to proceed: ") == 'YES': | |
subprocess.Popen(f"find -type l -name '*{SYMLINK_PREFIX}{file_name}*' -delete", shell=True).wait() | |
for r, d, f in os.walk(path): | |
for file in f: | |
if f"{SYMLINK_PREFIX}{file_name}" in file: | |
print(f"Error, found file {SYMLINK_PREFIX}{file_name} that was not deleted!\n" | |
f"This should not have happened! There is probably a bug in the code.") | |
exit(1) | |
else: | |
print(f"Successfully Removed all '{SYMLINK_PREFIX}{file_name}' files.") | |
def create_symlink_notes(path: str, file_name: str, file_path: str) -> None: | |
os.chdir(path) | |
symlink_file_name = create_symlink(file_name, file_path) | |
copy_symlink_to_subdirs(symlink_file_name, path) | |
print(f"Created ↑-notes for '{file_name}'.") | |
def main() -> NoReturn: | |
while True: | |
func = input("Select command for ↑-notes: 'add','remove','quit': ") | |
if "add".startswith(func): | |
create_symlink_notes(*get_path_from_user()) | |
elif "remove".startswith(func): | |
remove_symlink_notes(*get_path_from_user()) | |
elif "quit".startswith(func): | |
print("Exiting.") | |
exit(0) | |
else: | |
print("Invalid command.") | |
continue | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment