|
|
@@ -1,64 +1,131 @@
|
|
|
#!/usr/bin/env python3
|
|
|
-from sys import argv
|
|
|
+import json
|
|
|
+from sys import argv, stderr
|
|
|
|
|
|
|
|
|
def main(path):
|
|
|
- lines = []
|
|
|
-
|
|
|
- f = open(path, "r")
|
|
|
- read_lines = f.readlines()
|
|
|
- for line in read_lines:
|
|
|
- lines.append(line.strip())
|
|
|
-
|
|
|
- parsing_method = False
|
|
|
-
|
|
|
+ print(f"Opening {path}", file=stderr)
|
|
|
+ with open(path, "r") as f:
|
|
|
+ lines = [line.strip() for line in f.readlines()]
|
|
|
+
|
|
|
methods = []
|
|
|
+ parsing = False
|
|
|
+ parsing_request = False
|
|
|
+ parsing_response = False
|
|
|
+
|
|
|
method = ""
|
|
|
comment = ""
|
|
|
- send = ""
|
|
|
- recv = ""
|
|
|
-
|
|
|
- for (idx, i) in enumerate(lines):
|
|
|
- if not i.startswith("//"):
|
|
|
+ request_lines = []
|
|
|
+ response_lines = []
|
|
|
+ start_line = 0
|
|
|
+
|
|
|
+ for idx, line in enumerate(lines):
|
|
|
+ if not line.startswith("//"):
|
|
|
+ if parsing_response and response_lines:
|
|
|
+ request_json = " ".join(request_lines)
|
|
|
+ response_json = " ".join(response_lines)
|
|
|
+
|
|
|
+ parsed_req = json.loads(request_json)
|
|
|
+ parsed_res = json.loads(response_json)
|
|
|
+
|
|
|
+ methods.append({
|
|
|
+ "method": parsed_req["method"],
|
|
|
+ "comment": comment.strip(),
|
|
|
+ "request": json.dumps(parsed_req, indent=2),
|
|
|
+ "response": json.dumps(parsed_res, indent=2),
|
|
|
+ "line": start_line
|
|
|
+ })
|
|
|
+
|
|
|
+ parsing = False
|
|
|
+ parsing_request = False
|
|
|
+ parsing_response = False
|
|
|
+ request_lines = []
|
|
|
+ response_lines = []
|
|
|
continue
|
|
|
-
|
|
|
- if i == ("// RPCAPI:"):
|
|
|
- parsing_method = True
|
|
|
+
|
|
|
+ text = line[2:].strip()
|
|
|
+
|
|
|
+ if text == "RPCAPI:":
|
|
|
+ if parsing_response and response_lines:
|
|
|
+ request_json = " ".join(request_lines)
|
|
|
+ response_json = " ".join(response_lines)
|
|
|
+
|
|
|
+ parsed_req = json.loads(request_json)
|
|
|
+ parsed_res = json.loads(response_json)
|
|
|
+
|
|
|
+ methods.append({
|
|
|
+ "method": parsed_req["method"],
|
|
|
+ "comment": comment.strip(),
|
|
|
+ "request": json.dumps(parsed_req, indent=2),
|
|
|
+ "response": json.dumps(parsed_res, indent=2),
|
|
|
+ "line": start_line
|
|
|
+ })
|
|
|
+
|
|
|
+ parsing_request = False
|
|
|
+ parsing_response = False
|
|
|
+ request_lines = []
|
|
|
+ response_lines = []
|
|
|
+
|
|
|
+ parsing = True
|
|
|
+ start_line = idx + 1
|
|
|
+ comment = ""
|
|
|
continue
|
|
|
-
|
|
|
- if parsing_method:
|
|
|
- if i.startswith("// --> "):
|
|
|
- method = i.split()[5][1:-2]
|
|
|
- recv = i[3:]
|
|
|
- continue
|
|
|
-
|
|
|
- if i.startswith("// <-- "):
|
|
|
- send = i[3:]
|
|
|
- parsing_method = False
|
|
|
- methods.append((method, comment.strip(), recv, send, idx + 2))
|
|
|
- comment = ""
|
|
|
- continue
|
|
|
-
|
|
|
- comment += i[3:] + "\n"
|
|
|
-
|
|
|
- for i in methods:
|
|
|
- print(f"* [`{i[0]}`](#{i[0].replace('.', '')})")
|
|
|
-
|
|
|
- print("\n")
|
|
|
- for i in methods:
|
|
|
- print(f"### `{i[0]}`\n")
|
|
|
- print(f"{i[1]}")
|
|
|
- ghlink = "%s%s%s%d" % (
|
|
|
- "https://codeberg.org/darkrenaissance/darkfi/src/branch/master/",
|
|
|
- path.replace("../", ""),
|
|
|
- "#L",
|
|
|
- i[4],
|
|
|
- )
|
|
|
- print(f'<br><sup><a href="{ghlink}">[src]</a></sup>')
|
|
|
- print("\n```json")
|
|
|
- print(i[2])
|
|
|
- print(i[3])
|
|
|
- print("```")
|
|
|
+
|
|
|
+ if not parsing:
|
|
|
+ continue
|
|
|
+
|
|
|
+ if text.startswith("-->"):
|
|
|
+ parsing_request = True
|
|
|
+ request_lines = [text[3:].strip()]
|
|
|
+ continue
|
|
|
+
|
|
|
+ if text.startswith("<--"):
|
|
|
+ parsing_request = False
|
|
|
+ parsing_response = True
|
|
|
+ response_lines = [text[3:].strip()]
|
|
|
+ continue
|
|
|
+
|
|
|
+ if parsing_response:
|
|
|
+ response_lines.append(text)
|
|
|
+ elif parsing_request:
|
|
|
+ request_lines.append(text)
|
|
|
+ else:
|
|
|
+ comment += text + "\n"
|
|
|
+
|
|
|
+ if parsing_response and response_lines:
|
|
|
+ request_json = " ".join(request_lines)
|
|
|
+ response_json = " ".join(response_lines)
|
|
|
+
|
|
|
+ parsed_req = json.loads(request_json)
|
|
|
+ parsed_res = json.loads(response_json)
|
|
|
+
|
|
|
+ methods.append({
|
|
|
+ "method": parsed_req["method"],
|
|
|
+ "comment": comment.strip(),
|
|
|
+ "request": json.dumps(parsed_req, indent=2),
|
|
|
+ "response": json.dumps(parsed_res, indent=2),
|
|
|
+ "line": start_line
|
|
|
+ })
|
|
|
+
|
|
|
+ for m in methods:
|
|
|
+ anchor = m["method"].replace(".", "").replace("/", "").lower()
|
|
|
+ print(f"### `{m['method']}`\n")
|
|
|
+
|
|
|
+ if m["comment"]:
|
|
|
+ print(f"{m['comment']}\n")
|
|
|
+
|
|
|
+ ghlink = f"https://codeberg.org/darkrenaissance/darkfi/src/branch/master/{path.replace('../', '')}#L{m['line']}"
|
|
|
+ print(f'<sup><a href="{ghlink}">[source]</a></sup>\n')
|
|
|
+
|
|
|
+ print("**Request:**\n")
|
|
|
+ print("```json")
|
|
|
+ print(m["request"])
|
|
|
+ print("```\n")
|
|
|
+
|
|
|
+ print("**Response:**\n")
|
|
|
+ print("```json")
|
|
|
+ print(m["response"])
|
|
|
+ print("```\n")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|