Skip to content

Commit

Permalink
fix is_4cat_class (#398)
Browse files Browse the repository at this point in the history
* fix is_4cat_class

* add back helpful explanation comment
  • Loading branch information
dale-wahl authored Feb 9, 2024
1 parent e329b2e commit 42ffa55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
12 changes: 12 additions & 0 deletions backend/lib/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,4 +863,16 @@ def process(self):
"""
pass

@staticmethod
def is_4cat_processor():
"""
Is this a 4CAT processor?
This is used to determine whether a class is a 4CAT
processor.
:return: True
"""
return True


24 changes: 24 additions & 0 deletions backend/lib/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,27 @@ def work(self):
classes should implement this method.
"""
pass

@staticmethod
def is_4cat_class():
"""
Is this a 4CAT class?
This is used to determine whether a class is a 4CAT worker or a
processor. This method should always return True for workers.
:return: True
"""
return True

@staticmethod
def is_4cat_processor():
"""
Is this a 4CAT processor?
This is used to determine whether a class is a 4CAT
processor.
:return: False
"""
return False
29 changes: 15 additions & 14 deletions common/lib/module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,21 @@ def is_4cat_class(object, only_processors=False):
# it would be super cool to just use issubclass() here!
# but that requires importing the classes themselves, which leads to
# circular imports
# todo: fix this because this sucks
# agreed - Dale
parent_classes = {"BasicWorker", "BasicProcessor", "Search", "SearchWithScope", "Search4Chan",
"ProcessorPreset", "TwitterStatsBase", "BaseFilter", "TwitterAggregatedStats", "ColumnFilter",
"BasicJSONScraper", "BoardScraper4chan", "ThreadScraper4chan"}
if only_processors:
# only allow processors
for worker_only_class in ["BasicWorker", "BasicJSONScraper", "BoardScraper4chan", "ThreadScraper4chan"]:
parent_classes.remove(worker_only_class)

return inspect.isclass(object) and \
parent_classes & set([f.__name__ for f in object.__bases__]) and \
object.__name__ not in("BasicProcessor", "BasicWorker") and \
not inspect.isabstract(object)
if inspect.isclass(object):
if object.__name__ in("BasicProcessor", "BasicWorker") or inspect.isabstract(object):
# ignore abstract and base classes
return False

if hasattr(object, "is_4cat_class"):
if only_processors:
if hasattr(object, "is_4cat_processor"):
return object.is_4cat_processor()
else:
return False
else:
return object.is_4cat_class()

return False

def load_modules(self):
"""
Expand Down

0 comments on commit 42ffa55

Please sign in to comment.