luqasz / librouteros

Python implementation of MikroTik RouterOS API
GNU General Public License v2.0
221 stars 49 forks source link

Be able to "filter()" without specifying props in "select()" #213

Open ornati opened 12 months ago

ornati commented 12 months ago

I would like to write something like this:

api.path('/ip/route').select().where(Key('active') == True)

In other words: be able to filter without being forced to specify props in select(...).

The quick workaround I'm currently using:

class BetterQuery(librouteros.query.Query):
    def __iter__(self) -> ResponseIter:
        words = []
        if self.keys:
            keys = ','.join(str(key) for key in self.keys)
            keys = f'=.proplist={keys}'
            words.append(keys)
        cmd = str(self.path.join('print'))
        words.extend(self.query)
        return iter(self.api.rawCmd(cmd, *words))

And then I can write:

BetterQuery(api.path('/ip/route'), keys=None, api=api).where(Key('active') == True)

luqasz commented 12 months ago

I can modify https://github.com/luqasz/librouteros/blob/master/librouteros/api.py#L96 so it will accept 0 or more keys. Or you can init Query object with empty tuple as keys argument.

Will this be ok ?

ornati commented 11 months ago

Yes, I think that changing "select()" (and "Query.iter") to accept 0 or more keys will make calling code more readable.

Also having a ".where()" directly on "Path" so I can write: api.path('/ip/route').where(Key('active') == True)

instead of: api.path('/ip/route').select().where(Key('active') == True)

would be nice I think :)