forked from columbia-ossd/photoalbum-python-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
249 lines (206 loc) · 6.52 KB
/
Copy pathmanager.py
File metadata and controls
249 lines (206 loc) · 6.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
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
246
247
248
249
import sys
from photo import Photo
from album import Album
from graphics import *
def can_read_file(filename):
"""
Returns True if the given filename can be opened for reading,
False otherwise.
"""
try:
fp = open(filename, "r")
fp.close()
return True
except IOError:
return False
def get_filename():
"""
Determines the input filename to use.
First tries the first command line argument (if provided).
If that argument is missing or cannot be read, the user is
repeatedly prompted to enter a filename until a readable file
is provided.
"""
filename = None
if len(sys.argv) > 1:
candidate = sys.argv[1]
if can_read_file(candidate):
filename = candidate
else:
print("Could not read file '%s'" % candidate)
while filename is None:
candidate = input("Enter the name of the input file: ")
if can_read_file(candidate):
filename = candidate
else:
print("Could not read file '%s'. Please try again." % candidate)
return filename
def get_value_between(prompt, low, high):
while True:
try:
choice = int(input(prompt))
except ValueError:
print("Please enter a valid number!")
continue
if choice < low or choice > high:
print("Please enter a value between %d and %d" % (low, high))
continue
return choice
def initialize(filename, title):
album = Album(title)
fp = open(filename, "r")
for line in fp:
line = line.strip()
parts = line.split(",")
fname = parts[0]
creator = parts[1]
description = parts[2]
tags = []
for i in range(3, len(parts)):
tags.append(parts[i])
album.add_photo(Photo(fname, creator, description, tags))
fp.close()
return album
def menu():
print()
print("1: List all photos")
print("2: Add a photo")
print("3: Search photos by tag")
print("4: View a photo")
print("5: Edit a photo's tags")
print("6: Exit")
choice = get_value_between("Choose an option: ", 1, 6)
return choice
def viewPhoto(album):
"""
Function for Menu Option 4
"""
print()
photos = album.get_photos()
for i in range(len(photos)):
print("%d: %s" % (i+1, photos[i].get_description()))
choice = get_value_between("Choose a photo: ", 1, len(photos))
name = photos[choice-1].get_filename()
display_image(name)
def display_image(name):
"""
Helper function that we provide
"""
try:
image = Image(Point(250, 250), name)
win = GraphWin(name, image.getWidth(), image.getHeight())
win.setCoords(0, 0, 500, 500)
image.draw(win)
try:
win.getMouse()
win.close()
except:
pass
except:
print("An error occurred trying to open %s" % name)
def searchByTag(album):
"""
Function for Menu Option 3
"""
print()
tags = " ".join(album.get_tags())
print("Here are the tags: " + tags)
search_terms = input("Enter the tag(s) to search for, separated by spaces: ").lower().split()
# Remove duplicate search terms while keeping the original order.
unique_terms = []
for term in search_terms:
if term not in unique_terms:
unique_terms.append(term)
if len(unique_terms) == 0:
print("No tags entered.")
return
all_matches= []
partial_matches = []
for photo in album.get_photos():
photo_tags = photo.get_tags()
matches_all = True
matches_any = False
for term in unique_terms:
if term in photo_tags:
matches_any = True
else:
matches_all = False
if matches_all:
all_matches.append(photo)
elif matches_any:
partial_matches.append(photo)
if len(unique_terms) == 1:
if len(all_matches) > 0:
print("Here are the photos for that tag:")
for photo in all_matches:
print(str(photo))
else:
print("No photos found for tag " + unique_terms[0])
else:
if len(all_matches) > 0:
print("Here are the photos that match all of those tags:")
for photo in all_matches:
print(str(photo))
else:
print("No photos found with all of those tags.")
if len(partial_matches) > 0:
print("Here are the photos that match at least one of those tags:")
for photo in partial_matches:
print(str(photo))
else:
print("No photos found that match any of those tags.")
def get_input(prompt):
resp = ""
while len(resp.strip()) == 0:
resp = input(prompt)
return resp
def addPhoto(album):
"""
Function for Menu Option 2
"""
print("Add Photo")
fname = get_input("Enter the name of the file: ")
creator = get_input("Enter the name of the creator: ")
description = get_input("Enter the description: ")
tagString = input("Enter all the tags, separated by spaces: ")
tags = tagString.split(" ")
#print(tags)
if album.add_photo(Photo(fname, creator, description, tags)):
print("Photo successfully added")
else:
print("Could not add photo to album")
def editTags(album, filename):
"""
Function for Menu Option 5
"""
print()
photos = album.get_photos()
for i in range(len(photos)):
print("%d: %s" % (i+1, photos[i].get_description()))
choice = get_value_between("Choose a photo: ", 1, len(photos))
photo = photos[choice-1]
print("Current tags: " + " ".join(photo.get_tags()))
tagString = input("Enter the new tags, separated by spaces: ")
photo.set_tags(tagString.split(" "))
album.save(filename)
print("Tags updated")
def main():
filename = get_filename()
album = initialize(filename, "cartoon dog photos")
choice = -1
while choice != 6:
choice = menu()
if choice == 1: # list all photos
for photo in album.get_photos():
print(str(photo))
elif choice == 2: # add a photo
addPhoto(album)
elif choice == 3: # search by tag
searchByTag(album)
elif choice == 4: # view a photo
viewPhoto(album)
elif choice == 5: # edit tags
editTags(album, filename)
print("Good bye!")
if __name__ == "__main__":
main()