Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/tashi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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']):
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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"""
Expand All @@ -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):
Expand All @@ -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__"""
Expand All @@ -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"""
Expand All @@ -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"""
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -272,13 +289,15 @@ def stringPartition(s, field):
r = s[index+len(field):]
return (l, sep, r)


def scrubString(s, allowed="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_."):
ns = ""
for c in s:
if (c in allowed):
ns = ns + c
return ns


class Connection:

def __init__(self, host, port, authAndEncrypt=False, credentials=None):
Expand Down