Skip to content

Instantly share code, notes, and snippets.

@KoStard
Created July 18, 2019 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KoStard/66773225826167632be7e0f983130844 to your computer and use it in GitHub Desktop.
Save KoStard/66773225826167632be7e0f983130844 to your computer and use it in GitHub Desktop.
Checker is a base class for all checkers - it makes all children classes to have same methods.
class Checker:
@staticmethod
def is_this_type(expr):
raise NotImplementedError()
def does_match(self, value):
raise NotImplementedError()
class NameChecker(Checker):
@staticmethod
def is_this_type(expr):
"""
Check if the expr is in a name checker format
"""
def __init__(self, expr):
self.expr = expr
def does_match(self, value):
"Check if value matches to the expr"
class SizeChecker(Checker):
@staticmethod
def is_this_type(expr):
"""
Check if the expr is in a size checker format
"""
def __init__(self, expr):
self.expr = expr
def does_match(self, value):
"Check if value matches to the expr"
class TypeChecker(Checker):
@staticmethod
def is_this_type(expr):
"""
Check if the expr is in a type checker format
"""
def __init__(self, expr):
self.expr = expr
def does_match(self, value):
"Check if value matches to the expr"
class CreateTimeChecker(Checker):
@staticmethod
def is_this_type(expr):
"""
Check if the expr is in a creation time checker format
"""
def __init__(self, expr):
self.expr = expr
def does_match(self, value):
"Check if value matches to the expr"
class Finder:
checkers = [NameChecker, SizeChecker, TypeChecker, CreateTimeChecker]
def __init__(self, path, expression):
self.current_checkers = []
exps = [e.strip() for e in expression.split('|')]
for exp in exps:
found = False
for ch in self.checkers:
if ch.is_this_type(exp):
found = True
self.current_checkers.append(ch(exp))
break
if not found:
raise ValueError("Invalid pattern '{}'.".format(exp))
def find(self, current_path, results):
"""
Check if content files match the expressions and check it's inner folders
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment