import inspect import os, re import sys import __main__ global log log = None # dprint constants ALWAYS_PRINT = 1 LOG_ONLY = 2 def LineNumber(): return inspect.currentframe().f_back.f_lineno def dprint(statement, flag=0): if flag == True: flag = ALWAYS_PRINT global log if not(__main__.__dict__.has_key('SimEnvironment')): print statement return env = __main__.__dict__['SimEnvironment'] if getattr(env, "SimLib", None) == None: print statement return lib = env.SimLib if log == None: log_dir = lib.BuildPath([env.ETC_PATH, "log"]) if not(os.path.exists(log_dir)): try: os.makedirs(log_dir) except OSError, e: print "Error: could not create logging directory \"%s\", %s" % (log_dir, e) sys.exit(1) log_path = lib.BuildPath([log_dir, "simfactory.log"]) try: log = open(log_path, "a+") except: print "Could not open log file \"%s\" for writing" % log_path sys.exit(1) log.write("[LOG] %s\n" % statement) log.flush() if flag == LOG_ONLY: return if flag == ALWAYS_PRINT or env.VERBOSE == True: print statement return def ReConvert(expression): if expression == None: return None classes = [ '[:alnum:]', '[:alpha:]', '[:digit:]', '[:word:]', '[:xdigit:]', '[:upper:]', '[:space:]', '[:lower:]', '[:blank:]'] equivs = [ 'a-zA-Z0-9', 'a-zA-Z', '0-9', 'A-Za-z0-9_', 'A-Fa-f0-9', 'A-Z', ' \t\r\n\v\f', 'a-z', ' \t'] for i in range(len(classes)): expression = expression.replace(classes[i], equivs[i]) return expression def FileExists(file): if file == None: return False return os.path.exists(file) def Matches(pattern, value): pattern = ReConvert(pattern) p = re.compile(pattern) m = p.search(value) if m == None: return False return True def BuildWithSpace(str, numspaces): num_spaces = numspaces - len(str) fullString = str for i in range(num_spaces): fullString = "%s " % fullString return fullString def BuildStartEndString(start, end, numspaces): num_spaces = numspaces - len(start) fullString = start for i in range(num_spaces): fullString = "%s " % fullString return "%s%s" % (fullString, end) def PrintManyLeadingSpace(str, leading): lines = str.split("\n") for line in lines: for i in range(leading): print " ", print line def CoerseBool(val): true_values = ['True', 'true', 't', '1', 1, True] for v in true_values: if val == v: return True return False