Skip to content

Commit

Permalink
Changed all files to use 4spaces instead of 2spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Benham committed May 22, 2012
1 parent a6ca937 commit 9d1e824
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 126 deletions.
23 changes: 11 additions & 12 deletions setup.py
Original file line number Original file line Diff line number Diff line change
@@ -1,22 +1,21 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim: sw=2 ts=2


from setuptools import setup, find_packages from setuptools import setup, find_packages


with open('README.md') as f: with open('README.md') as f:
readme = f.read() readme = f.read()


with open('LICENSE') as f: with open('LICENSE') as f:
license = f.read() license = f.read()


setup( setup(
name='squish', name='squish',
version='0.0.1', version='0.0.1',
description='Simple image optimization tool', description='Simple image optimization tool',
long_description=readme, long_description=readme,
author='Josh Benham', author='Josh Benham',
author_email='joshbenham@gmail.com', author_email='joshbenham@gmail.com',
url='https://github.com/joshbenham/squish', url='https://github.com/joshbenham/squish',
license=license, license=license,
packages=find_packages(exclude=('tests', 'docs')) packages=find_packages(exclude=('tests', 'docs'))
) )
13 changes: 6 additions & 7 deletions squish/colors.py
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # # -*- coding: utf-8 -*- #
# vim: sw=2 ts=2


from clint.textui import colored from clint.textui import colored




def e(s): def e(s):
"""Color: Error""" """Color: Error"""
return colored.red(s) return colored.red(s)


def s(s): def s(s):
"""Color: Success""" """Color: Success"""
return colored.green(s) return colored.green(s)


def p(s): def p(s):
"""Color: Primary""" """Color: Primary"""
return colored.cyan(s) return colored.cyan(s)
95 changes: 43 additions & 52 deletions squish/core.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # # -*- coding: utf-8 -*- #
# vim: sw=2 ts=2


import sys import sys


Expand All @@ -14,71 +13,63 @@


class Core: class Core:


def main(self): def main(self):
self._check_dependencies() self._check_dependencies()


if not args.get(0) or not len(args.files) or "--help" in args.flags: if not args.get(0) or not len(args.files) or "--help" in args.flags:
self._show_help() self._show_help()


if "--watch" not in args.flags: if "--watch" not in args.flags:
self._iterate_over_files() self._iterate_over_files()




# Iterate over the files and apply optimizations # Iterate over the files and apply optimizations
def _iterate_over_files(self): def _iterate_over_files(self):
stats = Statistics()


stats = Statistics() for file in args.files:


for file in args.files: if isimage(file):
stats.pre(file)
if "--lossy" in args.flags:
Optimize.lossy(file)
if "--lossless" in args.flags:
Optimize.lossless(file)
stats.post(file)


if isimage(file): if "--stats" in args.flags:
stats.show()


stats.pre(file)


if "--lossy" in args.flags: # Check to see if all the dependencies have been installed
Optimize.lossy(file) def _check_dependencies(self):
imgmin = exists('imgmin')
image_optim = exists('image_optim')


if "--lossless" in args.flags: if not imgmin or not image_optim:
Optimize.lossless(file) puts(p('Dependencies have not been installed:'))


stats.post(file) with indent(3):
message = 'imgmin - https://github.com/rflynn/imgmin'
message = s('✓ ' + message) if imgmin else e('✗ ' + message)
puts(message)


if "--stats" in args.flags: message = 'image_optim - http://rubygems.org/gems/image_optim'
stats.show() message = s('✓ ' + message) if image_optim else e('✗ ' + message)
puts(message)


sys.exit(1)




# Check to see if all the dependencies have been installed # Show the use the help system
def _check_dependencies(self): def _show_help(self):
puts(e("Usage: %s [options] <file/folder>" % sys.argv[0]))


imgmin = exists('imgmin') with indent(3):
image_optim = exists('image_optim') puts(p("--help ") + "Show the help system")
puts(p("--lossy ") + "Apply a lossy optimization on the image(s)")
puts(p("--lossless ") + "Apply a lossless optimization on the image(s)")
puts(p("--watch ") + "Watch a folder and apply optimizations straight up")
puts(p("--stats ") + "Show stats of the optimizations")


if not imgmin or not image_optim: sys.exit(1)
puts(p('Dependencies have not been installed:'))

with indent(3):
message = 'imgmin - https://github.com/rflynn/imgmin'
message = s('✓ ' + message) if imgmin else e('✗ ' + message)
puts(message)

