Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions bblfsh/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import literal_eval
import os
import sys
import importlib
Expand Down Expand Up @@ -63,6 +64,51 @@ def parse(self, filename, language=None, contents=None, timeout=None):
language=self._scramble_language(language))
return self._stub.Parse(request, timeout=timeout)

def native_parse(self, filename, language=None, contents=None, timeout=None):
"""
Queries the Babelfish server and receives the native AST response for the specified
file.

:param filename: The path to the file. Can be arbitrary if contents \
is not None.
:param language: The programming language of the file. Refer to \
https://doc.bblf.sh/languages.html for the list of \
currently supported languages. None means autodetect.
:param contents: The contents of the file. IF None, it is read from \
filename.
:param timeout: The request timeout in seconds.
:type filename: str
:type language: str
:type contents: str
:type timeout: float
:return: Native AST object.
"""

NativeParseRequest = importlib.import_module(
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
).NativeParseRequest

if contents is None:
with open(filename, "rb") as fin:
contents = fin.read()
request = NativeParseRequest(filename=os.path.basename(filename),
content=contents,
language=self._scramble_language(language))
return self._stub.NativeParse(request, timeout=timeout)

def version(self):
"""
Queries the Babelfish server for version and runtime information.

:return: A dictionary with the keys "version" for the semantic version and
"build" for the build timestamp.
"""
VersionRequest = importlib.import_module(
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
).VersionRequest

return self._stub.Version(VersionRequest())

@staticmethod
def _scramble_language(lang):
if lang is None:
Expand Down
11 changes: 11 additions & 0 deletions bblfsh/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ def tearDownClass(cls):
def setUp(self):
self.client = BblfshClient("0.0.0.0:9432")

def testVersion(self):
version = self.client.version()
assert(hasattr(version, "version"))
assert(version.version)
assert(hasattr(version, "build"))
assert(version.build)

def testNativeParse(self):
reply = self.client.native_parse(__file__)
assert(reply.ast)

def testUASTDefaultLanguage(self):
uast = self.client.parse(__file__)
self._validate_uast(uast)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def main():
},
name="bblfsh",
description="Fetches Universal Abstract Syntax Trees from Babelfish.",
version="2.3.1",
version="2.4.0",
license="Apache 2.0",
author="source{d}",
author_email="[email protected]",
Expand Down