示例#1
0
文件: support.py 项目: msander/lepl
    def __args_as_attributes(self):
        '''
        Validate the arguments passed to the constructor against the spec for 
        the factory (necessary because we use *args and so the user doesn't
        get the feedback they will expect if they make a mistake).  As a side
        effect we also associated arguments with names and expand defaults
        so that attributes are more predictable.
        '''
        try:
            # function wrapper, so we have two levels, and we must construct
            # a new, empty function (ie this is a fake function that helps
            # us use the code below, even though there's no arguments because
            # factory is a dummy generated in make_factory below)
            def empty():
                return

            document(empty, self.factory.factory)
            spec = getargspec(empty)
        except:
            spec = getargspec(self.factory)
        names = list(spec.args)
        defaults = dict(
            zip(names[::-1], spec.defaults[::-1] if spec.defaults else []))
        for name in names:
            if name in self.__kargs:
                self._karg(**{name: self.__kargs[name]})
                del self.__kargs[name]
            elif self.__args:
                self._arg(**{name: self.__args[0]})
                self.__args = self.__args[1:]
            elif name in defaults:
                self._karg(**{name: defaults[name]})
            else:
                raise TypeError(
                    fmt("No value for argument '{0}' in "
                        "{1}(...)", name, self._small_str))
        if self.__args:
            if spec.varargs:
                self._args(**{spec.varargs: self.__args})
            else:
                raise TypeError(
                    fmt(
                        "No parameter matches the argument "
                        "{0!r} in {1}(...)", self.__args[0], self._small_str))
        if self.__kargs:
            if spec.keywords:
                self.__kargs(**{spec.keywords: self.__kargs})
            else:
                name = list(self.__kargs.keys())[0]
                value = self.__kargs[name]
                raise TypeError(
                    fmt(
                        "No parameter matches the argument "
                        "{0}={1!r} in {2}(...)", name, value, self._small_str))
示例#2
0
 def __args_as_attributes(self):
     '''
     Validate the arguments passed to the constructor against the spec for 
     the factory (necessary because we use *args and so the user doesn't
     get the feedback they will expect if they make a mistake).  As a side
     effect we also associated arguments with names and expand defaults
     so that attributes are more predictable.
     '''
     try:
         # function wrapper, so we have two levels, and we must construct
         # a new, empty function (ie this is a fake function that helps
         # us use the code below, even though there's no arguments because
         # factory is a dummy generated in make_factory below)
         def empty(): return
         document(empty, self.factory.factory)
         spec = getargspec(empty)
     except:
         spec = getargspec(self.factory)
     names = list(spec.args)
     defaults = dict(zip(names[::-1], spec.defaults[::-1] if spec.defaults else []))
     for name in names:
         if name in self.__kargs:
             self._karg(**{name: self.__kargs[name]})
             del self.__kargs[name]
         elif self.__args:
             self._arg(**{name: self.__args[0]})
             self.__args = self.__args[1:]
         elif name in defaults:
             self._karg(**{name: defaults[name]})
         else:
             raise TypeError(fmt("No value for argument '{0}' in "
                                    "{1}(...)", 
                                    name, self._small_str))
     if self.__args:
         if spec.varargs:
             self._args(**{spec.varargs: self.__args})
         else:
             raise TypeError(fmt("No parameter matches the argument "
                                    "{0!r} in {1}(...)", 
                                    self.__args[0], self._small_str))
     if self.__kargs:
         if spec.keywords:
             self.__kargs(**{spec.keywords: self.__kargs})
         else:
             name = list(self.__kargs.keys())[0]
             value = self.__kargs[name]
             raise TypeError(fmt("No parameter matches the argument "
                                    "{0}={1!r} in {2}(...)", 
                                    name, value, self._small_str))
示例#3
0
文件: support.py 项目: msander/lepl
def make_factory(maker, matcher):
    '''
    A helper function that assembles a matcher from a wrapper class and 
    a function that contains the logic.
    
    This works by generating a dummy factory and delegating to 
    `make_wrapper_factory`.
    '''
    def factory(*args, **kargs):
        if args or kargs:
            raise TypeError(fmt('{0}() takes no arguments', matcher.__name__))
        return matcher

    document(factory, matcher)
    factory.factory = matcher
    return maker(factory)
示例#4
0
def make_factory(maker, matcher):
    '''
    A helper function that assembles a matcher from a wrapper class and 
    a function that contains the logic.
    
    This works by generating a dummy factory and delegating to 
    `make_wrapper_factory`.
    '''
    def factory(*args, **kargs):
        if args or kargs:
            raise TypeError(format('{0}() takes no arguments', 
                                   matcher.__name__))
        return matcher
    document(factory, matcher)
    factory.factory = matcher
    return maker(factory)
示例#5
0
def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first, start, stop, rest=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest)
    return document(new_factory, factory)
示例#6
0
def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first, start, stop, rest=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest)

    return document(new_factory, factory)
示例#7
0
def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first, start, stop, rest=None,
                    generator_manager_queue_len=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest, generator_manager_queue_len)
    return document(new_factory, factory)
示例#8
0
def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first,
                    start,
                    stop,
                    rest=None,
                    generator_manager_queue_len=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest, generator_manager_queue_len)

    return document(new_factory, factory)