Skip to content

Commit

Permalink
making directory handling smarter so it will only rename the top leve…
Browse files Browse the repository at this point in the history
…l directory and can process directories recursively

adding verbose mode so you can specify --verbose to see more information when running the script
  • Loading branch information
ccampbell committed Sep 15, 2010
1 parent 373d650 commit b9c6c70
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 28 deletions.
5 changes: 4 additions & 1 deletion modules/config.py
Expand Up @@ -36,6 +36,7 @@ def __init__(self):
self.view_extension = "html"
self.show_savings = False
self.compress_html = False
self.verbose = False

def getArgCount(self):
"""gets the count of how many arguments are present
Expand Down Expand Up @@ -99,7 +100,7 @@ def processArgs(self):
"""
try:
opts, args = getopt.getopt(sys.argv[1:], "v:cjheifsldmo", ["css=", "views=", "js=", "help", "view-ext=", "ignore=", "framework=", "selectors=", "class-selectors=", "id-selectors=", "compress-html", "show-savings"])
opts, args = getopt.getopt(sys.argv[1:], "v:cjheifsldmor", ["css=", "views=", "js=", "help", "view-ext=", "ignore=", "framework=", "selectors=", "class-selectors=", "id-selectors=", "compress-html", "show-savings", "verbose"])
except:
Optimizer.showUsage()

Expand Down Expand Up @@ -131,6 +132,8 @@ def processArgs(self):
self.compress_html = True
elif key in ("-o", "--show-savings"):
self.show_savings = True
elif key in ("-r", "--verbose"):
self.verbose = True

# you have to at least have a view
if views_set is False:
Expand Down
132 changes: 107 additions & 25 deletions modules/optimizer.py
Expand Up @@ -67,20 +67,45 @@ def run(self):
void
"""
self.output("searching for classes and ids...", False)
self.processCss()
self.processViews()
self.processJs()

self.output("mapping classes and ids to new names...", False)
# maps all classes and ids found to shorter names
self.processMaps()

# optimize everything
self.output("munching css files...", False)
self.optimizeFiles(self.config.css, self.optimizeCss)

self.output("munching html files...", False)
self.optimizeFiles(self.config.views, self.optimizeHtml, self.config.view_extension, self.config.compress_html)

self.output("munching js files...", False)
self.optimizeFiles(self.config.js, self.optimizeJavascript)

if self.config.show_savings:
print SizeTracker.savings()
self.output(SizeTracker.savings(), False)

self.output("done", False)

def output(self, text, verbose_only = True):
"""outputs text during the script run
Arguments:
text -- string of text to output
verbose_only -- should we only show this in verbose mode?
Returns:
void
"""
if verbose_only and not self.config.verbose:
return

print text

def processCss(self):
"""gets all css files from config and processes them to see what to replace
Expand Down Expand Up @@ -135,6 +160,9 @@ def processCssFile(self, path, inline = False):
void
"""
if Util.isDir(path):
return

contents = Util.fileGetContents(path)
if inline is True:
blocks = self.getCssBlocks(contents)
Expand All @@ -157,6 +185,9 @@ def processJsFile(self, path, inline = False):
void
"""
if Util.isDir(path):
return

contents = Util.fileGetContents(path)
if inline is True:
blocks = self.getJsBlocks(contents)
Expand Down Expand Up @@ -334,35 +365,86 @@ def optimizeFiles(self, paths, callback, extension = "", minimize = False):
"""
for file in paths:
if not Util.isDir(file):
content = callback(file)
new_path = Util.prependExtension("opt", file)
if minimize is True:
print "minimizing " + file
content = self.minimize(content)
print "optimizing " + file + " to " + new_path
Util.filePutContents(new_path, content)

if self.config.show_savings:
SizeTracker.trackFile(file, new_path)
self.optimizeFile(file, callback, minimize)
continue

self.optimizeDirectory(file, callback, extension, minimize)

def optimizeFile(self, file, callback, minimize = False, new_path = None, prepend = "opt"):
"""optimizes a single file
Arguments:
file -- path to file
callback -- function to run the file through
minimize -- whether or not we should minimize the file contents (html)
prepend -- what extension to prepend
Returns:
void
"""
content = callback(file)
if new_path is None:
new_path = Util.prependExtension(prepend, file)
if minimize is True:
self.output("minimizing " + file)
content = self.minimize(content)
self.output("optimizing " + file + " to " + new_path)
Util.filePutContents(new_path, content)

if self.config.show_savings:
SizeTracker.trackFile(file, new_path)

def optimizeDirectory(self, path, callback, extension = "", minimize = False):
"""optimizes a directory
Arguments:
path -- path to directory
callback -- function to run the file through
extension -- extension to search for in the directory
minimize -- whether or not we should minimize the file contents (html)
Returns:
void
"""
directory = path + "_opt"
Util.unlinkDir(directory)
self.output("creating directory " + directory)
os.mkdir(directory)
for dir_file in Util.getFilesFromDir(path, extension):
if Util.isDir(dir_file):
self.optimizeSubdirectory(dir_file, callback, directory, extension, minimize)
continue

directory = file + "_opt"
Util.unlinkDir(directory)
print "creating directory " + directory
os.mkdir(directory)
for dir_file in Util.getFilesFromDir(file, extension):
content = callback(dir_file)
if minimize is True:
print "minimizing " + dir_file
content = self.minimize(content)
new_path = directory + "/" + Util.getFileName(dir_file)
self.optimizeFile(dir_file, callback, minimize, new_path)

def optimizeSubdirectory(self, path, callback, new_path, extension = "", minimize = False):
"""optimizes a subdirectory within a directory being optimized
new_path = directory + "/" + Util.getFileName(dir_file)
print "optimizing " + dir_file + " to " + new_path
Arguments:
path -- path to directory
callback -- function to run the file through
new_path -- path to optimized parent directory
extension -- extension to search for in the directory
minimize -- whether or not we should minimize the file contents (html)
Util.filePutContents(new_path, content)
Returns:
void
"""
subdir_path = new_path + "/" + path.split("/").pop()
Util.unlinkDir(subdir_path)
self.output("creating directory " + subdir_path)
os.mkdir(subdir_path)
for dir_file in Util.getFilesFromDir(path, extension):
if Util.isDir(dir_file):
self.optimizeSubdirectory(dir_file, callback, subdir_path, extension, minimize)
continue

if self.config.show_savings:
SizeTracker.trackFile(dir_file, new_path)
new_file_path = subdir_path + "/" + Util.getFileName(dir_file)
self.optimizeFile(dir_file, callback, minimize, new_file_path)

def minimize(self, content):
content = re.sub(r'\n', '', content)
Expand Down
2 changes: 0 additions & 2 deletions optimize.py
Expand Up @@ -22,5 +22,3 @@
config.processArgs()
optimizer = Optimizer(config)
optimizer.run()

print "done"

0 comments on commit b9c6c70

Please sign in to comment.