Moved finding files into find_files function

This commit is contained in:
dbr
2008-12-29 17:00:50 +10:30
parent eac58ad04a
commit b45386ead1

View File

@@ -207,114 +207,126 @@ class Episode:
#end Episode #end Episode
################################### def find_files(loc):
# Find all valid files """Recursivly finds files in a directory
################################### """
allfiles=[] allfiles=[]
for (path,dirs,files) in os.walk(loc): for (path,dirs,files) in os.walk(loc):
for file in files: for file in files:
filename = os.path.join(os.path.abspath(path), file) filename = os.path.join(os.path.abspath(path), file)
allfiles.append( str(filename) ) allfiles.append( str(filename) )
#end for f return allfiles
#end find_files
# Strip out dotfiles/folder.jpg
decrappified = []
for current_file in allfiles:
current_file_path,current_file_name = os.path.split(current_file)
crappy = False
for cur_decrap in tv_regex['decrappify']:
if cur_decrap.match(current_file_name):
crappy = True
break
if not crappy: decrappified.append(current_file)
#end for current_file
allfiles = decrappified
# Warn if no files are found, then exit
if len(allfiles) == 0:
print colour('No files found','red')
sys.exit(1)
################################### def main():
# Validate filenames ###################################
################################### # Find all valid files
###################################
allfiles = find_files(loc)
valid = [] # Strip out dotfiles/folder.jpg
invalid = [] decrappified = []
for current_file in allfiles:
for cur in allfiles: current_file_path,current_file_name = os.path.split(current_file)
cpath,cfile = os.path.split(cur) crappy = False
cfile,cext = os.path.splitext(cfile) for cur_decrap in tv_regex['decrappify']:
if cur_decrap.match(current_file_name):
for cur_checker in tv_regex['valid_path']: crappy = True
# Check if path is valid break
check = cur_checker.findall(cpath) if not crappy: decrappified.append(current_file)
if check: #end for current_file
break allfiles = decrappified
else:
invalid.append({'errorno':3, 'path':cpath,'filename':cfile, # Warn if no files are found, then exit
'cext':cext}) if len(allfiles) == 0:
#end for cur_checker print colour('No files found','red')
sys.exit(1)
for cur_checker in tv_regex['with_ep_name']:
# Check if filename is valid (with ep name) ###################################
check = cur_checker.findall(cfile) # Validate filenames
if check: ###################################
# Valid file name
valid.append({'path':cpath,'filename':cfile, valid = []
'cext':cext, 'match':check[0]}) invalid = []
break # Found valid episode, skip to the next one
#end if for cur in allfiles:
else: cpath,cfile = os.path.split(cur)
for cur_checker in tv_regex['missing_ep_name']: cfile,cext = os.path.splitext(cfile)
# Check for valid name with missing episode name
check = cur_checker.findall(cfile) for cur_checker in tv_regex['valid_path']:
if check: # Check if path is valid
invalid.append({'errorno':2, 'path':cpath,'filename':cfile, check = cur_checker.findall(cpath)
'cext':cext}) if check:
break break
#end if check
else: else:
# Doesn't match valid-name or missing-ep-name regexs, it's invalid invalid.append({'errorno':3, 'path':cpath,'filename':cfile,
invalid.append({'errorno':1, 'path':cpath,'filename':cfile,
'cext':cext}) 'cext':cext})
#end for cur_checker #end for cur_checker
#end for cur_checker
#end for
################################### for cur_checker in tv_regex['with_ep_name']:
# Show invalid names # Check if filename is valid (with ep name)
################################### check = cur_checker.findall(cfile)
if len(invalid) > 0: if check:
print colour('WARNING', 'red'), ': Invalid file-names found' # Valid file name
valid.append({'path':cpath,'filename':cfile,
'cext':cext, 'match':check[0]})
break # Found valid episode, skip to the next one
#end if
else:
for cur_checker in tv_regex['missing_ep_name']:
# Check for valid name with missing episode name
check = cur_checker.findall(cfile)
if check:
invalid.append({'errorno':2, 'path':cpath,'filename':cfile,
'cext':cext})
break
#end if check
else:
# Doesn't match valid-name or missing-ep-name regexs, it's invalid
invalid.append({'errorno':1, 'path':cpath,'filename':cfile,
'cext':cext})
#end for cur_checker
#end for cur_checker
#end for
for errorno,errordescr in errors.items(): ###################################
errors = getError(invalid,errorno) # Show invalid names
if len(errors) == 0: continue ###################################
if len(invalid) > 0:
print colour('WARNING', 'red'), ': Invalid file-names found'
errormsg = "# %s (error code %d) #" % (errordescr, errorno) for errorno,errordescr in errors.items():
print "#"*len(errormsg) errors = getError(invalid,errorno)
print errormsg if len(errors) == 0: continue
print "#"*len(errormsg)
for c in errors: errormsg = "# %s (error code %d) #" % (errordescr, errorno)
cur_path = os.path.join(c['path'], c['filename'] + c['cext']) print "#"*len(errormsg)
print cur_path print errormsg
print "#"*len(errormsg)
################################### for c in errors:
# Show valid names cur_path = os.path.join(c['path'], c['filename'] + c['cext'])
################################### print cur_path
if len(valid) > 0:
print colour('INFO','green'), ': Valid file-names found:'
allepisodes = ShowContainer()
for cur in valid: ###################################
if len(cur['match']) == 4: # Show valid names
showname,seasno,epno,title = cur['match'] ###################################
elif len(cur['match']) == 3: if len(valid) > 0:
seasno = 1 print colour('INFO','green'), ': Valid file-names found:'
showname,epno,title = cur['match'] allepisodes = ShowContainer()
allepisodes[showname][seasno][epno]['name'] = title for cur in valid:
#end for cur in valid if len(cur['match']) == 4:
print allepisodes showname,seasno,epno,title = cur['match']
elif len(cur['match']) == 3:
seasno = 1
showname,epno,title = cur['match']
allepisodes[showname][seasno][epno]['name'] = title
#end for cur in valid
print allepisodes
if __name__ == '__main__':
main()