<s> import json <EOL> import os <EOL> import string <EOL> import sys <EOL> import mock <EOL> from django . template import TemplateDoesNotExist <EOL> from django . template . loader import find_template <EOL> from nose . plugins . base import Plugin <EOL> LINE_LENGTH = <NUM_LIT> <EOL> def heading ( stream , value ) : <EOL> print >> stream , '<STR_LIT:=>' * LINE_LENGTH <EOL> print >> stream , value <EOL> print >> stream , '<STR_LIT:->' * LINE_LENGTH <EOL> def bulleted ( stream , values ) : <EOL> for value in values : <EOL> print >> stream , '<STR_LIT>' , value <EOL> def files ( directory ) : <EOL> """<STR_LIT>""" <EOL> paths = set ( ) <EOL> for dirpath , dirnames , filenames in os . walk ( directory ) : <EOL> for filename in filenames : <EOL> path = os . path . join ( dirpath , filename ) <EOL> paths . add ( os . path . relpath ( path , directory ) ) <EOL> return paths <EOL> class TemplateUsageReportPlugin ( Plugin ) : <EOL> enabled = False <EOL> score = sys . maxint <EOL> name = '<STR_LIT>' <EOL> def options ( self , parser , env ) : <EOL> parser . add_option ( '<STR_LIT>' % self . name , dest = '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , help = '<STR_LIT>' , <EOL> default = [ ] ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> def configure ( self , options , conf ) : <EOL> self . enabled = options . enabled <EOL> if not self . enabled : <EOL> return <EOL> ignore_prefixes = options . ignore_prefixes <EOL> if len ( ignore_prefixes ) == <NUM_LIT:1> : <EOL> ignore_prefixes = map ( string . strip , ignore_prefixes [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) ) <EOL> self . ignore_prefixes = ignore_prefixes <EOL> self . outfile = options . outfile <EOL> def begin ( self ) : <EOL> self . used_templates = set ( ) <EOL> def register_template_usage ( name , * args , ** kwargs ) : <EOL> result = find_template ( name , * args , ** kwargs ) <EOL> self . used_templates . add ( name ) <EOL> return result <EOL> self . patch = mock . patch ( '<STR_LIT>' , <EOL> side_effect = register_template_usage ) <EOL> self . patch . start ( ) <EOL> def report ( self , stream ) : <EOL> heading ( stream , '<STR_LIT>' % len ( self . used_templates ) ) <EOL> bulleted ( stream , sorted ( self . used_templates ) ) <EOL> from django . conf import settings <EOL> from django . template . loader import template_source_loaders <EOL> from django . template . loaders . filesystem import Loader as FileSystemLoader <EOL> from django . template . loaders . app_directories import Loader as AppDirectoryLoader <EOL> def filter_ignored ( paths ) : <EOL> for path in paths : <EOL> for prefix in self . ignore_prefixes : <EOL> if os . path . commonprefix ( ( prefix , path ) ) == prefix : <EOL> break <EOL> else : <EOL> yield path <EOL> available_templates = set ( ) <EOL> for loader in template_source_loaders : <EOL> if isinstance ( loader , FileSystemLoader ) : <EOL> for directory in settings . TEMPLATE_DIRS : <EOL> available_templates . update ( filter_ignored ( files ( directory ) ) ) <EOL> elif isinstance ( loader , AppDirectoryLoader ) : <EOL> from django . template . loaders . app_directories import app_template_dirs <EOL> for directory in app_template_dirs : <EOL> available_templates . update ( filter_ignored ( files ( directory ) ) ) <EOL> self . unused_templates = available_templates - self . used_templates <EOL> heading ( stream , '<STR_LIT>' % len ( self . unused_templates ) ) <EOL> bulleted ( stream , sorted ( self . unused_templates ) ) <EOL> if self . outfile : <EOL> with open ( self . outfile , '<STR_LIT:w>' ) as out : <EOL> json . dump ( { <EOL> '<STR_LIT>' : list ( self . used_templates ) , <EOL> '<STR_LIT>' : list ( self . unused_templates ) , <EOL> } , out ) </s>
