Retabbed (to 4-space soft-tab)

This commit is contained in:
dbr
2008-06-30 12:56:20 +09:30
parent 2ed8fb56fe
commit 328a1d03f1

View File

@@ -5,21 +5,21 @@ import os,re,sys
# Colourized output helper # Colourized output helper
################################### ###################################
def colour(text,colour="red"): def colour(text,colour="red"):
nocolour=False nocolour=False
if nocolour: # Colour no supported, return plain text if nocolour: # Colour no supported, return plain text
return text return text
#end if #end if
c = {'red':'[31m', c = {'red':'[31m',
'green':'[32m', 'green':'[32m',
'blue':'[34m', 'blue':'[34m',
} }
CLR=chr(27)+'[0m' CLR=chr(27)+'[0m'
if not colour in c.keys(): if not colour in c.keys():
raise ValueError("Invalid colour") raise ValueError("Invalid colour")
else: else:
return chr(27)+c[colour] + text + CLR return chr(27)+c[colour] + text + CLR
#end if #end if
#end colour #end colour
################################### ###################################
@@ -37,15 +37,15 @@ def colour(text,colour="red"):
# Show name - [01x23] - The Episode Name # Show name - [01x23] - The Episode Name
# Show name - [04x01] # Show name - [04x01]
tv_ep_name = [ tv_ep_name = [
re.compile("([-\w ]+) - \[(\d{2})x(\d{2}|\d{2}-\d{2})\](?= - ([\w\(\) ]+))?"), re.compile("([-\w ]+) - \[(\d{2})x(\d{2}|\d{2}-\d{2})\](?= - ([\w\(\) ]+))?"),
] ]
################################### ###################################
# Regex to match valid, but not-to-be-processed files (dot-files, folder.jpg artwork) # Regex to match valid, but not-to-be-processed files (dot-files, folder.jpg artwork)
################################### ###################################
decrappify = [ decrappify = [
re.compile("(?=^[.]{1}.*)"), re.compile("(?=^[.]{1}.*)"),
re.compile("folder.jpg"), re.compile("folder.jpg"),
] ]
# Location to process # Location to process
@@ -60,25 +60,25 @@ import path
d = path.path(loc) d = path.path(loc)
allfiles=[] allfiles=[]
for f in d.walkfiles("*"): for f in d.walkfiles("*"):
allfiles.append( str(f) ) allfiles.append( str(f) )
#end for f #end for f
files = [x for x in allfiles if os.path.isfile(x)] # only get files, not folders files = [x for x in allfiles if os.path.isfile(x)] # only get files, not folders
# Strip out dotfiles/folder.jpg # Strip out dotfiles/folder.jpg
for current_file in allfiles: for current_file in allfiles:
current_file_path,current_file_name = os.path.split(current_file) current_file_path,current_file_name = os.path.split(current_file)
for cur_decrap in decrappify: for cur_decrap in decrappify:
if cur_decrap.match(current_file_name): if cur_decrap.match(current_file_name):
files.remove(current_file) files.remove(current_file)
#end for file #end for file
#files = [os.path.join(loc,x) for x in files] # append path to file name #files = [os.path.join(loc,x) for x in files] # append path to file name
# Warn if no files are found, then exit # Warn if no files are found, then exit
if files.__len__() == 0: if files.__len__() == 0:
print colour('No files found','red') print colour('No files found','red')
sys.exit(0) sys.exit(0)
################################### ###################################
# Validate filenames # Validate filenames
@@ -88,17 +88,17 @@ valid = []
invalid = [] invalid = []
for cur in files: for cur in files:
for cur_checker in tv_ep_name: for cur_checker in tv_ep_name:
check = cur_checker.findall(cur) check = cur_checker.findall(cur)
if check: if check:
# Valid file name # Valid file name
valid.append(check) valid.append(check)
break # Found valid episode, skip to the next one break # Found valid episode, skip to the next one
#end if #end if
else: else:
# No match found (Invalid name) # No match found (Invalid name)
invalid.append(cur) invalid.append(cur)
#end for cur_checker #end for cur_checker
#end for #end for
################################### ###################################
@@ -106,113 +106,113 @@ for cur in files:
################################### ###################################
if invalid.__len__() > 0: if invalid.__len__() > 0:
print colour('WARNING','red') print colour('WARNING','red')
print '[!] Invalid file-names found:' print '[!] Invalid file-names found:'
print '\n'.join(invalid) print '\n'.join(invalid)
print "-" * 28 print "-" * 28
print print
################################### ###################################
# Show valid names # Show valid names
################################### ###################################
if valid.__len__() > 0: if valid.__len__() > 0:
print '[?] Valid file-names found:' print '[?] Valid file-names found:'
allepisodes = {} allepisodes = {}
# Make a dict of shows, each show contains an array of episode-info. # Make a dict of shows, each show contains an array of episode-info.
# Example: # Example:
# { 'showname': [ [1,24,'Title'], ], # { 'showname': [ [1,24,'Title'], ],
# 'another': [ [4,12,'More'], [4,13,'Stuff'], ] # 'another': [ [4,12,'More'], [4,13,'Stuff'], ]
# } # }
# This could(/should) change into something more managable. # This could(/should) change into something more managable.
for x in valid: for x in valid:
showname,season,epno,title = x[0] # Put regex-groups into named vars showname,season,epno,title = x[0] # Put regex-groups into named vars
if allepisodes.has_key(showname): # Does the show exist in allepisodes? if allepisodes.has_key(showname): # Does the show exist in allepisodes?
allepisodes[showname].append([season,epno,title]) # yes, append episode allepisodes[showname].append([season,epno,title]) # yes, append episode
else: else:
allepisodes[showname] = [[season,epno,title]] # no, init episodes array allepisodes[showname] = [[season,epno,title]] # no, init episodes array
#end for x #end for x
# Now we have a nice dict of all shows, output them as: # Now we have a nice dict of all shows, output them as:
# Showname # Showname
# Season 01 # Season 01
# Episodes 01, 02, 03 # Episodes 01, 02, 03
# Season 01 # Season 01
# Episodes 01, 02 # Episodes 01, 02
for showname,alleps in allepisodes.items(): for showname,alleps in allepisodes.items():
print colour(showname,'green') print colour(showname,'green')
# Make a dict for the current show, # Make a dict for the current show,
# an index for each season, containing an array of episodes. # an index for each season, containing an array of episodes.
# Example: # Example:
# { '02' : [20,21,22,23], # { '02' : [20,21,22,23],
# '03' : [01,02,03] # '03' : [01,02,03]
# } # }
# This is for ease-of-output. It'd be possible to work # This is for ease-of-output. It'd be possible to work
# directly of the allepisodes dict, but a lot messier.. # directly of the allepisodes dict, but a lot messier..
curshow={} curshow={}
for curep in alleps: for curep in alleps:
season,epno,title = curep season,epno,title = curep
try: try:
season = int(season) season = int(season)
except ValueError: except ValueError:
print "Error: Season number not integer. Check naming or tv_ep_name regex." print "Error: Season number not integer. Check naming or tv_ep_name regex."
#end try #end try
try: try:
epno = int(epno) epno = int(epno)
except: except:
# deal with episode numbered like - [01x01-02] # deal with episode numbered like - [01x01-02]
# TODO: Currently very hackish, improve the handling of [01x01-02] names # TODO: Currently very hackish, improve the handling of [01x01-02] names
split_two_eps = epno.split("-") split_two_eps = epno.split("-")
if len(split_two_eps) == 2: if len(split_two_eps) == 2:
p1,p2 = split_two_eps p1,p2 = split_two_eps
epno,ep2 = int(p1),int(p2) epno,ep2 = int(p1),int(p2)
# Add the second part, then add the first as usual. Messy, but works # Add the second part, then add the first as usual. Messy, but works
if curshow.has_key(season): if curshow.has_key(season):
curshow[season].append(ep2) curshow[season].append(ep2)
else: else:
curshow[season]=[ep2] curshow[season]=[ep2]
#end if curshow #end if curshow
else: else:
print "Error: Episode number not integer, or two integers (01-02)" print "Error: Episode number not integer, or two integers (01-02)"
#end try #end try
if curshow.has_key(season): if curshow.has_key(season):
curshow[season].append(epno) curshow[season].append(epno)
else: else:
curshow[season]=[epno] curshow[season]=[epno]
#end for curep #end for curep
for seas,eps in curshow.items(): for seas,eps in curshow.items():
# Check for missing episodes (by looking for gaps in sequence) # Check for missing episodes (by looking for gaps in sequence)
mi,ma=int(min(eps)),int(max(eps)) mi,ma=int(min(eps)),int(max(eps))
missing_eps=[] missing_eps=[]
for x in range(1,int(ma)+1): for x in range(1,int(ma)+1):
try: try:
if not x in [int(y) for y in eps]: if not x in [int(y) for y in eps]:
missing_eps.append(x) missing_eps.append(x)
except ValueError: except ValueError:
pass # start value is 01-02 probably. pass # start value is 01-02 probably.
#end try #end try
#end for x #end for x
# if we are missing an episode, "season 01" title becomes red. # if we are missing an episode, "season 01" title becomes red.
if missing_eps.__len__() == 0: if missing_eps.__len__() == 0:
cur_colour = "blue" cur_colour = "blue"
else: else:
cur_colour = "red" cur_colour = "red"
# Print season title in above colour. # Print season title in above colour.
print "\t",colour("Season " + str(seas),colour=cur_colour) print "\t",colour("Season " + str(seas),colour=cur_colour)
# And list episodes # And list episodes
print "\t\t", "Episodes:", ", ".join([str(e) for e in eps]) print "\t\t", "Episodes:", ", ".join([str(e) for e in eps])
# And if there is missing episodes, list them. # And if there is missing episodes, list them.
if missing_eps.__len__() != 0: if missing_eps.__len__() != 0:
print "\t\t","Missing:", ", ".join([str(m) for m in missing_eps]) print "\t\t","Missing:", ", ".join([str(m) for m in missing_eps])
#end for seas,eps #end for seas,eps