From c064ee1531516799b83891f7c323053330b7118b Mon Sep 17 00:00:00 2001 From: Barbudor Date: Fri, 20 Nov 2020 21:49:52 +0100 Subject: [PATCH] Replaced pycurl by request, cleanup and comments --- tools/templates/templates.py | 80 +++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/tools/templates/templates.py b/tools/templates/templates.py index 992e04c75..d58e57f39 100644 --- a/tools/templates/templates.py +++ b/tools/templates/templates.py @@ -30,22 +30,29 @@ Usage: """ +import sys import io -import pycurl -import certifi +import requests from io import BytesIO from io import StringIO from datetime import datetime import json -column = 27 # Start position of {"NAME":... in line +LIST_URL = "https://templates.blakadder.com/list.json" +TEMPLATES = "../../TEMPLATES.md" + +COLUMN = 27 # Start position of {"NAME":... in line + +# Some values from support.ino and tasmota_templates.h AGPIO = lambda x : x << 5 GPIO_NONE = 0 +GPIO_USER = AGPIO(GPIO_NONE)+1 GPIO_ADC_INPUT = 147 -GPIO_USER = 2047 # User configurable needs to be 2047 +GPIO_ADC_JOY = 104 -kGpioConvert = [ +# Conversion table extracted from Tasmota code +GPIO_CONVERT = [ 0x0000, 0x04A0, 0x04C0, @@ -265,28 +272,34 @@ kGpioConvert = [ 0x14E0 ] + def is_old_template(template): + """find out it the template is old format""" + # Old format <=> 13 gpios AND all gpios <= 255 if "GPIO" in template and len(template["GPIO"]) == 13: - gpio = 0 for g in template["GPIO"]: - if g < 256: - return True + if g > 255: + return False + return True return False def gpio_convert(gpio): - if gpio >= len(kGpioConvert): - return AGPIO(GPIO_NONE) +1 # USER - return kGpioConvert[gpio] + """Conversion as per https://tasmota.github.io/docs/Components/#gpio-conversion""" + if gpio >= len(GPIO_CONVERT): + return GPIO_USER + return GPIO_CONVERT[gpio] def adc0_convert(adc0): + """Conversion as per https://tasmota.github.io/docs/Components/#adc-conversion""" adc0 = adc0 & 0x0F - if adc0 > 7: - return AGPIO(GPIO_NONE) +1 # USER - elif 0 == adc0: + if 0 == adc0: return GPIO_NONE + elif 8 == adc0: + return AGPIO(GPIO_ADC_JOY); # Joystick + elif adc0 > 7: + return GPIO_USER return AGPIO(GPIO_ADC_INPUT + adc0 -1) - def convert_gpios(gpios, flag): new_gpios = [] for g in gpios: @@ -302,28 +315,31 @@ def convert_template(template): "FLAG":0, "BASE": template["BASE"] } +def get_templates_list(): + """Download templates list from template website""" + response = requests.get(LIST_URL) + if response.status_code != 200: + printf(f"Error GET({URL}) returned code {response.status_code}\n") + exit() + return response.text.split('\n') def main(): print ("\n*** templates.py v20200514 by Theo Arends ***") - # Download from template website - buffer = BytesIO() - url = "https://templates.blakadder.com/list.json" - c = pycurl.Curl() - c.setopt(c.URL, url) - c.setopt(c.WRITEDATA, buffer) - c.setopt(c.CAINFO, certifi.where()) - c.perform() - c.close() - body = buffer.getvalue() - fin = StringIO(body.decode('UTF-8')) - now = datetime.now() month = now.strftime('%B') year = now.strftime('%Y') + # if stdin is a file, read the file instead of downloading (usefull for testing) + if sys.__stdin__.isatty(): + print(f"Get template list from {LIST_URL}\n") + template_list = get_templates_list() + else: + print("Get template list from stdin\n") + template_list = sys.__stdin__.readlines() + # Write to root/TEMPLATES.md - fout = open("../../TEMPLATES.md","w+") + fout = open(TEMPLATES,"w+") fout.write("\"Logo\"\n") fout.write("\n") @@ -332,14 +348,13 @@ def main(): fout.write("Find below the available templates as of " + month + " " + year + ". More template information can be found in the [Tasmota Device Templates Repository](http://blakadder.github.io/templates)\n") first = True - fline = fin.readlines() - for line in fline: + for line in template_list: if line.strip(): if line.startswith("##"): if not first: fout.write('```\n') fout.write('\n') - fout.write(line) + fout.write(line+'\n') fout.write('```\n') first = False elif line.startswith("#"): @@ -353,12 +368,11 @@ def main(): except json.decoder.JSONDecodeError as ex: template = "Not available" - fout.write(f"{name:<{column}} {json.dumps(template)}\n") + fout.write(f"{name:<{COLUMN}} {json.dumps(template)}\n") fout.write('```\n') fout.close() - fin.close() if __name__ == "__main__": main()