This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from dennwc/v3
Python client v3 (UASTv2)
- Loading branch information
Showing
17 changed files
with
1,970 additions
and
844 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,26 @@ | ||
from bblfsh.client import BblfshClient | ||
from bblfsh.pyuast import filter, filter_bool, filter_number, filter_string, iterator | ||
from bblfsh.pyuast import decode, iterator, uast | ||
from bblfsh.tree_order import TreeOrder | ||
from bblfsh.aliases import * | ||
|
||
class TreeOrder: | ||
PRE_ORDER = 0 | ||
POST_ORDER = 1 | ||
LEVEL_ORDER = 2 | ||
POSITION_ORDER = 3 | ||
|
||
# "in" is a reserved keyword in Python thus can't be used as package name, so | ||
# we import by string | ||
|
||
class RoleSearchException(Exception): | ||
pass | ||
|
||
|
||
def role_id(role_name: str) -> int: | ||
def role_id(rname: str) -> int: | ||
try: | ||
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[role_name].number | ||
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[rname].number | ||
except KeyError: | ||
raise RoleSearchException("Role with name '{}' not found".format(role_name)) | ||
raise RoleSearchException("Role with name '{}' not found".format(rname)) | ||
|
||
return name | ||
|
||
|
||
def role_name(role_id: int) -> str: | ||
def role_name(rid: int) -> str: | ||
try: | ||
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[role_id].name | ||
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[rid].name | ||
except KeyError: | ||
raise RoleSearchException("Role with ID '{}' not found".format(role_id)) | ||
raise RoleSearchException("Role with ID '{}' not found".format(rid)) | ||
|
||
return id_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,62 @@ | ||
import argparse | ||
import pprint | ||
import sys | ||
|
||
import bblfsh | ||
from bblfsh.pyuast import filter | ||
|
||
from bblfsh.client import BblfshClient | ||
from bblfsh.launcher import ensure_bblfsh_is_running | ||
|
||
|
||
def setup(): | ||
def setup() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser( | ||
description="Query for a UAST to Babelfish and dump it to stdout." | ||
) | ||
parser.add_argument("-e", "--endpoint", default="0.0.0.0:9432", | ||
help="bblfsh gRPC endpoint.") | ||
help="bblfsh gRPC endpoint.", type=str) | ||
parser.add_argument("-f", "--file", required=True, | ||
help="File to parse.") | ||
help="File to parse.", type=str) | ||
parser.add_argument("-l", "--language", default=None, | ||
help="File's language. The default is to autodetect.") | ||
help="File's language. The default is to autodetect.", type=str) | ||
parser.add_argument("--disable-bblfsh-autorun", action="store_true", | ||
help="Do not automatically launch Babelfish server " | ||
"if it is not running.") | ||
|
||
parser.add_argument("-q", "--query", default="", help="xpath query") | ||
parser.add_argument("-m", "--mapn", default="", help="transform function of the results (n)") | ||
parser.add_argument("-a", "--array", help='print results as an array', action='store_true') | ||
parser.add_argument("-q", "--query", default="", help="xpath query", type=str) | ||
parser.add_argument("-a", "--array", help='print results as a parseable Python array', action='store_true') | ||
|
||
args = parser.parse_args() | ||
return args | ||
return parser.parse_args() | ||
|
||
def run_query(root: bblfsh.Node, query: str, mapn: str, as_array: bool) -> None: | ||
result = list(filter(root, query)) | ||
|
||
if not result: | ||
def run_query(uast, query: str, array: bool) -> None: | ||
result_iter = uast.filter(query) | ||
if not result_iter: | ||
print("Nothing found") | ||
|
||
else: | ||
if mapn: | ||
result = [eval(mapn) for n in result] | ||
result_list = [x.load() for x in result_iter] | ||
|
||
if as_array: | ||
print("results[{}] = {}".format(len(result), result)) | ||
else: | ||
print("Running xpath query: {}".format(query)) | ||
print("FOUND {} roots".format(len(result))) | ||
if array: | ||
pprint.pprint(result_list) | ||
else: | ||
print("%d Results:" % len(result_list)) | ||
for i, node in enumerate(result_list): | ||
print("== {} ==================================".format(i+1)) | ||
print(node) | ||
|
||
for i, node in enumerate(result): | ||
print("== {} ==================================".format(i+1)) | ||
print(node) | ||
|
||
def main(): | ||
def main() -> int: | ||
args = setup() | ||
if not args.disable_bblfsh_autorun: | ||
ensure_bblfsh_is_running() | ||
|
||
client = BblfshClient(args.endpoint) | ||
response = client.parse(args.file, args.language) | ||
root = response.uast | ||
if len(response.errors): | ||
sys.stderr.write("\n".join(response.errors) + "\n") | ||
query = args.query | ||
if query: | ||
run_query(root, query, args.mapn, args.array) | ||
ctx = client.parse(args.file, args.language) | ||
|
||
if args.query: | ||
run_query(ctx, args.query, array=args.array) | ||
else: | ||
print(root) | ||
pprint.pprint(ctx.load()) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,39 @@ | ||
__all__ = ["DESCRIPTOR", "Node", "Position", "ParseResponse", "NativeParseResponse", | ||
"ParseRequest", "NativeParseRequest", "VersionRequest", "ProtocolServiceStub"] | ||
|
||
import importlib | ||
|
||
from bblfsh.sdkversion import VERSION | ||
import google | ||
|
||
# "in" is a reserved keyword in Python thus can't be used as package name, so | ||
# we import by string | ||
uast_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.%s.uast.generated_pb2" % VERSION) | ||
protocol_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION) | ||
protocol_grpc_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2_grpc" % VERSION) | ||
uast_v2_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.v2.uast.generated_pb2") | ||
protocol_v2_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.v2.protocol.generated_pb2") | ||
protocol_grpc_v2_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.v2.protocol.generated_pb2_grpc") | ||
protocol_v1_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.v1.protocol.generated_pb2") | ||
protocol_grpc_v1_module = importlib.import_module( | ||
"bblfsh.gopkg.in.bblfsh.sdk.v1.protocol.generated_pb2_grpc") | ||
|
||
DESCRIPTOR = uast_v2_module.DESCRIPTOR | ||
ParseRequest = protocol_v2_module.ParseRequest | ||
ParseResponse = protocol_v2_module.ParseResponse | ||
ParseError = protocol_v2_module.ParseError | ||
Mode = protocol_v2_module.Mode | ||
ModeType = google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper | ||
|
||
|
||
class Modes: | ||
pass | ||
|
||
# Current values: {'DEFAULT_MODE': 0, 'NATIVE': 1, 'PREPROCESSED': 2, 'ANNOTATED': 4, 'SEMANTIC': 8} | ||
for k, v in Mode.DESCRIPTOR.values_by_name.items(): | ||
setattr(Modes, k, v.number) | ||
|
||
DriverStub = protocol_grpc_v2_module.DriverStub | ||
DriverServicer = protocol_grpc_v2_module.DriverServicer | ||
|
||
DESCRIPTOR = uast_module.DESCRIPTOR | ||
Node = uast_module.Node | ||
Position = uast_module.Position | ||
ParseResponse = protocol_module.ParseResponse | ||
NativeParseResponse = protocol_module.NativeParseResponse | ||
ParseRequest = protocol_module.ParseRequest | ||
NativeParseRequest = protocol_module.NativeParseRequest | ||
VersionRequest = protocol_module.VersionRequest | ||
SupportedLanguagesRequest = protocol_module.SupportedLanguagesRequest | ||
SupportedLanguagesResponse = protocol_module.SupportedLanguagesResponse | ||
ProtocolServiceStub = protocol_grpc_module.ProtocolServiceStub | ||
VersionRequest = protocol_v1_module.VersionRequest | ||
VersionResponse = protocol_v1_module.VersionResponse | ||
SupportedLanguagesRequest = protocol_v1_module.SupportedLanguagesRequest | ||
SupportedLanguagesResponse = protocol_v1_module.SupportedLanguagesResponse | ||
ProtocolServiceStub = protocol_grpc_v1_module.ProtocolServiceStub |
Oops, something went wrong.