<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> __docformat__ = "<STR_LIT>" <EOL> import ast , os <EOL> from docutils import nodes , statemachine , utils <EOL> from docutils . parsers . rst import Directive <EOL> from docutils . parsers . rst import directives <EOL> from docutils . nodes import fully_normalize_name <EOL> def setup ( app ) : <EOL> app . add_directive ( '<STR_LIT>' , AutoExampleDirective ) <EOL> def parse_example ( basepath , filename , img_folder , img_folder_os ) : <EOL> path = os . path . join ( basepath , filename ) <EOL> with open ( path ) as f : <EOL> content = f . read ( ) . strip ( ) <EOL> n = ast . parse ( content ) <EOL> docstring = ast . get_docstring ( n ) <EOL> if docstring == None : <EOL> return [ ] <EOL> s = [ filename , "<STR_LIT>" . ljust ( len ( filename ) , '<STR_LIT:->' ) ] <EOL> folder = os . path . basename ( basepath ) <EOL> s . append ( "<STR_LIT>" % ( folder , filename ) ) <EOL> s . append ( "<STR_LIT>" ) <EOL> s . append ( docstring ) <EOL> s . append ( "<STR_LIT>" ) <EOL> img_name , _ = os . path . splitext ( filename ) <EOL> img_name += "<STR_LIT>" <EOL> if img_folder != None : <EOL> img_path = os . path . join ( img_folder , img_name ) <EOL> img_path_os = os . path . join ( img_folder_os , img_name ) <EOL> if os . path . isfile ( img_path_os ) : <EOL> s . append ( "<STR_LIT>" ) <EOL> s . append ( "<STR_LIT>" + img_path ) <EOL> s . append ( "<STR_LIT>" ) <EOL> s . append ( "<STR_LIT>" ) <EOL> return s <EOL> def parse_examples ( path , img_folder , img_folder_os ) : <EOL> lines = [ ] <EOL> print ( "<STR_LIT>" + path ) <EOL> for root , dirs , files in os . walk ( path ) : <EOL> for file in files : <EOL> path = os . path . join ( root , file ) <EOL> if not os . path . isfile ( path ) : <EOL> continue <EOL> _ , ext = os . path . splitext ( path ) <EOL> if ext != "<STR_LIT>" : <EOL> continue <EOL> print ( "<STR_LIT>" + file ) <EOL> lines += parse_example ( root , file , img_folder , img_folder_os ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> class AutoExampleDirective ( Directive ) : <EOL> required_arguments = <NUM_LIT:1> <EOL> option_spec = { '<STR_LIT>' : str } <EOL> def run ( self ) : <EOL> source = self . state_machine . input_lines . source ( self . lineno - self . state_machine . input_offset - <NUM_LIT:1> ) <EOL> source_dir = os . path . dirname ( os . path . abspath ( source ) ) <EOL> path = directives . path ( self . arguments [ <NUM_LIT:0> ] ) <EOL> path = os . path . normpath ( os . path . join ( source_dir , path ) ) <EOL> path = utils . relative_path ( None , path ) <EOL> path = nodes . reprunicode ( path ) <EOL> img_folder = None <EOL> img_folder_os = None <EOL> if "<STR_LIT>" in self . options : <EOL> img_folder_os = os . path . normpath ( os . path . join ( source_dir , self . options [ "<STR_LIT>" ] ) ) <EOL> img_folder = self . options [ "<STR_LIT>" ] <EOL> rawtext = parse_examples ( path , img_folder , img_folder_os ) <EOL> include_lines = statemachine . string2lines ( rawtext , self . state . document . settings . tab_width , <EOL> convert_whitespace = True ) <EOL> self . state_machine . insert_input ( include_lines , path ) <EOL> return [ ] </s>
