Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modified program structure such that user is always returned to main …

…menu after aborting or finishing a task. Added new menu item to allow user to quit.
  • Loading branch information...
commit 7d2486099202d835ccd39db30e6286acdd11cbf6 1 parent 8c6e547
Jonathan authored

Showing 1 changed file with 86 additions and 43 deletions. Show diff stats Hide diff stats

  1. +86 43 music-cleanup.py
129 music-cleanup.py
@@ -8,11 +8,10 @@
8 8
9 9 # Interprets user response to a boolean query. Returns boolean flag for y/n/Y/N/yes/no/YES/NO
10 10 # Returns False for any unknown input
11   -# Ends program if user response is a/A/abort/ABORT
  11 +# Returns null if user input is a/A/abort/ABORT
12 12 def InterpretYesNoAbort(ans):
13 13 if ans.upper() == 'A' or ans.upper() == 'ABORT':
14   - print('User aborted processing')
15   - exit(0)
  14 + return None
16 15 elif ans.upper() == 'Y' or ans.upper() == 'YES':
17 16 return True
18 17 else:
@@ -40,6 +39,7 @@ def CombineDirectoryContents (dirToKeep, dirToCopy):
40 39 # Searches for and prompts user to combine folders that have similar names.
41 40 # Similarity is determined by removing whitespace and non alpha/non alphanumeric characters
42 41 # and comparing to other folder names.
  42 +# Returns None if user aborts operation, True otherwise
43 43 def CombineSimilarlyNamedFolders(rootDir):
44 44 if not rootDir.endswith('/'): rootDir = rootDir + '/'
45 45 print ('Searching ' + rootDir + ' for similarly named folders...')
@@ -61,7 +61,10 @@ def CombineSimilarlyNamedFolders(rootDir):
61 61 duplicateFolder = rootDir + artist
62 62
63 63 if existingFolder != duplicateFolder and os.path.isdir(existingFolder) and os.path.isdir(duplicateFolder):
64   - if InterpretYesNoAbort(raw_input("Move contents of " + duplicateFolder + " into " + existingFolder + "? (yes/no/abort) >")):
  64 + result = InterpretYesNoAbort(raw_input("Move contents of " + duplicateFolder + " into " + existingFolder + "? (yes/no/abort) >"))
  65 + if result is None:
  66 + return None
  67 + elif result:
65 68 # combine the two directories
66 69 CombineDirectoryContents(existingFolder, duplicateFolder)
67 70 else:
@@ -77,7 +80,10 @@ def CombineSimilarlyNamedFolders(rootDir):
77 80 duplicateFolder = rootDir + artist
78 81
79 82 if existingFolder != duplicateFolder and os.path.isdir(existingFolder) and os.path.isdir(duplicateFolder):
80   - if InterpretYesNoAbort(raw_input("Move contents of " + artist + " into " + artists[artistAlphaNumericOnly] + "? (yes/no/abort) >")):
  83 + result = InterpretYesNoAbort(raw_input("Move contents of " + artist + " into " + artists[artistAlphaNumericOnly] + "? (yes/no/abort) >"))
  84 + if result is None:
  85 + return None
  86 + elif result:
81 87 # combine the two directories
82 88 CombineDirectoryContents(existingFolder, duplicateFolder)
83 89 else:
@@ -103,7 +109,10 @@ def CombineSimilarlyNamedFolders(rootDir):
103 109 duplicateFolder = rootDir + otherArtistName
104 110
105 111 if existingFolder != duplicateFolder and os.path.isdir(existingFolder) and os.path.isdir(duplicateFolder):
106   - if InterpretYesNoAbort(raw_input("Move contents of " + otherArtistName + " into " + artist + "? (yes/no/abort) >")):
  112 + result = InterpretYesNoAbort(raw_input("Move contents of " + otherArtistName + " into " + artist + "? (yes/no/abort) >"))
  113 + if result is None:
  114 + return None
  115 + elif result:
107 116 # combine the two directories
108 117 CombineDirectoryContents(existingFolder, duplicateFolder)
109 118
@@ -114,10 +123,14 @@ def CombineSimilarlyNamedFolders(rootDir):
114 123 duplicateFolder = rootDir + otherArtistName
115 124
116 125 if existingFolder != duplicateFolder and os.path.isdir(existingFolder) and os.path.isdir(duplicateFolder):
117   - if InterpretYesNoAbort(raw_input("Move contents of " + otherArtistName + " into " + artist + "? (yes/no/abort) >")):
  126 + result = InterpretYesNoAbort(raw_input("Move contents of " + otherArtistName + " into " + artist + "? (yes/no/abort) >"))
  127 + if result is None:
  128 + return None
  129 + elif result:
