Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 30cef85

Browse files
authored
Merge pull request #58 from juanjux/feature/aliases
Adds imported symbols deep from the protofiles.
2 parents d3c59ce + 0480339 commit 30cef85

File tree

5 files changed

+95
-44
lines changed

5 files changed

+95
-44
lines changed

bblfsh/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
from bblfsh.client import BblfshClient
22
from bblfsh.pyuast import filter
3+
from bblfsh.aliases import *
4+
5+
# "in" is a reserved keyword in Python thus can't be used as package name, so
6+
# we import by string
7+
8+
9+
class RoleSearchException(Exception):
10+
pass
11+
12+
13+
def role_id(role_name):
14+
try:
15+
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[role_name].number
16+
except KeyError:
17+
raise RoleSearchException("Role with name '{}' not found".format(role_name))
18+
19+
return name
20+
21+
22+
def role_name(role_id):
23+
try:
24+
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[role_id].name
25+
except KeyError:
26+
raise RoleSearchException("Role with ID '{}' not found".format(role_id))
27+
28+
return id_

bblfsh/aliases.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
__all__ = ["DESCRIPTOR", "Node", "ParseResponse", "NativeParseResponse",
2+
"ParseRequest", "NativeParseRequest", "VersionRequest",
3+
"ProtocolServiceStub"]
4+
5+
import importlib
6+
import os
7+
import sys
8+
9+
from bblfsh.sdkversion import VERSION
10+
11+
# The following two insertions fix the broken pb import paths
12+
sys.path.insert(0, os.path.join(os.path.dirname(__file__),
13+
"gopkg/in/bblfsh/sdk/%s/protocol" % VERSION))
14+
sys.path.insert(0, os.path.dirname(__file__))
15+
16+
# "in" is a reserved keyword in Python thus can't be used as package name, so
17+
# we import by string
18+
19+
DESCRIPTOR = importlib.import_module(
20+
"bblfsh.gopkg.in.bblfsh.sdk.%s.uast.generated_pb2" % VERSION).DESCRIPTOR
21+
22+
Node = importlib.import_module(
23+
"bblfsh.gopkg.in.bblfsh.sdk.%s.uast.generated_pb2" % VERSION).Node
24+
25+
ParseResponse = importlib.import_module(
26+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION).ParseResponse
27+
28+
NativeParseResponse = importlib.import_module(
29+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
30+
).NativeParseResponse
31+
32+
ParseRequest = importlib.import_module(
33+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION).ParseRequest
34+
35+
NativeParseRequest = importlib.import_module(
36+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
37+
).NativeParseRequest
38+
39+
VersionRequest = importlib.import_module(
40+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
41+
).VersionRequest
42+
43+
ProtocolServiceStub = importlib.import_module(
44+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2_grpc" % VERSION
45+
).ProtocolServiceStub

bblfsh/client.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from ast import literal_eval
21
import os
32
import sys
4-
import importlib
53

64
import grpc
75

6+
from bblfsh.aliases import ParseRequest, NativeParseRequest, VersionRequest, ProtocolServiceStub
87
from bblfsh.sdkversion import VERSION
98

109
# The following two insertions fix the broken pb import paths
@@ -26,10 +25,6 @@ def __init__(self, endpoint):
2625
for example "0.0.0.0:9432"
2726
:type endpoint: str
2827
"""
29-
ProtocolServiceStub = importlib.import_module(
30-
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2_grpc" % VERSION
31-
).ProtocolServiceStub
32-
3328
self._channel = grpc.insecure_channel(endpoint)
3429
self._stub = ProtocolServiceStub(self._channel)
3530

@@ -52,9 +47,6 @@ def parse(self, filename, language=None, contents=None, timeout=None):
5247
:type timeout: float
5348
:return: UAST object.
5449
"""
55-
ParseRequest = importlib.import_module(
56-
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
57-
).ParseRequest
5850

5951
if contents is None:
6052
with open(filename, "rb") as fin:
@@ -84,10 +76,6 @@ def native_parse(self, filename, language=None, contents=None, timeout=None):
8476
:return: Native AST object.
8577
"""
8678

87-
NativeParseRequest = importlib.import_module(
88-
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
89-
).NativeParseRequest
90-
9179
if contents is None:
9280
with open(filename, "rb") as fin:
9381
contents = fin.read()
@@ -103,10 +91,6 @@ def version(self):
10391
:return: A dictionary with the keys "version" for the semantic version and
10492
"build" for the build timestamp.
10593
"""
106-
VersionRequest = importlib.import_module(
107-
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
108-
).VersionRequest
109-
11094
return self._stub.Version(VersionRequest())
11195

