-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkfilelist.py
More file actions
executable file
·171 lines (121 loc) · 3.52 KB
/
mkfilelist.py
File metadata and controls
executable file
·171 lines (121 loc) · 3.52 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
#!/usr/bin/python3
import os.path
# Defines the character which separates our fields
sepVar = ","
# Change this if you want to convert a list from a old seperator to a new one.
# Defines what separator is used for loading the file.
retroSepVar = ","
# The default headers for a new file
defaultHeaders = ["FileName", "Category", "Link Path", "Prefix"]
# Helper functions
def readWholeFile():
tempTable = []
line = fileobj.readline()
while line != "":
splitLine = line.split(retroSepVar)
# Remove trailing newline on last element
splitLine[-1] = splitLine[-1][:-1]
tempTable.append(splitLine)
line = fileobj.readline()
return tempTable
# Operation functions
def headersFunc(uInput):
output = ""
for header in splitHeaders:
output += header + " | "
# Python string comprehension is weird...
output = output[:-3]
print("Headers: " + output)
return
def add(uInput):
entry = []
for header in splitHeaders:
try:
entry.append(input(header + ": "))
except (EOFError, KeyboardInterrupt):
exit()
table.append(entry)
view()
return
def view(uInput=""):
if len(uInput) == 2 and uInput[1] == "raw":
print(splitHeaders)
for entry in table:
print(entry)
return
print()
#Dirty Hack (tm). I modified the tabulate source to use the arguments as specified by that symbol table
exec(open(".tabulate.py").read(), {"mArg1": table, "mArg2": splitHeaders, "mArg3": "orgtbl"})
print()
return
def save(uInput):
fileobj.seek(0)
fileobj.truncate()
fileobj.write(headers + "\n")
for entry in table:
fileobj.write(sepVar.join(entry) + "\n")
print("Saved table to file!")
return
def remove(uInput):
if len(uInput) < 2:
print("Wrong number of args. Usage: remove <file_name>")
for entry in table:
# Removal argument matches first field, aka file name
if entry[0] == uInput[1]:
table.remove(entry)
view()
return
print("No matching file found for: " + uInput[1])
return
def new(uInput):
global table, headers, splitHeaders
table = []
# Write the basic headers to the file
headers = sepVar.join(defaultHeaders)
fileobj.write(headers + "\n")
splitHeaders = headers.split(sepVar)
view()
return
def otherwise(uInput):
print("Command not found: " + " ".join(uInput))
return
# Actual code
fileName = "filelist"
if os.path.isfile("./" + fileName):
fileobj = open(fileName, "r+")
else:
fileobj = open(fileName, "w+")
headers = fileobj.readline()
# Remove trailing newline
headers = headers[:-1]
if headers == "":
print ("No (or empty) filelist found, starting from scratch...")
# Write the basic headers to the file
headers = sepVar.join(defaultHeaders)
fileobj.write(headers + "\n")
splitHeaders = headers.split(sepVar)
# Read the whole file into a variable
table = readWholeFile()
# Main loop
userInput = ""
while True:
# Actually get new user input
try:
userInput = input("> ")
except (EOFError, KeyboardInterrupt):
exit()
if userInput == "exit":
exit()
switchDict = {
"headers": headersFunc,
"add": add,
"view": view,
"save": save,
"remove": remove,
"new": new
}
userInput = userInput.split(" ")
if userInput[0] in switchDict:
switchDict[userInput[0]](userInput)
else:
otherwise(userInput)