-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_readme.py
202 lines (128 loc) · 6.19 KB
/
create_readme.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: nils
"""
readme_file_name = "README.md"
# a new index.json is created for all individual block manifests that have a supported optionsVersion
supported_options_versions = [1]
import os
import json
from os import path
import urllib.parse
from functools import cmp_to_key
def create_header(f):
f.write("# Omniscope Custom Blocks")
f.write(" · ")
f.write("[![Refresh index](https://github.com/visokio/omniscope-custom-blocks/actions/workflows/refresh_index.yml/badge.svg)](https://github.com/visokio/omniscope-custom-blocks/actions/workflows/refresh_index.yml)")
f.write("[![Refresh readme](https://github.com/visokio/omniscope-custom-blocks/actions/workflows/refresh_readme.yml/badge.svg)](https://github.com/visokio/omniscope-custom-blocks/actions/workflows/refresh_readme.yml)")
f.write("\n\n")
f.write("Public repository for custom blocks for Omniscope Evo.\n")
f.write("\n")
f.write("#### [Python / R API docs](https://help.visokio.com/support/solutions/articles/42000071109-custom-block-r-python-api-reference)\n")
f.write("\n")
def compare(a, b):
if a is None:
return 1
if b is None:
return -1
if a > b: return 1
elif a < b: return -1
else: return 0
def get_sections(blocks):
sections = {}
for block in blocks:
category = block["category"]
subcategory = None
if not block["category"] in sections:
sections[category] = {}
if "subcategory" in block and not block["subcategory"] is None:
subcategory = block["subcategory"]
if not subcategory in sections[category]:
sections[category][subcategory] = []
sections[category][subcategory].append(block)
return sections
def create_instructions(f):
f.write("## How to add a block to this repository\n")
f.write("### The simple way\n")
f.write("1. Design your custom block in Omniscope Evo 2020.1 or later.\n")
f.write(" The source code should be reasonably documented and potentially contain sections to describe input fields and parameters.\n")
f.write("2. Export as a ZIP file from the block dialog.\n")
f.write("3. Send the file to [email protected] and we will include it for you.\n\n")
f.write("### The hard way\n")
f.write("1. Follow points 1-2 from the simple way.\n")
f.write("2. Fork the repository.\n")
f.write("3. Create or use a directory in the forked repository under one of the main sections that specifies the general area of what the block does.\n")
f.write("4. Extract the ZIP file into this directory.\n")
f.write("5. Consider adding a README.md for convenience, and a thumbnail.png.\n")
f.write("6. Run the python scripts create_index.py and create_readme.py located in the root of the repository.\n")
f.write("7. Create a pull request.\n\n")
def create_table_of_contents(f, blocks):
f.write("## Table of blocks (Omniscope 2020+)\n")
sections = get_sections(blocks)
category_index = 1
for category in sections:
f.write(f"{category_index}. {category}\n")
subcategories = list(sections[category].keys())
subcategories.sort(key=cmp_to_key(compare))
subcategory_index = 1
for subcategory in subcategories:
has_subcategory = not subcategory is None
if has_subcategory:
f.write(f" {subcategory_index}. {subcategory}\n")
block_index = 1 if has_subcategory else subcategory_index
for block in sections[category][subcategory]:
if has_subcategory:
f.write(" ")
link = "#"+block["id"]
f.write(f' {block_index}. [{block["name"]}]({link})\n')
block_index = block_index + 1
subcategory_index = subcategory_index + 1
category_index = category_index + 1
def create_block_overview(f, blocks):
f.write("## Block Overview\n")
sections = get_sections(blocks)
for category in sections:
subcategories = list(sections[category].keys())
subcategories.sort(key=cmp_to_key(compare))
for subcategory in subcategories:
for block in sections[category][subcategory]:
f.write(f'<div id="{block["id"]}"/>\n\n')
f.write(f'### {block["name"]}\n\n')
has_thumbnail = "thumbnail" in block
if has_thumbnail:
f.write(f'<img align="right" src="https://github.com/visokio/omniscope-custom-blocks/blob/master/{block["thumbnail"]}" width="125" height="125"/>\n\n')
f.write(f'{block["description"]}\n\n')
f.write(f'[Link to Github page]({block["relative_path"]})\n\n')
def process_directory(root_path: str, path_parts, blocks):
with open(root_path+"/manifest.json", 'r') as manifest_file:
manifest = json.load(manifest_file)
if not "optionsVersion" in manifest or not manifest["optionsVersion"] in supported_options_versions:
return
relative_path = os.sep.join(path_parts)
id = "".join(list(map(lambda s: s.replace(" ", ""), path_parts)))
block = {}
block["id"] = id
block["path"] = root_path
block["relative_path"] = urllib.parse.quote(relative_path)
block["name"] = manifest["name"]
block["language"] = manifest["language"]
block["description"] = manifest["description"]
block["category"] = manifest["category"]
if "subcategory" in manifest and not manifest["subcategory"] is None:
block["subcategory"] = manifest["subcategory"]
if path.isfile(root_path+"/thumbnail.png"):
block["thumbnail"] = relative_path+"/thumbnail.png"
blocks.append(block)
blocks = []
for root, dirs, files in os.walk("."):
root_path = root.split(os.sep)
path_parts = root_path[1:]
for file in files:
if file == "manifest.json":
process_directory(root, path_parts, blocks)
with open(readme_file_name, 'w') as readme_file:
create_header(readme_file)
create_instructions(readme_file)
create_table_of_contents(readme_file, blocks)
create_block_overview(readme_file, blocks)