11296
@staticmethod

bblfsh/test.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import unittest
2-
import importlib
32

43
import docker
54

6-
from bblfsh import BblfshClient, filter
5+
from bblfsh import BblfshClient, filter, role_id, role_name, Node, ParseResponse
76
from bblfsh.launcher import ensure_bblfsh_is_running
8-
from bblfsh.sdkversion import VERSION
9-
10-
# "in" is a reserved keyword in Python thus can't be used as package name, so
11-
# we import by string
12-
ParseResponse = importlib.import_module(
13-
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION).ParseResponse
14-
Node = importlib.import_module(
15-
"bblfsh.gopkg.in.bblfsh.sdk.%s.uast.generated_pb2" % VERSION).Node
167

178

189
class BblfshTests(unittest.TestCase):
@@ -44,19 +35,17 @@ def testNativeParse(self):
4435
assert(reply.ast)
4536

4637
def testUASTDefaultLanguage(self):
47-
uast = self.client.parse(__file__)
48-
self._validate_uast(uast)
38+
self._validate_resp(self.client.parse(__file__))
4939

5040
def testUASTPython(self):
51-
uast = self.client.parse(__file__, language="Python")
52-
self._validate_uast(uast)
41+
self._validate_resp(self.client.parse(__file__, language="Python"))
5342

5443
def testUASTFileContents(self):
5544
with open(__file__, "rb") as fin:
5645
contents = fin.read()
57-
uast = self.client.parse("file.py", contents=contents)
58-
self._validate_uast(uast)
59-
self._validate_filter(uast)
46+
resp = self.client.parse("file.py", contents=contents)
47+
self._validate_resp(resp)
48+
self._validate_filter(resp)
6049

6150
def testBrokenFilter(self):
6251
self.assertRaises(RuntimeError, filter, 0, "foo")
@@ -123,18 +112,25 @@ def testFilterEndCol(self):
123112
self.assertTrue(any(filter(node, "//*[@endCol=50]")))
124113
self.assertFalse(any(filter(node, "//*[@endCol=5]")))
125114

126-
def _validate_uast(self, uast):
127-
self.assertIsNotNone(uast)
128-
# self.assertIsInstance() does not work - must be some metaclass magic
129-
self.assertEqual(type(uast).DESCRIPTOR.full_name,
115+
def testRoleIdName(sedlf):
116+
assert(role_id(role_name(1)) == 1)
117+
assert(role_name(role_id("IDENTIFIER")) == "IDENTIFIER")
118+
119+
def _validate_resp(self, resp):
120+
self.assertIsNotNone(resp)
121+
self.assertEqual(type(resp).DESCRIPTOR.full_name,
130122
ParseResponse.DESCRIPTOR.full_name)
131-
self.assertEqual(len(uast.errors), 0)
132-
self.assertIsInstance(uast.uast, Node)
123+
self.assertEqual(len(resp.errors), 0)
124+
# self.assertIsInstance() does not work - must be some metaclass magic
125+
# self.assertIsInstance(resp.uast, Node)
126+
127+
# Sometimes its fully qualified, sometimes is just "Node"... ditto
128+
assert(resp.uast.__class__.__name__.endswith('Node'))
133129

134-
def _validate_filter(self, uast):
135-
results = filter(uast.uast, "//Import[@roleImport and @roleDeclaration]//alias")
130+
def _validate_filter(self, resp):
131+
results = filter(resp.uast, "//Import[@roleImport and @roleDeclaration]//alias")
136132
self.assertEqual(next(results).token, "unittest")
137-
self.assertEqual(next(results).token, "importlib")
133+
self.assertEqual(next(results).token, "docker")
138134

139135

140136
if __name__ == "__main__":

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def main():
133133
},
134134
name="bblfsh",
135135
description="Fetches Universal Abstract Syntax Trees from Babelfish.",
136-
version="2.5.1",
136+
version="2.6.0",
137137
license="Apache 2.0",
138138
author="source{d}",
139139
author_email="language-analysis@sourced.tech",

0 commit comments

Comments
 (0)