Restructure project directory, python files are now in their own folder
This commit is contained in:
		
							
								
								
									
										86
									
								
								python/bin/cptemplate
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										86
									
								
								python/bin/cptemplate
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,86 @@
 | 
			
		||||
#!/usr/bin/python3
 | 
			
		||||
"""
 | 
			
		||||
cptemplate - copy a template file from a specified template directory
 | 
			
		||||
 | 
			
		||||
Dependencies
 | 
			
		||||
============
 | 
			
		||||
* fd
 | 
			
		||||
* fzf
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
# Module Imports
 | 
			
		||||
import argparse
 | 
			
		||||
 | 
			
		||||
import file_scripts.fzf as fzf
 | 
			
		||||
import file_scripts.search as search
 | 
			
		||||
import file_scripts.error as error
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from sys import platform
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========== Constants ==========
 | 
			
		||||
# ----- Paths -----
 | 
			
		||||
DEFAULT_TEMPLATE_DIR = Path.home() / "Templates"
 | 
			
		||||
 | 
			
		||||
# ----- Error Messages -----
 | 
			
		||||
TEMPLATE_DIR_DOESNT_EXIST = "Warning: template dir does not exist, exiting."
 | 
			
		||||
 | 
			
		||||
# ----- Exit Codes -----
 | 
			
		||||
E_TEMP_DIR_NON_EXIST = 1
 | 
			
		||||
 | 
			
		||||
# ========== Functions==========
 | 
			
		||||
# ========== Main Script ==========
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    if platform == "win32":
 | 
			
		||||
        exit(error.E_INTERRUPT)
 | 
			
		||||
 | 
			
		||||
    parser = argparse.ArgumentParser()
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-d",
 | 
			
		||||
        "--template-dir",
 | 
			
		||||
        dest="dir",
 | 
			
		||||
        type=str,
 | 
			
		||||
        help="choose a template directory (default: ~/Templates)",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-f",
 | 
			
		||||
        "--force",
 | 
			
		||||
        dest="force_overwrite",
 | 
			
		||||
        action="store_true",
 | 
			
		||||
        help="overwrite dest if it exists",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument("dest", type=str)
 | 
			
		||||
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
    template_dir = DEFAULT_TEMPLATE_DIR if args.dir is None else Path(args.dir)
 | 
			
		||||
 | 
			
		||||
    # Check if default template directory is non-existent
 | 
			
		||||
    if not template_dir.exists():
 | 
			
		||||
        print(TEMPLATE_DIR_DOESNT_EXIST)
 | 
			
		||||
        exit(E_TEMP_DIR_NON_EXIST)
 | 
			
		||||
 | 
			
		||||
    files = search.find_files(directory=template_dir)
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        selected_file = fzf.select_file_with_fzf(files)
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        exit(error.E_INTERRUPT)
 | 
			
		||||
    except fzf.FZFError as f:
 | 
			
		||||
        print(f)
 | 
			
		||||
        exit(f.exit_code)
 | 
			
		||||
 | 
			
		||||
    dest_file = Path(args.dest)
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        dest_file.touch(mode=0o600, exist_ok=False)
 | 
			
		||||
    except FileExistsError as e:
 | 
			
		||||
        if args.force_overwrite:
 | 
			
		||||
            dest_file.touch(mode=0o600, exist_ok=True)
 | 
			
		||||
            dest_file.write_bytes(selected_file.read_bytes())
 | 
			
		||||
        else:
 | 
			
		||||
            print(e)
 | 
			
		||||
            exit(error.E_FILE_EXISTS)
 | 
			
		||||
    else:
 | 
			
		||||
        dest_file.write_bytes(selected_file.read_bytes())
 | 
			
		||||
							
								
								
									
										107
									
								
								python/bin/fedit
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										107
									
								
								python/bin/fedit
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,107 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
"""
 | 
			
		||||
Fuzzy-find a file and edit it.
 | 
			
		||||
 | 
			
		||||
Dependencies
 | 
			
		||||
============
 | 
			
		||||
* fd
 | 
			
		||||
* fzf
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
import argparse
 | 
			
		||||
import subprocess
 | 
			
		||||
from sys import platform
 | 
			
		||||
 | 
			
		||||
import file_scripts.fzf as fzf
 | 
			
		||||
import file_scripts.editor as editor
 | 
			
		||||
import file_scripts.search as search
 | 
			
		||||
import file_scripts.error as error
 | 
			
		||||
 | 
			
		||||
# ========== Constants ==========
 | 
			
		||||
# ----- Paths -----
 | 
			
		||||
BOOT_DIR = "/boot"
 | 
			
		||||
ETC_DIR = "/etc"
 | 
			
		||||
 | 
			
		||||
