Skip to content

Commit

Permalink
ndb: merge pull request #1108 from svinota/ndb-recordset-pipe
Browse files Browse the repository at this point in the history
ndb: recordset pipes for the new API

Bug-Url: #1108
  • Loading branch information
svinota authored Aug 2, 2023
2 parents 5128094 + 6c733b3 commit 0888fde
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pyroute2/cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class Console(code.InteractiveConsole):
def __init__(self, stdout=None, log=None, sources=None):
global HAS_READLINE
self.db = NDB(log=log, sources=sources)
self.db.config = {'show_format': 'json'}
self.db.config.update(
{'show_format': 'json', 'recordset_pipe': 'true'}
)
self.stdout = stdout or sys.stdout
self.session = Session(self.db, self.stdout, self.set_prompt)
self.matches = []
Expand Down
4 changes: 3 additions & 1 deletion pyroute2/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ def __init__(
self.ndb = ndb
else:
self.ndb = NDB(sources=sources, log=log)
self.ndb.config = {'show_format': 'json'}
self.ndb.config.update(
{'show_format': 'json', 'recordset_pipe': 'true'}
)
HTTPServer.__init__(self, (address, port), Handler)
1 change: 1 addition & 0 deletions pyroute2/ndb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def __init__(
'rtnl_debug': rtnl_debug,
'db_cleanup': db_cleanup,
'auto_netns': auto_netns,
'recordset_pipe': 'false',
}
self.task_manager = TaskManager(self)
self._dbm_thread = threading.Thread(
Expand Down
26 changes: 25 additions & 1 deletion pyroute2/ndb/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ def __repr__(self):
return ''.join(ret)


class RecordSetConfig(dict):
def __init__(self, prime):
if isinstance(prime, dict):
for key, value in prime.items():
self[key] = value
else:
raise ValueError('only dict allowed')

def __setitem__(self, key, value):
if isinstance(value, str):
value = json.loads(value)
return super().__setitem__(key, value)


class RecordSet(BaseRecordSet):
'''
NDB views return objects of this class with `summary()` and `dump()`
Expand All @@ -221,9 +235,10 @@ class RecordSet(BaseRecordSet):
to make chains of filters.
'''

def __init__(self, generator, ellipsis=True):
def __init__(self, generator, config=None, ellipsis=True):
super().__init__(generator, ellipsis)
self.filters = []
self.config = RecordSetConfig(config) if config is not None else {}

def __next__(self):
while True:
Expand All @@ -235,6 +250,7 @@ def __next__(self):
else:
return record

@cli.show_result
def select_fields(self, *fields):
'''
Select only chosen fields for every record:
Expand All @@ -253,7 +269,10 @@ def select_fields(self, *fields):
2,'eth0'
'''
self.filters.append(lambda x: x._select_fields(*fields))
if self.config.get('recordset_pipe'):
return RecordSet(self, config=self.config)

@cli.show_result
def select_records(self, f=None, **spec):
'''
Select records based on a function f() or a spec match. A spec
Expand All @@ -272,7 +291,10 @@ def select_records(self, f=None, **spec):
'localhost',0,'eth0','192.168.122.28',24
'''
self.filters.append(lambda x: x if x._match(f, **spec) else None)
if self.config.get('recordset_pipe'):
return RecordSet(self, config=self.config)

@cli.show_result
def transform_fields(self, **kwarg):
'''
Transform fields with a function. Function must accept
Expand All @@ -295,6 +317,8 @@ def transform_fields(self, **kwarg):
'eth0','192.168.122.28/24'
'''
self.filters.append(lambda x: x._transform_fields(**kwarg))
if self.config.get('recordset_pipe'):
return RecordSet(self, config=self.config)

@cli.show_result
def transform(self, **kwarg):
Expand Down
18 changes: 16 additions & 2 deletions pyroute2/ndb/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,27 @@ def _native(self, dump):
@check_auth('obj:list')
def dump(self):
iclass = self.classes[self.table]
return RecordSet(self._native(iclass.dump(self)))
return RecordSet(
self._native(iclass.dump(self)),
config={
'recordset_pipe': self.ndb.config.get(
'recordset_pipe', 'false'
)
},
)

@cli.show_result
@check_auth('obj:list')
def summary(self):
iclass = self.classes[self.table]
return RecordSet(self._native(iclass.summary(self)))
return RecordSet(
self._native(iclass.summary(self)),
config={
'recordset_pipe': self.ndb.config.get(
'recordset_pipe', 'false'
)
},
)

def __repr__(self):
if self.chain and 'ifname' in self.chain:
Expand Down

0 comments on commit 0888fde

Please sign in to comment.