<s> """<STR_LIT>""" <EOL> import sphinx <EOL> if sphinx . __version__ < '<STR_LIT>' : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> import os , re , pydoc <EOL> from docscrape_sphinx import get_doc_object , SphinxDocString <EOL> from sphinx . util . compat import Directive <EOL> import inspect <EOL> def mangle_docstrings ( app , what , name , obj , options , lines , <EOL> reference_offset = [ <NUM_LIT:0> ] ) : <EOL> cfg = dict ( use_plots = app . config . numpydoc_use_plots , <EOL> show_class_members = app . config . numpydoc_show_class_members ) <EOL> if what == '<STR_LIT>' : <EOL> title_re = re . compile ( ur '<STR_LIT>' , <EOL> re . I | re . S ) <EOL> lines [ : ] = title_re . sub ( u'<STR_LIT>' , u"<STR_LIT:\n>" . join ( lines ) ) . split ( u"<STR_LIT:\n>" ) <EOL> else : <EOL> doc = get_doc_object ( obj , what , u"<STR_LIT:\n>" . join ( lines ) , config = cfg ) <EOL> lines [ : ] = unicode ( doc ) . split ( u"<STR_LIT:\n>" ) <EOL> if app . config . numpydoc_edit_link and hasattr ( obj , '<STR_LIT>' ) and obj . __name__ : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> v = dict ( full_name = u"<STR_LIT>" % ( obj . __module__ , obj . __name__ ) ) <EOL> else : <EOL> v = dict ( full_name = obj . __name__ ) <EOL> lines += [ u'<STR_LIT>' , u'<STR_LIT>' , '<STR_LIT>' ] <EOL> lines += [ u'<STR_LIT>' % x for x in <EOL> ( app . config . numpydoc_edit_link % v ) . split ( "<STR_LIT:\n>" ) ] <EOL> references = [ ] <EOL> for line in lines : <EOL> line = line . strip ( ) <EOL> m = re . match ( ur '<STR_LIT>' , line , re . I ) <EOL> if m : <EOL> references . append ( m . group ( <NUM_LIT:1> ) ) <EOL> references . sort ( key = lambda x : - len ( x ) ) <EOL> if references : <EOL> for i , line in enumerate ( lines ) : <EOL> for r in references : <EOL> if re . match ( ur '<STR_LIT>' , r ) : <EOL> new_r = u"<STR_LIT>" % ( reference_offset [ <NUM_LIT:0> ] + int ( r ) ) <EOL> else : <EOL> new_r = u"<STR_LIT>" % ( r , reference_offset [ <NUM_LIT:0> ] ) <EOL> lines [ i ] = lines [ i ] . replace ( u'<STR_LIT>' % r , <EOL> u'<STR_LIT>' % new_r ) <EOL> lines [ i ] = lines [ i ] . replace ( u'<STR_LIT>' % r , <EOL> u'<STR_LIT>' % new_r ) <EOL> reference_offset [ <NUM_LIT:0> ] += len ( references ) <EOL> def mangle_signature ( app , what , name , obj , options , sig , retann ) : <EOL> if ( inspect . isclass ( obj ) and <EOL> ( not hasattr ( obj , '<STR_LIT>' ) or <EOL> '<STR_LIT>' in pydoc . getdoc ( obj . __init__ ) ) ) : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> if not ( callable ( obj ) or hasattr ( obj , '<STR_LIT>' ) ) : return <EOL> if not hasattr ( obj , '<STR_LIT>' ) : return <EOL> doc = SphinxDocString ( pydoc . getdoc ( obj ) ) <EOL> if doc [ '<STR_LIT>' ] : <EOL> sig = re . sub ( u"<STR_LIT>" , u"<STR_LIT>" , doc [ '<STR_LIT>' ] ) <EOL> return sig , u'<STR_LIT>' <EOL> def setup ( app , get_doc_object_ = get_doc_object ) : <EOL> global get_doc_object <EOL> get_doc_object = get_doc_object_ <EOL> app . connect ( '<STR_LIT>' , mangle_docstrings ) <EOL> app . connect ( '<STR_LIT>' , mangle_signature ) <EOL> app . add_config_value ( '<STR_LIT>' , None , False ) <EOL> app . add_config_value ( '<STR_LIT>' , None , False ) <EOL> app . add_config_value ( '<STR_LIT>' , True , True ) <EOL> app . add_domain ( NumpyPythonDomain ) <EOL> app . add_domain ( NumpyCDomain ) <EOL> from docutils . statemachine import ViewList <EOL> from sphinx . domains . c import CDomain <EOL> from sphinx . domains . python import PythonDomain <EOL> class ManglingDomainBase ( object ) : <EOL> directive_mangling_map = { } <EOL> def __init__ ( self , * a , ** kw ) : <EOL> super ( ManglingDomainBase , self ) . __init__ ( * a , ** kw ) <EOL> self . wrap_mangling_directives ( ) <EOL> def wrap_mangling_directives ( self ) : <EOL> for name , objtype in self . directive_mangling_map . items ( ) : <EOL> self . directives [ name ] = wrap_mangling_directive ( <EOL> self . directives [ name ] , objtype ) <EOL> class NumpyPythonDomain ( ManglingDomainBase , PythonDomain ) : <EOL> name = '<STR_LIT>' <EOL> directive_mangling_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:class>' : '<STR_LIT:class>' , <EOL> '<STR_LIT>' : '<STR_LIT:class>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class NumpyCDomain ( ManglingDomainBase , CDomain ) : <EOL> name = '<STR_LIT>' <EOL> directive_mangling_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT:class>' , <EOL> '<STR_LIT>' : '<STR_LIT:object>' , <EOL> } <EOL> def wrap_mangling_directive ( base_directive , objtype ) : <EOL> class directive ( base_directive ) : <EOL> def run ( self ) : <EOL> env = self . state . document . settings . env <EOL> name = None <EOL> if self . arguments : <EOL> m = re . match ( r'<STR_LIT>' , self . arguments [ <NUM_LIT:0> ] ) <EOL> name = m . group ( <NUM_LIT:2> ) . strip ( ) <EOL> if not name : <EOL> name = self . arguments [ <NUM_LIT:0> ] <EOL> lines = list ( self . content ) <EOL> mangle_docstrings ( env . app , objtype , name , None , None , lines ) <EOL> self . content = ViewList ( lines , self . content . parent ) <EOL> return base_directive . run ( self ) <EOL> return directive </s>
