-
Notifications
You must be signed in to change notification settings - Fork 0
/
vent
245 lines (205 loc) · 7.51 KB
/
vent
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
import json, sys, os
from pprint import pprint
from pathlib import Path
import argparse
local_file = 'vent.json'
global_file = str(Path.home()) + '/.ventures_profile'
commands = "commands"
projects = "ventures"
init_local_file = {
commands:{}
}
init_global_file = {
projects:{}
}
def _get_local_directory():
length = len(str(Path.cwd().parent)) - len(str(Path.cwd())) + 1
return str(Path.cwd())[length:]
def open_local(path=Path.cwd()):
try:
with open(path / local_file, 'r') as f:
data = json.load(f)
return data
except:
return None
def save_local(contents):
with open(local_file, 'w+') as f:
data = f.write(json.dumps(contents))
return data
def open_global():
try:
with open(global_file, 'r') as f:
data = json.load(f)
return data
except:
return None
def save_global(contents):
with open(global_file, 'w+') as f:
data = f.write(json.dumps(contents))
return data
def go(args):
global_check = open_global()
if global_check == None:
save_global(init_global_file)
global_check = init_global_file
if args.partial:
possibilities = []
for project in global_check[projects]:
if args.venture in project.lower():
possibilities.append(project)
if len(possibilities) == 1:
name = possibilities[0]
print(global_check[projects][name])
return
elif len(possibilities) > 1:
sys.stderr.write(f"Error: Found more than one match for '{args.venture}'\n")
# Limit to 5 matches
for name in possibilities[:5]:
sys.stderr.write(f" {name}\n")
else:
sys.stderr.write(f"Error: Found no matches for '{args.venture}'\n")
else:
venture = args.venture
if venture not in global_check[projects]:
sys.stderr.write(f"Error: venture '{venture}' not found\n")
else:
print(global_check[projects][venture])
return
# Do not change current directory if there is an issue
print(".")
def init(args):
global_check = open_global()
if global_check == None:
save_global(init_global_file)
global_check = init_global_file
name = args.vent_name
if name in global_check[projects]:
sys.stderr.write("Error: cannot init a new venture when an existing venture is already named: " + name + "\n")
return
local_check = open_local()
if local_check != None:
sys.stderr.write("Error: cannot init a new venture on a already created project\n")
return
global_check[projects][name] = str(Path.cwd())
init_local_file["name"] = name
save_global(global_check)
save_local(init_local_file)
def source(args):
global_check = open_global()
if global_check == None:
save_global(init_global_file)
global_check = init_global_file
path = Path(args.vent_path)
file = open_local(path)
name = file["name"]
if name in global_check[projects]:
sys.stderr.write("Error: cannot init a new venture when an existing venture is already named: " + name + "\n")
return
global_check[projects][name] = str(Path.cwd())
save_global(global_check)
def add(args):
file = open_local()
if file == None:
sys.stderr.write("Error: cannot add a new command without initing a venture\n")
return
file[commands][args.command_name.lower()] = args.value
save_local(file)
def delete(args):
file = open_local()
if file == None:
sys.stderr.write("Error: cannot delete a command without initing a venture\n")
return
if args.command_name.lower() in file[commands]:
del file[commands][args.command_name.lower()]
save_local(file)
print("Successfully deleted " + args.command_name)
else:
sys.stderr.write("Error: cannot find " + args.command_name + " in list of local commands\n")
def find(args):
file = open_global()
if file == None:
sys.stderr.write("Error: must have created a venture first\n")
return
key = args.query
key = key.lower()
max_length = 12
possibilities = []
for project in file[projects]:
if key in project.lower():
possibilities.append(project)
if len(project) > max_length + 2:
max_length = len(project) + 2
print(("%" + str(max_length) + "s")%"Venture Name")
print("-"*max_length)
for project in possibilities:
print(("%" + str(max_length) + "s")%(project))
def list(args):
file = open_local()
if file == None:
sys.stderr.write("Error: cannot list commands without initing a venture\n")
return
max_name = 8
for name,_ in file[commands].items():
if len(name) > max_name + 2:
max_name = len(name) + 2
print(("%" + str(max_name) + "s %s")%("Name", "Command"))
print("-" * (max_name + 9))
for name, comd in file[commands].items():
print(("%" + str(max_name) + "s %-s")%(name, comd))
def run(args):
file = open_local()
if file == None:
sys.stderr.write("Error: cannot run a command without initing a venture\n")
return
if args[0].lower() in file[commands]:
os.system(file[commands][args.command_name.lower()])
else:
sys.stderr.write("Error: command '" + args.command_name.lower() + "' is not defined\n")
return
# commands = ["go", "g", "init", "i", "add", "list", "ls",
# "delete", "del", "run", "r", "find", "f"]
def parse_args(args):
parser = argparse.ArgumentParser(description='CLI for shortcuts to commands, bookmarking and navigating projects')
parser.add_argument('--version', action='version', version='%(prog)s 1.1.1')
subparsers = parser.add_subparsers(help='sub-command help', dest="cmd")
subparsers.required = True
parser_go = subparsers.add_parser("go", help='go help', description="Prints the path of the venture")
parser_go.add_argument("venture", help="The venture you wish to navigate to")
parser_go.add_argument('--partial', action="store_true", help="Signifies to search based on a portion of the venture name")
parser_go.set_defaults(func=go)
parser_init = subparsers.add_parser("init", help='init help', description="Initializes a new venture")
parser_init.add_argument("vent_name",
nargs='?',
help="The name of the new venture, defaults to current directory",
default=_get_local_directory()
)
parser_init.set_defaults(func=init)
parser_source = subparsers.add_parser("source", help='source help', description="Sources a new venture from an existing vent.json file")
parser_source.add_argument("vent_path",
nargs='?',
help="The path to the new venture, defaults to current directory",
default=str(Path.cwd())
)
parser_source.set_defaults(func=source)
parser_add = subparsers.add_parser("add", help='add help', description="Add a new command to the current venture")
parser_add.add_argument("command_name", help="The name of the new command for the current venture")
parser_add.add_argument("value", help="The value of the new command")
parser_add.set_defaults(func=add)
parser_list = subparsers.add_parser("list", help='list help', description="List commands for the current venture")
parser_list.set_defaults(func=list)
parser_delete = subparsers.add_parser("delete", help='delete help', description="Deletes a command from the current venture")
parser_delete.add_argument("command_name", help="The name of the command to be deleted")
parser_delete.set_defaults(func=delete)
parser_run = subparsers.add_parser("run", help='run help', description="Runs a command from the current venture")
parser_run.add_argument("command_name", help="The name of the command to be run")
parser_run.set_defaults(func=run)
parser_find = subparsers.add_parser("find", help='find help', description="Finds a venture based on an optional query")
parser_find.add_argument("query", nargs='?', default="", help="The query to match agains the names of ventures")
parser_find.set_defaults(func=find)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
#args = [i.lower() for i in sys.argv]
#if len(sys.argv) > 1:
parse_args(sys.argv)