Example #1
0
 def test_webp_generation(self):
     if hasattr(Image.core, "webp_decoder"):
         sprite = Sprite(self.paths,
                         sprite_path=os.getcwd() + "/tests/",
                         image_format="RGBA",
                         image_extension="webp",
                         sprite_name="sprite.webp")
         sprite.do_write_image()
Example #2
0
    def post(self, **kwargs):
        msg = ''
        success = True
        urls_paths = []
        paths = []
        original_names = []

        if not self.request.files.get('file', None):
            if self.request.arguments.get('urls'):
                urls_paths = self.request.arguments['urls'][0].split()

                for url in urls_paths:
                    original_names.append(url.split('/')[-1].split(".")[0])
            else:
                msg = "No file given."
                success = False
        else:
            for r_file in self.request.files.get('file'):
                original_fname = r_file['filename']
                original_names.append(original_fname.split(".")[0])
                extension = os.path.splitext(original_fname)[1]

                if extension != '.png':
                    msg = "Invalid file format: " + extension
                    success = False
                    break

                final_filepath = settings.UPLOAD_PATH + '/' + original_fname

                with open(final_filepath, 'w') as output_file:
                    output_file.write(r_file['body'])

                paths.append(final_filepath)

        fname = ''.join(
            random.choice(string.ascii_lowercase + string.digits)
            for x in range(6))
        fname += '.png'

        sprite = Sprite(paths=paths,
                        urls_paths=urls_paths,
                        sprite_path=settings.STATIC_DEMO_PATH,
                        sprite_name=fname,
                        sprite_url=settings.SERVER_NAME + '/static/demo/',
                        css_path=settings.STATIC_DEMO_PATH,
                        class_name="demo")

        sprite.do_write_css()
        sprite.do_write_image()

        self.render("try.html",
                    SERVER_NAME=settings.SERVER_NAME,
                    message=msg,
                    success=success,
                    css=sprite.get_css(),
                    css_url=settings.SERVER_NAME + '/static/demo/sprite.css',
                    img_url=settings.SERVER_NAME + '/static/demo/' + fname,
                    entidades=original_names)
Example #3
0
    def post(self, **kwargs):
        msg = ''
        success = True
        urls_paths = []
        paths = []
        original_names = []

        if not self.request.files.get('file', None):
            if self.request.arguments.get('urls'):
                urls_paths = self.request.arguments['urls'][0].split()

                for url in urls_paths:
                    original_names.append(url.split('/')[-1].split(".")[0])
            else:
                msg = "No file given."
                success = False
        else:
            for r_file in self.request.files.get('file'):
                original_fname = r_file['filename']
                original_names.append(original_fname.split(".")[0])
                extension = os.path.splitext(original_fname)[1]

                if extension != '.png':
                    msg = "Invalid file format: " + extension
                    success = False
                    break

                final_filepath = settings.UPLOAD_PATH + '/' + original_fname

                with open(final_filepath, 'w') as output_file:
                    output_file.write(r_file['body'])

                paths.append(final_filepath)

        fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6))
        fname += '.png'

        sprite = Sprite(paths=paths,
                        urls_paths=urls_paths,
                        sprite_path=settings.STATIC_DEMO_PATH,
                        sprite_name=fname,
                        sprite_url=settings.SERVER_NAME + '/static/demo/',
                        css_path=settings.STATIC_DEMO_PATH,
                        class_name="demo")

        sprite.do_write_css()
        sprite.do_write_image()

        self.render("try.html",
                    SERVER_NAME=settings.SERVER_NAME,
                    message=msg,
                    success=success,
                    css=sprite.get_css(),
                    css_url=settings.SERVER_NAME + '/static/demo/sprite.css',
                    img_url=settings.SERVER_NAME + '/static/demo/' + fname,
                    entidades=original_names)
Example #4
0
 def test_create_sprite_path(self):
     path = os.path.join(os.getcwd(), "sprite_path" + datetime.datetime.now().strftime("%Y%M%d%H%M%S"))
     self.assertFalse(os.path.exists(path))
     sprite = Sprite(self.paths, sprite_path=path)
     sprite_path = sprite.do_write_image()
     self.assertTrue(os.path.exists(path))
     self.assertIn(path, sprite_path)
     os.remove(sprite_path)
     os.rmdir(path)
Example #5
0
 def test_create_sprite_path(self):
     path = os.path.join(
         os.getcwd(),
         "sprite_path" + datetime.datetime.now().strftime("%Y%M%d%H%M%S"))
     self.assertFalse(os.path.exists(path))
     sprite = Sprite(self.paths, sprite_path=path)
     sprite_path = sprite.do_write_image()
     self.assertTrue(os.path.exists(path))
     self.assertIn(path, sprite_path)
     os.remove(sprite_path)
     os.rmdir(path)
Example #6
0
 def test_write_a_default_image_when_some_path_does_not_exist(self):
     self.paths.append("tests/ERROR.png")
     default_path = os.getcwd() + "/tests/fixtures/default.png"
     sprite = Sprite(self.paths, default_path=default_path)
     sprite.gen_image()
     path = sprite.do_write_image()
     css = sprite.get_css()
     self.assertIn(".sprite.serror", css)
     sprite_img = Image.open(path)
     compare = Image.open(os.getcwd() + "/tests/fixtures/with_default.png")
     #This assert the default image is in the sprite
     self.assertEquals(sprite_img.histogram(), compare.histogram())
