Changeset 48

Show
Ignore:
Timestamp:
07/24/07 02:15:27 (1 year ago)
Author:
edsuom
Message:

Major work on the foss.tellectual.com example, for API docs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/twisted-goodies/trunk/misc/var_www_foss.tellectual.com_index.py

    r47 r48  
    2424""" 
    2525 
     26import tempfile, shutil 
    2627from textwrap import TextWrapper 
    2728import os.path as ospath 
    28 from twisted.web2 import static 
     29from twisted.internet import defer, utils 
     30from twisted.web2 import static, resource 
    2931from twisted.python.log import msg as log 
    3032 
     
    7173    ) 
    7274 
     75 
     76def projectURL(projectName): 
     77    """ 
     78    """ 
     79    return "/trac/%s/wiki/Start_%s" % (projectName, projectName) 
     80 
     81 
     82class API(object): 
     83    """ 
     84    """ 
     85    def __init__(self, vhostPath, projectName, request): 
     86        self.packageName = projectName.lower().replace("-", "_") 
     87        path = "/api/%s/%s.html" % (projectName, self.packageName) 
     88        newURL = (request.scheme, request.host, path, '', '', '') 
     89        filePath = ospath.join(vhostPath, newURL[2].lstrip("/")) 
     90        if ospath.exists(filePath): 
     91            d = defer.succeed(None) 
     92        else: 
     93            tmpDir = tempfile.mkdtemp() 
     94            d = self.svn(tmpDir, projectName) 
     95            d.addCallback( 
     96                lambda _: self.pydoctor(tmpDir, vhostPath, projectName)) 
     97            d.addCallback(lambda _: shutil.rmtree(tmpDir)) 
     98        d.addCallback(lambda _: (resource.RedirectResource(*newURL), ())) 
     99        self.d = d 
     100 
     101    def executable(self, name): 
     102        """ 
     103        """ 
     104        result = procutils.which(name)[0] 
     105        if not result: 
     106            raise ImportError("Can't locate %s executable" % name) 
     107 
     108    def svn(self, tmpDir, vhostPath, projectName): 
     109        """ 
     110        TODO 
     111        """ 
     112        return defer.succeed(None) 
     113 
     114    def pydoctor(self, tmpDir, vhostPath, projectName): 
     115        """ 
     116        TODO 
     117        """ 
     118        return 
     119        # TODO: Nothing below does anything yet 
     120        args = [ 
     121            "--add-package=%s" % self.packageName, 
     122            "--project-name=%s" % projectName, 
     123            "--make-html", 
     124            "--html-output=%s/api/%s" % (vhostPath, projectName), 
     125            "--project-url=%s" % projectURL(projectName)] 
     126        return util.getProcessValue( 
     127            self.executable('pydoctor'), args=args, path=tmpDir) 
     128     
    73129 
    74130class Resource(StanResource): 
     
    105161        self.tr = TracResource(TRAC_DIR, env) 
    106162 
    107     def _reformat(self, name): 
    108         return name.replace("-", "_").lower() 
    109  
    110163    def _possibleStatic(self, *pathParts): 
     164        """ 
     165        """ 
    111166        staticPath = ospath.join(self.vhostPath, *pathParts) 
    112167        if ospath.exists(staticPath): 
     
    121176            projectDescription = " ".join( 
    122177                [x.strip() for x in doc.strip().split("\n")]) 
    123             href = "/trac/%s/wiki/Start_%s" % (projectName, projectName) 
     178            href = projectURL(projectName) 
    124179            projectDiv = [ 
    125180                T.a(id="project_link", href=href)[projectName]] 
     
    181236        if not subSegments: 
    182237            extendRequest('wiki') 
     238        root = subSegments[0] 
    183239        if len(subSegments) == 1: 
    184             section = subSegments[0] 
    185             if section == 'wiki': 
     240            if root == 'wiki': 
    186241                extendRequest("Start_%s" % projectName) 
    187             elif section == 'browser': 
    188                 extendRequest('projects', self._reformat(projectName), 'trunk') 
    189             elif section == 'api': 
    190                 subSegments.append("classIndex.html") 
    191         if subSegments[0] == 'api': 
    192             return self.apiResource(projectName, subSegments[1]), () 
    193         else: 
    194             possibleResult = self._possibleStatic('trac', *subSegments) 
    195             if possibleResult: 
    196                 return possibleResult, () 
     242            elif root == 'browser': 
     243                extendRequest('projects', projectName.lower(), 'trunk') 
     244            elif root == 'api': 
     245                return API(self.vhostPath, projectName, request).d 
     246        possibleResult = self._possibleStatic('trac', *subSegments) 
     247        if possibleResult: 
     248            return possibleResult, () 
    197249        return self.tr, subSegments 
    198  
    199     def apiResource(self, projectName, fileName): 
    200         """ 
    201         Returns a L{static.File} child resource directed to an API document for 
    202         the specified project, dynamically generating the document with 
    203         pyDoctor as needed. 
    204  
    205         TODO: SVN export, pydoctor, static.File(...), temp file cleanup 
    206         """ 
    207         projectDir = self._reformat(projectName) 
    208         return self._possibleStatic('api', projectDir, fileName)