-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
199 lines (152 loc) · 5.68 KB
/
utils.py
File metadata and controls
199 lines (152 loc) · 5.68 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=unspecified-encoding
import os
from shutil import copy, move
import subprocess
import readline
import nerd_icons
import yaml
from simple_term_menu import TerminalMenu
def rlinput(prompt, default_value=""):
readline.set_startup_hook(lambda: readline.insert_text(default_value))
user_input = input(f"{prompt}")
readline.set_startup_hook()
return user_input
def normalize_path(path):
if path.startswith("~"):
return os.path.expanduser(path)
if path.startswith("$"):
return os.path.expandvars(path)
return path
def templates_dict(files, icons=False, full_path=False):
menu_items = {}
for file in files:
if not full_path:
basename = os.path.basename(file)
original_basename = basename
if icons:
basename = f"{devicon_handler(original_basename)} {basename}"
original_basename = basename
# handle multiple files with the same name
count_duplicates = 1
while basename in menu_items:
basename = f"{original_basename} ({count_duplicates})"
count_duplicates += 1
menu_items[basename] = file
else:
if icons:
menu_items[f"{devicon_handler(os.path.basename(file))} {file}"] = file
else:
menu_items[file] = file
return menu_items
def find_templates(paths=[], use_xdg_path=False, sort_by="name", reverse=False):
if use_xdg_path:
try:
completed_process = subprocess.run(
["xdg-user-dir", "TEMPLATES"],
capture_output=True,
text=True,
check=True,
)
if completed_process.returncode == 0:
paths.insert(0, completed_process.stdout.strip() + "/")
except FileNotFoundError:
print("[NeFT] Command xdg-user-dir not found. Ignoring the option.")
# path normalizing
normalized_paths = [normalize_path(path) for path in paths]
templates = []
# finding files
for path in normalized_paths:
files = os.listdir(path)
templates.extend([os.path.join(path, file) for file in files])
# removing directories
templates = [path for path in templates if os.path.isfile(path)]
return sort_files(templates, sort_by, reverse)
def devicon_handler(file_name):
try:
extension = os.path.splitext(file_name)[-1][1:]
return nerd_icons.EXTENSION_ICONS[extension]
except KeyError:
return nerd_icons.Icons().FILE
def generate_menu(files, icons=False, full_path=False, multi_select=False):
# generates items
menu_items = {"None": "None"}
menu_items.update(templates_dict(files, icons, full_path))
def get_path(item):
if item == "None":
return "File path: "
return menu_items[item]
options = {
"menu_entries": list(menu_items.keys()),
"status_bar": get_path,
"multi_select": multi_select,
"multi_select_select_on_accept": False,
}
# returns the menu
return TerminalMenu(**options).show()
def load_config(paths):
for path in paths:
if path is not None and os.path.exists(normalize_path(path)):
with open(normalize_path(path), "r") as file:
config = yaml.safe_load(file)
return config
return {}
def sort_files(files, sort_by, reverse):
match sort_by:
case "name":
return sorted(
files, key=lambda x: os.path.basename(x).lower(), reverse=reverse
)
case "extension":
return sorted(
files, key=lambda x: os.path.splitext(x)[1].lower(), reverse=reverse
)
case _:
return files
# # # # # # # # # # # #
# # # SUBCOMMANDS # # #
# # # # # # # # # # # #
def add_template(file, template_dir):
template_dir = normalize_path(template_dir)
file_name, extension = os.path.splitext(os.path.basename(file))
# if file with that name already exists, add index in the name
if os.path.exists(os.path.join(template_dir, f"{file_name}{extension}")):
index = 1
while os.path.exists(
os.path.join(template_dir, f"{file_name} ({index}){extension}")
):
index += 1
new_file_name = f"{file_name} ({index}){extension}"
else:
new_file_name = f"{file_name}{extension}"
# copy the file w/ new name
copy(file, os.path.join(template_dir, new_file_name))
print(f"[NeFT] Added '{new_file_name}' to '{template_dir}'.")
def remove_template(files):
for file in files:
try:
os.remove(file)
print(f"[NeFT] Removed '{file}'.")
except Exception as e:
print(f"[NeFT] An error ocurred while removing '{file}':\n")
def rename_template(old_path, new_name):
path, old_name = os.path.split(old_path)
# Making sure path ends with a separator
path = os.path.join(path, "")
new_path = os.path.join(path, new_name)
# Making sure the file exists at the old path
if os.path.isfile(old_path):
# Making sure the new path doesn't exist already
if os.path.exists(new_path):
print(f"[NeFT] '{new_path}' already exists. Skipping rename.")
return
# Renaming the file
move(old_path, new_path)
print(f"[NeFT] Renamed '{old_name}' to '{new_name}'.")
else:
print("[NeFT] Couldn't find the template.")
def list_templates(files, icons=False, full_path=False):
menu_items = templates_dict(files, icons, full_path)
for item in menu_items:
print(item)