message = 'image_optim - http://rubygems.org/gems/image_optim'
message = s('✓ ' + message) if image_optim else e('✗ ' + message)
puts(message)

sys.exit(1)


# Show the use the help system
def _show_help(self):

puts(e("Usage: %s [options] <file/folder>" % sys.argv[0]))

with indent(3):
puts(p("--help ") + "Show the help system")
puts(p("--lossy ") + "Apply a lossy optimization on the image(s)")
puts(p("--lossless ") + "Apply a lossless optimization on the image(s)")
puts(p("--watch ") + "Watch a folder and apply optimizations straight up")
puts(p("--stats ") + "Show stats of the optimizations")

sys.exit(1)
15 changes: 8 additions & 7 deletions squish/helpers.py
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # # -*- coding: utf-8 -*- #
# vim: sw=2 ts=2


import envoy import envoy


from PIL import Image from PIL import Image




def exists(string): def exists(string):
return envoy.run("which %s" % string).status_code == 0 """Check to see if file exists"""
return envoy.run("which %s" % string).status_code == 0


def isimage(file): def isimage(file):
try: """Check to see if image exists"""
Image.open(file) try:
return True Image.open(file)
except IOError: return True
return False except IOError:
return False
13 changes: 6 additions & 7 deletions squish/optimize.py
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # # -*- coding: utf-8 -*- #
# vim: sw=2 ts=2


import envoy import envoy




class Optimize: class Optimize:


@staticmethod @staticmethod
def lossy(file): def lossy(file):
envoy.run("imgmin %s %s" % (file, file)) envoy.run("imgmin %s %s" % (file, file))


@staticmethod @staticmethod
def lossless(file): def lossless(file):
envoy.run("image_optim %s" % file) envoy.run("image_optim %s" % file)
79 changes: 39 additions & 40 deletions squish/statistics.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # # -*- coding: utf-8 -*- #
# vim: sw=2 ts=2


import os import os


Expand All @@ -11,54 +10,54 @@


class Statistics: class Statistics:


# Total files # Total files
files = 0 files = 0


# File types # File types
pngs = 0 pngs = 0
jpgs = 0 jpgs = 0
gifs = 0 gifs = 0


# Total size # Total size
preSize = 0 preSize = 0
postSize = 0 postSize = 0


def pre(self, file): def pre(self, file):
self._baseCalculations(file) self._baseCalculations(file)
self.preSize += self._getFileSize(file) self.preSize += self._getFileSize(file)


def post(self, file): def post(self, file):
self.postSize += self._getFileSize(file) self.postSize += self._getFileSize(file)


def show(self): def show(self):
puts(p("Statistics:")) puts(p("Statistics:"))


with indent(3): with indent(3):
puts("Total Files: %s (PNGs %s/JPGs %s/GIFs %s)" % ( puts("Total Files: %s (PNGs %s/JPGs %s/GIFs %s)" % (
s(self.files), s(self.files),
s(self.pngs), s(self.pngs),
s(self.jpgs), s(self.jpgs),
s(self.gifs) s(self.gifs)
)) ))
puts("Original File/Folder Size: %s" % s(round(self.preSize, 2))) puts("Original File/Folder Size: %s" % s(round(self.preSize, 2)))
puts("New File/Folder Size: %s" % s(round(self.postSize, 2))) puts("New File/Folder Size: %s" % s(round(self.postSize, 2)))
puts("Savings of: %s%%" % s(self._getSavings())) puts("Savings of: %s%%" % s(self._getSavings()))


def _getSavings(self): def _getSavings(self):
return round(100 - ((100 * self.postSize) / self.preSize), 2) return round(100 - ((100 * self.postSize) / self.preSize), 2)


def _getFileSize(self, file): def _getFileSize(self, file):
return os.stat(file).st_size / 1024.0 return os.stat(file).st_size / 1024.0


def _baseCalculations(self, file): def _baseCalculations(self, file):
self.files += 1 self.files += 1
stream = Image.open(file) stream = Image.open(file)


if stream.format == "PNG": if stream.format == "PNG":
self.pngs += 1 self.pngs += 1


if stream.format == "JPG": if stream.format == "JPG":
self.jpgs += 1 self.jpgs += 1


if stream.format == "GIF": if stream.format == "GIF":
self.gifs += 1 self.gifs += 1
2 changes: 1 addition & 1 deletion squish_r
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ from squish.core import Core




if __name__ == "__main__": if __name__ == "__main__":
Core().main() Core().main()


1 comment on commit 9d1e824

@richo
Copy link
Contributor

@richo richo commented on 9d1e824 May 22, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

Please sign in to comment.