-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.py
executable file
·146 lines (146 loc) · 5.5 KB
/
indexer.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
def program():
import configparser, os
config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.config/indexer/indexer.conf'))
thing = config["Config"]["Item"]
group = config["Config"]["Category"]
place = config["Config"]["Object"]
indexfile = os.path.expanduser(config["Config"]["File"])
global index
index = open(indexfile,'r+')
mainprogram(thing, group, place, indexfile)
def listall(thing, group, place):
index.seek(0)
for line_list in index:
displayline(line_list, thing, group, place)
def displayline(line, thing, group, place):
linelist=line.split('^')
print(linelist[0] + " with/by " + group + ' ' + linelist[1] + " is on " + place + ' ' + linelist[2])
def listthing(thing, group, place):
thing_tmp = input("Which " + thing + " are you looking for? ")
index.seek(0)
for line_thing in index:
if thing_tmp in line_thing:
displayline(line_thing, thing, group, place)
def listplace(thing, group, place):
place_tmp = input("Which " + place + " are you looking for? ")
index.seek(0)
for line_place in index:
if place_tmp in line_place:
displayline(line_place, thing, group, place)
def listgroup(thing, group, place):
group_tmp = input("Which " + group + " are you looking for? ")
index.seek(0)
for line_group in index:
if group_tmp in line_group:
displayline(line_group, thing, group, place)
def add(thing, group, place):
thing_tmp_add = input("Which " + thing + " are you adding? ")
place_tmp_add = input("Which " + place + " is it on? ")
group_tmp_add = input("Which " + group + " is it (by)? ")
index.write(thing_tmp_add + "^" + place_tmp_add + "^" + group_tmp_add + "\n")
def delete(thing, group, place):
thing_tmp_rm = input("Which " + thing + " are you deleting? ")
place_tmp_rm = input("Which " + place + " is it on? ")
group_tmp_rm = input("Which " + group + " is it (by)? ")
lines_to_delete = []
index.seek(0)
for line_rm in index:
if thing_tmp_rm in line_rm and place_tmp_rm in line_rm and group_tmp_rm in line_rm:
displayline(line_rm, thing, group, place)
lines_to_delete.append(line_rm)
response_rm = input("Are you SURE you want to delete these lines? ").upper()
response_rm = response_rm[:1]
if response_rm == "Y":
lines_rm = index.readlines()
index.seek(0)
for i in lines_rm:
if i not in lines_to_delete:
index.write(i)
def reset(thing, group, place, indexfile):
import os
response_wipe = input("Are you SURE you want to wipe the database? ").upper()
response_wipe = response_wipe[:1]
if response_wipe == "Y":
index.close()
os.remove(indexfile)
os.mknod(indexfile)
exit()
def mainprogram(thing, group, place, indexfile):
while True:
print("What type of operation do you want to do?")
print("1: read-only operations")
print("2: edit the database")
print("3: change the settings")
print("q: quit")
choice_main = input()
if choice_main == '1':
ro(thing, group, place)
elif choice_main == '2':
rw(thing, group, place, indexfile)
elif choice_main == '3':
conf()
elif choice_main == 'q':
index.close()
exit()
else:
print("Input 1-3!")
def ro(thing, group, place):
while True:
print("Which operation do you want to do?")
print("1: list all " + thing +"s")
print("2: find information about a/an " + thing)
print("3: list everything with/by a specific " + group)
print("4: list everything on a/an " + place)
print("b: back")
print("q: exit")
choice_ro = input()
if choice_ro == '1':
listall(thing, group, place)
elif choice_ro == '2':
listthing(thing, group, place)
elif choice_ro == '3':
listgroup(thing, group, place)
elif choice_ro == '4':
listplace(thing, group, place)
elif choice_ro == 'b':
break
elif choice_ro == 'q':
index.close()
exit()
else:
print("Input 1-4!")
def rw(thing, group, place, indexfile):
while True:
print("Which operation do you want to do?")
print("1: add a/an " + thing)
print("2: remove a/an " + thing)
print("3: wipe the database")
print("b: back")
print("q: exit")
choice_rw = input()
if choice_rw == '1':
add(thing, group, place)
elif choice_rw == '2':
delete(thing, group, place)
elif choice_rw == '3':
reset(thing, group, place, indexfile)
elif choice_rw == 'b':
break
elif choice_rw == 'q':
index.close()
exit()
else:
print("Input 1-3!")
def conf():
import configparser, os
config = configparser.ConfigParser()
config["Config"] = {}
config["Config"]["Item"] = input("Enter the item that is being indexed (e.g. track) ")
config["Config"]["Category"] = input("Enter the group that the item is in (e.g. artist) ")
config["Config"]["Object"] = input("Enter the physical object containing the items (e.g. CD) ")
config["Config"]["File"] = input("Enter the filename of the database ")
with open(os.path.expanduser('~/.config/indexer/indexer.conf'), "w") as configfile:
config.write(configfile)
exit()
program()