-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy paththrift_demos.py
72 lines (66 loc) · 2.89 KB
/
thrift_demos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from collections import Counter
from os.path import join, dirname, getmtime
from polyglot_piranha import Rule, RuleGraph, execute_piranha, PiranhaArguments, Filter
import logging
from logging import info
find_Replace_dir = join(dirname(__file__), "find_replace")
def thrift_demo():
file_path = join(find_Replace_dir, "thrift", "sample.thrift")
r1 = Rule(
name="Match Exception Definition",
query="""(
(exception_definition (identifier) @exception_name) @exception_definition
(#match? @exception_name "@input_exception_name")
)""",
replace_node="exception_definition",
replace="""@exception_definition(
rpc.code = "@rpc_code"
)""",
filters={
Filter(matcher="(exception_definition) @c_e",
not_contains=["(annotation_definition) @ad"])
},
holes={
"input_exception_name", "rpc_code"
}
)
error_code_mapping = {"Invalid": "INVALID_ARGUMENT",
"BadRequest": "INVALID_ARGUMENT",
"UserNotAllowed": "INVALID_ARGUMENT",
"Precondition": "FAILED_PRECONDITION",
"Range": "OUT_OF_RANGE",
"Unauthenticated": "UNAUTHENTICATED",
"Permission": "PERMISSION_DENIED",
"NotFound": "NOT_FOUND",
"DoesNotExist": "NOT_FOUND",
"Aborted": "ABORTED",
"Exists": "ALREADY_EXISTS",
"Exhausted": "RESOURCE_EXHAUSTED",
"Cancelled": "CANCELLED",
"DataLoss": "DATA_LOSS",
"UnknownError": "UNKNOWN",
"UnknownException": "UNKNOWN",
"InternalError": "INTERNAL",
"InternalException": "INTERNAL",
"ServiceError": "INTERNAL",
"ServiceException": "INTERNAL",
"ServerError": "INTERNAL",
"ServerException": "INTERNAL",
"Unimplemented": "UNIMPLEMENTED",
"Unavailable": "UNAVAILABLE",
"DeadlineExceeded": "DEADLINE_EXCEEDED",
}
for k, v in error_code_mapping.items():
args = PiranhaArguments(
language="thrift",
paths_to_codebase=[file_path],
rule_graph=RuleGraph(rules=[r1], edges=[]),
substitutions={"input_exception_name": k, "rpc_code": v}
)
output = execute_piranha(args)
print(output)
FORMAT = "%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.INFO)
thrift_demo()
print("Completed running Thrift refactoring")