2010-11-08 04:40:41 -06:00
|
|
|
'''Appy allows you to create easily complete applications in Python.'''
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2009-10-18 07:52:27 -05:00
|
|
|
import os.path
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2010-11-08 04:40:41 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
2009-10-18 07:52:27 -05:00
|
|
|
def getPath(): return os.path.dirname(__file__)
|
2010-02-01 04:09:26 -06:00
|
|
|
def versionIsGreaterThanOrEquals(version):
|
|
|
|
'''This method returns True if the current Appy version is greater than or
|
|
|
|
equals p_version. p_version must have a format like "0.5.0".'''
|
|
|
|
import appy.version
|
|
|
|
if appy.version.short == 'dev':
|
|
|
|
# We suppose that a developer knows what he is doing, so we return True.
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
paramVersion = [int(i) for i in version.split('.')]
|
2010-02-04 07:41:51 -06:00
|
|
|
currentVersion = [int(i) for i in appy.version.short.split('.')]
|
2010-02-01 04:09:26 -06:00
|
|
|
return currentVersion >= paramVersion
|
2010-11-08 04:40:41 -06:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Object:
|
|
|
|
'''At every place we need an object, but without any requirement on its
|
|
|
|
class (methods, attributes,...) we will use this minimalist class.'''
|
|
|
|
def __init__(self, **fields):
|
2015-10-27 15:10:24 -05:00
|
|
|
for k, v in fields.items():
|
2010-11-08 04:40:41 -06:00
|
|
|
setattr(self, k, v)
|
|
|
|
def __repr__(self):
|
2015-10-27 15:10:24 -05:00
|
|
|
res = '<Object '
|
|
|
|
for attrName, attrValue in self.__dict__.items():
|
2010-11-08 04:40:41 -06:00
|
|
|
v = attrValue
|
|
|
|
if hasattr(v, '__repr__'):
|
|
|
|
v = v.__repr__()
|
|
|
|
try:
|
2015-10-27 15:10:24 -05:00
|
|
|
res += '%s=%s ' % (attrName, v)
|
2010-11-08 04:40:41 -06:00
|
|
|
except UnicodeDecodeError:
|
2015-10-27 15:10:24 -05:00
|
|
|
res += '%s=<encoding problem> ' % attrName
|
2010-11-08 04:40:41 -06:00
|
|
|
res = res.strip() + '>'
|
|
|
|
return res.encode('utf-8')
|
2015-10-27 15:10:24 -05:00
|
|
|
def __bool__(self):
|
|
|
|
return bool(self.__dict__)
|
2013-05-10 05:16:57 -05:00
|
|
|
def get(self, name, default=None): return getattr(self, name, default)
|
2014-11-20 02:47:37 -06:00
|
|
|
def __getitem__(self, k): return getattr(self, k)
|
2013-08-21 05:35:30 -05:00
|
|
|
def update(self, other):
|
2015-10-27 15:10:24 -05:00
|
|
|
'''Includes information from p_other into p_self.'''
|
|
|
|
for k, v in other.__dict__.items():
|
2013-08-21 05:35:30 -05:00
|
|
|
setattr(self, k, v)
|
2015-02-19 06:40:12 -06:00
|
|
|
def clone(self):
|
|
|
|
res = Object()
|
|
|
|
res.update(self)
|
|
|
|
return res
|
2014-03-25 16:59:06 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Hack:
|
|
|
|
'''This class proposes methods for patching some existing code with
|
|
|
|
alternative methods.'''
|
|
|
|
@staticmethod
|
2014-05-03 08:18:41 -05:00
|
|
|
def patch(method, replacement, klass=None):
|
2014-03-25 16:59:06 -05:00
|
|
|
'''This method replaces m_method with a p_replacement method, but
|
|
|
|
keeps p_method on its class under name
|
|
|
|
"_base_<initial_method_name>_". In the patched method, one may use
|
2014-05-03 08:18:41 -05:00
|
|
|
Hack.base to call the base method. If p_method is static, you must
|
|
|
|
specify its class in p_klass.'''
|
2015-10-27 15:10:24 -05:00
|
|
|
# Get the class on which the surgery will take place.
|
2014-05-03 08:18:41 -05:00
|
|
|
isStatic = klass
|
2015-10-27 15:10:24 -05:00
|
|
|
klass = klass or method.__self__.__class__
|
|
|
|
# On this class, store m_method under its "base" name.
|
|
|
|
name = isStatic and method.__name__ or method.__func__.__name__
|
2014-03-25 16:59:06 -05:00
|
|
|
baseName = '_base_%s_' % name
|
2015-03-14 05:26:26 -05:00
|
|
|
if isStatic:
|
|
|
|
# If "staticmethod" isn't called hereafter, the static functions
|
|
|
|
# will be wrapped in methods.
|
|
|
|
method = staticmethod(method)
|
|
|
|
replacement = staticmethod(replacement)
|
2014-03-25 16:59:06 -05:00
|
|
|
setattr(klass, baseName, method)
|
|
|
|
setattr(klass, name, replacement)
|
|
|
|
|
|
|
|
@staticmethod
|
2014-05-03 08:18:41 -05:00
|
|
|
def base(method, klass=None):
|
|
|
|
'''Allows to call the base (replaced) method. If p_method is static,
|
|
|
|
you must specify its p_klass.'''
|
|
|
|
isStatic = klass
|
2015-10-27 15:10:24 -05:00
|
|
|
klass = klass or method.__self__.__class__
|
|
|
|
name = isStatic and method.__name__ or method.__func__.__name__
|
2015-03-14 05:26:26 -05:00
|
|
|
return getattr(klass, '_base_%s_' % name)
|
2014-11-22 06:14:52 -06:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def inject(patchClass, klass, verbose=False):
|
|
|
|
'''Injects any method or attribute from p_patchClass into klass.'''
|
|
|
|
patched = []
|
|
|
|
added = []
|
2015-10-27 15:10:24 -05:00
|
|
|
for name, attr in patchClass.__dict__.iteritems():
|
2014-11-22 06:14:52 -06:00
|
|
|
if name.startswith('__'): continue # Ignore special methods
|
2015-03-14 05:26:26 -05:00
|
|
|
# Unwrap functions from static methods
|
|
|
|
if attr.__class__.__name__ == 'staticmethod':
|
|
|
|
attr = attr.__get__(attr)
|
|
|
|
static = True
|
|
|
|
else:
|
|
|
|
static = False
|
2014-11-22 06:14:52 -06:00
|
|
|
# Is this name already defined on p_klass ?
|
|
|
|
if hasattr(klass, name):
|
|
|
|
hasAttr = True
|
|
|
|
klassAttr = getattr(klass, name)
|
|
|
|
else:
|
|
|
|
hasAttr = False
|
|
|
|
klassAttr = None
|
|
|
|
if hasAttr and callable(attr) and callable(klassAttr):
|
|
|
|
# Patch this method via Hack.patch
|
2015-03-14 05:26:26 -05:00
|
|
|
if static:
|
|
|
|
Hack.patch(klassAttr, attr, klass)
|
|
|
|
else:
|
|
|
|
Hack.patch(klassAttr, attr)
|
2014-11-22 06:14:52 -06:00
|
|
|
patched.append(name)
|
|
|
|
else:
|
|
|
|
# Simply replace the static attr or add the new static
|
|
|
|
# attribute or method.
|
|
|
|
setattr(klass, name, attr)
|
|
|
|
added.append(name)
|
|
|
|
if verbose:
|
|
|
|
pName = patchClass.__name__
|
|
|
|
cName = klass.__name__
|
2015-10-27 16:36:51 -05:00
|
|
|
print('%d method(s) patched from %s to %s (%s)' % \
|
|
|
|
(len(patched), pName, cName, str(patched)))
|
|
|
|
print('%d method(s) and/or attribute(s) added from %s to %s (%s)'%\
|
|
|
|
(len(added), pName, cName, str(added)))
|
2010-11-08 04:40:41 -06:00
|
|
|
# ------------------------------------------------------------------------------
|