This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the DirectiveAnalyzer for processing templates for directiv…
…e usage Hoping to form this into a fully-fledged reporting tool so I can gauge usage of directives to start cutting some out.
- Loading branch information
R. Tyler Ballance
committed
Nov 17, 2009
1 parent
e43765a
commit 7c8e1d1
Showing
5 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import pprint | ||
|
||
try: | ||
from functools import reduce | ||
except ImportError: | ||
# Assume we have reduce | ||
pass | ||
|
||
from Cheetah import Parser | ||
from Cheetah import Compiler | ||
from Cheetah import Template | ||
|
||
class Analyzer(Parser.Parser): | ||
def __init__(self, *args, **kwargs): | ||
self.calls = {} | ||
super(Analyzer, self).__init__(*args, **kwargs) | ||
|
||
def eatDirective(self): | ||
directive = self.matchDirective() | ||
try: | ||
self.calls[directive] += 1 | ||
except KeyError: | ||
self.calls[directive] = 1 | ||
super(Analyzer, self).eatDirective() | ||
|
||
class AnalysisCompiler(Compiler.ModuleCompiler): | ||
parserClass = Analyzer | ||
|
||
|
||
def analyze(source): | ||
klass = Template.Template.compile(source, compilerClass=AnalysisCompiler) | ||
return klass._CHEETAH_compilerClass._parser.calls | ||
|
||
def main_file(f): | ||
fd = open(f, 'r') | ||
try: | ||
calls = analyze(fd.read()) | ||
return calls | ||
finally: | ||
fd.close() | ||
|
||
|
||
def _find_templates(directory, suffix): | ||
for root, dirs, files in os.walk(directory): | ||
for f in files: | ||
if not f.endswith(suffix): | ||
continue | ||
yield root + os.path.sep + f | ||
|
||
def _analyze_templates(iterable): | ||
for template in iterable: | ||
yield main_file(template) | ||
|
||
def main_dir(opts): | ||
results = _analyze_templates(_find_templates(opts.dir, opts.suffix)) | ||
totals = {} | ||
for series in results: | ||
if not series: | ||
continue | ||
for k, v in series.iteritems(): | ||
try: | ||
totals[k] += v | ||
except KeyError: | ||
totals[k] = v | ||
return totals | ||
|
||
|
||
def main(): | ||
from optparse import OptionParser | ||
op = OptionParser() | ||
op.add_option('-f', '--file', dest='file', default=None, | ||
help='Specify a single file to analyze') | ||
op.add_option('-d', '--dir', dest='dir', default=None, | ||
help='Specify a directory of templates to analyze') | ||
op.add_option('--suffix', default='tmpl', dest='suffix', | ||
help='Specify a custom template file suffix for the -d option (default: "tmpl")') | ||
opts, args = op.parse_args() | ||
|
||
if not opts.file and not opts.dir: | ||
op.print_help() | ||
return | ||
|
||
results = None | ||
if opts.file: | ||
results = main_file(opts.file) | ||
if opts.dir: | ||
results = main_dir(opts) | ||
|
||
pprint.pprint(results) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
|
||
import unittest | ||
|
||
from Cheetah import DirectiveAnalyzer | ||
|
||
|
||
class AnalyzerTests(unittest.TestCase): | ||
def test_set(self): | ||
template = ''' | ||
#set $foo = "bar" | ||
Hello ${foo}! | ||
''' | ||
calls = DirectiveAnalyzer.analyze(template) | ||
self.assertEquals(1, calls.get('set')) | ||
|
||
def test_compilersettings(self): | ||
template = ''' | ||
#compiler-settings | ||
useNameMapper = False | ||
#end compiler-settings | ||
''' | ||
calls = DirectiveAnalyzer.analyze(template) | ||
self.assertEquals(1, calls.get('compiler-settings')) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|