118 130 # combine the two directories
119 131 CombineDirectoryContents(existingFolder, duplicateFolder)
120   - print('Done');
  132 + print('Done')
  133 + return True
121 134
122 135
123 136 # Does the above, but searches the entire subtree of rootDir
@@ -126,6 +139,7 @@ def CombineSimilarlyNamedFoldersRecursive(rootDir):
126 139
127 140 dirsToSearch = []
128 141
  142 + # recursively assemble list of sub dirs
129 143 for root, dirs, files in os.walk(rootDir, topdown=False):
130 144 for name in dirs:
131 145 if root not in dirsToSearch:
@@ -135,15 +149,19 @@ def CombineSimilarlyNamedFoldersRecursive(rootDir):
135 149 if (root + '/' + name) != startingDir:
136 150 dirsToSearch.append(root + '/' + name)
137 151
  152 + # process them in alphabetical order
138 153 dirsToSearch.sort()
139   -
140 154 for search in dirsToSearch:
141 155 if not os.path.isdir(search): continue
142   - CombineSimilarlyNamedFolders(search)
  156 +
  157 + # if user hits abort, respect it
  158 + if CombineSimilarlyNamedFolders(search) is None:
  159 + break
143 160
144 161
145 162 # Searches for and prompts user to rename folders that contain non-alphanumeric characters.
146 163 # If user specifies a new folder name that already exists, folder contents are combined.
  164 +# Returns None if user aborts, True otherwise
147 165 def RenameFoldersNonAlphanumeric(rootDir):
148 166 if not rootDir.endswith('/'): rootDir = rootDir + '/'
149 167 print ('Searching ' + rootDir + ' for non-alphanumeric folders...')
@@ -156,7 +174,10 @@ def RenameFoldersNonAlphanumeric(rootDir):
156 174
157 175 # if artist contains non-alphanumeric characters, optionally rename the folder
158 176 if len(re.sub(r'\s', '', artist)) > 1 and (not re.sub(r'\s', '', artist).isalnum()) and os.path.isdir(rootDir + artist):
159   - if InterpretYesNoAbort(raw_input("Would you like to rename " + artist + "? (yes/no/abort) >")):
  177 + result = InterpretYesNoAbort(raw_input("Would you like to rename " + artist + "? (yes/no/abort) >"))
  178 + if result is None:
  179 + return None
  180 + elif result:
160 181 # rename directory
161 182 newName = raw_input("What should we rename it to? ")
162 183
@@ -166,7 +187,10 @@ def RenameFoldersNonAlphanumeric(rootDir):
166 187 badlyNamedFolder = rootDir + artist
167 188
168 189 if existingFolder != badlyNamedFolder and os.path.isdir(existingFolder) and os.path.isdir(badlyNamedFolder):
169   - if InterpretYesNoAbort(raw_input("Specified Directory exists. Combine contents? (yes/no/abort) >")):
  190 + result = InterpretYesNoAbort(raw_input("Specified Directory exists. Combine contents? (yes/no/abort) >"))
  191 + if result is None:
  192 + return None
  193 + elif result:
170 194 CombineDirectoryContents(existingFolder, badlyNamedFolder)
171 195 else:
172 196 # if not, just rename as requested
@@ -174,14 +198,15 @@ def RenameFoldersNonAlphanumeric(rootDir):
174 198
175 199 print('Renamed')
176 200 print('Done')
  201 + return True
177 202
178 203
179 204 # Does the above, but searches the entire subtree of rootDir
180 205 def RenameFoldersNonAlphanumericRecursive(rootDir):
181 206 print ('Searching ' + rootDir + ' for non-alphanumeric folders...')
182 207
  208 + # recursively assemble list of subdirs to search
183 209 dirsToSearch = []
184   -
185 210 for root, dirs, files in os.walk(rootDir, topdown=False):
186 211 for name in dirs:
187 212 if root not in dirsToSearch:
@@ -191,11 +216,15 @@ def RenameFoldersNonAlphanumericRecursive(rootDir):
191 216 if (root + '/' + name) != startingDir:
192 217 dirsToSearch.append(root + '/' + name)
193 218
  219 + # process them in alphabetical order
194 220 dirsToSearch.sort()
195   -
196 221 for search in dirsToSearch:
197 222 if not os.path.isdir(search): continue
198   - RenameFoldersNonAlphanumeric(search)
  223 +
  224 + # respect user abort
  225 + if RenameFoldersNonAlphanumeric(search) is None:
  226 + break
  227 +
