2012-06-16 25 views
1

pydoc允許我查看在瀏覽器中添加到我的PYTHONPATH中的目錄中的Python模塊和包的文檔。有沒有辦法瀏覽這些文件的完整代碼?類似pydoc的程序在瀏覽器中瀏覽Python文件的內容/代碼?

我轉換使用3to2愛德華多伊萬內茨的代碼:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from __future__ import with_statement 
import CGIHTTPServer, SimpleHTTPServer, BaseHTTPServer 
import os 
from pygments import highlight 
from pygments.lexers import PythonLexer 
from pygments.formatters import HtmlFormatter 
from io import open 

class SourceViewer(SimpleHTTPServer.SimpleHTTPRequestHandler): 
    def do_GET(self): 
     path = os.path.join(os.getcwdu(), self.path[1:]) 
     if os.path.exists(path) and path.endswith(u'.py'): 
      with open(path) as file: 
       code = file.read() 
       hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos=u'table')) 
       self.send_response(200) 
       self.end_headers() 
       self.wfile.write(str(hl).encode('UTF-8')) 
       return 
     else:  
      super(self.__class__, self).do_GET() 


if __name__ == u"__main__": 
    server = BaseHTTPServer.HTTPServer((u'localhost', 8080), SourceViewer) 
    server.serve_forever() 

localhost:8080示出了no data received消息。我應該如何提供數據?

回答

2

pygments的幫助下,您可以擴展標準庫中的HTTP服務器,以便在目錄中提供突出顯示的行編號版本的Python源文件。

這是Python的3個例子:

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 

import http.server 
import os 
from pygments import highlight 
from pygments.lexers import PythonLexer 
from pygments.formatters import HtmlFormatter 

class SourceViewer(http.server.SimpleHTTPRequestHandler): 
    def do_GET(self): 
     path = os.path.join(os.getcwd(), self.path[1:]) 
     if os.path.exists(path) and path.endswith('.py'): 
      with open(path) as file: 
       code = file.read() 
       hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos='table')) 
       self.send_response(200) 
       self.end_headers() 
       self.wfile.write(bytes(hl, 'UTF-8')) 
       return 
     else:  
      super().do_GET() 


if __name__ == "__main__": 
    server = http.server.HTTPServer(('localhost', 8080), SourceViewer) 
    server.serve_forever() 

Python 2中,根據您的3to2轉換:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from __future__ import with_statement 
import SimpleHTTPServer, BaseHTTPServer 
import os 
from pygments import highlight 
from pygments.lexers import PythonLexer 
from pygments.formatters import HtmlFormatter 

class SourceViewer(SimpleHTTPServer.SimpleHTTPRequestHandler): 
    def do_GET(self): 
     path = os.path.join(os.getcwdu(), self.path[1:]) 
     if os.path.exists(path) and path.endswith(u'.py'): 
      with open(path) as file: 
       code = file.read() 
       hl = highlight(code, PythonLexer(), HtmlFormatter(noclasses=True, linenos=u'table')) 
       self.send_response(200) 
       self.end_headers() 
       self.wfile.write(hl) 
       return 
     else:  
      SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 

if __name__ == u"__main__": 
    server = BaseHTTPServer.HTTPServer((u'localhost', 8080), SourceViewer) 
    server.serve_forever() 

和示例結果:

Example result

+0

嘿愛德華多!我使用'3to2'來轉換代碼(請參閱已編輯的原始文章)。但是,我收到了「無數據收到」消息,應如何傳遞數據? – Bentley4

+0

這太糟糕了 - 我會在幾分鐘後回家時嘗試。 –

+0

似乎對SimpleHTTPRequestHandler的'super()'調用失敗 - 這可能是因爲它是一箇舊式類,雖然這看起來很奇怪,但我沒有真正檢查。請參閱編輯中的新版本,它可以對顯示的父類進行調用。 –