Skip to main content

Filter agents

Wait

If you have not read how to add metadata to Agent, please refer to Adding Metadata to Agent.

Having explained the process of adding attributes to an Agent Class, let's now delve into filtering Agent using these attributes effectively.

For this example, we will reuse the previous example where we defined an attribute called EXTENSIONS.
We will use this attribute to filter Agents capable to parse the input file.

recap

Here is a quick recap of the Base Class and our Agent Class.

The Base Class has the attribute EXTENSIONS defined to None.

This means that by default, Agent will be able to parse every file, not restricted to specific extensions.

agents/base.py
class Base(ABC):
NAME = "Base Agent"
ACTIVATED = False
VERSION = "0.0"
MISSION = "No description"
EXTENSIONS = None # EXTENSIONS attribute, default is None

The Agents are retrieved through the get_enabled_agents function in /agents/__init__.py.

Just add your condition in this function to filter Agents to select. Here is an example with the EXTENSIONS attribute.

agents/__init__.py
def get_enabled_agents(agents):
for agent in agents:
if agents[agent].ACTIVATED: # If Agent is activated
if agents[agent].EXTENSIONS: # If Agent overrides EXTENSIONS
if any(ext == args.get('file', '').split('.')[-1]
for ext in agents[agent].EXTENSIONS): # If file extension is in EXTENSIONS elements
yield agent
else:
yield agent