pinout.vvzero.com/pinout.py

140 lines
3.7 KiB
Python
Raw Normal View History

2015-03-27 01:27:30 +08:00
import json
import time
2018-02-18 21:27:37 +08:00
import os
2016-09-14 01:27:55 +08:00
try:
import yaml
except ImportError:
exit("This script requires the yaml module\nInstall with: sudo pip install PyYAML")
2016-09-14 01:27:55 +08:00
2019-10-10 05:51:17 +08:00
try:
unicode('')
except NameError:
unicode = str
2016-09-14 01:27:55 +08:00
2019-10-10 05:51:17 +08:00
BUILD_ID = str(int(time.time()))
PINOUT_FILE = 'pinout.yaml'
SETTINGS_FILE = 'settings.yaml'
STRINGS_FILE = 'localised.yaml'
2015-03-27 01:27:30 +08:00
2018-02-18 21:27:37 +08:00
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
2015-03-27 01:27:30 +08:00
pins = None
settings = None
2019-10-10 05:51:17 +08:00
master_template = open(os.path.join(BASE_DIR, 'common/layout.html')).read()
twitter_template = open(os.path.join(BASE_DIR, 'common/twittercard.html')).read()
2016-11-02 04:40:35 +08:00
2015-11-18 21:52:15 +08:00
2019-10-10 05:51:17 +08:00
def get_setting(setting, default=None):
if setting in settings and settings[setting] is not None:
return settings[setting]
return default
2019-10-10 05:51:17 +08:00
def get_string(string, default=None):
if string in strings and strings[string] is not None:
return strings[string]
2019-10-10 05:51:17 +08:00
return default
2015-11-18 21:52:15 +08:00
2015-03-27 01:27:30 +08:00
def render_html(*args, **kwargs):
2016-11-02 04:40:35 +08:00
html = master_template
2019-10-10 05:51:17 +08:00
html = html.replace('{{main_content}}', args[0])
html = html.replace('{{footer}}', args[1])
2016-11-02 04:40:35 +08:00
2017-12-05 07:53:48 +08:00
if "twittercard" in kwargs:
if kwargs["twittercard"]:
html = html.replace('{{twittercard}}', twitter_template)
2019-10-10 05:51:17 +08:00
2017-12-05 07:53:48 +08:00
html = html.replace('{{twittercard}}', "")
2016-11-02 05:21:57 +08:00
strings = args[2]
for key in strings:
if type(strings[key]) in [str, unicode]:
html = html.replace('{{strings:' + key + '}}', strings[key])
settings = args[3]
for key in settings:
if type(settings[key]) in [str, unicode]:
html = html.replace('{{settings:' + key + '}}', settings[key])
2019-10-10 05:51:17 +08:00
kwargs['v'] = BUILD_ID
2016-11-02 05:21:57 +08:00
2015-03-27 01:27:30 +08:00
for key in kwargs:
if type(kwargs[key]) == dict:
2019-10-10 05:51:17 +08:00
for (d_key, d_value) in kwargs[key].items():
html = html.replace('{{' + key + '_' + d_key + '}}', d_value)
2016-11-02 05:21:57 +08:00
elif type(kwargs[key]) in [str, unicode]:
html = html.replace('{{' + key + '}}', kwargs[key])
2016-11-02 05:21:57 +08:00
2015-03-27 01:27:30 +08:00
return html
2015-11-18 21:52:15 +08:00
2015-03-27 01:27:30 +08:00
def bcm_to_physical(pin):
return physical_from(pin, 'bcm')
2015-03-27 01:27:30 +08:00
2015-11-18 21:52:15 +08:00
def wiringpi_to_physical(pin):
return physical_from(pin, 'wiringpi')
def physical_from(pin, scheme='bcm'):
if scheme in ['bcm', 'wiringpi']:
for idx in pins:
compare_pin = pins[idx]
if 'scheme' in compare_pin:
if scheme in compare_pin['scheme']:
if compare_pin['scheme'][scheme] == int(pin):
2019-10-10 06:39:55 +08:00
# print("Mapping {}{} to {}".format(scheme, pin, str(idx)))
return str(idx)
elif scheme == 'physical':
return pin
2015-03-27 01:27:30 +08:00
return None
2015-11-18 21:52:15 +08:00
def physical_to_bcm(pin):
return physical_to(pin, 'bcm')
2015-03-27 01:27:30 +08:00
def physical_to_wiringpi(pin):
return physical_to(pin, 'wiringpi')
2015-03-27 01:27:30 +08:00
2015-11-18 21:52:15 +08:00
2015-03-27 01:27:30 +08:00
def physical_to(pin, scheme='bcm'):
2015-11-18 21:52:15 +08:00
if scheme in ['bcm', 'wiringpi']:
2015-03-27 01:27:30 +08:00
pin = pins[pin]
if 'scheme' in pin:
if scheme in pin['scheme']:
return str(pin['scheme'][scheme])
elif scheme == 'physical':
return pin
return None
2015-11-18 21:52:15 +08:00
2015-11-14 07:16:37 +08:00
def load(lang='en'):
global pins, settings, strings
2018-02-18 21:27:37 +08:00
2019-10-10 05:51:17 +08:00
settings_path = os.path.join(BASE_DIR, 'src/{}/{}'.format(lang, SETTINGS_FILE))
strings_path = os.path.join(BASE_DIR, 'src/{}/template/{}'.format(lang, STRINGS_FILE))
pinout_path = os.path.join(BASE_DIR, 'src/{}/template/{}'.format(lang, PINOUT_FILE))
2018-02-18 21:27:37 +08:00
if SETTINGS_FILE.endswith('.yaml'):
settings = yaml.safe_load(open(settings_path).read())
else:
2018-02-18 21:27:37 +08:00
settings = json.load(open(settings_path))
if STRINGS_FILE.endswith('.yaml'):
strings = yaml.safe_load(open(strings_path).read())
else:
2018-02-18 21:27:37 +08:00
strings = json.load(open(strings_path))
if PINOUT_FILE.endswith('.yaml'):
pinout = yaml.safe_load(open(pinout_path).read())
else:
2018-02-18 21:27:37 +08:00
pinout = json.load(open(pinout_path))
pins = pinout['pins']