Filter agents
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.
Here is a quick recap of the Base
Class and our Agent Class.
- Base.py
- Agent my-parser
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.
class Base(ABC):
NAME = "Base Agent"
ACTIVATED = False
VERSION = "0.0"
MISSION = "No description"
EXTENSIONS = None # EXTENSIONS attribute, default is None
The Agent my-parser
overrides the attribute EXTENSIONS
to indicate file extensions he can parse.
class MyParserAgent(Base):
NAME = "MyParser"
ACTIVATED = True
VERSION = "1.0"
MISSION = "Yet another parser to parse awesome stuff"
FILE = None
EXTENSIONS = ["zip", "tar"] # Attribute overrides, restrict to Zip and Tar archives
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.
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