Esempio n. 1
0
def applyWithTypeConversion(callable_method, request, **kwargs):
    '''
        Currently useful kwargs:
            `resource` => instance of the resource executing
    '''
    types = getattr(callable_method, 'types', {})
    returnConverter = getattr(callable_method, 'returnConverter', None)
    inputConverter = getattr(callable_method, 'inputConverter', None)

    fs = request.get_field_storage()
    parameters = storage_to_dict(fs)

    if inputConverter:
        if not isinstance(inputConverter, (list, tuple)):
            inputConverter = (inputConverter, )

        for conv in inputConverter:
            rc = conv(kwargs.get('resource'), request, parameters)
            parameters = rc or parameters

    try:
        converted = {}
        for (key, value) in types.items():
            request.target_name = key
            request.target = value
            if parameters.has_key(key):
                val = parameters[key]
            else:
                val = getattr(value, 'default', None)

            try:
                val = convertTypes(request, val)
            except (TypeError, ValueError):
                val = None

            if val is not None:
                converted[key] = val

        result = callable_method(**converted)
    except TypeError:
        raise
    if result is None:
        result = ''
    if not returnConverter:
        return False, result

    if not isinstance(returnConverter, (list, tuple)):
        returnConverter = (returnConverter, )

    for conv in returnConverter:
        result = conv(result)

    return True, result
Esempio n. 2
0
def elementType( eLetter ):
    """
    Classify an atom as polar or unpolar::
      atomType( eLetter ) -> list of types this element belongs to

    @param eLetter: atom name
    @type  eLetter: str
    
    @return: return 'p' for polar, 'u' for unpolar and None if not
             in classified
    @rtype: p|u OR None
    """
    types = {'p' : ['N','O','H','Cl'],  ## polar
             'u' : ['C','S'] }          ## unpolar

    for key, values in types.items():
        if eLetter in values:
            return key
    return None
        def new_f(*args, **kwds):
            argument_types = ", ".join( "%s=%s" %(v,t) for v,t in types.items() )
            for i,v in enumerate(args):
                if types.has_key(f.func_code.co_varnames[i]) and \
                    not isinstance(v, types[f.func_code.co_varnames[i]]):
                    raise exception("Function argument '%s' with a "
                                    "value of %r does not match the allowed "
                                    "types %s. \n           The arguments "
                                    "of this function have allowed types of %s" % \
                        (f.func_code.co_varnames[i],v,
                         types[f.func_code.co_varnames[i]],argument_types))
                    del types[f.func_code.co_varnames[i]]

            for k,v in kwds.iteritems():
                if types.has_key(k) and not isinstance(v, types[k]):
                    raise exception("Function argument '%s' with a "
                                    "value of %r does not match one of the allowed "
                                    "types %s. \n           The arguments "
                                    "of this function have allowed types of %s" % \
                        (k,v,types[k],argument_types))

            return f(*args, **kwds)
Esempio n. 4
0
        def new_f(*args, **kwds):
            argument_types = ", ".join("%s=%s" % (v, t)
                                       for v, t in types.items())
            for i, v in enumerate(args):
                if types.has_key(f.func_code.co_varnames[i]) and \
                    not isinstance(v, types[f.func_code.co_varnames[i]]):
                    raise exception("Function argument '%s' with a "
                                    "value of %r does not match the allowed "
                                    "types %s. \n           The arguments "
                                    "of this function have allowed types of %s" % \
                        (f.func_code.co_varnames[i],v,
                         types[f.func_code.co_varnames[i]],argument_types))
                    del types[f.func_code.co_varnames[i]]

            for k, v in kwds.iteritems():
                if types.has_key(k) and not isinstance(v, types[k]):
                    raise exception("Function argument '%s' with a "
                                    "value of %r does not match one of the allowed "
                                    "types %s. \n           The arguments "
                                    "of this function have allowed types of %s" % \
                        (k,v,types[k],argument_types))

            return f(*args, **kwds)
        def new_f(*args, **kwds):
            argument_types = ", ".join( "%s=%s" %(v,t) for v,t in types.items() )
            for i,v in enumerate(args): # no need to check self argument
                if i == 0 : continue
                if types.has_key(f.func_code.co_varnames[i]) and \
                    not isinstance(v, types[f.func_code.co_varnames[i]]):
                    raise exception("Method argument '%s' with a "
                                    "value of %r does not match the "
                                    "allowed types %s. \n           The "
                                    "arguments of this method have "
                                    "allowed types of %s" % \
                        (f.func_code.co_varnames[i],v,types[f.func_code.co_varnames[i]],argument_types))
                    del types[f.func_code.co_varnames[i]]

            for k,v in kwds.iteritems():
                if types.has_key(k) and not isinstance(v, types[k]):
                    raise exception("Method argument '%s' with a "
                                    "value of %r does not match one "
                                    "of the allowed types %s. \n           The "
                                    "arguments of this method have allowed types of %s" % \
                        (k,v,types[k],argument_types))

            return f(*args, **kwds)
Esempio n. 6
0
        def new_f(*args, **kwds):
            argument_types = ", ".join("%s=%s" % (v, t)
                                       for v, t in types.items())
            for i, v in enumerate(args):  # no need to check self argument
                if i == 0: continue
                if types.has_key(f.func_code.co_varnames[i]) and \
                    not isinstance(v, types[f.func_code.co_varnames[i]]):
                    raise exception("Method argument '%s' with a "
                                    "value of %r does not match the "
                                    "allowed types %s. \n           The "
                                    "arguments of this method have "
                                    "allowed types of %s" % \
                        (f.func_code.co_varnames[i],v,types[f.func_code.co_varnames[i]],argument_types))
                    del types[f.func_code.co_varnames[i]]

            for k, v in kwds.iteritems():
                if types.has_key(k) and not isinstance(v, types[k]):
                    raise exception("Method argument '%s' with a "
                                    "value of %r does not match one "
                                    "of the allowed types %s. \n           The "
                                    "arguments of this method have allowed types of %s" % \
                        (k,v,types[k],argument_types))

            return f(*args, **kwds)
Esempio n. 7
0
 def set_properties(cls, types={}, descriptor=UnitProperty):
     """Set Unit Properties for cls."""
     for key, typ in types.items():
         cls.set_property(key, typ, False, descriptor)