From 28d8e29cb734d9faf7aab8593db443c4e29a58bc Mon Sep 17 00:00:00 2001 From: Richard Gass Date: Wed, 1 Aug 2012 12:21:42 +0200 Subject: [PATCH] style changes and git testing --- src/tashi/util.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/tashi/util.py b/src/tashi/util.py index c2bd6b8..bc144dd 100644 --- a/src/tashi/util.py +++ b/src/tashi/util.py @@ -35,6 +35,7 @@ from tashi.rpycservices.rpyctypes import TashiException, Errors, InstanceState, HostState from tashi.utils.timeout import * + def broken(oldFunc): """Decorator that is used to mark a function as temporarily broken""" def newFunc(*args, **kw): @@ -47,6 +48,7 @@ def newFunc(*args, **kw): newFunc.__module__ = oldFunc.__module__ return newFunc + def deprecated(oldFunc): """Decorator that is used to deprecate functions""" def newFunc(*args, **kw): @@ -56,8 +58,10 @@ def newFunc(*args, **kw): newFunc.__module__ = oldFunc.__module__ return newFunc + def logged(oldFunc): - """Decorator that is used to log a function's calls -- currently uses sys.stderr""" + """Decorator that is used to log a function's calls + -- currently uses sys.stderr""" def newFunc(*args, **kw): logMsg = "%s(%s, %s) -> " % (oldFunc.__name__, str(args).strip("[]"), str(kw).strip("{}").replace(": ", "=")) sys.stderr.write(logMsg) @@ -77,6 +81,7 @@ def newFunc(*args, **kw): newFunc.__module__ = oldFunc.__module__ return newFunc + def timed(oldFunc): """Decorator that is used to time a function's execution""" def newFunc(*args, **kw): @@ -85,16 +90,18 @@ def newFunc(*args, **kw): res = oldFunc(*args, **kw) finally: finish = time.time() - print "%s: %f" % (oldFunc.__name__, finish-start) + print "%s: %f" % (oldFunc.__name__, finish - start) return res return newFunc + def editAndContinue(filespec, mod, name): def wrapper(oldFunc): persist = {} persist['lastMod'] = time.time() persist['oldFunc'] = oldFunc persist['func'] = oldFunc + def newFunc(*args, **kw): modTime = os.stat(filespec)[8] if (modTime > persist['lastMod']): @@ -106,6 +113,7 @@ def newFunc(*args, **kw): return newFunc return wrapper + class failsafe(object): """Class that attempts to make RPCs, but will fall back to a local object that implements the same methods""" def __attempt__(self, cur, fail): @@ -134,6 +142,7 @@ def __setattr__(self, name, value): def __delattr__(self, name): return delattr(self.__dict__['__current_obj__'], name) + class reference(object): """Class used to create a replacable reference to an object""" @deprecated @@ -152,6 +161,7 @@ def __setattr__(self, name, value): def __delattr__(self, name): return delattr(self.__dict__['__real_obj__'], name) + def signalHandler(signalNumber): """Used to denote a particular function as the signal handler for a specific signal""" @@ -160,6 +170,7 @@ def __decorator(function): return function return __decorator + def boolean(value): """Convert a variable to a boolean""" if (type(value) == types.BooleanType): @@ -181,6 +192,7 @@ def boolean(value): else: raise ValueError + def instantiateImplementation(className, *args): """Create an instance of an object with the given class name and list of args to __init__""" @@ -194,6 +206,7 @@ def instantiateImplementation(className, *args): # XXXstroucki: this is correct, even though pydev complains return _obj + def convertExceptions(oldFunc): """This converts any exception type into a TashiException so that it can be passed over an RPC""" @@ -209,6 +222,7 @@ def newFunc(*args, **kw): raise return newFunc + def getConfig(additionalNames=[], additionalFiles=[]): """Creates many permutations of a list of locations to look for config files and then loads them""" @@ -222,6 +236,7 @@ def getConfig(additionalNames=[], additionalFiles=[]): raise Exception("No config file could be found: %s" % (str(allLocations))) return (config, configFiles) + def __getShellFn(): try: from IPython.Shell import IPShellEmbed @@ -230,6 +245,7 @@ def __getShellFn(): import IPython return (2, IPython.embed) + def debugConsole(globalDict): """A debugging console that optionally uses pysh""" def realDebugConsole(globalDict): @@ -263,6 +279,7 @@ def resetConsole(): if (os.getenv("DEBUG", "0") == "1"): threading.Thread(name="debugConsole", target=lambda: realDebugConsole(globalDict)).start() + def stringPartition(s, field): index = s.find(field) if (index == -1): @@ -272,6 +289,7 @@ def stringPartition(s, field): r = s[index+len(field):] return (l, sep, r) + def scrubString(s, allowed="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."): ns = "" for c in s: @@ -279,6 +297,7 @@ def scrubString(s, allowed="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ns = ns + c return ns + class Connection: def __init__(self, host, port, authAndEncrypt=False, credentials=None):