<s> from optparse import OptionParser <EOL> import os <EOL> import sys <EOL> import shutil <EOL> import codecs <EOL> class DirHelper ( object ) : <EOL> def __init__ ( self , is_dir , list_dir , walk , rmtree ) : <EOL> self . is_dir = is_dir <EOL> self . list_dir = list_dir <EOL> self . walk = walk <EOL> self . rmtree = rmtree <EOL> class FileSystemHelper ( object ) : <EOL> def __init__ ( self , open_ , path_join , move , exists ) : <EOL> self . open_ = open_ <EOL> self . path_join = path_join <EOL> self . move = move <EOL> self . exists = exists <EOL> class Replacer ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , from_ , to ) : <EOL> self . from_ = from_ <EOL> self . to = to <EOL> def process ( self , text ) : <EOL> return text . replace ( self . from_ , self . to ) <EOL> class FileHandler ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , name , replacers , opener ) : <EOL> self . name = name <EOL> self . replacers = replacers <EOL> self . opener = opener <EOL> def process ( self ) : <EOL> text = self . opener ( self . name , "<STR_LIT:r>" ) . read ( ) <EOL> for replacer in self . replacers : <EOL> text = replacer . process ( text ) <EOL> self . opener ( self . name , "<STR_LIT:w>" ) . write ( text ) <EOL> class Remover ( object ) : <EOL> def __init__ ( self , exists , remove ) : <EOL> self . exists = exists <EOL> self . remove = remove <EOL> def __call__ ( self , name ) : <EOL> if self . exists ( name ) : <EOL> self . remove ( name ) <EOL> class ForceRename ( object ) : <EOL> def __init__ ( self , renamer , remove ) : <EOL> self . renamer = renamer <EOL> self . remove = remove <EOL> def __call__ ( self , from_ , to ) : <EOL> self . remove ( to ) <EOL> self . renamer ( from_ , to ) <EOL> class VerboseRename ( object ) : <EOL> def __init__ ( self , renamer , stream ) : <EOL> self . renamer = renamer <EOL> self . stream = stream <EOL> def __call__ ( self , from_ , to ) : <EOL> self . stream . write ( <EOL> "<STR_LIT>" <EOL> % ( os . path . basename ( from_ ) , os . path . basename ( to ) ) <EOL> ) <EOL> self . renamer ( from_ , to ) <EOL> class DirectoryHandler ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , name , root , renamer ) : <EOL> self . name = name <EOL> self . new_name = name [ <NUM_LIT:1> : ] <EOL> self . root = root + os . sep <EOL> self . renamer = renamer <EOL> def path ( self ) : <EOL> return os . path . join ( self . root , self . name ) <EOL> def relative_path ( self , directory , filename ) : <EOL> path = directory . replace ( self . root , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return os . path . join ( path , filename ) <EOL> def new_relative_path ( self , directory , filename ) : <EOL> path = self . relative_path ( directory , filename ) <EOL> return path . replace ( self . name , self . new_name , <NUM_LIT:1> ) <EOL> def process ( self ) : <EOL> from_ = os . path . join ( self . root , self . name ) <EOL> to = os . path . join ( self . root , self . new_name ) <EOL> self . renamer ( from_ , to ) <EOL> class HandlerFactory ( object ) : <EOL> def create_file_handler ( self , name , replacers , opener ) : <EOL> return FileHandler ( name , replacers , opener ) <EOL> def create_dir_handler ( self , name , root , renamer ) : <EOL> return DirectoryHandler ( name , root , renamer ) <EOL> class OperationsFactory ( object ) : <EOL> def create_force_rename ( self , renamer , remover ) : <EOL> return ForceRename ( renamer , remover ) <EOL> def create_verbose_rename ( self , renamer , stream ) : <EOL> return VerboseRename ( renamer , stream ) <EOL> def create_replacer ( self , from_ , to ) : <EOL> return Replacer ( from_ , to ) <EOL> def create_remover ( self , exists , remove ) : <EOL> return Remover ( exists , remove ) <EOL> class Layout ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory_handlers , file_handlers ) : <EOL> self . directory_handlers = directory_handlers <EOL> self . file_handlers = file_handlers <EOL> def process ( self ) : <EOL> for handler in self . file_handlers : <EOL> handler . process ( ) <EOL> for handler in self . directory_handlers : <EOL> handler . process ( ) <EOL> class NullLayout ( object ) : <EOL> """<STR_LIT>""" <EOL> def process ( self ) : <EOL> pass <EOL> class LayoutFactory ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , operations_factory , handler_factory , file_helper , dir_helper , verbose , stream , force ) : <EOL> self . operations_factory = operations_factory <EOL> self . handler_factory = handler_factory <EOL> self . file_helper = file_helper <EOL> self . dir_helper = dir_helper <EOL> self . verbose = verbose <EOL> self . output_stream = stream <EOL> self . force = force <EOL> def create_layout ( self , path ) : <EOL> contents = self . dir_helper . list_dir ( path ) <EOL> renamer = self . file_helper . move <EOL> if self . force : <EOL> remove = self . operations_factory . create_remover ( self . file_helper . exists , self . dir_helper . rmtree ) <EOL> renamer = self . operations_factory . create_force_rename ( renamer , remove ) <EOL> if self . verbose : <EOL> renamer = self . operations_factory . create_verbose_rename ( renamer , self . output_stream ) <EOL> directories = [ d for d in contents if self . is_underscore_dir ( path , d ) ] <EOL> underscore_directories = [ <EOL> self . handler_factory . create_dir_handler ( d , path , renamer ) <EOL> for d in directories <EOL> ] <EOL> if not underscore_directories : <EOL> if self . verbose : <EOL> self . output_stream . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % path <EOL> ) <EOL> return NullLayout ( ) <EOL> replacers = [ ] <EOL> for handler in underscore_directories : <EOL> for directory , dirs , files in self . dir_helper . walk ( handler . path ( ) ) : <EOL> for f in files : <EOL> replacers . append ( <EOL> self . operations_factory . create_replacer ( <EOL> handler . relative_path ( directory , f ) , <EOL> handler . new_relative_path ( directory , f ) <EOL> ) <EOL> ) <EOL> filelist = [ ] <EOL> for root , dirs , files in self . dir_helper . walk ( path ) : <EOL> for f in files : <EOL> if f . endswith ( "<STR_LIT>" ) : <EOL> filelist . append ( <EOL> self . handler_factory . create_file_handler ( <EOL> self . file_helper . path_join ( root , f ) , <EOL> replacers , <EOL> self . file_helper . open_ ) <EOL> ) <EOL> if f . endswith ( "<STR_LIT>" ) : <EOL> filelist . append ( <EOL> self . handler_factory . create_file_handler ( <EOL> self . file_helper . path_join ( root , f ) , <EOL> [ self . operations_factory . create_replacer ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> self . file_helper . open_ <EOL> ) <EOL> ) <EOL> return Layout ( underscore_directories , filelist ) <EOL> def is_underscore_dir ( self , path , directory ) : <EOL> return ( self . dir_helper . is_dir ( self . file_helper . path_join ( path , directory ) ) <EOL> and directory . startswith ( "<STR_LIT:_>" ) ) <EOL> def sphinx_extension ( app , exception ) : <EOL> "<STR_LIT>" <EOL> if not app . builder . name in ( "<STR_LIT:html>" , "<STR_LIT>" ) : <EOL> return <EOL> if not app . config . sphinx_to_github : <EOL> if app . config . sphinx_to_github_verbose : <EOL> print "<STR_LIT>" <EOL> return <EOL> if exception : <EOL> if app . config . sphinx_to_github_verbose : <EOL> print "<STR_LIT>" <EOL> return <EOL> dir_helper = DirHelper ( <EOL> os . path . isdir , <EOL> os . listdir , <EOL> os . walk , <EOL> shutil . rmtree <EOL> ) <EOL> file_helper = FileSystemHelper ( <EOL> lambda f , mode : codecs . open ( f , mode , app . config . sphinx_to_github_encoding ) , <EOL> os . path . join , <EOL> shutil . move , <EOL> os . path . exists <EOL> ) <EOL> operations_factory = OperationsFactory ( ) <EOL> handler_factory = HandlerFactory ( ) <EOL> layout_factory = LayoutFactory ( <EOL> operations_factory , <EOL> handler_factory , <EOL> file_helper , <EOL> dir_helper , <EOL> app . config . sphinx_to_github_verbose , <EOL> sys . stdout , <EOL> force = True <EOL> ) <EOL> layout = layout_factory . create_layout ( app . outdir ) <EOL> layout . process ( ) <EOL> def setup ( app ) : <EOL> "<STR_LIT>" <EOL> app . add_config_value ( "<STR_LIT>" , True , '<STR_LIT>' ) <EOL> app . add_config_value ( "<STR_LIT>" , True , '<STR_LIT>' ) <EOL> app . add_config_value ( "<STR_LIT>" , '<STR_LIT:utf-8>' , '<STR_LIT>' ) <EOL> app . connect ( "<STR_LIT>" , sphinx_extension ) <EOL> def main ( args ) : <EOL> usage = "<STR_LIT>" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , <EOL> dest = "<STR_LIT>" , default = "<STR_LIT:utf-8>" , help = "<STR_LIT>" ) <EOL> opts , args = parser . parse_args ( args ) <EOL> try : <EOL> path = args [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> return <EOL> dir_helper = DirHelper ( <EOL> os . path . isdir , <EOL> os . listdir , <EOL> os . walk , <EOL> shutil . rmtree <EOL> ) <EOL> file_helper = FileSystemHelper ( <EOL> lambda f , mode : codecs . open ( f , mode , opts . encoding ) , <EOL> os . path . join , <EOL> shutil . move , <EOL> os . path . exists <EOL> ) <EOL> operations_factory = OperationsFactory ( ) <EOL> handler_factory = HandlerFactory ( ) <EOL> layout_factory = LayoutFactory ( <EOL> operations_factory , <EOL> handler_factory , <EOL> file_helper , <EOL> dir_helper , <EOL> opts . verbose , <EOL> sys . stdout , <EOL> force = False <EOL> ) <EOL> layout = layout_factory . create_layout ( path ) <EOL> layout . process ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
