1- from typing import List , Dict , Optional , Tuple , Set
2- import pandas as pd
1+ from typing import Dict , List , Optional , Set , Tuple
32
43from nubia .internal import context
4+
55from suzieq .shared .context import SqContext
6+ from suzieq .shared .utils import lru_cache , timed_lru_cache
67from suzieq .sqobjects import get_sqobject , get_tables
7- from suzieq .shared .utils import timed_lru_cache , lru_cache
8-
98
109virt_tbl_cmd_mapping : Dict [str , str ] = {
1110 'endpoint' : 'device' ,
1514 'route' : 'routes' ,
1615 'mac' : 'macs' ,
1716 'interface' : 'interfaces' ,
18- 'assert' : 'device' , # only for namespace/hostname
19- 'query' : 'device' , # only for namespace/hostname
2017}
2118
2219
@@ -39,11 +36,7 @@ def completion_get_data(cmd: str, column: str,
3936 if isinstance (v , tuple ):
4037 kwargs [k ] = list (v )
4138
42- if cmd in ["assert" , "query" ]:
43- df = get_sqobject (cmd )(context = ctxt ).get (columns = [column ], ** kwargs )
44- if not df .empty and 'error' not in df .columns :
45- result = set (df [column ].unique ())
46- elif cmd == "extdb" :
39+ if cmd == "extdb" :
4740 result = set ()
4841 # TODO: implement autocompletion with external tables
4942 # ext_table = kwargs.pop('ext_table', None)
@@ -56,33 +49,8 @@ def completion_get_data(cmd: str, column: str,
5649 return result
5750
5851
59- @timed_lru_cache (60 )
60- def get_assert_query_completions (cmd : str , column : str ,
61- ** kwargs ) -> pd .DataFrame :
62- '''Get completion for assert/query which return a dict'''
63-
64- nubia_ctxt = context .get_context ()
65- if not nubia_ctxt :
66- return pd .DataFrame ({column : []})
67-
68- ctxt : SqContext = nubia_ctxt .ctxt
69-
70- # kwargs are passed with values as tuples, not lists which
71- # causes a bunch of problems in asserts. So, convert tuples
72- # to lists again
73- for k , v in kwargs .items ():
74- if isinstance (v , tuple ):
75- kwargs [k ] = list (v )
76- try :
77- df = get_sqobject (cmd )(context = ctxt ) \
78- .get (columns = [column ], ** kwargs )
79- return df
80- except Exception :
81- return pd .DataFrame ({column : []})
82-
83-
8452@lru_cache
85- def completion_get_columns (cmd : str , component : Optional [str ] = None
53+ def completion_get_columns (cmd : str , component : Optional [str ] = None ,
8654 ) -> List [str ]:
8755 '''Return columns associated with a table for unique/top/assert'''
8856 ctxt : SqContext = context .get_context ().ctxt
@@ -115,7 +83,6 @@ def return_completions(cmd, column, val_so_far, quote_result=False,
11583 ** kwargs ) -> List [str ]:
11684 '''Given a cmd and column, return valid completion list'''
11785
118- result = []
11986 try :
12087 valid_set = completion_get_data (cmd , column , ** kwargs )
12188 except Exception :
@@ -174,67 +141,9 @@ def hostname_completer(cmd: str, _subcmd: str, last_token: str,
174141
175142 kwargs = get_kwargs_so_far (raw_cmd , ['namespace' ])
176143
177- cmd = virt_tbl_cmd_mapping .get (cmd , cmd )
178-
179144 return return_completions ('device' , 'hostname' , hostname_so_far , ** kwargs )
180145
181146
182- def assert_name_completer (_cmd : str , _ : str ,
183- last_token : str ,
184- raw_cmd : str ) -> List [str ]:
185- '''Return the completion for asserts'''
186-
187- asrt_so_far = None
188-
189- if isinstance (last_token , str ):
190- asrt_so_far = last_token .split ('=' )[- 1 ]
191-
192- kwargs = get_kwargs_so_far (raw_cmd , ['workflow' ])
193-
194- return return_completions ('assert' , 'name' , asrt_so_far , ** kwargs )
195-
196-
197- def assert_vars_name_completer (_cmd : str , _ : str ,
198- last_token : str ,
199- _raw_cmd : str ) -> List [str ]:
200- """assert_vars completition"""
201- vars_so_far = None
202-
203- if isinstance (last_token , str ):
204- vars_so_far = last_token .split ('=' )[- 1 ]
205-
206- kwargs = {'assert_vars' : tuple (['*' ])}
207-
208- return return_completions ('assert' , 'name' , vars_so_far , ** kwargs )
209-
210-
211- def query_vars_name_completer (_cmd : str , _ : str , last_token : str ,
212- _raw_cmd : str ) -> List [str ]:
213- """query_vars completitions"""
214- vars_so_far : Optional [str ] = None
215-
216- if isinstance (last_token , str ):
217- vars_so_far = last_token .split ('=' )[- 1 ]
218-
219- kwargs = {'query_vars' : tuple (['*' ])}
220-
221- return return_completions ('query' , 'name' , vars_so_far , ** kwargs )
222-
223-
224- def workflow_name_completer (_cmd : str , _subcmd : str , last_token : str ,
225- _raw_cmd : str ) -> List [str ]:
226- '''Return the completion for workflows'''
227-
228- wf_so_far = None
229-
230- if isinstance (last_token , str ):
231- wf_so_far = last_token .split ('=' )[- 1 ]
232-
233- kwargs = {'workflow' : tuple (['*' ])}
234-
235- return return_completions ('assert' , 'name' , wf_so_far , ** kwargs )
236-
237-
238147def column_name_completer (cmd : str , subcmd : str , last_token : str ,
239148 raw_cmd : str ) -> List [str ]:
240149 '''Return completions for column name'''
@@ -250,8 +159,7 @@ def column_name_completer(cmd: str, subcmd: str, last_token: str,
250159 [None ])[0 ]
251160 }
252161 else :
253- ext_table = get_kwargs_so_far (raw_cmd , ['table' ])
254- kwargs = {'ext_table' : (ext_table .get ('table' , [None ]) or [None ])[0 ]}
162+ kwargs = {}
255163
256164 if (subcmd in ['unique' , 'top' , 'assert' ]
257165 and cmd not in ["endpoint" , "assert" ]):
@@ -291,7 +199,7 @@ def service_completer(_cmd: str, _subcmd: str, last_token: str,
291199
292200 svcs = set ({x for x in get_tables ()
293201 if x not in ['path' , 'topology' , 'namespace' , 'endpoint' ,
294- 'topcpu' , 'topmem' , 'assert' , 'query' ,
202+ 'topcpu' , 'topmem' ,
295203 'extdb' ]})
296204 if svcs :
297205 if svc_so_far :
0 commit comments