Exemplo n.º 1
0
        def addDefaultValue((name, type)):
            if name in defaults:
                comment = Composable.getComment(type)
                type = Composable.getValue(type)
                if Grimoire.Utils.isDescendant(type, Derived.ValuedType):
                    restricted = Grimoire.Utils.isDescendant(type, Derived.RestrictedType)
                    resvalues = []
                    try:
                        defaultvalues = defaults[name].values
                    except:
                        defaultvalues = defaults[name]
                    for value in defaultvalues:
                        if extendResticted or not restricted or value in type.values:
                            resvalues.append(value)
                    for value in type.values:
                        if value not in resvalues:
                            resvalues.append(value)

                    class type(type):
                        values = tuple(resvalues)

                else:
                    type = Derived.HintedType.derive(type, defaults[name])
                if comment is not None:
                    type = Composable.AnnotatedValue(type, comment)
            return (name, type)
Exemplo n.º 2
0
    def __init__(self, args=[], extraArgs=[], kws={}, checkTypes=1, listify=0, falseAsAbsent=0, convert=1):
        """Compiles a set of arguments, that is, either check their
        types, or convert them to the correct types.

        If listify, args contains all required arguments and all
        optional up to the first one not included (any one after that
        one is in kws), otherwize args is empty.

        Exceptions: checkTypes will raise a TypeError for any argument
        of the wrong type.
        """
        if Grimoire.Utils.isInstance(args, GenericParamsType):
            extraArgs = args.extraArgs
            kws = args.kws
            args = args.args
        paramsType = type(self)
        self.args = []
        self.extraArgs = []
        self.kws = {}

        # Put all arguments in the order they will be returned in if listify == 0
        self.kws.update(kws)
        for (name, t), arg in Grimoire.Utils.Zip(paramsType.arglist, args):
            self.kws[name] = arg
        if len(args) > len(paramsType.arglist):
            self.extraArgs = args[len(paramsType.arglist) :]
        self.extraArgs += extraArgs

        # Check/convert types
        for name in self.kws.keys():
            arg = self.kws[name]
            if name in paramsType.argdict:
                t = paramsType.argdict[name]
            else:
                t = paramsType.reskwtype
            t = Composable.getValue(t)
            if not t:
                raise TypeError("Unknown keyword argument " + name)
            if not checkTypes:
                try:
                    self.kws[name] = t(arg)
                except Exception, e:
                    if falseAsAbsent and not arg:
                        del self.kws[name]
                    else:
                        raise e
            elif not Grimoire.Utils.isInstance(arg, t):
                raise TypeError(
                    "Argument %s %s is not of correct type %s"
                    % (name, Grimoire.Utils.objInfo(arg), Grimoire.Utils.objInfo(t))
                )
Exemplo n.º 3
0
     if not t:
         raise TypeError("Unknown keyword argument " + name)
     if not checkTypes:
         try:
             self.kws[name] = t(arg)
         except Exception, e:
             if falseAsAbsent and not arg:
                 del self.kws[name]
             else:
                 raise e
     elif not Grimoire.Utils.isInstance(arg, t):
         raise TypeError(
             "Argument %s %s is not of correct type %s"
             % (name, Grimoire.Utils.objInfo(arg), Grimoire.Utils.objInfo(t))
         )
 t = Composable.getValue(paramsType.resargstype)
 argPos = 0
 while argPos < len(self.extraArgs):
     if not paramsType.resargstype:
         raise TypeError("Too many non-keyword arguments")
     arg = self.extraArgs[argPos]
     if not checkTypes:
         try:
             self.extraArgs[argPos] = t(arg)
         except Exception, e:
             if falseAsAbsent and not arg:
                 del self.extraArgs[argPos]
                 break
             else:
                 raise e
     elif not Grimoire.Utils.isInstance(arg, t):