Example #1
0
    def get_provider_formatters(self, provider, prefix='fake.',
                                with_args=True, with_defaults=True):

        formatters = {}

        for name, method in inspect.getmembers(provider, inspect.ismethod):

            # skip 'private' method and inherited methods
            if name.startswith('_') or name in self.already_generated:
                continue

            arguments = []

            if with_args:
                # retrieve all parameter
                argspec = inspect.getargspec(method)

                lst = [x for x in argspec.args if x not in ['self', 'cls']]
                for i, arg in enumerate(lst):

                    if argspec.defaults and with_defaults:

                        try:
                            default = argspec.defaults[i]
                            if utils.is_string(default):
                                default = utils.quote(default)
                            else:
                                # TODO check default type
                                default = "{0}".format(default)

                            arg = "{0}={1}".format(arg, default)

                        except IndexError:
                            pass

                    arguments.append(arg)
                    if with_args == 'first':
                        break

                if with_args != 'first':
                    if argspec.varargs:
                        arguments.append('*' + argspec.varargs)
                    if argspec.keywords:
                        arguments.append('**' + argspec.keywords)

            # build fake method signature
            signature = "{0}{1}({2})".format(prefix,
                                             name,
                                             ", ".join(arguments))

            # make a fake example
            example = self.generator.format(name)

            formatters[signature] = example

            self.max_name_len = max(self.max_name_len, len(signature))
            self.already_generated.append(name)

        return formatters
Example #2
0
    def get_provider_formatters(self,
                                provider,
                                prefix='fake.',
                                with_args=True,
                                with_defaults=True):

        formatters = {}

        for name, method in inspect.getmembers(provider, inspect.ismethod):
            # skip 'private' method and inherited methods
            if name.startswith('_') or name in self.already_generated:
                continue

            arguments = []
            faker_args = []
            faker_kwargs = {}

            if name == 'binary':
                faker_kwargs['length'] = 1024

            if with_args:
                # retrieve all parameter
                argspec = getargspec(method)

                lst = [x for x in argspec.args if x not in ['self', 'cls']]
                for i, arg in enumerate(lst):

                    if argspec.defaults and with_defaults:

                        try:
                            default = argspec.defaults[i]
                            if utils.is_string(default):
                                default = utils.quote(default)
                            else:
                                # TODO check default type
                                default = "{0}".format(default)

                            arg = "{0}={1}".format(arg, default)

                        except IndexError:
                            pass

                    arguments.append(arg)
                    if with_args == 'first':
                        break

                if with_args != 'first':
                    if argspec.varargs:
                        arguments.append('*' + argspec.varargs)
                    if argspec.varkw:
                        arguments.append('**' + argspec.varkw)

            # build fake method signature
            signature = "{0}{1}({2})".format(prefix, name,
                                             ", ".join(arguments))

            # make a fake example
            example = self.generator.format(name, *faker_args, **faker_kwargs)

            formatters[signature] = example

            self.max_name_len = max(self.max_name_len, len(signature))
            self.already_generated.append(name)

        return formatters