compile.py :  » Ajax » pyjamas » src » examples » showcase » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » examples » showcase » compile.py
#!/usr/bin/env python

""" compile.py

    Build the pyjamas-showcase web application.

    We firstly scan through all the demo modules, pulling out the module
    docstrings and the source code, and adding this information to the
    demoInfo.py module.  We then call Pyjamas to compile the application, and
    finally open a web browser window to show the compiled application.

"""
import cStringIO
import os
import os.path
import sys
import webbrowser

import pyColourize

#############################################################################

# Modify the following to refer to the full path to your Pyjamas installation,
# relative to the pyjamas-showcase source directory:

here = os.path.dirname(os.path.abspath(__file__))
PATH_TO_PYJAMAS = os.path.dirname(os.path.dirname(here))

#############################################################################

def main():
    """ Our main program.
    """
    # Extract the source code to each demo file, storing the results into the
    # demoInfo.py file.

    # Load all our demonstrations into memory.

    demoInfo = {}
    for section in ["widgets", "panels"]:
        for fName in os.listdir(os.path.join("src", "demos_" + section)):
            if fName.startswith(".") or not fName.endswith(".py"):
                continue

            f = file(os.path.join("src", "demos_" + section, fName), "r")
            src = f.read()
            f.close()

            demoName = fName[:-3]
            docstring,htmlSrc = parseDemo(fName, src)

            demoInfo[demoName] = {'section' : section,
                                  'doc'     : docstring,
                                  'src'     : src,
                                  'htmlSrc' : htmlSrc}

    # Calculate the list of imports to use for the combined module.

    imports = set()
    for demo in demoInfo.values():
        imports.update(extractImports(demo['src']))

    # Write the combined demos into a single file.

    s = []
    s.append('""" demoInfo.py')
    s.append('')
    s.append('    DO NOT EDIT THE CONTENTS OF THIS FILE!')
    s.append('')
    s.append('    This file is created automatically by the compile.py')
    s.append('    script out of the various demonstration modules.')
    s.append('"""')
    s.append('')
    for imp in imports:
        s.append(imp + "")
    s.append('')
    s.append('')

    for demo in demoInfo.keys():
        s.append(removeDocstringAndImports(demoInfo[demo]['src']))
        s.append('')
        s.append('')

    s.append('def getDemos():')
    s.append('    demos = []')

    sortKeys = []
    for name in demoInfo.keys():
        sortKeys.append((demoInfo[name]['section'], name))
    sortKeys.sort()

    for section,name in sortKeys:
        demo = demoInfo[name]
        capName = name[0].upper() + name[1:]
        s.append('    demos.append({"name" : "' + name + '",')
        s.append('                  "title" : "ui.' + capName + '",')
        s.append('                  "section" : "' + demo['section'] + '",')
        s.append('                  "doc" : """' + demo['doc'] + '""",')
        s.append('                  "src" : """' + demo['htmlSrc'] + '""",')
        s.append('                  "example" : ' + capName + 'Demo()})')
        s.append('')

    s.append('    return demos')
    s.append('')

    f = file(os.path.join("src", "demoInfo.py"), "w")
    f.write("\n".join(s))
    f.close()

    options = " ".join(sys.argv[1:])
    # Compile the application using Pyjamas.
    stmt = (os.path.join(PATH_TO_PYJAMAS, 'bin', 'pyjsbuild') +
            " " + options +
            " -o " + os.path.join(here,'build') + " " +
            " -I " + os.path.join(here, 'src') + " " +
            'Showcase' +
            " > /dev/null")
    if os.system(stmt) != 0: return

    # Finally, launch a web browser to show the compiled application.

    #webbrowser.open("file://" + os.path.abspath("build/Showcase.html"))

#############################################################################

def parseDemo(fName, src):
    """ Parse the given demonstration file.

        'fName' is the name of the demonstration module, and 'src' is a copy of
        the module's contents.

        We return a (docstring, src) tuple, containing the documentation string
        and source code for the given demonstration module.
    """
    if src.startswith('"""'):
        # We have a docstring.  Search for the ending docstring, and pull the
        # remaining source code out.
        i = src.find('"""', 3)
        if i == -1:
            docstring = src[3:]
            src       = ""
        else:
            docstring = src[3:i].lstrip()
            src       = src[i+3:].lstrip()
    else:
        # We have no docstring at all.
        docstring = ""

    # Tidy up the paragraphs in the docstring.  We do the following:
    #
    #  * If a paragraph starts with " * ", treat it as an unordered list item.
    #
    #  * If all the lines in a paragraph start with "    ", treat it as an
    #    indented code block.
    #
    #  * Otherwise, join the lines in the paragraph together to avoid line
    #    break issues.

    paragraphs = docstring.split("\n\n")
    for i in range(len(paragraphs)):
        indented = True
        for line in paragraphs[i].split("\n"):
            if not line.startswith("    "):
                indented = False

        if indented:
            # Treat the paragraph as a code block.
            paragraphs[i] = "<blockquote><pre>" + paragraphs[i] + \
                            "</pre></blockquote>"
        else:
            paragraphs[i] = paragraphs[i].replace("\n", " ")

            if paragraphs[i].startswith(" * "):
                paragraphs[i] = "<ul><li>" + paragraphs[i][3:] + "</li></ul>"

    docstring = "\n\n".join(paragraphs)

    # Colourize the source code.

    buff = cStringIO.StringIO()
    pyColourize.Parser(src, pyColourize._Eriks_Style,
                       fName, buff).format(None, None)
    html = buff.getvalue()

    # Replace any quotes in the source code with the equivalent HTML entity
    # codes.

    html = html.replace('"""', '&quot;&quot;&quot;')

    # That's all, folks!

    return docstring,html

#############################################################################

def extractImports(src):
    """ Extract the set of import statements in the given source code.

        We return a set object containing the import statements in the given
        source code.
    """
    imports = set()
    for line in src.split("\n"):
        if line.startswith("import"):
            imports.add(line)
        elif line.startswith("from "):
            i = line.find(" import ")
            if i > -1:
                fromModule = line[5:i]
                modules = line[i+8:].split(",")
                for module in modules:
                    imports.add("from "+fromModule+" import "+module.strip())
    return imports

#############################################################################

def removeDocstringAndImports(src):
    """ Remove the docstring and import statements from the given code.

        We return the source code, minus the initial docstring (if any) and any
        import statements.
    """
    if src.startswith('"""'):
        # We have a docstring.  Search for the ending docstring, and pull the
        # remaining source code out.
        i = src.find('"""', 3)
        if i == -1:
            src = ""
        else:
            src = src[i+3:].lstrip()

    dst = []
    for line in src.split("\n"):
        if not line.startswith("import") and not line.startswith("from"):
            dst.append(line)
    return "\n".join(dst)

#############################################################################

if __name__ == "__main__":
    main()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.