<s> """<STR_LIT>""" <EOL> from docopt import docopt <EOL> from functools import partial <EOL> import os <EOL> import re <EOL> import shutil <EOL> import sys <EOL> from sphinx import quickstart <EOL> RE_COMMAND_NAME = re . compile ( r'<STR_LIT>' ) <EOL> RE_RST_STATEMENT = re . compile ( r'<STR_LIT>' ) <EOL> RE_HTML_THEME = re . compile ( r"<STR_LIT>" ) <EOL> RE_PROJECT_NAME = re . compile ( r'<STR_LIT>' , re . I ) <EOL> SETUP_TEMPLATE = u"""<STR_LIT>""" <EOL> SETUP_CFG_TEMPLATE = """<STR_LIT>""" <EOL> BSD_LICENSE_HEADER = """<STR_LIT>""" <EOL> NAMESPACE_INIT = "<STR_LIT>" <EOL> DOC_CMDLINE = """<STR_LIT>""" <EOL> DOC_PYTHON_REFERENCE = """<STR_LIT>""" <EOL> CMDREGEN_RULE = u"""<STR_LIT>""" <EOL> def die ( msg , * args , ** kwargs ) : <EOL> output = msg . format ( * args , ** kwargs ) <EOL> if isinstance ( output , unicode ) : <EOL> output = output . encode ( '<STR_LIT:utf-8>' ) <EOL> sys . stderr . write ( output ) <EOL> sys . stderr . write ( '<STR_LIT:\n>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> ask = raw_input <EOL> except : <EOL> ask = input <EOL> def write_init ( config , output_path , namespace_package = False ) : <EOL> with open ( output_path , '<STR_LIT:w>' ) as init_file : <EOL> init_file . write ( BSD_LICENSE_HEADER ) <EOL> if namespace_package : <EOL> init_file . write ( NAMESPACE_INIT ) <EOL> init_file . write ( '<STR_LIT:\n>' ) <EOL> def write_setup ( config , output_path ) : <EOL> values = { '<STR_LIT:name>' : str ( config [ '<STR_LIT>' ] ) } <EOL> for key , value in config . items ( ) : <EOL> if key != '<STR_LIT>' and isinstance ( value , unicode ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> elif key == '<STR_LIT>' and isinstance ( value , str ) : <EOL> value = value . decode ( '<STR_LIT:utf-8>' ) <EOL> values [ key ] = value <EOL> values [ '<STR_LIT:_>' + key ] = '<STR_LIT:#>' if not value else '<STR_LIT>' <EOL> with open ( output_path , '<STR_LIT:w>' ) as setup_file : <EOL> setup_file . write ( SETUP_TEMPLATE . format ( ** values ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def write_setup_cfg ( config , output_path ) : <EOL> with open ( output_path , '<STR_LIT:w>' ) as setup_file : <EOL> setup_file . write ( SETUP_CFG_TEMPLATE ) <EOL> def write_empty ( config , output_path ) : <EOL> with open ( output_path , '<STR_LIT:w>' ) : <EOL> pass <EOL> def write_cmdline ( config , output_path ) : <EOL> with open ( output_path , '<STR_LIT:w>' ) as cmdline_file : <EOL> name = str ( config [ '<STR_LIT>' ] ) . capitalize ( ) <EOL> match = RE_PROJECT_NAME . match ( config [ '<STR_LIT>' ] ) <EOL> if match : <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> title = "<STR_LIT>" % name <EOL> heading = "<STR_LIT>" % ( title , '<STR_LIT:=>' * len ( title ) ) <EOL> cmdline_file . write ( DOC_CMDLINE . format ( <EOL> command = config [ '<STR_LIT>' ] , <EOL> heading = heading ) ) <EOL> def write_makefile ( config , output_path ) : <EOL> with open ( output_path , '<STR_LIT:w>' ) as cmdline_file : <EOL> cmdline_file . write ( '<STR_LIT>' ) <EOL> def modify_doc_makefile ( config , path ) : <EOL> with open ( path , '<STR_LIT:w>' ) as new : <EOL> new . write ( '<STR_LIT>' ) <EOL> def modify_doc_index ( config , path ) : <EOL> s_wait_toc , s_wait_empty_line , s_done = range ( <NUM_LIT:3> ) <EOL> state = s_wait_toc <EOL> new_path = path + '<STR_LIT>' <EOL> with open ( path , '<STR_LIT:r>' ) as orig : <EOL> with open ( new_path , '<STR_LIT:w>' ) as new : <EOL> for line in orig . readlines ( ) : <EOL> if state == s_wait_toc and line . startswith ( '<STR_LIT>' ) : <EOL> state = s_wait_empty_line <EOL> new . write ( line ) <EOL> elif ( state == s_wait_empty_line <EOL> and RE_RST_STATEMENT . match ( line ) ) : <EOL> new . write ( '<STR_LIT:U+0020>' + <EOL> RE_RST_STATEMENT . match ( line ) . group ( <NUM_LIT:1> ) + '<STR_LIT:\n>' ) <EOL> elif state == s_wait_empty_line and line == '<STR_LIT:\n>' : <EOL> new . write ( '<STR_LIT>' ) <EOL> state = s_done <EOL> else : <EOL> new . write ( line ) <EOL> os . remove ( path ) <EOL> shutil . move ( new_path , path ) <EOL> def make_doc_directory ( config , path ) : <EOL> sphinx_conf = { <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT:_>' , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : config [ '<STR_LIT>' ] , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:index>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True } <EOL> quickstart . generate ( sphinx_conf ) <EOL> src_path = os . path . join ( path , '<STR_LIT>' ) <EOL> with open ( src_path , '<STR_LIT:r>' ) as src : <EOL> with open ( os . path . join ( path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as dst : <EOL> for line in src . readlines ( ) : <EOL> if RE_HTML_THEME . match ( line ) : <EOL> line = '<STR_LIT>' <EOL> dst . write ( line ) <EOL> os . unlink ( src_path ) <EOL> write_cmdline ( config , os . path . join ( path , '<STR_LIT>' ) ) <EOL> modify_doc_makefile ( config , os . path . join ( path , '<STR_LIT>' ) ) <EOL> modify_doc_index ( config , os . path . join ( path , '<STR_LIT>' ) ) <EOL> with open ( os . path . join ( path , '<STR_LIT>' ) , '<STR_LIT:w>' ) as dst : <EOL> name = str ( config [ '<STR_LIT>' ] ) . capitalize ( ) <EOL> match = RE_PROJECT_NAME . match ( config [ '<STR_LIT>' ] ) <EOL> if match : <EOL> name = match . group ( <NUM_LIT:1> ) <EOL> title = "<STR_LIT>" % name <EOL> heading = "<STR_LIT>" % ( title , '<STR_LIT:=>' * len ( title ) ) <EOL> dst . write ( DOC_PYTHON_REFERENCE . format ( <EOL> name = config [ '<STR_LIT>' ] , heading = heading ) ) <EOL> STRUCTURE = { <EOL> '<STR_LIT>' : make_doc_directory , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : partial ( write_init , namespace_package = True ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : partial ( write_init , namespace_package = True ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : write_init <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT>' : write_setup , <EOL> '<STR_LIT>' : write_setup_cfg , <EOL> '<STR_LIT>' : write_empty , <EOL> '<STR_LIT>' : write_makefile , <EOL> } <EOL> def make_file ( config , filepath , prescription ) : <EOL> filepath = filepath . format ( ** config ) <EOL> if isinstance ( prescription , dict ) : <EOL> os . mkdir ( filepath ) <EOL> for filename , presc in prescription . items ( ) : <EOL> make_file ( config , os . path . join ( filepath , filename ) , presc ) <EOL> else : <EOL> prescription ( config , filepath ) <EOL> def make_command ( config , base_dir ) : <EOL> cmd_dir = os . path . join ( base_dir , config [ '<STR_LIT>' ] ) <EOL> if os . path . exists ( cmd_dir ) : <EOL> die ( u'<STR_LIT>' , config [ '<STR_LIT>' ] ) <EOL> make_file ( config , cmd_dir , STRUCTURE ) <EOL> def parse_command_line ( args = None ) : <EOL> if args is None : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> usage = __doc__ . format ( cmd = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> options = docopt ( usage , args ) <EOL> for opt_name in ( '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT:description>' ) : <EOL> options [ opt_name ] = options . pop ( '<STR_LIT>' + opt_name ) <EOL> options [ '<STR_LIT>' ] = options . pop ( '<STR_LIT>' ) <EOL> options [ '<STR_LIT>' ] = options . pop ( '<STR_LIT>' ) <EOL> if not RE_COMMAND_NAME . match ( options [ '<STR_LIT>' ] ) : <EOL> die ( "<STR_LIT>" , <EOL> RE_COMMAND_NAME . pattern ) <EOL> for info , question , default_value in ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT:email>' , '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT:description>' , '<STR_LIT>' , None ) ) : <EOL> if options . get ( info , None ) : <EOL> continue <EOL> try : <EOL> options [ info ] = ask ( question ) <EOL> except EOFError : <EOL> pass <EOL> if not options [ info ] : <EOL> if default_value is None : <EOL> options [ info ] = '<STR_LIT>' <EOL> else : <EOL> options [ info ] = default_value <EOL> if options [ '<STR_LIT>' ] is None : <EOL> default = '<STR_LIT>' . format ( <EOL> command = options [ '<STR_LIT>' ] . capitalize ( ) ) <EOL> try : <EOL> options [ '<STR_LIT>' ] = ask ( <EOL> '<STR_LIT>' . format ( default = default ) ) <EOL> except EOFError : <EOL> pass <EOL> if not options [ '<STR_LIT>' ] : <EOL> options [ '<STR_LIT>' ] = default <EOL> options = { k : ( v . decode ( '<STR_LIT:utf-8>' ) if isinstance ( v , str ) else v ) <EOL> for k , v in options . items ( ) } <EOL> return options <EOL> def main ( ) : <EOL> script_directory = os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) <EOL> config = parse_command_line ( ) <EOL> if not os . path . samefile ( script_directory , os . getcwd ( ) ) : <EOL> script_directory = os . path . normpath ( <EOL> os . path . join ( os . getcwd ( ) , script_directory ) ) <EOL> make_command ( config , script_directory ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