199 228
200 229 # Searches for folders that have some name x where another folder with the name The x also exists.
201 230 # User is prompted to combine the folders
@@ -210,11 +239,17 @@ def CombineFoldersIgnorePrefix():
210 239
211 240 if not artist.upper().startswith('THE '):
212 241 if os.path.isdir(startingDir + 'The ' + artist):
213   - if InterpretYesNoAbort(raw_input('Would you like to combine ' + artist + ' with The ' + artist + '? (yes/no/abort) >')):
  242 + result = InterpretYesNoAbort(raw_input('Would you like to combine ' + artist + ' with The ' + artist + '? (yes/no/abort) >'))
  243 + if result is None:
  244 + return
  245 + elif result:
214 246 CombineDirectoryContents(startingDir + 'The ' + artist, startingDir + artist)
215 247 if not artist.upper().startswith('A '):
216 248 if os.path.isdir(startingDir + 'A ' + artist):
217   - if InterpretYesNoAbort(raw_input('Would you like to combine ' + artist + ' with A ' + artist + '? (yes/no/abort) >')):
  249 + result = InterpretYesNoAbort(raw_input('Would you like to combine ' + artist + ' with A ' + artist + '? (yes/no/abort) >'))
  250 + if result is None:
  251 + return
  252 + elif result:
218 253 CombineDirectoryContents(startingDir + 'A ' + artist, startingDir + artist)
219 254 print ('Done')
220 255
@@ -237,7 +272,11 @@ def DeleteUnwantedFileTypes():
237 272 elif extension not in ok:
238 273 # prompt user about this file type
239 274 print('Found new extension *' + extension)
240   - if InterpretYesNoAbort(raw_input('Delete it? (yes/no/abort) >')):
  275 +
  276 + result = InterpretYesNoAbort(raw_input('Delete it? (yes/no/abort) >'))
  277 + if result is None:
  278 + return
  279 + elif result:
241 280 # delete the file and remember for next time
242 281 os.remove(root + '/' + name)
243 282 bad.append(extension)
@@ -268,28 +307,32 @@ def DeleteEmptyDirectories():
268 307 startingDir = raw_input('Enter directory to clean: ')
269 308 if not startingDir.endswith('/'): startingDir = startingDir + '/'
270 309
271   -print('')
272   -print ('Select an action to perform:')
273   -print ('1. Combine Similarly Named Folders')
274   -print ('2. Combine Similarly Named Folders (Recursive)')
275   -print ('3. Rename Folders That Contain Non-Alphanumeric Characters')
276   -print ('4. Rename Folders That Contain Non-Alphanumeric Characters (Recursive)')
277   -print ('5. Combine Folders By Ignoring Prefixes The and A')
278   -print ('6. Delete Unwanted File Types (Recursive)')
279   -print ('7. Delete Empty Directories (Recursive)')
280   -action = raw_input('>')
281   -
282   -if action == '1':
283   - CombineSimilarlyNamedFolders(startingDir)
284   -elif action == '2':
285   - CombineSimilarlyNamedFoldersRecursive(startingDir)
286   -elif action == '3':
287   - RenameFoldersNonAlphanumeric(startingDir)
288   -elif action == '4':
289   - RenameFoldersNonAlphanumericRecursive(startingDir)
290   -elif action == '5':
291   - CombineFoldersIgnorePrefix()
292   -elif action == '6':
293   - DeleteUnwantedFileTypes()
294   -elif action == '7':
295   - DeleteEmptyDirectories()
  310 +while True:
  311 + print('')
  312 + print ('Select an action to perform:')
  313 + print ('1. Combine Similarly Named Folders')
  314 + print ('2. Combine Similarly Named Folders (Recursive)')
  315 + print ('3. Rename Folders That Contain Non-Alphanumeric Characters')
  316 + print ('4. Rename Folders That Contain Non-Alphanumeric Characters (Recursive)')
  317 + print ('5. Combine Folders By Ignoring Prefixes The and A')
  318 + print ('6. Delete Unwanted File Types (Recursive)')
  319 + print ('7. Delete Empty Directories (Recursive)')
  320 + print ('8. Quit')
  321 + action = raw_input('>')
  322 +
  323 + if action == '1':
  324 + CombineSimilarlyNamedFolders(startingDir)
  325 + elif action == '2':
  326 + CombineSimilarlyNamedFoldersRecursive(startingDir)
  327 + elif action == '3':
  328 + RenameFoldersNonAlphanumeric(startingDir)
  329 + elif action == '4':
  330 + RenameFoldersNonAlphanumericRecursive(startingDir)
  331 + elif action == '5':
  332 + CombineFoldersIgnorePrefix()
  333 + elif action == '6':
  334 + DeleteUnwantedFileTypes()
  335 + elif action == '7':
  336 + DeleteEmptyDirectories()
  337 + elif action == '8':
  338 + break

0 comments on commit 7d24860

Please sign in to comment.
Something went wrong with that request. Please try again.