def get_docstring_and_rest(filename):
 with open(filename, 'rb') as fid:
  content = fid.read()
 content = content.replace('\r\n', '\n')
 try:
  node = ast.parse(content)
 except SyntaxError:
  return (SYNTAX_ERROR_DOCSTRING, content.decode('utf-8'))
 if (not isinstance(node, ast.Module)):
  raise TypeError('This function only supports modules. You provided {0}'.format(node.__class__.__name__))
 if (node.body and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Str)):
  docstring_node = node.body[0]
  docstring = docstring_node.value.s
  if hasattr(docstring, 'decode'):
   docstring = docstring.decode('utf-8')
  rest = content.decode('utf-8').split('\n', docstring_node.lineno)[(-1)]
  return (docstring, rest)
 else:
  raise ValueError('Could not find docstring in file "{0}". A docstring is required by sphinx-gallery'.format(filename))

def get_docstring_and_rest ( filename ) : with open ( filename , 'rb' ) as fid : content = fid . read ( ) content = content . replace ( '\r\n' , '\n' ) try : node = ast . parse ( content ) except SyntaxError : return ( SYNTAX_ERROR_DOCSTRING , content . decode ( 'utf-8' ) ) if ( not isinstance ( node , ast . Module ) ) : raise TypeError ( 'This function only supports modules. You provided {0}' . format ( node . __class__ . __name__ ) ) if ( node . body and isinstance ( node . body [ 0 ] , ast . Expr ) and isinstance ( node . body [ 0 ] . value , ast . Str ) ) : docstring_node = node . body [ 0 ] docstring = docstring_node . value . s if hasattr ( docstring , 'decode' ) : docstring = docstring . decode ( 'utf-8' ) rest = content . decode ( 'utf-8' ) . split ( '\n' , docstring_node . lineno ) [ ( - 1 ) ] return ( docstring , rest ) else : raise ValueError ( 'Could not find docstring in file "{0}". A docstring is required by sphinx-gallery' . format ( filename ) )
separate filename content between docstring and the rest strongly inspired from ast .