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

Commit 57783e4

Browse files
authored
Merge pull request #54 from juanjux/feature/version_parsenative
Added "native_parse" and "version" requests.
2 parents b64ff3a + a0f5bd8 commit 57783e4

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

bblfsh/client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ast import literal_eval
12
import os
23
import sys
34
import importlib
@@ -63,6 +64,51 @@ def parse(self, filename, language=None, contents=None, timeout=None):
6364
language=self._scramble_language(language))
6465
return self._stub.Parse(request, timeout=timeout)
6566

67+
def native_parse(self, filename, language=None, contents=None, timeout=None):
68+
"""
69+
Queries the Babelfish server and receives the native AST response for the specified
70+
file.
71+
72+
:param filename: The path to the file. Can be arbitrary if contents \
73+
is not None.
74+
:param language: The programming language of the file. Refer to \
75+
https://doc.bblf.sh/languages.html for the list of \
76+
currently supported languages. None means autodetect.
77+
:param contents: The contents of the file. IF None, it is read from \
78+
filename.
79+
:param timeout: The request timeout in seconds.
80+
:type filename: str
81+
:type language: str
82+
:type contents: str
83+
:type timeout: float
84+
:return: Native AST object.
85+
"""
86+
87+
NativeParseRequest = importlib.import_module(
88+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
89+
).NativeParseRequest
90+
91+
if contents is None:
92+
with open(filename, "rb") as fin:
93+
contents = fin.read()
94+
request = NativeParseRequest(filename=os.path.basename(filename),
95+
content=contents,
96+
language=self._scramble_language(language))
97+
return self._stub.NativeParse(request, timeout=timeout)
98+
99+
def version(self):
100+
"""
101+
Queries the Babelfish server for version and runtime information.
102+
103+
:return: A dictionary with the keys "version" for the semantic version and
104+
"build" for the build timestamp.
105+
"""
106+
VersionRequest = importlib.import_module(
107+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
108+
).VersionRequest
109+
110+
return self._stub.Version(VersionRequest())
111+
66112
@staticmethod
67113
def _scramble_language(lang):
68114
if lang is None:

bblfsh/test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ def tearDownClass(cls):
3232
def setUp(self):
3333
self.client = BblfshClient("0.0.0.0:9432")
3434

35+
def testVersion(self):
36+
version = self.client.version()
37+
assert(hasattr(version, "version"))
38+
assert(version.version)
39+
assert(hasattr(version, "build"))
40+
assert(version.build)
41+
42+
def testNativeParse(self):
43+
reply = self.client.native_parse(__file__)
44+
assert(reply.ast)
45+
3546
def testUASTDefaultLanguage(self):
3647
uast = self.client.parse(__file__)
3748
self._validate_uast(uast)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def main():
137137
},
138138
name="bblfsh",
139139
description="Fetches Universal Abstract Syntax Trees from Babelfish.",
140-
version="2.3.1",
140+
version="2.4.0",
141141
license="Apache 2.0",
142142
author="source{d}",
143143
author_email="language-analysis@sourced.tech",

0 commit comments

Comments
 (0)