<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import re , codecs <EOL> from epydoc import log <EOL> from epydoc . util import py_src_filename <EOL> from epydoc . apidoc import * <EOL> import tokenize , token , cgi , keyword <EOL> try : from cStringIO import StringIO <EOL> except : from StringIO import StringIO <EOL> """<STR_LIT>""" <EOL> PYSRC_JAVASCRIPTS = '''<STR_LIT>''' <EOL> PYSRC_EXPANDTO_JAVASCRIPT = '''<STR_LIT>''' <EOL> class PythonSourceColorizer : <EOL> """<STR_LIT>""" <EOL> CSS_CLASSES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:@>' : '<STR_LIT>' , <EOL> } <EOL> START_DEF_BLOCK = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> END_DEF_BLOCK = '<STR_LIT>' <EOL> UNICODE_CODING_RE = re . compile ( r'<STR_LIT>' ) <EOL> ADD_DEF_BLOCKS = True <EOL> ADD_LINE_NUMBERS = True <EOL> ADD_TOOLTIPS = True <EOL> GUESS_LINK_TARGETS = False <EOL> def __init__ ( self , module_filename , module_name , <EOL> docindex = None , url_func = None , name_to_docs = None , <EOL> tab_width = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> try : module_filename = py_src_filename ( module_filename ) <EOL> except : pass <EOL> self . module_filename = module_filename <EOL> self . module_name = module_name <EOL> self . docindex = docindex <EOL> self . name_to_docs = name_to_docs <EOL> self . url_func = url_func <EOL> self . pos = <NUM_LIT:0> <EOL> self . line_offsets = [ ] <EOL> self . cur_line = [ ] <EOL> self . context = [ ] <EOL> self . context_types = [ ] <EOL> self . indents = [ ] <EOL> self . lineno = <NUM_LIT:0> <EOL> self . def_name = None <EOL> self . def_type = None <EOL> self . tab_width = tab_width <EOL> def find_line_offsets ( self ) : <EOL> """<STR_LIT>""" <EOL> self . line_offsets = [ None , <NUM_LIT:0> ] <EOL> pos = self . text . find ( '<STR_LIT:\n>' ) <EOL> while pos != - <NUM_LIT:1> : <EOL> self . line_offsets . append ( pos + <NUM_LIT:1> ) <EOL> pos = self . text . find ( '<STR_LIT:\n>' , pos + <NUM_LIT:1> ) <EOL> self . line_offsets . append ( len ( self . text ) ) <EOL> def lineno_to_html ( self ) : <EOL> template = '<STR_LIT>' % self . linenum_size <EOL> n = template % self . lineno <EOL> return '<STR_LIT>' % ( self . lineno , n ) <EOL> def colorize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pos = <NUM_LIT:0> <EOL> self . cur_line = [ ] <EOL> self . context = [ ] <EOL> self . context_types = [ ] <EOL> self . indents = [ ] <EOL> self . lineno = <NUM_LIT:1> <EOL> self . def_name = None <EOL> self . def_type = None <EOL> self . has_decorators = False <EOL> self . doclink_targets_cache = { } <EOL> self . text = open ( self . module_filename ) . read ( ) <EOL> self . text = self . text . expandtabs ( self . tab_width ) . rstrip ( ) + '<STR_LIT:\n>' <EOL> self . find_line_offsets ( ) <EOL> num_lines = self . text . count ( '<STR_LIT:\n>' ) + <NUM_LIT:1> <EOL> self . linenum_size = len ( ` num_lines + <NUM_LIT:1> ` ) <EOL> try : <EOL> output = StringIO ( ) <EOL> self . out = output . write <EOL> tokenize . tokenize ( StringIO ( self . text ) . readline , self . tokeneater ) <EOL> html = output . getvalue ( ) <EOL> if self . has_decorators : <EOL> html = self . _FIX_DECORATOR_RE . sub ( r'<STR_LIT>' , html ) <EOL> except tokenize . TokenError , ex : <EOL> html = self . text <EOL> m = self . UNICODE_CODING_RE . match ( self . text ) <EOL> if m : coding = m . group ( <NUM_LIT:1> ) <EOL> else : coding = '<STR_LIT>' <EOL> try : <EOL> html = html . decode ( coding ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> except LookupError : <EOL> coding = '<STR_LIT>' <EOL> html = html . decode ( coding ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> html += PYSRC_EXPANDTO_JAVASCRIPT <EOL> return html <EOL> def tokeneater ( self , toktype , toktext , ( srow , scol ) , ( erow , ecol ) , line ) : <EOL> """<STR_LIT>""" <EOL> if toktype == token . ERRORTOKEN : <EOL> raise tokenize . TokenError , toktype <EOL> startpos = self . line_offsets [ srow ] + scol <EOL> if startpos > self . pos : <EOL> skipped = self . text [ self . pos : startpos ] <EOL> self . cur_line . append ( ( None , skipped ) ) <EOL> self . pos = startpos + len ( toktext ) <EOL> self . cur_line . append ( ( toktype , toktext ) ) <EOL> if toktype == token . NEWLINE or toktype == token . ENDMARKER : <EOL> self . handle_line ( self . cur_line ) <EOL> self . cur_line = [ ] <EOL> _next_uid = <NUM_LIT:0> <EOL> def handle_line ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def_name = None <EOL> def_type = None <EOL> starting_def_block = False <EOL> in_base_list = False <EOL> in_param_list = False <EOL> in_param_default = <NUM_LIT:0> <EOL> at_module_top = ( self . lineno == <NUM_LIT:1> ) <EOL> ended_def_blocks = <NUM_LIT:0> <EOL> if self . ADD_LINE_NUMBERS : <EOL> s = self . lineno_to_html ( ) <EOL> self . lineno += <NUM_LIT:1> <EOL> else : <EOL> s = '<STR_LIT>' <EOL> s += '<STR_LIT>' <EOL> for i , ( toktype , toktext ) in enumerate ( line ) : <EOL> if type ( s ) is not str : <EOL> if type ( s ) is unicode : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % self . module_name ) <EOL> s = s . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> type ( s ) . __name__ ) <EOL> css_class = None <EOL> url = None <EOL> tooltip = None <EOL> onclick = uid = targets = None <EOL> if i >= <NUM_LIT:2> and line [ i - <NUM_LIT:2> ] [ <NUM_LIT:1> ] == '<STR_LIT:class>' : <EOL> in_base_list = True <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> def_name = toktext <EOL> def_type = '<STR_LIT:class>' <EOL> if '<STR_LIT>' not in self . context_types : <EOL> cls_name = self . context_name ( def_name ) <EOL> url = self . name2url ( cls_name ) <EOL> s = self . mark_def ( s , cls_name ) <EOL> starting_def_block = True <EOL> elif i >= <NUM_LIT:2> and line [ i - <NUM_LIT:2> ] [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> in_param_list = True <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> def_name = toktext <EOL> def_type = '<STR_LIT>' <EOL> if '<STR_LIT>' not in self . context_types : <EOL> cls_name = self . context_name ( ) <EOL> func_name = self . context_name ( def_name ) <EOL> url = self . name2url ( cls_name , def_name ) <EOL> s = self . mark_def ( s , func_name ) <EOL> starting_def_block = True <EOL> elif toktype == token . INDENT : <EOL> self . indents . append ( toktext ) <EOL> self . context . append ( self . def_name ) <EOL> self . context_types . append ( self . def_type ) <EOL> elif toktype == token . DEDENT : <EOL> self . indents . pop ( ) <EOL> self . context_types . pop ( ) <EOL> if self . context . pop ( ) : <EOL> ended_def_blocks += <NUM_LIT:1> <EOL> elif toktype in ( None , tokenize . NL , token . NEWLINE , <EOL> token . ENDMARKER ) : <EOL> css_class = None <EOL> elif toktype == token . NAME and keyword . iskeyword ( toktext ) : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> elif in_base_list and toktype == token . NAME : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> elif ( in_param_list and toktype == token . NAME and <EOL> not in_param_default ) : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> elif ( self . def_name and line [ i - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == token . INDENT and <EOL> self . is_docstring ( line , i ) ) : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> elif at_module_top and self . is_docstring ( line , i ) : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> elif ( toktype == token . NAME and <EOL> ( ( i > <NUM_LIT:0> and line [ i - <NUM_LIT:1> ] [ <NUM_LIT:1> ] == '<STR_LIT:@>' ) or <EOL> ( i > <NUM_LIT:1> and line [ i - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == None and line [ i - <NUM_LIT:2> ] [ <NUM_LIT:1> ] == '<STR_LIT:@>' ) ) ) : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> self . has_decorators = True <EOL> elif toktype == token . NAME : <EOL> css_class = self . CSS_CLASSES [ '<STR_LIT>' ] <EOL> if ( self . GUESS_LINK_TARGETS and self . docindex is not None <EOL> and self . url_func is not None ) : <EOL> context = [ n for n in self . context if n is not None ] <EOL> container = self . docindex . get_vardoc ( <EOL> DottedName ( self . module_name , * context ) ) <EOL> if isinstance ( container , NamespaceDoc ) : <EOL> doc = container . variables . get ( toktext ) <EOL> if doc is not None : <EOL> url = self . url_func ( doc ) <EOL> tooltip = str ( doc . canonical_name ) <EOL> if ( url is None and self . name_to_docs is not None <EOL> and self . url_func is not None ) : <EOL> docs = self . name_to_docs . get ( toktext ) <EOL> if docs : <EOL> tooltip = '<STR_LIT:\n>' . join ( [ str ( d . canonical_name ) <EOL> for d in docs ] ) <EOL> if len ( docs ) == <NUM_LIT:1> and self . GUESS_LINK_TARGETS : <EOL> url = self . url_func ( docs [ <NUM_LIT:0> ] ) <EOL> else : <EOL> uid , onclick , targets = self . doclink ( toktext , docs ) <EOL> else : <EOL> if toktype == token . OP and toktext in self . CSS_CLASSES : <EOL> css_class = self . CSS_CLASSES [ toktext ] <EOL> elif token . tok_name [ toktype ] in self . CSS_CLASSES : <EOL> css_class = self . CSS_CLASSES [ token . tok_name [ toktype ] ] <EOL> else : <EOL> css_class = None <EOL> if toktext == '<STR_LIT::>' : <EOL> in_base_list = False <EOL> in_param_list = False <EOL> if toktext == '<STR_LIT:=>' and in_param_list : <EOL> in_param_default = True <EOL> if in_param_default : <EOL> if toktext in ( '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' ) : in_param_default += <NUM_LIT:1> <EOL> if toktext in ( '<STR_LIT:)>' , '<STR_LIT:]>' , '<STR_LIT:}>' ) : in_param_default -= <NUM_LIT:1> <EOL> if toktext == '<STR_LIT:U+002C>' and in_param_default == <NUM_LIT:1> : <EOL> in_param_default = <NUM_LIT:0> <EOL> if tooltip and self . ADD_TOOLTIPS : <EOL> tooltip_html = '<STR_LIT>' % tooltip <EOL> else : tooltip_html = '<STR_LIT>' <EOL> if css_class : css_class_html = '<STR_LIT>' % css_class <EOL> else : css_class_html = '<STR_LIT>' <EOL> if onclick : <EOL> if targets : targets_html = '<STR_LIT>' % targets <EOL> else : targets_html = '<STR_LIT>' <EOL> s += ( '<STR_LIT>' % <EOL> ( uid , css_class_html , targets_html , tooltip_html , <EOL> css_class_html , onclick ) ) <EOL> elif url : <EOL> if isinstance ( url , unicode ) : <EOL> url = url . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> s += ( '<STR_LIT>' % <EOL> ( tooltip_html , css_class_html , url ) ) <EOL> elif css_class_html or tooltip_html : <EOL> s += '<STR_LIT>' % ( tooltip_html , css_class_html ) <EOL> if i == len ( line ) - <NUM_LIT:1> : <EOL> s += '<STR_LIT>' <EOL> s += cgi . escape ( toktext ) <EOL> else : <EOL> try : <EOL> s += self . add_line_numbers ( cgi . escape ( toktext ) , css_class ) <EOL> except Exception , e : <EOL> print ( toktext , css_class , toktext . encode ( '<STR_LIT:ascii>' ) ) <EOL> raise <EOL> if onclick : s += "<STR_LIT>" <EOL> elif url : s += '<STR_LIT>' <EOL> elif css_class_html or tooltip_html : s += '<STR_LIT>' <EOL> if self . ADD_DEF_BLOCKS : <EOL> for i in range ( ended_def_blocks ) : <EOL> self . out ( self . END_DEF_BLOCK ) <EOL> s = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , s ) <EOL> self . out ( s ) <EOL> if def_name and starting_def_block : <EOL> self . out ( '<STR_LIT>' ) <EOL> if ( self . ADD_DEF_BLOCKS and def_name and starting_def_block and <EOL> ( line [ - <NUM_LIT:2> ] [ <NUM_LIT:1> ] == '<STR_LIT::>' ) ) : <EOL> indentation = ( '<STR_LIT>' . join ( self . indents ) + '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:+>' ) <EOL> linenum_padding = '<STR_LIT:+>' * self . linenum_size <EOL> name = self . context_name ( def_name ) <EOL> self . out ( self . START_DEF_BLOCK % ( name , linenum_padding , <EOL> indentation , name ) ) <EOL> self . def_name = def_name <EOL> self . def_type = def_type <EOL> def context_name ( self , extra = None ) : <EOL> pieces = [ n for n in self . context if n is not None ] <EOL> if extra is not None : pieces . append ( extra ) <EOL> return '<STR_LIT:.>' . join ( pieces ) <EOL> def doclink ( self , name , docs ) : <EOL> uid = '<STR_LIT>' % self . _next_uid <EOL> self . _next_uid += <NUM_LIT:1> <EOL> context = [ n for n in self . context if n is not None ] <EOL> container = DottedName ( self . module_name , * context ) <EOL> targets = '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' % ( str ( self . doc_descr ( d , container ) ) , <EOL> str ( self . url_func ( d ) ) ) <EOL> for d in docs ] ) <EOL> if targets in self . doclink_targets_cache : <EOL> onclick = ( "<STR_LIT>" % <EOL> ( uid , name , self . doclink_targets_cache [ targets ] ) ) <EOL> return uid , onclick , None <EOL> else : <EOL> self . doclink_targets_cache [ targets ] = uid <EOL> onclick = ( "<STR_LIT>" % <EOL> ( uid , name , uid ) ) <EOL> return uid , onclick , targets <EOL> def doc_descr ( self , doc , context ) : <EOL> name = str ( doc . canonical_name ) <EOL> descr = '<STR_LIT>' % ( self . doc_kind ( doc ) , name ) <EOL> if isinstance ( doc , RoutineDoc ) : <EOL> descr += '<STR_LIT>' <EOL> return descr <EOL> def doc_kind ( self , doc ) : <EOL> if isinstance ( doc , ModuleDoc ) and doc . is_package == True : <EOL> return '<STR_LIT>' <EOL> elif ( isinstance ( doc , ModuleDoc ) and <EOL> doc . canonical_name [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) ) : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( doc , ModuleDoc ) : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( doc , ClassDoc ) : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( doc , ClassMethodDoc ) : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( doc , StaticMethodDoc ) : <EOL> return '<STR_LIT>' <EOL> elif isinstance ( doc , RoutineDoc ) : <EOL> if ( self . docindex is not None and <EOL> isinstance ( self . docindex . container ( doc ) , ClassDoc ) ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def mark_def ( self , s , name ) : <EOL> replacement = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( name , name , name , name ) ) <EOL> return re . sub ( '<STR_LIT>' , replacement , s ) <EOL> def is_docstring ( self , line , i ) : <EOL> if line [ i ] [ <NUM_LIT:0> ] != token . STRING : return False <EOL> for toktype , toktext in line [ i : ] : <EOL> if toktype not in ( token . NEWLINE , tokenize . COMMENT , <EOL> tokenize . NL , token . STRING , None ) : <EOL> return False <EOL> return True <EOL> def add_line_numbers ( self , s , css_class ) : <EOL> result = '<STR_LIT>' <EOL> start = <NUM_LIT:0> <EOL> end = s . find ( '<STR_LIT:\n>' ) + <NUM_LIT:1> <EOL> while end : <EOL> result += s [ start : end - <NUM_LIT:1> ] <EOL> if css_class : result += '<STR_LIT>' <EOL> result += '<STR_LIT>' <EOL> result += '<STR_LIT:\n>' <EOL> if self . ADD_LINE_NUMBERS : <EOL> result += self . lineno_to_html ( ) <EOL> result += '<STR_LIT>' <EOL> if css_class : result += '<STR_LIT>' % css_class <EOL> start = end <EOL> end = s . find ( '<STR_LIT:\n>' , end ) + <NUM_LIT:1> <EOL> self . lineno += <NUM_LIT:1> <EOL> result += s [ start : ] <EOL> return result <EOL> def name2url ( self , class_name , func_name = None ) : <EOL> if class_name : <EOL> class_name = '<STR_LIT>' % ( self . module_name , class_name ) <EOL> if func_name : <EOL> return '<STR_LIT>' % ( class_name , func_name ) <EOL> else : <EOL> return '<STR_LIT>' % class_name <EOL> else : <EOL> return '<STR_LIT>' % ( self . module_name , func_name ) <EOL> _FIX_DECORATOR_RE = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , re . MULTILINE ) <EOL> _HDR = '''<STR_LIT>''' <EOL> _FOOT = '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> s = PythonSourceColorizer ( '<STR_LIT>' , '<STR_LIT>' ) . colorize ( ) <EOL> import codecs <EOL> f = codecs . open ( '<STR_LIT>' , '<STR_LIT:w>' , '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> f . write ( _HDR + '<STR_LIT>' + s + '<STR_LIT>' + _FOOT ) <EOL> f . close ( ) </s>
