Example: cat hunters
... or how to integrate groups of tasks with similar options.
This section will present a more complex use case where we have three commands: bigdog, catkiller and eagle which purpose is to find cats.
We will start by integrating bigdog to secator before realizing that most options can be mutualized between the three tools, and a common output type Cat can be created for all three.
Bigdog
Let's suppose we have a fictional utility called bigdog which purpose is to hunt cats on the internet. We want to add bigdog to secator.
Here are some of bigdog's options:
bigdog can be run on a single site using -site:
$ bigdog -site loadsofcats.com
/ \__
( @\___ =============
/ O BIGDOG v1.0.0
/ (_____/ =============
/_____/
garfield [boss, 14]
tony [admin, 18]bigdog can be run on a list of sites using -list:
$ bigdog -list sites.txt -json
/ \__
( @\___ =============
/ O BIGDOG v1.0.0
/ (_____/ =============
/_____/
garfield [boss, 14]
romuald [minion, 5]
tony [admin, 18]bigdog can output JSON lines using -json:
$ bigdog -site loadsofcats.com -json
/ \__
( @\___ =============
/ O BIGDOG v1.0.0
/ (_____/ =============
/_____/
{"name": "garfield", "age": 14, "host": "loadsofcat.com", "position": "boss"}
{"name": "tony", "age": 18, "host": "loadsofcats.com", "position": "admin"}A basic definition of bigdog using basic secator concepts will be:
from secator.runners import Command
from secator.decorators import task
@task
class bigdog(Command):
cmd = 'bigdog'
json_flag = '-json'
input_flag = '-site'
file_flag = '-list'You can now run bigdog from the CLI or the library:
Okay, this is a good start.
Now what if the bigdog command has some more options that you would like to integrate ?
-timeoutallows to specify a request timeout.-rateallows to specify the max requests per minute.
You can add the opts parameter to your Command object to define the cmd options:
You can now use bigdog with this set of options:
Cat hunters category
One advantage of having class-based definitions is that we can group similar tools together in categories.
Let's assume we have 2 other tools that can hunt cats: catkiller and eagle...
... but each of those tools might be written by a different person, and so the interface and output is different for each of them:
Inputs:
--hostis equivalent tobigdog's-site.--max-waitis equivalent tobigdog's-timeout, but in milliseconds instead of seconds.--max-rateis equivalent tobigdog's-rate.--jsonis equivalent tobigdog's-jsonoption, but uses a different option character "--".cat hosts.txt | catkilleris the equivalent tobigdog's-list.
Output:
_infohas the data fornameandage, butageis nowyears.siteis the equivalent ofbigdog'shost.jobis the equivalent ofbigdog'sposition.
Inputs:
-uis equivalent tobigdog's-site.-lis equivalent tobigdog's-list.-timeexpiresis equivalent tobigdog's-timeout.eagledoes not support setting the maximum requests per seconds (bigdog's-rate).-jsonlis the flag to output JSON lines, instead ofbigdog's-json.
Output:
aliasis the equivalent ofbigdog'sname.occupationis the equivalent ofbigdog'sjob.human_ageis the human age conversion of the cat age.
Cat output type
We first define a base Cat dataclass to define the common output schema and a CatHunter category as an input interface.
We take bigdog's output schema as reference to create the Cat output type:
CatHunter category
We take bigdog's options names as reference and add the ones that can be mutualized to the CatHunter category:
Tools implementation
Finally we inherit all commands implementation from CatHunter and write the option mapping for the remaining cat-hunter commands:
Using these definitions, we can now use all the cat-hunter commands with a common interface (input options & output schema):
Last updated
Was this helpful?