# -*- coding: utf-8 -*- """ Created on Fri Jun 26 19:10:58 2015 @author: pietro """ from __future__ import print_function import argparse import sys try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen from build_html import * def parse_options(lines, startswith='Opt'): def split_in_groups(lines): def count_par_diff(line): open_par = line.count('(') close_par = line.count(')') return open_par - close_par res = None diff = 0 for line in lines: if line.startswith('case'): optname = line.split()[1][:-1] res = [] # if optname == 'G_OPT_R_BASENAME_INPUT': # import ipdb; ipdb.set_trace() elif line == 'break;': diff = 0 yield optname, res elif line.startswith('G_'): diff = count_par_diff(line) elif diff > 0: diff -= count_par_diff(line) else: res.append(line) if res is not None else None def split_opt_line(line): index = line.index('=') key = line[:index].strip() default = line[index + 1:].strip() if default.startswith('_('): default = default[2:] return key, default def parse_glines(glines): res = {} key = None dynamic_answer = False for line in glines: if line.strip() == "/* start dynamic answer */": dynamic_answer = True if line.strip() == "/* end dynamic answer */": dynamic_answer = False if dynamic_answer or line.startswith('/*'): continue if line.startswith('/*'): continue if line.startswith(startswith) and line.endswith(';'): key, default = [w.strip() for w in split_opt_line(line[5:])] res[key] = default elif line.startswith(startswith): key, default = split_opt_line(line[5:]) res[key] = [default, ] else: if key is not None: if key not in res: res[key] = [] start, end = 0, -1 if line.startswith('_('): start = 2 if line.endswith(');'): end = -3 elif line.endswith(';'): end = -2 res[key].append(line[start:end]) # pprint(glines) # pprint(res) return res def clean_value(val): if isinstance(val, list): val = ' '.join(val) return (val.replace('"', '').replace("\'", "'").strip().strip(';') ).strip().strip('_(').strip().strip(')').strip() # with open(optionfile, mode='r') as optfile: lines = [line.strip() for line in lines] result = [] for optname, glines in split_in_groups(lines): if glines: res = parse_glines(glines) if res: for key, val in res.items(): res[key] = clean_value(val) result.append((optname, res)) return result class OptTable(object): """""" def __init__(self, list_of_dict): self.options = list_of_dict self.columns = sorted(set([key for _, d in self.options for key in d.keys()])) def csv(self, delimiter=';', endline='\n'): """Return a CSV string with the options""" csv = [] csv.append(delimiter.join(self.columns)) for optname, options in self.options: opts = [options.get(col, '') for col in self.columns] csv.append(delimiter.join([optname, ] + opts)) return endline.join(csv) def html(self, endline='\n', indent=' ', toptions='border=1'): """Return a HTML table with the options""" html = ['
{0} | ".format('option')) for col in self.columns: html.append(indent * 2 + "{0} | ".format(col)) html.append(indent + "
---|---|
{0} | ".format(optname)) for col in self.columns: html.append(indent * 2 + "{0} | ".format(options.get(col, ''))) html.append(indent + "