-
Notifications
You must be signed in to change notification settings - Fork 987
Expand file tree
/
Copy pathsync-rpc-cmds.py
More file actions
118 lines (101 loc) · 3.48 KB
/
sync-rpc-cmds.py
File metadata and controls
118 lines (101 loc) · 3.48 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
from urllib.parse import quote
from time import sleep
import requests
import re
from enum import Enum
# readme url
URL = "https://api.readme.com/v2/branches/stable"
# category id for API reference
CATEGORY_ID = "685ce4df1df887006ff221c5"
CATEGORY_SLUG = "JSON-RPC API Reference"
class Action(Enum):
ADD = 'add'
UPDATE = 'update'
DELETE = 'delete'
def getListOfRPCDocs(headers):
response = requests.get(f"{URL}/categories/reference/{quote(CATEGORY_SLUG)}/pages", headers=headers)
if response.status_code == 200:
return response.json().get('data', [])
else:
return []
def publishDoc(action, title, body, order, headers):
payload = {
"title": title,
"type": "basic",
"body": body,
"category": {
"id": CATEGORY_ID
},
"hidden": False,
"order": order,
}
if action == Action.ADD:
# create doc
payload['slug'] = title
response = requests.post(URL + "/reference", json=payload, headers=headers)
if response.status_code != 201:
print(response.text)
else:
print("Created ", title)
elif action == Action.UPDATE:
# update doc
response = requests.patch(f"{URL}/reference/{title}", json=payload, headers=headers)
if response.status_code != 200:
print(response.text)
else:
print("Updated ", title)
elif action == Action.DELETE:
# delete doc
response = requests.delete(f"{URL}/reference/{title}", headers=headers)
if response.status_code != 204:
print(response.text)
else:
print("Deleted ", title)
else:
print("Invalid action")
def extract_rpc_commands(rst_content):
manpages_block = re.search(
r"\.\. block_start manpages(.*?)" r"\.\. block_end manpages",
rst_content,
re.DOTALL,
)
if manpages_block:
commands = re.findall(
r"\b([a-zA-Z0-9_-]+)" r"\s+<([^>]+)>\n", manpages_block.group(1)
)
return commands
return []
def main():
# define headers for requests
headers = {
"accept": "application/json",
"content-type": "application/json",
"Authorization": "Bearer " + os.environ.get("README_API_KEY"),
}
# path to the rst file from where we fetch all the RPC commands
path_to_rst = "doc/index.rst"
with open(path_to_rst, "r") as file:
rst_content = file.read()
commands_from_local = extract_rpc_commands(rst_content)
commands_from_readme = getListOfRPCDocs(headers)
# Compare local and server commands list to get the list of command to add or delete
commands_local_title = set(command[0] for command in commands_from_local)
commands_readme_title = set(command['title'] for command in commands_from_readme)
commands_to_delete = commands_readme_title - commands_local_title
commands_to_add = commands_local_title - commands_readme_title
for name in commands_to_delete:
publishDoc(Action.DELETE, name, "", 0, headers)
sleep(3)
if commands_from_local:
order = 0
for name, file in commands_from_local:
with open("doc/" + file) as f:
body = f.read()
publishDoc(Action.ADD if name in commands_to_add else Action.UPDATE, name, body, order, headers)
order = order + 1
sleep(3)
else:
print("No commands found in the Manpages block.")
if __name__ == "__main__":
main()