def test_css_format(self): sprite = Sprite(self.paths) css = sprite.get_css() self.assertEquals( css, ".sprite{background:url(\"%s/sprite.png\") 0 0 no-repeat;display:inline-block}.sprite.ssad{background-position: 0px 0px;width:64px;height:64px}.sprite.shappy{background-position: -64px 0px;width:64px;height:64px}" % os.getcwd())
class TestBase64Images(unittest.TestCase): def setUp(self): self.paths = ["tests/fixtures/happy.png"] self.sprite = Sprite(self.paths) def test_get_base_64_str(self): img = self.sprite.get_base64_str() self.assertEquals(img, HAPPY_BASE64) def test_get_css_str_with_base64_sprite(self): css_template = self.sprite.get_css_base64() self.assertIn("image/png", css_template) self.assertIn(HAPPY_BASE64, css_template) def test_gen_css_file_with_base64_sprite(self): css_path = self.sprite.do_write_css(is_base64=True) with open(css_path) as arq: css_str = arq.read() self.assertIn(HAPPY_BASE64, css_str) def test_gen_sprite_files_with_base64_sprite(self): css_path, image_path = self.sprite.gen_sprite(is_base64=True) with open(css_path) as arq: css_str = arq.read() self.assertIn(HAPPY_BASE64, css_str) self.assertIsNone(image_path)
def test_gen_image(self): sprite = Sprite(self.paths, sprite_path=os.getcwd() + "/tests/") sprite.gen_image() assert sprite.image.size[0] == 128 assert sprite.image.size[1] == 64 assert sprite.image.size[0] == sprite.width assert sprite.image.size[1] == sprite.height assert sprite.image.mode == "RGBA"
def test_do_write_css(self): """Tests whether a CSS can be correctly written or not""" sprite = Sprite(self.paths, sprite_path=os.getcwd() + "/tests/", css_path=os.getcwd() + "/tests/") sprite.do_write_css() assert os.path.exists(os.getcwd() + "/tests/sprite.css")
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()
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)
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)
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())
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())
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())
def test_demission_sprite(self): """Tests width and height after gen sprite""" sprite = Sprite(self.paths) #128 each image has 64 px self.assertEqual(sprite.width, 64 + 64) self.assertEqual(sprite.height, 64)
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)
def dispatch(self, event): if event.is_directory: return False path = os.path.dirname(event.src_path) print("Image {0} {1}").format(os.path.basename(event.src_path), event.event_type) files = [] for arg in os.listdir(path): j = os.path.join(path, arg) if imghdr.what(j) is not None: files.append(j) sprite = Sprite(files, css_path=css_dir, sprite_path=sprite_dir, class_name=class_name) sprite.gen_sprite()
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)
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)
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)
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)
def setUp(self): self.paths = ["tests/fixtures/happy.png"] self.sprite = Sprite(self.paths)
def test_format_css_url(self): sprite = Sprite(self.paths, css_url="http://localhost/sprite.css") self.assertEquals(sprite.css_url, "http://localhost/sprite.css")
def test_content_url_sprite(self): sprite = Sprite(self.paths, sprite_url="http://localhost:8000/") css = sprite.get_css() self.assertIn("http://localhost:8000/sprite.png", css)
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))
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))
def test_css_format(self): sprite = Sprite(self.paths) css = sprite.get_css() self.assertEquals(css, ".sprite{background:url(\"%s/sprite.png\") 0 0 no-repeat;display:inline-block}.sprite.ssad{background-position: 0px 0px;width:64px;height:64px}.sprite.shappy{background-position: -64px 0px;width:64px;height:64px}" % os.getcwd())