Skip to content

Commit 1dda243

Browse files
committed
clean up
1 parent 63aface commit 1dda243

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

tools/generate_definitions.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@
77
import httpx
88

99
CAPITALIZATION_EXCEPTIONS = {
10-
"NFTOKEN": "NFToken",
11-
"URITOKEN": "URIToken",
12-
"URI": "URI",
13-
"UNL": "UNL",
1410
"XCHAIN": "XChain",
15-
"DID": "DID",
16-
"ID": "ID",
17-
"AMM": "AMM",
1811
}
1912

2013
if len(sys.argv) != 2 and len(sys.argv) != 3:
@@ -66,22 +59,21 @@ def _translate(inp: str) -> str:
6659
return inp.replace("UINT", "Hash")
6760
else:
6861
return inp.replace("UINT", "UInt")
69-
if inp == "OBJECT" or inp == "ARRAY":
70-
return "ST" + inp[0:1].upper() + inp[1:].lower()
71-
if inp == "ACCOUNT":
72-
return "AccountID"
73-
if inp == "LEDGERENTRY":
74-
return "LedgerEntry"
75-
if inp == "NOTPRESENT":
76-
return "NotPresent"
77-
if inp == "PATHSET":
78-
return "PathSet"
79-
if inp == "VL":
80-
return "Blob"
81-
if inp == "DIR_NODE":
82-
return "DirectoryNode"
83-
if inp == "PAYCHAN":
84-
return "PayChannel"
62+
63+
non_standard_renames = {
64+
"OBJECT": "STObject",
65+
"ARRAY": "STArray",
66+
"AMM": "AMM",
67+
"ACCOUNT": "AccountID",
68+
"LEDGERENTRY": "LedgerEntry",
69+
"NOTPRESENT": "NotPresent",
70+
"PATHSET": "PathSet",
71+
"VL": "Blob",
72+
"DIR_NODE": "DirectoryNode",
73+
"PAYCHAN": "PayChannel",
74+
}
75+
if inp in non_standard_renames:
76+
return non_standard_renames[inp]
8577

8678
parts = inp.split("_")
8779
result = ""
@@ -96,6 +88,7 @@ def _translate(inp: str) -> str:
9688
output = ""
9789

9890

91+
# add a new line of content to the output
9992
def _add_line(line: str) -> None:
10093
global output
10194
output += line + "\n"
@@ -173,13 +166,17 @@ def _add_line(line: str) -> None:
173166
],"""
174167
)
175168

169+
# Parse STypes
170+
# Example line:
171+
# STYPE(STI_UINT32, 2) \
176172
type_hits = re.findall(
177173
r"^ *STYPE\(STI_([^ ]*?) *, *([0-9-]+) *\) *\\?$", sfield_h, re.MULTILINE
178174
)
179175
if len(type_hits) == 0:
180176
type_hits = re.findall(
181177
r"^ *STI_([^ ]*?) *= *([0-9-]+) *,?$", sfield_h, re.MULTILINE
182178
)
179+
# name-to-value map - needed for SField processing
183180
type_map = {x[0]: x[1] for x in type_hits}
184181

185182

@@ -206,6 +203,9 @@ def _is_signing_field(t: str, not_signing_field: str) -> str:
206203

207204

208205
# Parse SField.cpp for all the SFields and their serialization info
206+
# Example lines:
207+
# TYPED_SFIELD(sfFee, AMOUNT, 8)
208+
# UNTYPED_SFIELD(sfSigners, ARRAY, 3, SField::sMD_Default, SField::notSigning)
209209
sfield_hits = re.findall(
210210
r"^ *[A-Z]*TYPED_SFIELD *\( *sf([^,\n]*),[ \n]*([^, \n]+)[ \n]*,[ \n]*"
211211
r"([0-9]+)(,.*?(notSigning))?",
@@ -251,6 +251,9 @@ def _unhex(x: str) -> str:
251251
return x
252252

253253

254+
# Parse ledger entries
255+
# Example line:
256+
# LEDGER_ENTRY(ltNFTOKEN_OFFER, 0x0037, NFTokenOffer, nft_offer, ({
254257
lt_hits = re.findall(
255258
r"^ *LEDGER_ENTRY[A-Z_]*\(lt[A-Z_]+ *, *([x0-9a-f]+) *, *([^,]+), *([^,]+), \({$",
256259
ledger_entries_file,
@@ -274,6 +277,7 @@ def _unhex(x: str) -> str:
274277
_add_line(' "TRANSACTION_RESULTS": {')
275278
ter_h = str(ter_h).replace("[[maybe_unused]]", "")
276279

280+
# Parse TER codes
277281
ter_code_hits = re.findall(
278282
r"^ *((tel|tem|tef|ter|tes|tec)[A-Z_]+)( *= *([0-9-]+))? *,? *(\/\/[^\n]*)?$",
279283
ter_h,
@@ -282,16 +286,18 @@ def _unhex(x: str) -> str:
282286
ter_codes = []
283287
upto = -1
284288

289+
# Get the exact values of the TER codes and sort them
285290
for x in range(len(ter_code_hits)):
286291
if ter_code_hits[x][3] != "":
287292
upto = int(ter_code_hits[x][3])
288293
ter_codes.append((ter_code_hits[x][0], upto))
289294

290295
upto += 1
291-
292296
ter_codes.sort(key=lambda x: x[0])
297+
293298
current_type = ""
294299
for x in range(len(ter_codes)):
300+
# print newline between the different code types
295301
if current_type == "":
296302
current_type = ter_codes[x][0][:3]
297303
elif current_type != ter_codes[x][0][:3]:
@@ -313,13 +319,17 @@ def _unhex(x: str) -> str:
313319
########################################################################
314320
_add_line(' "TRANSACTION_TYPES": {')
315321

322+
# Parse transaction types
323+
# Example line:
324+
# TRANSACTION(ttCHECK_CREATE, 16, CheckCreate, ({
316325
tx_hits = re.findall(
317326
r"^ *TRANSACTION\(tt[A-Z_]+ *,* ([0-9]+) *, *([A-Za-z]+).*$",
318327
transactions_file,
319328
re.MULTILINE,
320329
)
321330
tx_hits.append(("-1", "Invalid"))
322331
tx_hits.sort(key=lambda x: x[1])
332+
323333
for x in range(len(tx_hits)):
324334
_add_line(
325335
' "'

0 commit comments

Comments
 (0)