Tentative Python3 support for #331
This commit is contained in:
parent
d327ca87ab
commit
f3908bc86f
|
@ -15,9 +15,17 @@ import markjaml
|
|||
import pinout
|
||||
import urlmapper
|
||||
|
||||
try:
|
||||
reload(sys)
|
||||
except NameError:
|
||||
from importlib import reload
|
||||
reload(sys)
|
||||
|
||||
try:
|
||||
sys.setdefaultencoding('utf8')
|
||||
except AttributeError: # Does not work in Python 3
|
||||
unicode = str
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
|
||||
DEBUG_LEVEL = 1
|
||||
GROUND_PINS = [6,9,14,20,25,30,34,39]
|
||||
|
@ -550,7 +558,7 @@ strings = pinout.get_string('strings', {})
|
|||
if type(strings) == list:
|
||||
_strings = {}
|
||||
for item in strings:
|
||||
_strings[item.keys()[0]] = item.values()[0]
|
||||
_strings[list(item.keys())[0]] = list(item.values())[0]
|
||||
strings = _strings
|
||||
|
||||
for key, val in default_strings.items():
|
||||
|
@ -590,7 +598,7 @@ if not os.path.isdir('output/{}/pinout'.format(lang)):
|
|||
exit('Failed to create output/{}/pinout dir'.format(lang))
|
||||
|
||||
print("\nRendering overlay pages...")
|
||||
overlays = map(load_overlay, overlays)
|
||||
overlays = list(map(load_overlay, overlays))
|
||||
overlay_subnav = ['featured']
|
||||
featured_boards_count = 0
|
||||
featured_boards_html = ''
|
||||
|
@ -762,6 +770,8 @@ navs['boards'] = default_nav
|
|||
|
||||
print('\nRendering pin pages...')
|
||||
|
||||
interfaces = interfaces_menu(None)
|
||||
|
||||
for pin in range(1, len(pinout.pins) + 1):
|
||||
(pin_url, pin_html, pin_title) = render_pin_page(pin)
|
||||
if pin_url is None:
|
||||
|
@ -786,7 +796,7 @@ for pin in range(1, len(pinout.pins) + 1):
|
|||
featured_boards=featured_boards_html,
|
||||
langcode=lang,
|
||||
nav_html=nav_html,
|
||||
interfaces=interfaces_menu(None),
|
||||
interfaces=interfaces,
|
||||
body_class='pin',
|
||||
crumbtrail=crumbtrail
|
||||
)
|
||||
|
|
12
markjaml.py
12
markjaml.py
|
@ -32,7 +32,7 @@ def load(file):
|
|||
Returns an object that includes the JSON data, and the parsed HTML.
|
||||
'''
|
||||
markson = open(file).read()
|
||||
markson = markson.replace('\r','')
|
||||
markson = markson.replace('\r', '')
|
||||
|
||||
_data = re.search(re.compile(r'<!--(JSON:|\n---\n)(.*)-->', re.DOTALL), markson)
|
||||
|
||||
|
@ -43,10 +43,10 @@ def load(file):
|
|||
# to be the first string starting with a single hash/pound ( # ) sign
|
||||
_title = re.search(re.compile(r'^#[^\#](.*)$', re.MULTILINE), markson)
|
||||
|
||||
if _title != None:
|
||||
if _title is not None:
|
||||
_title = _title.group(0).replace('#', '').strip()
|
||||
|
||||
if _data != None:
|
||||
if _data is not None:
|
||||
_type = _data.group(0)[4:8].upper().strip()
|
||||
|
||||
if _type == 'JSON':
|
||||
|
@ -60,7 +60,7 @@ def load(file):
|
|||
|
||||
_data['title'] = _title
|
||||
|
||||
elif _title != None:
|
||||
_data = {'title':_title}
|
||||
elif _title is not None:
|
||||
_data = {'title': _title}
|
||||
|
||||
return {'data':_data, 'html':_html}
|
||||
return {'data': _data, 'html': _html}
|
37
pinout.py
37
pinout.py
|
@ -9,7 +9,12 @@ try:
|
|||
except ImportError:
|
||||
exit("This script requires the yaml module\nInstall with: sudo pip install PyYAML")
|
||||
|
||||
try:
|
||||
unicode('')
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
BUILD_ID = str(int(time.time()))
|
||||
PINOUT_FILE = 'pinout.yaml'
|
||||
SETTINGS_FILE = 'settings.yaml'
|
||||
STRINGS_FILE = 'localised.yaml'
|
||||
|
@ -19,29 +24,31 @@ BASE_DIR = os.path.dirname(os.path.realpath(__file__))
|
|||
pins = None
|
||||
settings = None
|
||||
|
||||
master_template = open(os.path.join(BASE_DIR,'common/layout.html')).read()
|
||||
twitter_template = open(os.path.join(BASE_DIR,'common/twittercard.html')).read()
|
||||
master_template = open(os.path.join(BASE_DIR, 'common/layout.html')).read()
|
||||
twitter_template = open(os.path.join(BASE_DIR, 'common/twittercard.html')).read()
|
||||
|
||||
|
||||
def get_setting(setting, default = None):
|
||||
if setting in settings and settings[setting] != None:
|
||||
def get_setting(setting, default=None):
|
||||
if setting in settings and settings[setting] is not None:
|
||||
return settings[setting]
|
||||
return default
|
||||
|
||||
def get_string(string, default = None):
|
||||
if string in strings and strings[string] != None:
|
||||
|
||||
def get_string(string, default=None):
|
||||
if string in strings and strings[string] is not None:
|
||||
return strings[string]
|
||||
return default
|
||||
return default
|
||||
|
||||
|
||||
def render_html(*args, **kwargs):
|
||||
html = master_template
|
||||
html = html.replace('{{main_content}}',args[0])
|
||||
html = html.replace('{{footer}}',args[1])
|
||||
html = html.replace('{{main_content}}', args[0])
|
||||
html = html.replace('{{footer}}', args[1])
|
||||
|
||||
if "twittercard" in kwargs:
|
||||
if kwargs["twittercard"]:
|
||||
html = html.replace('{{twittercard}}', twitter_template)
|
||||
|
||||
|
||||
html = html.replace('{{twittercard}}', "")
|
||||
|
||||
strings = args[2]
|
||||
|
@ -56,11 +63,11 @@ def render_html(*args, **kwargs):
|
|||
if type(settings[key]) in [str, unicode]:
|
||||
html = html.replace('{{settings:' + key + '}}', settings[key])
|
||||
|
||||
kwargs['v'] = str(int(time.time()))
|
||||
kwargs['v'] = BUILD_ID
|
||||
|
||||
for key in kwargs:
|
||||
if type(kwargs[key]) == dict:
|
||||
for d_key, d_value in kwargs[key].iteritems():
|
||||
for (d_key, d_value) in kwargs[key].items():
|
||||
html = html.replace('{{' + key + '_' + d_key + '}}', d_value)
|
||||
elif type(kwargs[key]) in [str, unicode]:
|
||||
html = html.replace('{{' + key + '}}', kwargs[key])
|
||||
|
@ -112,9 +119,9 @@ def physical_to(pin, scheme='bcm'):
|
|||
def load(lang='en'):
|
||||
global pins, settings, strings
|
||||
|
||||
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))
|
||||
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))
|
||||
|
||||
if SETTINGS_FILE.endswith('.yaml'):
|
||||
settings = yaml.safe_load(open(settings_path).read())
|
||||
|
|
19
urlmapper.py
19
urlmapper.py
|
@ -9,9 +9,16 @@ import unicodedata
|
|||
import markjaml
|
||||
import pinout
|
||||
|
||||
try:
|
||||
reload(sys)
|
||||
except NameError:
|
||||
from importlib import reload
|
||||
reload(sys)
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
try:
|
||||
sys.setdefaultencoding('utf8')
|
||||
except AttributeError: # Does not work in Python 3
|
||||
unicode = str
|
||||
|
||||
|
||||
def url_slugify(value):
|
||||
|
@ -86,13 +93,15 @@ def generate_for_lang(lang="en"):
|
|||
if pin_url is None:
|
||||
continue
|
||||
|
||||
url_lookup['pin{}'.format(pin)] = '//{domain}{base_url}{url}'.format(domain=domain, base_url=base_url, url=pin_url)
|
||||
url_lookup['pin{}'.format(pin)] = '//{domain}{base_url}{url}'.format(
|
||||
domain=domain, base_url=base_url, url=pin_url)
|
||||
|
||||
for url in overlays:
|
||||
if url is None:
|
||||
continue
|
||||
|
||||
url_lookup['{}'.format(url[0])] = '//{domain}{base_url}{url}'.format(domain=domain, base_url=base_url, url=url[1])
|
||||
url_lookup['{}'.format(url[0])] = '//{domain}{base_url}{url}'.format(
|
||||
domain=domain, base_url=base_url, url=url[1])
|
||||
|
||||
|
||||
url_lookup['index'] = '//{}'.format(domain)
|
||||
|
@ -103,7 +112,7 @@ def generate_for_lang(lang="en"):
|
|||
|
||||
|
||||
def generate_urls(lang="en"):
|
||||
languages = [l.replace('src/', '') for l in glob.glob('src/??') if os.path.isdir(l)] # if not l == 'src/'+lang
|
||||
languages = [l.replace('src/', '') for l in glob.glob('src/??') if os.path.isdir(l)] # if not l == 'src/'+lang
|
||||
urls = {}
|
||||
for lang in languages:
|
||||
urls[lang] = generate_for_lang(lang)
|
||||
|
|
Loading…
Reference in New Issue