slopcheck.py for checking slop in files.
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 | import sys
import re
class Finder:
class Occurrence:
def __init__(self, regex, filename, line, value):
self.regex = regex
self.filename = filename
self.line = line
self.value = value
def __init__(self, regex):
self.regex = regex
self.occurrences = []
def __call__(self, filename, content):
for i, line in enumerate(content.splitlines()):
if self.regex.search(line):
self.occurrences.append(Finder.Occurrence(self.regex, filename, i + 1, line))
class SlopFinder:
def __init__(self):
# Set up slop finders
self.finders = [Finder(re.compile(i)) for i in [
'^"Besides,',
' ministration',
'[Dd]espite h[ie][mr]self',
'[Ff]or the first time ever',
'[Ff]or the first time in a ',
'[Mm]ischievous',
'[Mm]aybe, just maybe',
'[Tt]hat was\\.\\.\\.',
'[Aa] mix([a-z]*) of',
'a testament to',
'audible (["\'"]?)p[l]?op',
'barely above a whisper',
'barely audible',
'bruising kiss',
'buck([s]?) h[ei][rs] ',
'buck([s]?) my ',
'bucked [mh][yei][ rs]',
'bucking [mh][yei][ rs]',
'can\'t help but',
'cheeks flaming',
'couldn\'t help but',
'didn\'t need to be told twice',
'eyes gleaming',
'getting started',
'grin([s]?) wickedly',
'let\'s get started',
'perhaps, just perhaps',
'puckered hole',
'reckless abandon',
'shiver([s]?) down',
'slick slit',
'smile([s]?) weakly',
'smiling weakly',
'sparkling with amusement',
'sparkling with excitement',
'sweet nothings',
'to get started',
'unlike anything (s?)he',
'unlike anything I',
'wave after wave',
'whatever it takes',
'with mischief',
]]
def __call__(self, filename, content):
for finder in self.finders:
finder(filename, content)
def aggregate(self):
slop_occurrences = []
for finder in self.finders:
slop_occurrences.extend(finder.occurrences)
return slop_occurrences
def explain_slop(slop_occurrences, filename=None, filenamesub=None):
if filename is None:
filtered = slop_occurrences
else:
filtered = [i for i in slop_occurrences if i.filename == filename]
print(f"Slop occurrences: {len(filtered)}")
if len(filtered) > 0:
print("\033[91mERROR: SLOP NOT ALLOWED!\033[0m")
if filenamesub is None and filename is not None:
filenamesub = filename
prev_pat = None
for occurrence in filtered:
if prev_pat is None or prev_pat != occurrence.regex.pattern:
print(f"\n\033[92m{occurrence.regex.pattern}:\033[0m")
prev_pat = occurrence.regex.pattern
print(f"\033[93m{occurrence.filename if filenamesub is None else filenamesub}:{occurrence.line}\033[0m {occurrence.value}")
print(f"Slop occurrences: {len(filtered)}")
return False
return True
if __name__ == "__main__":
slopfinder = SlopFinder()
for file in sys.argv[1:]:
with open(file, 'r') as f:
slopfinder(file, f.read())
slop_occurrences = slopfinder.aggregate()
if not explain_slop(slop_occurrences):
sys.exit(1)
|