<s> import os <EOL> import sys <EOL> from pkg_resources import resource_listdir , resource_string <EOL> from trac . loader import load_components <EOL> from trac . test import EnvironmentStub , Mock , MockPerm <EOL> from trac . util . text import printout <EOL> from trac . web . chrome import web_context <EOL> from trac . web . href import Href <EOL> from trac . wiki . formatter import Formatter <EOL> from trac . wiki . model import WikiPage <EOL> TURN_ON = '<STR_LIT>' <EOL> TURN_OFF = '<STR_LIT>' <EOL> class DefaultWikiChecker ( Formatter ) : <EOL> def __init__ ( self , env , context , name ) : <EOL> Formatter . __init__ ( self , env , context ) <EOL> self . __name = name <EOL> self . __marks = [ ] <EOL> self . __super = super ( DefaultWikiChecker , self ) <EOL> def handle_match ( self , fullmatch ) : <EOL> rv = self . __super . handle_match ( fullmatch ) <EOL> if rv : <EOL> if not isinstance ( rv , basestring ) : <EOL> text = unicode ( rv ) <EOL> else : <EOL> text = rv <EOL> if text . startswith ( '<STR_LIT>' ) and text . endswith ( '<STR_LIT>' ) and '<STR_LIT>' in text : <EOL> self . __marks . append ( ( fullmatch . start ( <NUM_LIT:0> ) , fullmatch . end ( <NUM_LIT:0> ) ) ) <EOL> return rv <EOL> def handle_code_block ( self , line , startmatch = None ) : <EOL> prev_processor = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> return self . __super . handle_code_block ( line , startmatch ) <EOL> finally : <EOL> processor = self . code_processor <EOL> if startmatch and processor and processor != prev_processor and processor . error : <EOL> self . __marks . append ( ( startmatch . start ( <NUM_LIT:0> ) , startmatch . end ( <NUM_LIT:0> ) ) ) <EOL> def format ( self , text , out = None ) : <EOL> return self . __super . format ( SourceWrapper ( self , text ) , out ) <EOL> def next_callback ( self , line , idx ) : <EOL> marks = self . __marks <EOL> if marks : <EOL> buf = [ ] <EOL> prev = <NUM_LIT:0> <EOL> for start , end in self . __marks : <EOL> buf . append ( line [ prev : start ] ) <EOL> buf . append ( TURN_ON ) <EOL> buf . append ( line [ start : end ] ) <EOL> buf . append ( TURN_OFF ) <EOL> prev = end <EOL> buf . append ( line [ prev : ] ) <EOL> printout ( '<STR_LIT>' % ( self . __name , idx + <NUM_LIT:1> , '<STR_LIT>' . join ( buf ) ) ) <EOL> self . __marks [ : ] = ( ) <EOL> class SourceWrapper ( object ) : <EOL> def __init__ ( self , formatter , text ) : <EOL> self . formatter = formatter <EOL> self . text = text <EOL> def __iter__ ( self ) : <EOL> return LinesIterator ( self . formatter , self . text . splitlines ( ) ) <EOL> class LinesIterator ( object ) : <EOL> def __init__ ( self , formatter , lines ) : <EOL> self . formatter = formatter <EOL> self . lines = lines <EOL> self . idx = <NUM_LIT:0> <EOL> self . current = None <EOL> def next ( self ) : <EOL> idx = self . idx <EOL> if self . current is not None : <EOL> self . formatter . next_callback ( self . current , idx ) <EOL> if idx >= len ( self . lines ) : <EOL> self . current = None <EOL> raise StopIteration <EOL> self . idx = idx + <NUM_LIT:1> <EOL> self . current = self . lines [ idx ] <EOL> return self . current <EOL> class DummyIO ( object ) : <EOL> def write ( self , data ) : <EOL> pass <EOL> def parse_args ( ) : <EOL> from optparse import OptionParser <EOL> parser = OptionParser ( usage = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> return parser . parse_args ( ) <EOL> def download_default_pages ( names , prefix ) : <EOL> from httplib import HTTPSConnection <EOL> host = '<STR_LIT>' <EOL> if prefix and not prefix . endswith ( '<STR_LIT:/>' ) : <EOL> prefix += '<STR_LIT:/>' <EOL> conn = HTTPSConnection ( host ) <EOL> for name in names : <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> sys . stdout . write ( '<STR_LIT>' % ( prefix , name ) ) <EOL> conn . request ( '<STR_LIT:GET>' , '<STR_LIT>' % ( prefix , name ) ) <EOL> response = conn . getresponse ( ) <EOL> content = response . read ( ) <EOL> if prefix and ( response . status != <NUM_LIT:200> or not content ) : <EOL> sys . stdout . write ( '<STR_LIT>' % name ) <EOL> conn . request ( '<STR_LIT:GET>' , '<STR_LIT>' % name ) <EOL> response = conn . getresponse ( ) <EOL> content = response . read ( ) <EOL> if response . status == <NUM_LIT:200> and content : <EOL> with open ( '<STR_LIT>' + name , '<STR_LIT:w>' ) as f : <EOL> lines = content . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) . splitlines ( True ) <EOL> f . write ( '<STR_LIT>' . join ( line for line in lines <EOL> if line . strip ( ) != '<STR_LIT>' ) ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> else : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> conn . close ( ) <EOL> def main ( ) : <EOL> options , args = parse_args ( ) <EOL> names = sorted ( name for name in resource_listdir ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if not name . startswith ( '<STR_LIT:.>' ) ) <EOL> if args : <EOL> args = sorted ( set ( names ) & set ( map ( os . path . basename , args ) ) ) <EOL> else : <EOL> args = names <EOL> if options . download : <EOL> download_default_pages ( args , options . prefix ) <EOL> env = EnvironmentStub ( disable = [ '<STR_LIT>' ] ) <EOL> load_components ( env ) <EOL> with env . db_transaction : <EOL> for name in names : <EOL> wiki = WikiPage ( env , name ) <EOL> wiki . text = resource_string ( '<STR_LIT>' , '<STR_LIT>' + <EOL> name ) . decode ( '<STR_LIT:utf-8>' ) <EOL> if wiki . text : <EOL> wiki . save ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> printout ( '<STR_LIT>' % name ) <EOL> req = Mock ( href = Href ( '<STR_LIT:/>' ) , abs_href = Href ( '<STR_LIT>' ) , <EOL> perm = MockPerm ( ) ) <EOL> for name in args : <EOL> wiki = WikiPage ( env , name ) <EOL> if not wiki . exists : <EOL> continue <EOL> context = web_context ( req , wiki . resource ) <EOL> out = DummyIO ( ) <EOL> DefaultWikiChecker ( env , context , name ) . format ( wiki . text , out ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
