pinout.vvzero.com/urlmapper.py

120 lines
3.1 KiB
Python
Raw Permalink Normal View History

2015-11-09 01:36:07 +08:00
#!/usr/bin/env python
2016-09-14 01:27:55 +08:00
import glob
import os
2015-11-09 01:36:07 +08:00
import re
import sys
2016-09-14 01:27:55 +08:00
import unicodedata
2015-11-09 01:36:07 +08:00
import markjaml
2016-09-14 01:27:55 +08:00
import pinout
2019-10-10 05:51:17 +08:00
try:
reload(sys)
except NameError:
from importlib import reload
reload(sys)
2015-11-09 01:36:07 +08:00
2019-10-10 05:51:17 +08:00
try:
sys.setdefaultencoding('utf8')
except AttributeError: # Does not work in Python 3
unicode = str
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
2015-11-09 01:36:07 +08:00
def url_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)
2015-11-18 21:52:15 +08:00
2015-11-09 01:36:07 +08:00
def load_overlay_url(overlay, lang):
2017-01-15 21:32:45 +08:00
loaded = None
2019-10-10 06:39:55 +08:00
# print("Info: Trying {}".format('src/{}/overlay/{}.md'.format(lang, overlay)))
2015-11-18 21:52:15 +08:00
try:
data = markjaml.load('src/{}/overlay/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
try:
2019-10-10 06:39:55 +08:00
# print("Warning: Falling back to {}".format('src/{}/translate/{}.md'.format(lang, overlay)))
data = markjaml.load('src/{}/translate/{}.md'.format(lang, overlay))
loaded = data['data']
except IOError:
return None
2015-11-09 01:36:07 +08:00
2017-01-15 21:32:45 +08:00
if loaded is None:
2017-12-14 20:46:33 +08:00
print("Fatal: Invalid overlay formatting:: {}.md".format(overlay))
2017-01-16 02:01:51 +08:00
sys.exit(1)
2017-01-15 21:32:45 +08:00
2019-10-10 06:39:55 +08:00
if 'page_url' not in loaded:
2015-11-18 21:52:15 +08:00
loaded['page_url'] = url_slugify(loaded['name'])
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
return overlay, loaded['page_url']
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
def get_pin_url(pin_num, pinout):
pin = pinout.pins[str(pin_num)]
pin_url = pin['name']
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
if pin_url == 'Ground':
return None
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
if 'scheme' in pin:
if 'bcm' in pin['scheme']:
bcm = pin['scheme']['bcm']
pin_url = 'gpio{}'.format(bcm)
return url_slugify('pin{}_{}'.format(pin_num, pin_url))
2015-11-09 01:36:07 +08:00
2015-11-14 07:16:37 +08:00
def generate_for_lang(lang="en"):
2015-11-18 21:52:15 +08:00
url_lookup = {}
pinout.load(lang)
2019-06-09 06:46:25 +08:00
overlays = glob.glob("src/{}/overlay/*.md".format(lang)) + glob.glob("src/{}/translate/*.md".format(lang))
overlays = [overlay.split("/")[-1].replace(".md", "") for overlay in overlays]
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
base_url = pinout.get_setting('base_url', '/pinout/')
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
domain = pinout.get_setting('domain', 'pinout.xyz')
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
overlays_html = ''
2015-11-09 05:21:57 +08:00
2015-11-18 21:52:15 +08:00
overlays = map(lambda overlay: load_overlay_url(overlay, lang), overlays)
2015-11-09 05:21:57 +08:00
2015-11-18 21:52:15 +08:00
for pin in range(1, len(pinout.pins)+1):
pin_url = get_pin_url(pin, pinout)
if pin_url is None:
continue
2015-11-09 01:36:07 +08:00
2019-10-10 05:51:17 +08:00
url_lookup['pin{}'.format(pin)] = '//{domain}{base_url}{url}'.format(
domain=domain, base_url=base_url, url=pin_url)
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
for url in overlays:
if url is None:
continue
2015-11-09 01:36:07 +08:00
2019-10-10 05:51:17 +08:00
url_lookup['{}'.format(url[0])] = '//{domain}{base_url}{url}'.format(
domain=domain, base_url=base_url, url=url[1])
2015-11-09 01:36:07 +08:00
2015-11-18 21:52:15 +08:00
url_lookup['index'] = '//{}'.format(domain)
2015-11-09 05:21:57 +08:00
2016-11-02 07:34:04 +08:00
url_lookup['boards'] = '//{}/boards'.format(domain)
2015-11-18 21:52:15 +08:00
return url_lookup
2015-11-09 01:36:07 +08:00
2015-11-14 07:16:37 +08:00
def generate_urls(lang="en"):
2019-10-10 05:51:17 +08:00
languages = [l.replace('src/', '') for l in glob.glob('src/??') if os.path.isdir(l)] # if not l == 'src/'+lang
2015-11-18 21:52:15 +08:00
urls = {}
for lang in languages:
urls[lang] = generate_for_lang(lang)
return urls