slopcheck.py for checking slop in files.

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)
Edit Report
Pub: 17 Aug 2024 14:07 UTC
Views: 351