Example #7
0
 def test_image_from_urls(self):
     with mock.patch("urllib.urlopen") as mck:
         mck.return_value = Openned("http://pitomba.org/happy.png")
         mck.return_value = Openned("http://pitomba.org/sad.png")
         sprite = Sprite(paths=[],
                         urls_paths=["http://pitomba.org/happy.png",
                                     "http://pitomba.org/sad.png"],
                         sprite_name="sprite_url.png")
         path = sprite.do_write_image()
         self.assertIn("sprite_url.png", path)
         compare = Image.open(os.getcwd() + "/tests/fixtures/sprite_url.png")
         self.assertEquals(compare.histogram(), sprite.image.histogram())
Example #8
0
 def test_write_a_default_image_when_some_path_does_not_exist(self):
     self.paths.append("tests/ERROR.png")
     default_path = os.getcwd() + "/tests/fixtures/default.png"
     sprite = Sprite(self.paths, default_path=default_path)
     sprite.gen_image()
     path = sprite.do_write_image()
     css = sprite.get_css()
     self.assertIn(".sprite.serror", css)
     sprite_img = Image.open(path)
     compare = Image.open(os.getcwd() + "/tests/fixtures/with_default.png")
     #This assert the default image is in the sprite
     self.assertEquals(sprite_img.histogram(), compare.histogram())
Example #9
0
 def test_image_from_urls(self):
     with mock.patch("urllib.urlopen") as mck:
         mck.return_value = Openned("http://pitomba.org/happy.png")
         mck.return_value = Openned("http://pitomba.org/sad.png")
         sprite = Sprite(paths=[],
                         urls_paths=[
                             "http://pitomba.org/happy.png",
                             "http://pitomba.org/sad.png"
                         ],
                         sprite_name="sprite_url.png")
         path = sprite.do_write_image()
         self.assertIn("sprite_url.png", path)
         compare = Image.open(os.getcwd() +
                              "/tests/fixtures/sprite_url.png")
         self.assertEquals(compare.histogram(), sprite.image.histogram())
Example #10
0
def gen_sprite():
    css = ""
    image = ""
    files = []

    #get images absolut paths
    for (_, _, filenames) in os.walk(IMAGE_PATH):
        files.extend(map(lambda x: "{0}{1}".format(IMAGE_PATH, x), filenames))
        break

    if files != []:
        #instantiate Sprite class
        sprite = Sprite(
            files,
            #path where sprite will be generated
            sprite_path=SPRITE_PATH,

            #optional
            sprite_name="demo.png",

            #optional
            sprite_url="http://pitomba.org/static/demo/",

            #optional, defines where css will be saved
            css_path=CSS_PATH,

            #optional, defines a namespace for your sprite
            class_name="demo")

        #write css file and return OS path
        css = sprite.do_write_css()

        #write sprite file and return OS path
        image = sprite.do_write_image()

        #if you just want the CSS file content simply use:
        #sprite.get_css()
        #this will return a string that can be used anyway you want to.
    return (css, image)
Example #11
0
def gen_sprite():
    css = ""
    image = ""
    files = []

    #get images absolut paths
    for (_, _, filenames) in os.walk(IMAGE_PATH):
        files.extend(map(lambda x: "{0}{1}".format(IMAGE_PATH, x), filenames))
        break

    if files != []:
        #instantiate Sprite class
        sprite = Sprite(files,
                        #path where sprite will be generated
                        sprite_path=SPRITE_PATH,

                        #optional
                        sprite_name="demo.png",

                        #optional
                        sprite_url="http://pitomba.org/static/demo/",

                        #optional, defines where css will be saved
                        css_path=CSS_PATH,

                        #optional, defines a namespace for your sprite
                        class_name="demo")

        #write css file and return OS path
        css = sprite.do_write_css()

        #write sprite file and return OS path
        image = sprite.do_write_image()

        #if you just want the CSS file content simply use:
        #sprite.get_css()
        #this will return a string that can be used anyway you want to.
    return (css, image)
Example #12
0
 def test_webp_generation(self):
     if  hasattr(Image.core,  "webp_decoder"):
         sprite = Sprite(self.paths, sprite_path=os.getcwd() + "/tests/",
                         image_format="RGBA", image_extension="webp",
                         sprite_name="sprite.webp")
         sprite.do_write_image()
Example #13
0
 def test_do_write_image(self):
     sprite = Sprite(self.paths, sprite_path=os.getcwd() + "/tests/")
     path = sprite.do_write_image()
     sprite.gen_image()
     self.assertTrue(os.path.exists(path))
Example #14
0
 def test_image_optimizated_is_small_than_not_optimizated(self):
     sprite_op = Sprite(self.paths, optimize=True, sprite_name="op.png")
     sprite_nop = Sprite(self.paths, optimize=False, sprite_name="nop.png")
     path_op = sprite_op.do_write_image()
     path_nop = sprite_nop.do_write_image()
     self.assertLess(os.stat(path_op).st_size, os.stat(path_nop).st_size)
Example #15
0
 def test_gen_image_when_optimize_arg_is_passed(self):
     sprite = Sprite(self.paths, optimize=True)
     path = sprite.do_write_image()
     self.assertTrue(os.path.exists(path))
Example #16
0
 def test_do_write_image(self):
     sprite = Sprite(self.paths, sprite_path=os.getcwd() + "/tests/")
     path = sprite.do_write_image()
     sprite.gen_image()
     self.assertTrue(os.path.exists(path))