|
| 1 | +from ast import literal_eval |
1 | 2 | import os |
2 | 3 | import sys |
3 | 4 | import importlib |
@@ -63,6 +64,51 @@ def parse(self, filename, language=None, contents=None, timeout=None): |
63 | 64 | language=self._scramble_language(language)) |
64 | 65 | return self._stub.Parse(request, timeout=timeout) |
65 | 66 |
|
| 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 | + |
66 | 112 | @staticmethod |
67 | 113 | def _scramble_language(lang): |
68 | 114 | if lang is None: |
|
0 commit comments