1*e1fe3e4aSElliott Hughes# Copyright 2013 Google, Inc. All Rights Reserved. 2*e1fe3e4aSElliott Hughes# 3*e1fe3e4aSElliott Hughes# Google Author(s): Behdad Esfahbod, Roozbeh Pournader 4*e1fe3e4aSElliott Hughes 5*e1fe3e4aSElliott Hughes 6*e1fe3e4aSElliott Hughesclass Options(object): 7*e1fe3e4aSElliott Hughes class UnknownOptionError(Exception): 8*e1fe3e4aSElliott Hughes pass 9*e1fe3e4aSElliott Hughes 10*e1fe3e4aSElliott Hughes def __init__(self, **kwargs): 11*e1fe3e4aSElliott Hughes self.verbose = False 12*e1fe3e4aSElliott Hughes self.timing = False 13*e1fe3e4aSElliott Hughes self.drop_tables = [] 14*e1fe3e4aSElliott Hughes self.input_file = None 15*e1fe3e4aSElliott Hughes self.output_file = "merged.ttf" 16*e1fe3e4aSElliott Hughes self.import_file = None 17*e1fe3e4aSElliott Hughes 18*e1fe3e4aSElliott Hughes self.set(**kwargs) 19*e1fe3e4aSElliott Hughes 20*e1fe3e4aSElliott Hughes def set(self, **kwargs): 21*e1fe3e4aSElliott Hughes for k, v in kwargs.items(): 22*e1fe3e4aSElliott Hughes if not hasattr(self, k): 23*e1fe3e4aSElliott Hughes raise self.UnknownOptionError("Unknown option '%s'" % k) 24*e1fe3e4aSElliott Hughes setattr(self, k, v) 25*e1fe3e4aSElliott Hughes 26*e1fe3e4aSElliott Hughes def parse_opts(self, argv, ignore_unknown=[]): 27*e1fe3e4aSElliott Hughes ret = [] 28*e1fe3e4aSElliott Hughes opts = {} 29*e1fe3e4aSElliott Hughes for a in argv: 30*e1fe3e4aSElliott Hughes orig_a = a 31*e1fe3e4aSElliott Hughes if not a.startswith("--"): 32*e1fe3e4aSElliott Hughes ret.append(a) 33*e1fe3e4aSElliott Hughes continue 34*e1fe3e4aSElliott Hughes a = a[2:] 35*e1fe3e4aSElliott Hughes i = a.find("=") 36*e1fe3e4aSElliott Hughes op = "=" 37*e1fe3e4aSElliott Hughes if i == -1: 38*e1fe3e4aSElliott Hughes if a.startswith("no-"): 39*e1fe3e4aSElliott Hughes k = a[3:] 40*e1fe3e4aSElliott Hughes v = False 41*e1fe3e4aSElliott Hughes else: 42*e1fe3e4aSElliott Hughes k = a 43*e1fe3e4aSElliott Hughes v = True 44*e1fe3e4aSElliott Hughes else: 45*e1fe3e4aSElliott Hughes k = a[:i] 46*e1fe3e4aSElliott Hughes if k[-1] in "-+": 47*e1fe3e4aSElliott Hughes op = k[-1] + "=" # Ops is '-=' or '+=' now. 48*e1fe3e4aSElliott Hughes k = k[:-1] 49*e1fe3e4aSElliott Hughes v = a[i + 1 :] 50*e1fe3e4aSElliott Hughes ok = k 51*e1fe3e4aSElliott Hughes k = k.replace("-", "_") 52*e1fe3e4aSElliott Hughes if not hasattr(self, k): 53*e1fe3e4aSElliott Hughes if ignore_unknown is True or ok in ignore_unknown: 54*e1fe3e4aSElliott Hughes ret.append(orig_a) 55*e1fe3e4aSElliott Hughes continue 56*e1fe3e4aSElliott Hughes else: 57*e1fe3e4aSElliott Hughes raise self.UnknownOptionError("Unknown option '%s'" % a) 58*e1fe3e4aSElliott Hughes 59*e1fe3e4aSElliott Hughes ov = getattr(self, k) 60*e1fe3e4aSElliott Hughes if isinstance(ov, bool): 61*e1fe3e4aSElliott Hughes v = bool(v) 62*e1fe3e4aSElliott Hughes elif isinstance(ov, int): 63*e1fe3e4aSElliott Hughes v = int(v) 64*e1fe3e4aSElliott Hughes elif isinstance(ov, list): 65*e1fe3e4aSElliott Hughes vv = v.split(",") 66*e1fe3e4aSElliott Hughes if vv == [""]: 67*e1fe3e4aSElliott Hughes vv = [] 68*e1fe3e4aSElliott Hughes vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv] 69*e1fe3e4aSElliott Hughes if op == "=": 70*e1fe3e4aSElliott Hughes v = vv 71*e1fe3e4aSElliott Hughes elif op == "+=": 72*e1fe3e4aSElliott Hughes v = ov 73*e1fe3e4aSElliott Hughes v.extend(vv) 74*e1fe3e4aSElliott Hughes elif op == "-=": 75*e1fe3e4aSElliott Hughes v = ov 76*e1fe3e4aSElliott Hughes for x in vv: 77*e1fe3e4aSElliott Hughes if x in v: 78*e1fe3e4aSElliott Hughes v.remove(x) 79*e1fe3e4aSElliott Hughes else: 80*e1fe3e4aSElliott Hughes assert 0 81*e1fe3e4aSElliott Hughes 82*e1fe3e4aSElliott Hughes opts[k] = v 83*e1fe3e4aSElliott Hughes self.set(**opts) 84*e1fe3e4aSElliott Hughes 85*e1fe3e4aSElliott Hughes return ret 86