fabric/fabric — pull request #486 by koder-ua (2011-11-24 00:24:58+00:00)
Title:  _crawl will not try to map 'a.b' to mapping['a']['b'] if mapping['a.b'] exists

_crawl will not try to map 'a.b' to mapping['a']['b'] if mapping['a.b'] exists

This allows export class method as fabric recipes using
ClassName.methodName in command line

#-------------------------------------------------
# example
#-------------------------------------------------

def recipe(name=None):
if not isinstance(name, basestring):
func = name
func._recipe__ = func.__name__
return func

def closure(func):
func._recipe__ = name if name is not None else func.__name__
return func
return closure

class FabricMeta(type):
def __new__(cls, name, bases, dict):
new_cls = type.__new__(cls, name, bases, dict)
obj = None
up_globals = inspect.currentframe(1).f_globals
for fname, val in dict.items():
recipe = getattr(val, '_recipe__', None)
if recipe is not None:
if obj is None:
obj = new_cls()
up_globals['{0}.{1}'.format(name, recipe)] = getattr(obj, fname)
return new_cls

class FabricBase(object):
__metaclass__ = FabricMeta

class MyTestFabricClass(FabricBase):
@recipe
def recipe1(self):
print "recipe1"

@recipe("recipe2")
def tt(self):
print "tt"

# as result MyTestFabricClass.recipe1 and
# MyTestFabricClass.recipe2
# would be available as fabric recipes from cmd line