forked from pinout-xyz/Pinout.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-json.py
executable file
·143 lines (104 loc) · 3.68 KB
/
generate-json.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
#!/usr/bin/env python
import json
import re
import sys
import unicodedata
try:
import markdown
except ImportError:
exit("This script requires the psutil module\nInstall with: sudo pip install Markdown")
import markjaml
import pinout
reload(sys)
sys.setdefaultencoding('utf8')
lang = "en"
if len(sys.argv) > 1:
lang = sys.argv[1]
pinout.load(lang)
overlays = pinout.settings['overlays']
pages = {}
def cssify(value):
value = slugify(value);
if value[0] == '3' or value[0] == '5':
value = 'pow' + value
return value
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicode(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '_', value)
product_map = {}
def load_overlay(overlay):
try:
data = markjaml.load('src/{}/overlay/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
return None
details = []
if 'manufacturer' in loaded:
if loaded['manufacturer'] == "Pimoroni":
if 'buy' in loaded:
product_slug = loaded['buy'].split('/')[-1]
product_map[product_slug] = slugify(loaded['name'])
details.append('* Made by ' + loaded['manufacturer'])
if 'pincount' in loaded:
pincount = int(loaded['pincount'])
if pincount == 40:
details.append('* HAT form-factor')
elif pincount == 38:
details.append('* pHAT form-factor')
elif pincount == 26:
details.append('* Classic 26-pin')
else:
details.append('* {} pin header'.format(pincount))
if 'pin' in loaded:
uses_5v = False
uses_3v = False
uses = 0
for pin in loaded['pin']:
pin = str(pin)
if pin.startswith('bcm'):
pin = pinout.bcm_to_physical(pin[3:])
if pin in pinout.pins:
actual_pin = pinout.pins[pin]
if actual_pin['type'] in ['+3v3', '+5v', 'GND']:
if actual_pin['type'] == '+3v3':
uses_3v = True
if actual_pin['type'] == '+5v':
uses_5v = True
else:
uses += 1
if uses > 0:
details.append('* Uses {} GPIO pins'.format(uses))
if '3' in loaded['pin'] and '5' in loaded['pin']:
pin_3 = loaded['pin']['3']
pin_5 = loaded['pin']['5']
if pin_3 is not None and pin_5 is not None:
if 'mode' in pin_3 and 'mode' in pin_5:
if pin_3['mode'] == 'i2c' and pin_5['mode'] == 'i2c':
details.append('* Uses I2C')
if 'url' in loaded:
details.append('* [More Information]({url})'.format(url=loaded['url']))
if 'github' in loaded:
details.append('* [GitHub Repository]({url})'.format(url=loaded['github']))
if 'buy' in loaded:
details.append('* [Buy Now]({url})'.format(url=loaded['buy']))
if not 'page_url' in loaded:
loaded['page_url'] = slugify(loaded['name'])
pages[loaded['page_url']] = loaded
return loaded
def load_md(filename):
filename = 'src/{}/{}'.format(lang, filename)
try:
html = markdown.markdown(open(filename).read(), extensions=['fenced_code'])
return html
except IOError:
print('Unable to load markdown from {}'.format(filename))
return ''
overlays = map(load_overlay, overlays)
#print(json.dumps(overlays))
print(json.dumps(product_map))