def safe_eval(expr): toks = re.split(r'([a-zA-Z_\.]+|.)', expr) for t in toks: if len(t) > 1 and t not in safeCmds: error("Illegal/Unknown operator '%s' in $()." % t) try: return eval(expr) except: error("Illegal operation in '%s'." % expr)
def checkTrackType(name): if _P().TRACKTYPES is None or len(_P().TRACKTYPES) == 0: return for t in _P().TRACKTYPES: if name.upper() == t.upper() or name.upper().startswith(t.upper() + "-"): return if len(_P().TRACKTYPES) == 1: error("Plugin {}: track type must be {}.".format(_P().NAME, _P().TRACKTYPES[0])) else: error("Plugin {}: track type must be one of {}.".format(_P().NAME, ", ".join(_P().TRACKTYPES[:-1]) + " or " + _P().TRACKTYPES[-1]))
def checkTrackType(name): if _P().TRACKTYPES is None or len(_P().TRACKTYPES) == 0: return for t in _P().TRACKTYPES: if name.upper() == t.upper() or name.upper().startswith(t.upper() + "-"): return if len(_P().TRACKTYPES) == 1: error("Plugin {}: track type must be {}.".format(_P().NAME, _P().TRACKTYPES[0])) else: error("Plugin {}: track type must be one of {}.".format(_P().NAME, ", ".join(_P().TRACKTYPES[:-1]) + " or " + _P().TRACKTYPES[-1]))
def safe_eval(expr): #if expr.startswith( '_UNSAFE_'): # if environ.has_key('MMA_SAFE'): # return eval(expr[8:]) toks = re.split(r'([a-zA-Z_\.]+|.)', expr) for t in toks: if len(t) > 1 and t not in safeCmds: error("Illegal/Unknown operator '%s' in $()." % t) try: return eval(expr) except: error("Illegal operation in '%s'." % expr)
def safe_eval(expr): #if expr.startswith( '_UNSAFE_'): # if environ.has_key('MMA_SAFE'): # return eval(expr[8:]) toks = re.split(r'([a-zA-Z_\.]+|.)', expr) for t in toks: if len(t) > 1 and t not in safeCmds: error("Illegal/Unknown operator '%s' in $()." % t) try: return eval(expr) except: error("Illegal operation in '%s'." % expr)
def setMinMMAVersion(major, minor): vre = re.compile("([0-9]+)\\.([0-9]+)\\.?(.*)") m = vre.match(gbl.version) if m is None: error("Plugin utils: version %s not recognized." % gbl.version) cur_major = int(m.group(1)) cur_minor = int(m.group(2)) cur_rev = m.group(3) if cur_major > major: return if cur_major == major and cur_minor >= minor: return error("Plugin requires MMA version {:02}.{:02} or newer.".format(major, minor))
def setMinMMAVersion(major, minor): vre = re.compile("([0-9]+)\\.([0-9]+)\\.?(.*)") m = vre.match(gbl.version) if m is None: error("Plugin utils: version %s not recognized." % gbl.version) cur_major = int(m.group(1)) cur_minor = int(m.group(2)) cur_rev = m.group(3) if cur_major > major: return if cur_major == major and cur_minor >= minor: return error("Plugin requires MMA version {:02}.{:02} or newer.".format(major, minor))
def parseCommandLine(line, allowUnknown=False): s = " ".join(line) subst = [ [" =", "="], ["= ", "="], [" ", " "], [", ", ","], [" ,", ","], ] for s1, s2 in subst: while s1 in s: s = s.replace(s1, s2) res = {name: default for name, default, _ in _P().ARGUMENTS} positional = True for name, default, _ in _P().ARGUMENTS: res[name] = default for i, value in enumerate(s.split(",")): if value == "": continue if "=" in value: positional = False n, _, v = value.partition("=") if n not in res: if not allowUnknown: error("Plugin {}: unexpected argument name {}.".format( _P().NAME, n)) else: n = n.upper() elif positional: if i >= len(_P().ARGUMENTS): error("Plugin {}: unexpected argument provided ({}).".format( _P().NAME, value)) n, _, _ = _P().ARGUMENTS[i] v = value else: error( "Plugin {}: positional argument after named argument is not allowed." .format(_P().NAME)) res[n] = v for k, v in res.items(): if v is None: printUsage() error("Plugin {}: no value for argument {}.".format(_P().NAME, k)) return res
def parseCommandLine(line, allowUnknown=False): s = " ".join(line) subst = [ [" =", "="], ["= ", "="], [" ", " "], [", ", ","], [" ,", ","], ] for s1, s2 in subst: while s1 in s: s = s.replace(s1, s2) res = {name:default for name, default, _ in _P().ARGUMENTS} positional = True for name, default, _ in _P().ARGUMENTS: res[name] = default for i, value in enumerate(s.split(",")): if value == "": continue if "=" in value: positional = False n, _, v = value.partition("=") if n not in res: if not allowUnknown: error("Plugin {}: unexpected argument name {}.".format(_P().NAME, n)) else: n = n.upper() elif positional: if i >= len(_P().ARGUMENTS): error("Plugin {}: unexpected argument provided ({}).".format(_P().NAME, value)) n, _, _ = _P().ARGUMENTS[i] v = value else: error("Plugin {}: positional argument after named argument is not allowed.".format(_P().NAME)) res[n] = v for k, v in res.items(): if v is None: printUsage() error("Plugin {}: no value for argument {}.".format(_P().NAME, k)) return res
def setDebug(ln): """ Set debugging options dynamically. """ # This needs to be here to avoid circular import problem from MMA.common import opt2pair from MMA.common import dPrint global Ldebug, debug, LshowFilenames, showFilenames, \ Lpshow, pshow, Lseqshow, seqshow, Lshowrun, showrun, \ LnoWarn, noWarn, LnoOutput, noOutput, LshowExpand, showExpand, \ Lchshow, chshow, LplecShow, plecShow, LrmShow, rmShow, \ LgvShow, gvShow msg = ("Debug: Use MODE=On/Off where MODE is one or more of " "DEBUG, FILENAMES, PATTERNS, SEQUENCE, GROOVE, " "RUNTIME, WARNINGS, EXPAND, ROMAN or PLECTRUM.") if not len(ln): from MMA.common import error error(msg) # save current flags Ldebug = debug LshowFilenames = showFilenames Lpshow = pshow Lseqshow = seqshow Lshowrun = showrun LnoWarn = noWarn LnoOutput = noOutput LshowExpand = showExpand Lchshow = chshow LplecShow = plecShow LrmShow = rmShow LgvShow = gvShow ln, opts = opt2pair(ln, 1) if ln: error("Each debug option must be a opt=value pair.") for cmd, val in opts: if val == 'ON' or val == '1': val = 1 elif val == 'OFF' or val == '0': val = 0 else: error("Debug: %s needs ON, 1, OFF, or 0 arg." % cmd) if cmd == 'DEBUG': debug = val if debug: dPrint("Debug=%s." % val) elif cmd == 'FILENAMES': showFilenames = val if debug: dPrint("ShowFilenames=%s." % val) elif cmd == 'PATTERNS': pshow = val if debug: dPrint("Pattern display=%s." % val) elif cmd == 'SEQUENCE': seqshow = val if debug: dPrint("Sequence display=%s." % val) elif cmd == 'RUNTIME': showrun = val if debug: dPrint("Runtime display=%s." % val) elif cmd == 'WARNINGS': noWarn = not (val) if debug: dPrint("Warning display=%s" % val) elif cmd == 'EXPAND': showExpand = val if debug: dPrint("Expand display=%s." % val) elif cmd == 'ROMAN': rmShow = val if debug: dPrint("Roman numeral chords/slash display=%s" % val) elif cmd == 'GROOVE': gvShow = val if debug: dPrint("Groove re-define display=%s" % val) elif cmd == 'PLECTRUM': plecShow = val if debug: dPrint("Plectrum display=%s" % val) else: from MMA.common import error error(msg)