# ========== Functions ==========
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========== Main Script ==========
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    # This script doesn't support Windows
 | 
			
		||||
    if platform == "win32":
 | 
			
		||||
        exit(error.E_INTERRUPT)
 | 
			
		||||
 | 
			
		||||
    parser = argparse.ArgumentParser()
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-b",
 | 
			
		||||
        "--boot",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=BOOT_DIR,
 | 
			
		||||
        dest="dir",
 | 
			
		||||
        help="edit a file in /boot",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-d", "--dir", dest="dir", type=str, help="edit a file in a given directory"
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-E",
 | 
			
		||||
        "--etc",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=ETC_DIR,
 | 
			
		||||
        dest="dir",
 | 
			
		||||
        help="edit a file in /etc",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-I",
 | 
			
		||||
        "--no-ignore",
 | 
			
		||||
        action="append_const",
 | 
			
		||||
        const=search.EXTRA_FIND_OPTS["no_ignore"],
 | 
			
		||||
        dest="extra_find_opts",
 | 
			
		||||
        help="do not respect .(git|fd)ignore files",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-i",
 | 
			
		||||
        "--no-ignore-vcs",
 | 
			
		||||
        action="append_const",
 | 
			
		||||
        const=search.EXTRA_FIND_OPTS["no_ignore_vcs"],
 | 
			
		||||
        dest="extra_find_opts",
 | 
			
		||||
        help="do not respect .gitignore files",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument("-e", "--editor", help="use a given editor")
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "patterns", type=str, nargs="*", help="patterns to pass to locate"
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
    user_opts = [] if args.extra_find_opts is None else args.extra_find_opts
 | 
			
		||||
    user_opts.extend(search.DEFAULT_FIND_OPTS)
 | 
			
		||||
 | 
			
		||||
    selected_editor = None
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        selected_editor = editor.select_editor(args.editor)
 | 
			
		||||
    except FileNotFoundError as e:
 | 
			
		||||
        print(e)
 | 
			
		||||
        exit(error.E_NOEDITORFOUND)
 | 
			
		||||
 | 
			
		||||
    # If patterns were passed, use locate
 | 
			
		||||
    # Otherwise check for -d and use fd
 | 
			
		||||
    files = (
 | 
			
		||||
        search.find_files(opts=user_opts, directory=args.dir)
 | 
			
		||||
        if not args.patterns
 | 
			
		||||
        else search.locate_files(args.patterns)
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        selected_file = fzf.select_file_with_fzf(files)
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        exit()
 | 
			
		||||
    except fzf.FZFError as e:
 | 
			
		||||
        exit(e.exit_code)
 | 
			
		||||
 | 
			
		||||
    if selected_file != "":
 | 
			
		||||
        cmd = editor.gen_editor_cmd(selected_editor, selected_file)
 | 
			
		||||
        subprocess.run(cmd)
 | 
			
		||||
    else:
 | 
			
		||||
        exit(error.E_NOFILESELECTED)
 | 
			
		||||
							
								
								
									
										179
									
								
								python/bin/quickdel
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										179
									
								
								python/bin/quickdel
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,179 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
"""
 | 
			
		||||
quickdel - delete any file matching a query
 | 
			
		||||
 | 
			
		||||
Dependencies
 | 
			
		||||
============
 | 
			
		||||
* fd
 | 
			
		||||
* python-termcolor
 | 
			
		||||
 | 
			
		||||
Command-Line Arguments
 | 
			
		||||
======================
 | 
			
		||||
* -d, --directories-only
 | 
			
		||||
* -D, --directory       TODO: implement this
 | 
			
		||||
* -e, --empty-only
 | 
			
		||||
* -E, --extension
 | 
			
		||||
* -f, --files-only
 | 
			
		||||
* -F, --force-directory-delete
 | 
			
		||||
* -i, --no-ignore
 | 
			
		||||
* -I, --no-ignore-vcs
 | 
			
		||||
* -l, --links-only
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
import argparse
 | 
			
		||||
import os
 | 
			
		||||
import os.path
 | 
			
		||||
import re
 | 
			
		||||
import shutil
 | 
			
		||||
import file_scripts.error as error
 | 
			
		||||
import file_scripts.search as search
 | 
			
		||||
 | 
			
		||||
from termcolor import colored
 | 
			
		||||
 | 
			
		||||
# ========== Constants ==========
 | 
			
		||||
fd_opts = ["--hidden"]
 | 
			
		||||
# Matches 'y' or 'yes' only, ignoring case
 | 
			
		||||
USER_RESPONSE_YES = r"^[Yy]{1}([Ee]{1}[Ss]{1})?$"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========== Functions ==========
 | 
			
		||||
def color_file(filename):
 | 
			
		||||
    """Return correct color code for filetype of filename.
 | 
			
		||||
 | 
			
		||||
    Example
 | 
			
		||||
    -------
 | 
			
		||||
    >>> color_file('Test File', 'red')
 | 
			
		||||
    '\x1b[31mTest String\x1b[0m'
 | 
			
		||||
 | 
			
		||||
    :param filename: file to determine color output for
 | 
			
		||||
    :type filename: str
 | 
			
		||||
    :return: filename with ANSII escape codes for color
 | 
			
		||||
    :rtype: str
 | 
			
		||||
    """
 | 
			
		||||
    if os.path.isdir(filename):
 | 
			
		||||
        return colored(filename, "blue")
 | 
			
		||||
    elif os.path.islink(filename):
 | 
			
		||||
        return colored(filename, "green")
 | 
			
		||||
    else:
 | 
			
		||||
        return filename
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# ========== Main Script ==========
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    parser = argparse.ArgumentParser()
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-d",
 | 
			
		||||
        "--directories-only",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=["--type", "directory"],
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="filter results to directories",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-e",
 | 
			
		||||
        "--empty-only",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=["--type", "empty"],
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="filter results to empty files and directories",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-E",
 | 
			
		||||
        "--extension",
 | 
			
		||||
        action="append",
 | 
			
		||||
        dest="extensions",
 | 
			
		||||
        help="file extension",
 | 
			
		||||
        metavar="ext",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-f",
 | 
			
		||||
        "--files-only",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=["--type", "file"],
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="filter results to files",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-F",
 | 
			
		||||
        "--force-directory-delete",
 | 
			
		||||
        action="store_true",
 | 
			
		||||
        help="do not ignore non-empty directories, delete anyways",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-I",
 | 
			
		||||
        "--no-ignore-vcs",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const="--no-ignore-vcs",
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="do not ignore .gitignore",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-i",
 | 
			
		||||
        "--no-ignore",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const="--no-ignore",
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="do not ignore .gitignore and .fdignore",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument(
 | 
			
		||||
        "-l",
 | 
			
		||||
        "--links-only",
 | 
			
		||||
        action="store_const",
 | 
			
		||||
        const=["--type", "symlink"],
 | 
			
		||||
        dest="fd_extra_opts",
 | 
			
		||||
        help="filter results to symlinks",
 | 
			
		||||
    )
 | 
			
		||||
    parser.add_argument("patterns", nargs="+", help="file matching patterns")
 | 
			
		||||
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
    if args.fd_extra_opts is not None:
 | 
			
		||||
        fd_opts.extend(args.fd_extra_opts)
 | 
			
		||||
    if args.extensions is not None:
 | 
			
		||||
        for ext in args.extensions:
 | 
			
		||||
            fd_opts.extend(["--extension", ext])
 | 
			
		||||
 | 
			
		||||
    files = set()
 | 
			
		||||
    for p in args.patterns:
 | 
			
		||||
        files.update(
 | 
			
		||||
            search.find_files(opts=fd_opts, capture_text=True, pattern=p).splitlines()
 | 
			
		||||
        )
 | 
			
		||||
    files = sorted(files)
 | 
			
		||||
 | 
			
		||||
    if files == []:
 | 
			
		||||
        print("No results found, exiting")
 | 
			
		||||
        exit(error.E_NO_RESULTS)
 | 
			
		||||
 | 
			
		||||
    # Pretty print all filenames
 | 
			
		||||
    for index, filename in enumerate([color_file(f) for f in files], 1):
 | 
			
		||||
        print(f"{index}. {filename}")
 | 
			
		||||
    # Padding line
 | 
			
		||||
    print()
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        user_response = input("Would you like to delete these files? [y/N]: ")
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        exit(error.E_INTERRUPT)
 | 
			
		||||
 | 
			
		||||
    if re.match(USER_RESPONSE_YES, user_response) is None:
 | 
			
		||||
        print("Operation cancelled")
 | 
			
		||||
        exit(error.E_USER_RESPONSE_NO)
 | 
			
		||||
 | 
			
		||||
    # Remove files first
 | 
			
		||||
    for f in [fi for fi in files if os.path.isfile(fi)]:
 | 
			
		||||
        os.remove(f)
 | 
			
		||||
 | 
			
		||||
    # Check -f, --force-directory-delete option
 | 
			
		||||
    # shutil.rmtree forcibly removes directories
 | 
			
		||||
    # os.rmdir removes directories only if they are empty
 | 
			
		||||
    rmdir_func = shutil.rmtree if args.force_directory_delete else os.rmdir
 | 
			
		||||
 | 
			
		||||
    for d in filter(os.path.isdir, files):
 | 
			
		||||
        try:
 | 
			
		||||
            rmdir_func(d)
 | 
			
		||||
        except OSError:
 | 
			
		||||
            print(
 | 
			
		||||
                f"{colored('Warning', 'yellow')}: {colored(d, 'blue')} is not empty, not deleting directory"
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    print(colored("\nDeletions complete", "green"))
 | 
			
		||||
		Reference in New Issue
	
	Block a user