1+ """
2+ This file provides a compatibility layer with the old UAST V1 (or client-python
3+ v2) API. You can see a summary of that API here:
4+
5+ https://github.com/bblfsh/client-python/blob/d485273f457a174b40b820ad71195a739db04197/README.md
6+
7+ Note that this won't translate the XPath queries from the old projection to the new use;
8+ even when using this module you're expected to use expressions matching the new
9+ projection.
10+
11+ Note that since this is a pure Python translation layer, some performance
12+ impact is to be expected.
13+ """
114import os
215import sys
316from typing import Union , List , Any , Optional
2134
2235
2336class WrongTypeException (Exception ):
37+ """
38+ This exception is raised when the API receives an unexpected type
39+ """
2440 pass
2541
2642
2743class CompatParseResponse :
44+ """
45+ This class emulates the API of the old ParseResponse object.
46+ """
2847 def __init__ (self , ctx : ResultContext , filename : str = "" ) -> None :
2948 self ._res_context = ctx
3049 self ._filename = filename
3150
3251 @property
3352 def uast (self ) -> Node :
53+ """
54+ Returns the root Node.
55+ """
3456 return self ._res_context .uast
3557
3658 @property
3759 def ast (self ) -> Node :
60+ """
61+ Returns the root Node. This is provided for compatibility, but
62+ since the type of result is now expecified using CompatBblfshClient.parse
63+ or parse_native, it'll return the same as uast().
64+ """
3865 return self ._res_context .ast
3966
4067 @property
4168 def ctx (self ) -> ResultContext :
69+ """
70+ Returns the ResultContext of the response.
71+ """
4272 return self ._res_context
4373
4474 @property
4575 def elapsed (self ) -> int :
76+ """
77+ Provided for compatibility, but since the new API's ParseResponse doesn't
78+ provide an elapsed time it'll always return -1.
79+ """
4680 # FIXME(juanjux): check if the caller can get this, or measure it ourselves.
4781 return - 1
4882
4983 @property
5084 def language (self ) -> str :
85+ """
86+ Returns the language used for the request.
87+ """
5188 return self ._res_context .language
5289
5390 @property
5491 def filename (self ) -> str :
92+ """
93+ Returns the filename used for the request.
94+ """
5595 return self ._filename
5696
5797 @property
5898 def DESCRIPTOR (self ) -> Any :
99+ """
100+ Returns the gRPC context descriptor.
101+ """
59102 return self ._res_context .ctx .DESCRIPTOR
60103
61104 @property
62105 def errors (selfs ) -> List :
106+ """
107+ Provided for compatibility. Since the new API will raise exceptions on errors,
108+ this just returns and empty array.
109+ """
63110 # ParseResponse would have raised an exception on errors
64111 return []
65112
66113
67114class CompatBblfshClient :
115+ """
116+ This emulates the methods and properties of the old BblfshClient.
117+ """
68118 def __init__ (self , endpoint : Union [str , grpc .Channel ]) -> None :
119+ """
120+ Connects to the specified grpc endpoint which can be specified either as
121+ a grpc Channel object or a connection string (like "0.0.0.0:6432").
122+ """
69123 self ._bblfsh_cli = bcli .BblfshClient (endpoint )
70124
71125 self ._channel = self ._bblfsh_cli ._channel
@@ -86,28 +140,54 @@ def _parse(self, filename: str, language: str = None, contents: str = None,
86140 def parse (self , filename : str , language : str = None , contents : str = None ,
87141 timeout : float = None ) -> CompatParseResponse :
88142
143+ """
144+ Parse the specified filename or contents and return a CompatParseResponse.
145+ """
146+
89147 return self ._parse (filename , language , contents , timeout ,
90148 Mode .Value ('ANNOTATED' ))
91149
92150 def native_parse (self , filename : str , language : str = None ,
93151 contents : str = None ,
94152 timeout : float = None ) -> CompatParseResponse :
153+ """
154+ Same as parse() but the returned response will include only the native
155+ (non annotated) AST.
156+ """
95157
96158 return self ._parse (filename , language , contents , timeout ,
97159 Mode .Value ('NATIVE' ))
98160
99161 def supported_languages (self ) -> List [str ]:
162+ """
163+ Return a list of the languages that can be parsed by the connected
164+ endpoint (driver or bblfsh daemon).
165+ """
100166 return self ._bblfsh_cli .supported_languages ()
101167
102168 def version (self ) -> VersionResponse :
169+ """
170+ Returns the connected endpoint version.
171+ """
103172 return self ._bblfsh_cli .version ()
104173
105174 def close (self ) -> None :
175+ """
176+ Closes the connection to the endpoint.
177+ """
106178 return self ._bblfsh_cli .close ()
107179
108180
109181class CompatNodeIterator :
182+ """
183+ This emulates the API of the pre-v3 iterators.
184+ """
110185 def __init__ (self , nodeit : NodeIterator , only_nodes : bool = False ) -> None :
186+ """
187+ Creates a CompatNodeIterator compatibility object using a NodeIterator
188+ from the post-v3 API. If the only_nodes parameter is set to true,
189+ scalars and strings won't be included in the results.
190+ """
111191 self ._nodeit = nodeit
112192 self ._only_nodes = only_nodes
113193 # Used to forward calls of the old Node object
@@ -138,13 +218,19 @@ def __next__(self) -> Node:
138218 return ret_val
139219
140220 def filter (self , query : str ) -> Optional ['CompatNodeIterator' ]:
221+ """
222+ Further filter the results using this iterator as base.
223+ """
141224 if not self ._last_node :
142225 return None
143226
144227 return filter (self ._last_node , query )
145228
146229 @property
147230 def properties (self ) -> dict :
231+ """
232+ Returns the properties of the current node in the iteration.
233+ """
148234 if isinstance (self ._last_node , dict ):
149235 return self ._last_node .keys ()
150236 else :
@@ -153,6 +239,10 @@ def properties(self) -> dict:
153239
154240def iterator (n : Union [Node , CompatNodeIterator , dict ],
155241 order : TreeOrder = TreeOrder .PRE_ORDER ) -> CompatNodeIterator :
242+ """
243+ This function has the same signature as the pre-v3 iterator()
244+ call returning a compatibility CompatNodeIterator.
245+ """
156246
157247 if isinstance (n , CompatNodeIterator ):
158248 return CompatNodeIterator (n ._nodeit .iterate (order ), only_nodes = True )
@@ -169,15 +259,27 @@ def iterator(n: Union[Node, CompatNodeIterator, dict],
169259
170260
171261def filter (n : Node , query : str ) -> CompatNodeIterator :
262+ """
263+ This function has the same signature as the pre-v3 filter() returning a
264+ compatibility CompatNodeIterator.
265+ """
172266 ctx = uast ()
173267 return CompatNodeIterator (NodeIterator (ctx .filter (query , n .internal_node ), ctx ))
174268
175269
176270def filter_nodes (n : Node , query : str ) -> CompatNodeIterator :
271+ """
272+ Utility function. Same as filter() but will only filter for nodes (i. e.
273+ it will exclude scalars and positions).
274+ """
177275 return CompatNodeIterator (filter (n , query )._nodeit , only_nodes = True )
178276
179277
180278class TypedQueryException (Exception ):
279+ """
280+ This exception will be raised when a query for a specific type (str, int, float...)
281+ returns a different type of more than one result.
282+ """
181283 pass
182284
183285
@@ -203,18 +305,30 @@ def _scalariter2item(n: Node, query: str, wanted_type: type) -> Any:
203305
204306
205307def filter_string (n : Node , query : str ) -> str :
308+ """
309+ Filter and ensure that the returned value is of string type.
310+ """
206311 return _scalariter2item (n , query , str )
207312
208313
209314def filter_bool (n : Node , query : str ) -> bool :
315+ """
316+ Filter and ensure that the returned value is of type bool.
317+ """
210318 return _scalariter2item (n , query , bool )
211319
212320
213321def filter_int (n : Node , query : str ) -> int :
322+ """
323+ Filter and ensure that the returned value is of type int.
324+ """
214325 return _scalariter2item (n , query , int )
215326
216327
217328def filter_float (n : Node , query : str ) -> float :
329+ """
330+ Filter and ensure that the returned value is of type int.
331+ """
218332 return _scalariter2item (n , query , float )
219333
220334
0 commit comments