Пример #1
0
    def generate_skills(self, sd, locale=None, subtype=None, total=None):
        if locale and subtype:
            self.get_skills(locale)
            if subtype not in Skill.data[locale]['skills']:
                raise ParameterError('Not valid subtype')
            skills = Skill.data[locale]['skills'][subtype]

        elif locale and not subtype:
            skills = []
            self.get_skills(locale)
            for subtype in Skill.data[locale]['skills']:
                skills.extend(Skill.data[locale]['skills'][subtype])

        elif subtype and not locale:
            skills = []
            for locale in LOCALES:
                self.get_skills(locale)
                if subtype not in Skill.data[locale]['skills']:
                    raise ParameterError('Not valid subtype')
                skills.extend(Skill.data[locale]['skills'][subtype])

        else:
            skills = self.all_skills()

        random.shuffle(skills)

        if total:
            if not isinstance(total, int):
                raise ParameterError('Not valid total')
            return skills[0:total]

        return skills
    def image_stream(self, width, height, typ="simple"):
        if not PIL_INSTALLED:
            raise ImportError("PIL needed to use this function")

        if width <= 0 or height <= 0:
            raise ParameterError('width and width must be geater than 0')

        if typ == "simple":
            generator = ImgSimple()
        elif typ == "plasma":
            generator = ImgPlasma()
        elif typ == "mandelbrot":
            generator = ImgMandelbrot()
        elif typ == "ifs":
            generator = ImgIFS()
        elif typ == "random":
            generator = self.choice(
                [ImgSimple, ImgPlasma, ImgMandelbrot, ImgIFS])()
        else:
            raise ParameterError('Unknown image generator type')

        im = generator.generate(self, width, height)

        stream = io.BytesIO()
        im.save(stream, format="png")
        stream.seek(0)

        return stream
Пример #3
0
    def choices_key(self, choices):
        if not isinstance(choices, list) and not isinstance(choices, tuple):
            raise ParameterError('choices must be a list or a tuple')

        try:
            return self.choice(choices)[0]
        except (TypeError, ValueError, ParameterError):
            raise ParameterError('choices must be a valid django choices list')
    def fill_model(self, model, number, *args, **kwargs):
        if number <= 0:
            raise ParameterError('number must be greater than 0')

        if not issubclass(model, models.Model):
            raise ParameterError('model must be a django model subclass')

        logger.debug("Filling model %s.%s with %d instances" % (model._meta.app_label, model.__name__, number))

        for x in range(number):
            instance = model()
            logger.debug("Filling instance %d" % (x))
            self.fill_model_instance(instance, *args, **kwargs)
            instance.save()
Пример #5
0
    def __load_locale(self, locale):
        locale_path = os.path.join(NAMES_PATH, "{0}.json".format(locale))
        if not os.path.exists(locale_path):
            raise ParameterError('Not valid locale')

        fd = codecs.open(locale_path, 'r', encoding='utf-8')
        Surname.data[locale] = json.load(fd)
        fd.close()
    def image_path_from_directory(self,
                                  directory_path,
                                  valid_extensions=['.jpg', '.bmp', '.png']):
        if not os.path.exists(directory_path):
            raise ParameterError('directory_path must be a valid path')

        list_of_images = os.listdir(directory_path)
        list_of_images = list(
            filter(lambda x: os.path.splitext(x)[1] in valid_extensions,
                   list_of_images))

        if len(list_of_images) == 0:
            raise NotChoicesFound(
                'Not valid images found in directory_path for valid_extensions'
            )

        random_path = os.path.join(directory_path,
                                   random.choice(list_of_images))
        return random_path
Пример #7
0
    def generate(self, sd, locale=None, number=None, as_list=False):
        if locale:
            names = self.get_names(locale)
            if number is None:
                number = self.get_names_number(locale)
        else:
            names = self.all_names()

        if number is None:
            number = 1

        if number < 1:
            raise ParameterError("number must be greater than 1")

        result = []
        for x in range(number):
            result.append(sd.choice(names))

        if as_list:
            return result

        return ' '.join(result)
    def fill_model_instance(self, instance, *args, **kwargs):
        if not isinstance(instance, models.Model):
            raise ParameterError('instance must be a django model instance')

        for field_name, field_obj in self.__get_instance_fields(instance):
            if field_name not in kwargs:
                handler = register.get_handler(field_obj)
                if handler:
                    value = handler.generate()
                    setattr(instance, field_name, value)

        for field in args:
            if hasattr(field[1], '__call__'):
                value = field[1](instance, self.sd)
            else:
                value = field[1]
            setattr(instance, field[0], value)

        for field_name in kwargs.keys():
            if hasattr(kwargs[field_name], '__call__'):
                value = kwargs[field_name](instance, self.sd)
            else:
                value = kwargs[field_name]
            setattr(instance, field_name, value)