-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
174 lines (153 loc) · 5.25 KB
/
app.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
import re
import os
import traceback
from helper.tracker import Tracker
from helper.database import Database
from helper.utils import cls
'''UI for this app.'''
CURRENT_INDEX = ''
db = Database()
db.open()
def execute(command: str):
global CURRENT_INDEX
# INDEX AS
pattern = r'(index|init|add|import) (.+) as (.+)'
match = re.compile(pattern).fullmatch(command)
if match:
path = match.group(2)
name = match.group(3)
if not os.path.exists(path):
print('Invalid path.')
return
if db.is_added(path=path):
print('This path is already being tracked.')
return
if db.is_added(index_name=name):
print('This name is already used.')
return
print('Indexing...')
log = Tracker.gen_log(path)
db.add_log(name, path, log)
print('Indexed Successfully.')
return
# LIST
if command == 'list':
indexes = db.list_indexes()
for i, index in enumerate(indexes):
print(f'{i+1}. {index} - {db.get_path(index)}')
print(f'Found {len(indexes)} indexes.')
return
# LOAD
pattern = r'(load|use|check|checkout) (.+)'
match = re.compile(pattern).fullmatch(command)
if match:
name = match.group(2)
if name not in db.list_indexes():
print('invalid name.')
return
CURRENT_INDEX = name
return
# UNLOAD
if command == 'unload':
CURRENT_INDEX = ''
print('Unloaded successfully.')
return
# DIFF
pattern = r'(diff|changes)( added| renamed| moved| deleted| modified){0,1}'
match = re.compile(pattern).fullmatch(command)
if match:
if not CURRENT_INDEX:
print('Load an index first.')
return
print('Compairing...')
logs = db.list_logs(CURRENT_INDEX)
old_log = db.get_log(logs[-1])
new_log = Tracker.gen_log(db.get_path(CURRENT_INDEX))
changes = Tracker.compare(old_log, new_log)
if match.group(2):
ch_type = match.group(2).strip()
map = {'added': changes.ADDED, 'deleted': changes.DELETED,
'renamed': changes.RENAMED, 'moved': changes.MOVED,
'modified': changes.MODIFIED}
changes = changes.filter(map[ch_type])
changes.human_readable()
return
# OVERWRITE
if command == 'overwrite':
if not CURRENT_INDEX:
print('Load an index first.')
return
last_log = db.list_logs(CURRENT_INDEX)[-1]
print('Overwriting...')
log = Tracker.gen_log(db.get_path(CURRENT_INDEX))
db.overwrite_log(last_log, log)
print('Overwritten successfully.')
return
# REMOVE
pattern = r'(delete|del|remove|rem) (.+)'
match = re.compile(pattern).fullmatch(command)
if match:
index_name = match.group(2)
if index_name not in db.list_indexes():
print('Invalid index name.')
return
db.rem_index(index_name)
if CURRENT_INDEX == index_name:
CURRENT_INDEX = ''
print('removed successfully.')
return
# CLS
if command.lower() in ('cls', 'clear'):
cls()
return
# HELP
if command.lower() in ('help', '?', '/?'):
simple_help()
return
# SUDO HELP
if command.lower() in ('sudo help', 'sudo ?', 'sudo /?'):
advance_help()
return
print('Invalid command.')
def simple_help():
command_desc = {
'index [path] as [index_name]': 'Indexes specified folder and adds it to database.',
'list': 'Lists all added indexes.',
'remove [index_name]': 'Removes index from database.',
'load [index_name]': 'Loads index to memory.',
'unload': 'Unloads index from memory.',
'diff [added|renamed|moved|deleted|modified]': 'Displays changes occured since index was added.',
'overwrite': 'Overwrite current changes into database.',
'help': 'Displays simple help screen.',
'sudo help': 'Displays advance help screen.'
}
for command, desc in command_desc.items():
print(f'{command} --> {desc}')
print()
def advance_help():
command_desc = {
'(index|init|add|import) [path] as [index_name]': 'Indexes specified folder and adds it to database.',
'list': 'Lists all added indexes.',
'(remove|delete|del|rem) [index_name]': 'Removes index from database.',
'(load|use|check|checkout) [index_name]': 'Loads index to memory.',
'unload': 'Unloads index from memory.',
'(diff|changes) [added|renamed|moved|deleted|modified]': 'Displays changes occured since index was added.',
'overwrite': 'Overwrite current changes into database.',
'(help|?|/?)': 'Displays simple help screen.',
'sudo (help|?|/?)': 'Displays advance help screen.'
}
for command, desc in command_desc.items():
print(f'{command} --> {desc}')
print()
while True:
try:
command = input(f'{CURRENT_INDEX}>>> ')
if command.lower() in ('exit', 'quit'):
break
execute(command)
print()
except Exception:
db.close()
print('An error occurred!')
traceback.print_exc()
break