jsutil.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » 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 » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » jsutil.py
# -*- test-case-name: nevow.test.test_consolejstest -*-
# Copyright (c) 2004-2007 Divmod.
# See LICENSE for details.

from twisted.python.procutils import which
from twisted.python.util import sibpath

import nevow
from nevow.athena import (LivePage,allJavascriptPackages,AthenaModule
    jsModuleDeclaration)

_TEST_BOOTSTRAP = LivePage.BOOTSTRAP_MODULES[:]
_TEST_BOOTSTRAP.insert(_TEST_BOOTSTRAP.index('Divmod.Runtime'),
                       'Divmod.MockBrowser')

_DUMMY_MODULE_NAME = 'ConsoleJSTest'

def getDependencies(fname, ignore=[],
                    bootstrap=_TEST_BOOTSTRAP,
                    packages=None):
    """
    Get the javascript modules that the code in the file with name C{fname}
    depends on, recursively

    @param fname: javascript source file name
    @type fname: C{str}

    @param ignore: names of javascript modules to exclude from dependency list
    @type ignore: sequence

    @param bootstrap: names of javascript modules to always include, regardless
    of explicit dependencies (defaults to L{LivePage}'s list of bootstrap
    modules, plus the mock browser implementation.)
    @type boostrap: sequence

    @param packages: all javascript packages we know about.  defaults to the
    result of L{allJavascriptPackages}
    @type packages: C{dict}

    @return: modules included by javascript in file named C{fname}
    @rtype: dependency-ordered list of L{AthenaModule} instances
    """
    if packages is None:
        packages = allJavascriptPackages()

    # TODO if a module is ignored, we should ignore its dependencies
    bootstrapModules = [AthenaModule.getOrCreate(m, packages)
                        for m in bootstrap if m not in ignore]

    packages[_DUMMY_MODULE_NAME] = fname
    module = AthenaModule(_DUMMY_MODULE_NAME, packages)

    return (bootstrapModules +
            [dep for dep in module.allDependencies()
             if (dep.name not in bootstrap
                 and dep.name != _DUMMY_MODULE_NAME
                 and dep.name not in ignore)])



def findJavascriptInterpreter():
    """
    Return a string path to a JavaScript interpreter if one can be found in
    the executable path. If not, return None.
    """
    for script in ['smjs', 'js']:
        _jsInterps = which(script)
        if _jsInterps:
            return _jsInterps[0]
    return None



def generateTestScript(fname, after={}, dependencies=None):
    """
    Turn the contents of the Athena-style javascript test module in the file
    named C{fname} into a plain javascript script.  Recursively includes any
    modules that are depended on, as well as the utility module
    nevow/test/testsupport.js.

    @param fname: javascript source file name
    @type fname: C{str}

    @param after: mapping of javascript module names to sequences of lines of
    javascript source that should be injected into the output immediately
    after the source of the named module is included
    @type after: C{dict}

    @param dependencies: the modules the script depends on.  Defaults to the
    result of L{getDependencies}
    @type dependencies: dependency-ordered list of L{AthenaModule} instances

    @return: converted javascript source text
    @rtype: C{str}
    """
    if dependencies is None:
        dependencies = getDependencies(fname)

    load = lambda fname: 'load(%r);' % (fname,)
    initialized = {}
    js = [load(sibpath(nevow.__file__, 'test/testsupport.js'))]
    for m in dependencies:
        segments = m.name.split('.')
        if segments[-1] == '__init__':
            segments = segments[:-1]
        initname = '.'.join(segments)
        if initname not in initialized:
            initialized[initname] = 1
            js.append(jsModuleDeclaration(initname))
        js.append(load(m.mapping[m.name]))
        if m.name in after:
            js.extend(after[m.name])

    js.append(file(fname).read())

    return '\n'.join(js)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.