-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·177 lines (152 loc) · 6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!./venv/bin/python
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=redefined-outer-name
# NeFT - New From Template
# Create files from pre-defined templates.
import argparse
from shutil import copy
import os
import sys
from argparse_formatter import ParagraphFormatter, FlexiFormatter
from utils import (
rlinput,
find_templates,
generate_menu,
load_config,
add_template,
remove_template,
rename_template,
list_templates,
)
VERSION = "1.1.0"
def main(args):
# FINDING TEMPLATES
paths = config.get("paths", ["~/Templates"])
if args.get("version", False):
sys.exit(VERSION)
try:
template_files = find_templates(
paths=paths,
use_xdg_path=args.get("use_xdg_path", False),
sort_by=args.get("sort", "name"),
reverse=args.get("reverse", False),
)
except:
sys.exit(
"[NeFT] Couldn't find any template directories. Either create ~/Templates or define paths in neft.yaml."
)
# SUBCOMMANDS
if args.get("subcommand_name"):
try:
subcmd = args.get("subcommand_name")
match subcmd:
case "add":
add_template(args.get("PATH"), paths[0])
case "remove":
remove_selected = generate_menu(
template_files,
icons=args.get("icons", False),
full_path=args.get("full_path", False),
multi_select=True,
)
files = []
for file in remove_selected:
files.append(template_files[file - 1])
remove_template(files)
case "rename":
rename_selected = generate_menu(
template_files,
icons=args.get("icons", False),
full_path=args.get("full_path", False),
)
if rename_selected is None or rename_selected == 0:
sys.exit()
else:
selected = rename_selected - 1
new_name = rlinput(
f"[NeFT] Rename '{os.path.basename(template_files[selected])}' to: ",
os.path.basename(template_files[selected]),
)
# print(new_name)
rename_template(template_files[selected], new_name)
case "list":
list_templates(
template_files,
icons=args.get("icons", False),
full_path=args.get("full_path", False),
)
sys.exit()
except (AttributeError, KeyError, FileNotFoundError):
sys.exit("[NeFT] Invalid input.")
# INPUT HANDLING
menu_entry_index = generate_menu(
template_files,
icons=args.get("icons", False),
full_path=args.get("full_path", False),
)
if menu_entry_index is None or menu_entry_index == 0:
sys.exit()
else:
selected = menu_entry_index - 1
# FILE CREATION
new_name = ""
if args.get("output"):
new_name = args.get("output")
else:
new_name = rlinput(
"[NeFT] Create file: ", os.path.basename(template_files[selected])
)
if len(new_name.strip()) > 0:
# copying file
copied_file = os.path.join(os.getcwd(), new_name)
copy(str(os.path.join(template_files[selected])), copied_file)
else:
print("[NeFT] Invalid file name. Aborting.")
return args.get("loop", False)
if __name__ == "__main__":
# fmt: off
parser = argparse.ArgumentParser(
prog="neft",
usage="%(prog)s [OPTION]... [SUBCOMMAND]... [FILE]...",
description="NeFT - New From Template - Create files from pre-defined templates.",
)
parser.add_argument("-v", "--version", action="store_true", help="print NeFT's version and exit")
parser.add_argument("-i", "--icons", action="store_true", help="draw icons in the menu")
parser.add_argument("-l", "--loop", action="store_true", help="enable loop mode")
parser.add_argument("-f", "--full-path", action="store_true", help="draw the whole file path")
parser.add_argument("-xdg", "--use-xdg-path", action="store_true", help="include xdg-user-dir templates path",)
parser.add_argument("-c", "--config", metavar="", type=str, help="configuration file path")
parser.add_argument("-s", "--sort", metavar="", type=str, default="name", help="sort files by name or extension")
parser.add_argument("-r", "--reverse", action="store_true", help="reverse the sorting order")
parser.add_argument("-o", "--output", metavar="", type=str, help="output path")
subparsers = parser.add_subparsers(title="subcommands", dest="subcommand_name")
subparsers.add_parser("list", help="list all templates")
subparsers.add_parser("add", help="add file to templates").add_argument("PATH", help="path to the file to be added")
subparsers.add_parser("remove", help="remove a template")
subparsers.add_parser("rename", help="rename template")
# fmt: on
# add more arguments as needed
args = parser.parse_args()
# config overwriting
config_path = [
args.config,
"$XDG_CONFIG_HOME/neft/neft.yaml",
"$XDG_CONFIG_HOME/neft.yaml",
"~/.config/neft/neft.yaml",
"~/.config/neft.yaml",
"~/.neft.yaml",
]
# load config
config = load_config(config_path)
if config:
for key, value in config.items():
# propritize these passed options over the config one
skipable_args = ["sort", "reverse", "use_xdg_path"]
if key in skipable_args:
continue
# applying passed options
if hasattr(args, key) and value:
setattr(args, key, value)
while main(vars(args)):
pass