Check if template directory exists before continuing with script
This commit is contained in:
parent
c53d4e50b7
commit
c16ef9f747
@ -26,6 +26,12 @@ from sys import platform
|
|||||||
# ----- Paths -----
|
# ----- Paths -----
|
||||||
DEFAULT_TEMPLATE_DIR = Path.home() / "Templates"
|
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==========
|
# ========== Functions==========
|
||||||
# ========== Main Script ==========
|
# ========== Main Script ==========
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -38,7 +44,6 @@ if __name__ == "__main__":
|
|||||||
"--template-dir",
|
"--template-dir",
|
||||||
dest="dir",
|
dest="dir",
|
||||||
type=str,
|
type=str,
|
||||||
default=DEFAULT_TEMPLATerror.E_DIR,
|
|
||||||
help="choose a template directory (default: ~/Templates)",
|
help="choose a template directory (default: ~/Templates)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@ -52,20 +57,30 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
files = search.find_files(directory=args.dir)
|
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:
|
try:
|
||||||
selected_file = fzf.select_file_with_fzf(files)
|
selected_file = fzf.select_file_with_fzf(files)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
exit(error.E_INTERRUPT)
|
exit(error.E_INTERRUPT)
|
||||||
|
except fzf.FZFError as f:
|
||||||
|
print(f)
|
||||||
|
exit(error.E_INTERRUPT)
|
||||||
|
|
||||||
dest_file = Path(args.dest)
|
dest_file = Path(args.dest)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dest_file.touch(mode=0o600, exist_ok=False)
|
dest_file.touch(mode=0o600, exist_ok=False)
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
|
# force_overwrite true: overwrite
|
||||||
|
# force_overwrite false: print error message
|
||||||
raise NotImplementedError("Implement if file already exists")
|
raise NotImplementedError("Implement if file already exists")
|
||||||
else:
|
else:
|
||||||
dest_file.write_bytes(selected_file.read_bytes())
|
dest_file.write_bytes(selected_file.read_bytes())
|
||||||
# force_overwrite true: overwrite
|
|
||||||
# force_overwrite false: print error message
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user