Last active
July 14, 2017 11:47
-
-
Save ergoithz/0114c22936bfadafd71f93c60c43d170 to your computer and use it in GitHub Desktop.
Interactive rule-based duplcated ROM removal tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import os.path | |
import itertools | |
class FILTERS: | |
TIERS = [ | |
['european', 'spanish', 'verified'], | |
['european', 'multilanguage', 'verified'], | |
['european', 'verified'], | |
['american', 'multilanguage', 'verified'], | |
['american', 'nonspanish', 'verified'], | |
['european', 'spanish'], | |
['european', 'multilanguage'], | |
['european'], | |
['american', 'multilanguage'], | |
['american', 'nonspanish'], | |
['japanese', 'translation'], | |
['japanese'], | |
] | |
@staticmethod | |
def european(filename): | |
return any( | |
text in filename | |
for text in ('(E)', '(UE)', '(USA, Europe)', '(EU)') | |
) | |
@staticmethod | |
def spanish(filename): | |
return any( | |
text in filename | |
for text in ('[S]', '(S)') # spanish or spain | |
) | |
@classmethod | |
def nonspanish(cls, filename): | |
return not cls.spanish(filename) | |
@staticmethod | |
def american(filename): | |
return '(U)' in filename | |
@staticmethod | |
def japanese(filename): | |
return '(J)' in filename | |
@staticmethod | |
def multilanguage(filename, re=re.compile(r'[\[(]M\d*[\])]')): | |
return re.search(filename) is not None | |
@staticmethod | |
def translation(filename, re=re.compile(r'[\[(]T(\+[^\]]+)?[\])]')): | |
return re.search(filename) is not None | |
@staticmethod | |
def verified(filename): | |
return '[!]' in filename | |
@staticmethod | |
def bad(filename): | |
return '[B]' in filename | |
@classmethod | |
def tier(cls, filename): | |
for n, checks in enumerate(cls.TIERS): | |
if all(getattr(cls, name)(filename) for name in checks): | |
return n | |
return len(cls.TIERS) | |
def name(filename, re=re.compile(r'^[^(\[]+')): | |
match = re.search(filename) | |
return match.group(0) if match else None | |
def exclude(filename_list, *rules): | |
return [ | |
filename | |
for filename in filename_list | |
if not any(rule(filename) for rule in rules) | |
] | |
def group(path=os.getcwd(), ext='', namefunc=name, tierfunc=FILTERS.tier): | |
filenames = [ | |
filename | |
for filename in os.listdir(path) | |
if filename.endswith(ext) | |
] | |
filenames.sort(key=str.lower) | |
for key, group in itertools.groupby(filenames, namefunc): | |
options = sorted(group, key=tierfunc) | |
remove = exclude(options, FILTERS.translation, FILTERS.spanish) | |
if len(options) == len(remove): | |
remove.pop(0) # leave, at least, an option | |
if remove: | |
for option in options: | |
print('[%s] %s' % ( | |
'x' if option in remove else ' ', | |
option)) | |
if input('Type R (and ENTER) to remove: ').strip().lower() == 'r': | |
for filename in remove: | |
fullpath = os.path.join(path, filename) | |
os.remove(fullpath) | |
print('%d files removed' % len(remove)) | |
else: | |
print('ignored') | |
if __name__ == '__main__': | |
group() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment