<s> from __future__ import division , absolute_import , print_function <EOL> import sys <EOL> import re <EOL> import inspect <EOL> import textwrap <EOL> import pydoc <EOL> import sphinx <EOL> import collections <EOL> from . docscrape import NumpyDocString , FunctionDoc , ClassDoc <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> sixu = lambda s : s <EOL> else : <EOL> sixu = lambda s : unicode ( s , '<STR_LIT>' ) <EOL> class SphinxDocString ( NumpyDocString ) : <EOL> def __init__ ( self , docstring , config = { } ) : <EOL> NumpyDocString . __init__ ( self , docstring , config = config ) <EOL> self . load_config ( config ) <EOL> def load_config ( self , config ) : <EOL> self . use_plots = config . get ( '<STR_LIT>' , False ) <EOL> self . class_members_toctree = config . get ( '<STR_LIT>' , True ) <EOL> def _str_header ( self , name , symbol = '<STR_LIT>' ) : <EOL> return [ '<STR_LIT>' + name , '<STR_LIT>' ] <EOL> def _str_field_list ( self , name ) : <EOL> return [ '<STR_LIT::>' + name + '<STR_LIT::>' ] <EOL> def _str_indent ( self , doc , indent = <NUM_LIT:4> ) : <EOL> out = [ ] <EOL> for line in doc : <EOL> out += [ '<STR_LIT:U+0020>' * indent + line ] <EOL> return out <EOL> def _str_signature ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> if self [ '<STR_LIT>' ] : <EOL> return [ '<STR_LIT>' % self [ '<STR_LIT>' ] ] + [ '<STR_LIT>' ] <EOL> else : <EOL> return [ '<STR_LIT>' ] <EOL> def _str_summary ( self ) : <EOL> return self [ '<STR_LIT>' ] + [ '<STR_LIT>' ] <EOL> def _str_extended_summary ( self ) : <EOL> return self [ '<STR_LIT>' ] + [ '<STR_LIT>' ] <EOL> def _str_returns ( self , name = '<STR_LIT>' ) : <EOL> out = [ ] <EOL> if self [ name ] : <EOL> out += self . _str_field_list ( name ) <EOL> out += [ '<STR_LIT>' ] <EOL> for param , param_type , desc in self [ name ] : <EOL> if param_type : <EOL> out += self . _str_indent ( [ '<STR_LIT>' % ( param . strip ( ) , <EOL> param_type ) ] ) <EOL> else : <EOL> out += self . _str_indent ( [ param . strip ( ) ] ) <EOL> if desc : <EOL> out += [ '<STR_LIT>' ] <EOL> out += self . _str_indent ( desc , <NUM_LIT:8> ) <EOL> out += [ '<STR_LIT>' ] <EOL> return out <EOL> def _str_param_list ( self , name ) : <EOL> out = [ ] <EOL> if self [ name ] : <EOL> out += self . _str_field_list ( name ) <EOL> out += [ '<STR_LIT>' ] <EOL> for param , param_type , desc in self [ name ] : <EOL> if param_type : <EOL> out += self . _str_indent ( [ '<STR_LIT>' % ( param . strip ( ) , <EOL> param_type ) ] ) <EOL> else : <EOL> out += self . _str_indent ( [ '<STR_LIT>' % param . strip ( ) ] ) <EOL> if desc : <EOL> out += [ '<STR_LIT>' ] <EOL> out += self . _str_indent ( desc , <NUM_LIT:8> ) <EOL> out += [ '<STR_LIT>' ] <EOL> return out <EOL> @ property <EOL> def _obj ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _cls <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _f <EOL> return None <EOL> def _str_member_list ( self , name ) : <EOL> """<STR_LIT>""" <EOL> out = [ ] <EOL> if self [ name ] : <EOL> out += [ '<STR_LIT>' % name , '<STR_LIT>' ] <EOL> prefix = getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if prefix : <EOL> prefix = '<STR_LIT>' % prefix <EOL> autosum = [ ] <EOL> others = [ ] <EOL> for param , param_type , desc in self [ name ] : <EOL> param = param . strip ( ) <EOL> param_obj = getattr ( self . _obj , param , None ) <EOL> if not ( callable ( param_obj ) <EOL> or isinstance ( param_obj , property ) <EOL> or inspect . isgetsetdescriptor ( param_obj ) ) : <EOL> param_obj = None <EOL> if param_obj and ( pydoc . getdoc ( param_obj ) or not desc ) : <EOL> autosum += [ "<STR_LIT>" % ( prefix , param ) ] <EOL> else : <EOL> others . append ( ( param , param_type , desc ) ) <EOL> if autosum : <EOL> out += [ '<STR_LIT>' ] <EOL> if self . class_members_toctree : <EOL> out += [ '<STR_LIT>' ] <EOL> out += [ '<STR_LIT>' ] + autosum <EOL> if others : <EOL> maxlen_0 = max ( <NUM_LIT:3> , max ( [ len ( x [ <NUM_LIT:0> ] ) for x in others ] ) ) <EOL> hdr = sixu ( "<STR_LIT:=>" ) * maxlen_0 + sixu ( "<STR_LIT:U+0020>" ) + sixu ( "<STR_LIT:=>" ) * <NUM_LIT:10> <EOL> fmt = sixu ( '<STR_LIT>' ) % ( maxlen_0 , ) <EOL> out += [ '<STR_LIT>' , '<STR_LIT>' , hdr ] <EOL> for param , param_type , desc in others : <EOL> desc = sixu ( "<STR_LIT:U+0020>" ) . join ( x . strip ( ) for x in desc ) . strip ( ) <EOL> if param_type : <EOL> desc = "<STR_LIT>" % ( param_type , desc ) <EOL> out += [ fmt % ( param . strip ( ) , desc ) ] <EOL> out += [ hdr ] <EOL> out += [ '<STR_LIT>' ] <EOL> return out <EOL> def _str_section ( self , name ) : <EOL> out = [ ] <EOL> if self [ name ] : <EOL> out += self . _str_header ( name ) <EOL> out += [ '<STR_LIT>' ] <EOL> content = textwrap . dedent ( "<STR_LIT:\n>" . join ( self [ name ] ) ) . split ( "<STR_LIT:\n>" ) <EOL> out += content <EOL> out += [ '<STR_LIT>' ] <EOL> return out <EOL> def _str_see_also ( self , func_role ) : <EOL> out = [ ] <EOL> if self [ '<STR_LIT>' ] : <EOL> see_also = super ( SphinxDocString , self ) . _str_see_also ( func_role ) <EOL> out = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> out += self . _str_indent ( see_also [ <NUM_LIT:2> : ] ) <EOL> return out <EOL> def _str_warnings ( self ) : <EOL> out = [ ] <EOL> if self [ '<STR_LIT>' ] : <EOL> out = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> out += self . _str_indent ( self [ '<STR_LIT>' ] ) <EOL> return out <EOL> def _str_index ( self ) : <EOL> idx = self [ '<STR_LIT:index>' ] <EOL> out = [ ] <EOL> if len ( idx ) == <NUM_LIT:0> : <EOL> return out <EOL> out += [ '<STR_LIT>' % idx . get ( '<STR_LIT:default>' , '<STR_LIT>' ) ] <EOL> for section , references in idx . items ( ) : <EOL> if section == '<STR_LIT:default>' : <EOL> continue <EOL> elif section == '<STR_LIT>' : <EOL> out += [ '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( references ) ) ] <EOL> else : <EOL> out += [ '<STR_LIT>' % ( section , '<STR_LIT:U+002C>' . join ( references ) ) ] <EOL> return out <EOL> def _str_references ( self ) : <EOL> out = [ ] <EOL> if self [ '<STR_LIT>' ] : <EOL> out += self . _str_header ( '<STR_LIT>' ) <EOL> if isinstance ( self [ '<STR_LIT>' ] , str ) : <EOL> self [ '<STR_LIT>' ] = [ self [ '<STR_LIT>' ] ] <EOL> out . extend ( self [ '<STR_LIT>' ] ) <EOL> out += [ '<STR_LIT>' ] <EOL> if sphinx . __version__ >= "<STR_LIT>" : <EOL> out += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> out += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> items = [ ] <EOL> for line in self [ '<STR_LIT>' ] : <EOL> m = re . match ( r'<STR_LIT>' , line , re . I ) <EOL> if m : <EOL> items . append ( m . group ( <NUM_LIT:1> ) ) <EOL> out += [ '<STR_LIT:U+0020>' + "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % item for item in items ] ) , '<STR_LIT>' ] <EOL> return out <EOL> def _str_examples ( self ) : <EOL> examples_str = "<STR_LIT:\n>" . join ( self [ '<STR_LIT>' ] ) <EOL> if ( self . use_plots and '<STR_LIT>' in examples_str <EOL> and '<STR_LIT>' not in examples_str ) : <EOL> out = [ ] <EOL> out += self . _str_header ( '<STR_LIT>' ) <EOL> out += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> out += self . _str_indent ( self [ '<STR_LIT>' ] ) <EOL> out += [ '<STR_LIT>' ] <EOL> return out <EOL> else : <EOL> return self . _str_section ( '<STR_LIT>' ) <EOL> def __str__ ( self , indent = <NUM_LIT:0> , func_role = "<STR_LIT>" ) : <EOL> out = [ ] <EOL> out += self . _str_signature ( ) <EOL> out += self . _str_index ( ) + [ '<STR_LIT>' ] <EOL> out += self . _str_summary ( ) <EOL> out += self . _str_extended_summary ( ) <EOL> out += self . _str_param_list ( '<STR_LIT>' ) <EOL> out += self . _str_returns ( '<STR_LIT>' ) <EOL> out += self . _str_returns ( '<STR_LIT>' ) <EOL> for param_list in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> out += self . _str_param_list ( param_list ) <EOL> out += self . _str_warnings ( ) <EOL> out += self . _str_see_also ( func_role ) <EOL> out += self . _str_section ( '<STR_LIT>' ) <EOL> out += self . _str_references ( ) <EOL> out += self . _str_examples ( ) <EOL> for param_list in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> out += self . _str_member_list ( param_list ) <EOL> out = self . _str_indent ( out , indent ) <EOL> return '<STR_LIT:\n>' . join ( out ) <EOL> class SphinxFunctionDoc ( SphinxDocString , FunctionDoc ) : <EOL> def __init__ ( self , obj , doc = None , config = { } ) : <EOL> self . load_config ( config ) <EOL> FunctionDoc . __init__ ( self , obj , doc = doc , config = config ) <EOL> class SphinxClassDoc ( SphinxDocString , ClassDoc ) : <EOL> def __init__ ( self , obj , doc = None , func_doc = None , config = { } ) : <EOL> self . load_config ( config ) <EOL> ClassDoc . __init__ ( self , obj , doc = doc , func_doc = None , config = config ) <EOL> class SphinxObjDoc ( SphinxDocString ) : <EOL> def __init__ ( self , obj , doc = None , config = { } ) : <EOL> self . _f = obj <EOL> self . load_config ( config ) <EOL> SphinxDocString . __init__ ( self , doc , config = config ) <EOL> def get_doc_object ( obj , what = None , doc = None , config = { } ) : <EOL> if what is None : <EOL> if inspect . isclass ( obj ) : <EOL> what = '<STR_LIT:class>' <EOL> elif inspect . ismodule ( obj ) : <EOL> what = '<STR_LIT>' <EOL> elif isinstance ( obj , collections . Callable ) : <EOL> what = '<STR_LIT>' <EOL> else : <EOL> what = '<STR_LIT:object>' <EOL> if what == '<STR_LIT:class>' : <EOL> return SphinxClassDoc ( obj , func_doc = SphinxFunctionDoc , doc = doc , <EOL> config = config ) <EOL> elif what in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return SphinxFunctionDoc ( obj , doc = doc , config = config ) <EOL> else : <EOL> if doc is None : <EOL> doc = pydoc . getdoc ( obj ) <EOL> return SphinxObjDoc ( obj , doc , config = config ) </s>
