-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhere.py
More file actions
73 lines (59 loc) · 2.11 KB
/
here.py
File metadata and controls
73 lines (59 loc) · 2.11 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
#!/usr/bin/env python
import os, sys, subprocess
import json
from os.path import expanduser
HOME = expanduser("~")
class HTDataHandler(object):
def __init__(self, filepath = HOME+'/.ht_database'):
self.filepath = filepath
self.data_dict = dict()
if os.path.exists(self.filepath):
with open(self.filepath, 'r') as file:
self.data_dict = json.loads(file.read())
def addShortcutAlias(self, alias):
_, path = subprocess.getstatusoutput("pwd")
self.data_dict[alias] = path
self.update()
def removeShortcutAlias(self, alias):
if alias in self.data_dict:
self.data_dict.pop(alias)
self.update()
def update(self):
with open(self.filepath, 'w') as file:
file.write(json.dumps(self.data_dict))
def showAll(self):
for key in sorted(self.data_dict):
path = self.data_dict[key]
if key == '-':
print('there', ' => ', path)
else:
print('there-'+key, ' => ', path)
def generateSourceFile(self):
alias_source = "#!/bin/bash\n"
for key in sorted(self.data_dict):
path = self.data_dict[key]
if key == '-':
alias_source += "alias there='cd "+path+"; pwd'\n"
else:
alias_source += "alias there-"+key+"='cd "+path+"; pwd'\n"
with open(HOME+'/.there_source', 'w') as file:
file.write(alias_source)
if __name__ == "__main__":
if len(sys.argv) == 1:
data_io = HTDataHandler()
data_io.addShortcutAlias('-')
data_io.showAll()
data_io.generateSourceFile()
if len(sys.argv) == 2:
data_io = HTDataHandler()
if sys.argv[1] == '-l':
data_io.showAll()
else:
data_io.addShortcutAlias(sys.argv[1])
data_io.showAll()
data_io.generateSourceFile()
if len(sys.argv) == 3 and sys.argv[1] == '-D':
data_io = HTDataHandler()
data_io.removeShortcutAlias(sys.argv[2])
data_io.showAll()
data_io.generateSourceFile()