|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from __future__ import print_function |
| 5 | +import re |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import json |
| 9 | +import argparse |
| 10 | +import collections |
| 11 | + |
| 12 | +# tweaks per historically malformed entries |
| 13 | +hide = ('ESPModule', 'BoardModel') |
| 14 | + |
| 15 | +desc = 'Arduino boards.txt parser for FQBN details' |
| 16 | + |
| 17 | +parser = argparse.ArgumentParser(description=desc) |
| 18 | +parser.add_argument('--machine', action='store_true', |
| 19 | + help='machine version') |
| 20 | +args = parser.parse_args() |
| 21 | + |
| 22 | +if not args.machine: |
| 23 | + print('#') |
| 24 | + print('# -- ' + desc + ' --') |
| 25 | + print('#') |
| 26 | + print('# random example:') |
| 27 | + print('# esp8266com:esp8266:d1:xtal=80,vt=flash,exception=disabled,eesz=4M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=460800') |
| 28 | + print('#') |
| 29 | + print('# description of possible values in configurable menu entries:') |
| 30 | + print('# (run with \'--machine\' for machine version)') |
| 31 | + print('#') |
| 32 | + print('#') |
| 33 | + |
| 34 | +REmenuentry = re.compile('^([^\.]*)\.menu\.([^\.]*)\.([^\.]*)(?:\.linux|\.macos|\.windows|)=(.*)') |
| 35 | +REmenudesc = re.compile('^menu\.([^\.]*)=(.*)') |
| 36 | +REmenuinternal = re.compile('^([^\.]*)\.([^\.]*)\.([^\.]*)=(.*)') |
| 37 | + |
| 38 | +menudescs = collections.OrderedDict([ ( 'UploadTool', 'Upload Tool' ) ]) |
| 39 | +menuentries = collections.OrderedDict([ ]) |
| 40 | +boards = collections.OrderedDict([ ]) |
| 41 | + |
| 42 | +boardstxt = False |
| 43 | +for f in ( "../boards.txt", "boards.txt" ): |
| 44 | + if os.path.isfile(f): |
| 45 | + boardstxt = open(f) |
| 46 | +if not boardstxt: |
| 47 | + print("cannot open " + f) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | +line = boardstxt.readline() |
| 51 | + |
| 52 | +while line: |
| 53 | + |
| 54 | + catch = REmenudesc.match(line) |
| 55 | + if catch: |
| 56 | + #print("menudesc " + str(catch.groups())) |
| 57 | + menudescs.update(collections.OrderedDict([ ( catch.group(1), catch.group(2) ) ])) |
| 58 | + |
| 59 | + catch = REmenuentry.match(line) |
| 60 | + if catch: |
| 61 | + #print("menuentry " + str(catch.groups())) |
| 62 | + if not catch.group(2) in menuentries: |
| 63 | + menuentries.update(collections.OrderedDict([(catch.group(2), collections.OrderedDict([]))])) |
| 64 | + menuentries[catch.group(2)].update(collections.OrderedDict([(catch.group(3), catch.group(4))])) |
| 65 | + |
| 66 | + catch = REmenuinternal.match(line) |
| 67 | + if catch: |
| 68 | + #print("menuinternal " + str(catch.groups())) |
| 69 | + if catch.group(3) == 'board': |
| 70 | + boards.update(collections.OrderedDict([ (catch.group(1), catch.group(4)) ])) |
| 71 | + |
| 72 | + line = boardstxt.readline() |
| 73 | + |
| 74 | + |
| 75 | +for p in menuentries: |
| 76 | + for q in menuentries[p]: |
| 77 | + menuentries[p][q] = menudescs[p] + ': ' + menuentries[p][q] |
| 78 | + |
| 79 | +for h in hide: |
| 80 | + del menuentries[h] |
| 81 | + |
| 82 | +all = collections.OrderedDict([ |
| 83 | + ( 'boards', boards ), |
| 84 | + ( 'options', menudescs ), |
| 85 | + ( 'values', menuentries ) |
| 86 | +]) |
| 87 | + |
| 88 | +if args.machine: |
| 89 | + print(json.dumps(all)) |
| 90 | +else: |
| 91 | + print(json.dumps(all, indent=2)) |
| 92 | + print() |
0 commit comments