py_ext/modules/svgui/pyjs/pyjs.py
author Edouard Tisserant
Tue, 08 May 2012 17:08:45 +0200
changeset 721 ecf4d203c4d4
parent 717 confnodes/python/modules/svgui/pyjs/pyjs.py@1c23952dbde1
permissions -rw-r--r--
refactoring
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
# Copyright 2006 James Tauber and contributors
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
# Licensed under the Apache License, Version 2.0 (the "License");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     5
# you may not use this file except in compliance with the License.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     6
# You may obtain a copy of the License at
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
#     http://www.apache.org/licenses/LICENSE-2.0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
# Unless required by applicable law or agreed to in writing, software
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
# distributed under the License is distributed on an "AS IS" BASIS,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    13
# See the License for the specific language governing permissions and
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    14
# limitations under the License.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    15
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    16
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
import sys
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
from types import StringType
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
import compiler
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
from compiler import ast
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
import os
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
import copy
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
# the standard location for builtins (e.g. pyjslib) can be
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
# over-ridden by changing this.  it defaults to sys.prefix
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
# so that on a system-wide install of pyjamas the builtins
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
# can be found in e.g. {sys.prefix}/share/pyjamas
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
# over-rides can be done by either explicitly modifying
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
# pyjs.prefix or by setting an environment variable, PYJSPREFIX.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
prefix = sys.prefix
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
if os.environ.has_key('PYJSPREFIX'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
    prefix = os.environ['PYJSPREFIX']
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
# pyjs.path is the list of paths, just like sys.path, from which
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
# library modules will be searched for, for compile purposes.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
# obviously we don't want to use sys.path because that would result
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
# in compiling standard python modules into javascript!
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
path = [os.path.abspath('')]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
if os.environ.has_key('PYJSPATH'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
    for p in os.environ['PYJSPATH'].split(os.pathsep):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
        p = os.path.abspath(p)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
        if os.path.isdir(p):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
            path.append(p)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
# this is the python function used to wrap native javascript
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
NATIVE_JS_FUNC_NAME = "JS"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
UU = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
PYJSLIB_BUILTIN_FUNCTIONS=("cmp",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
                           "map",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
                           "filter",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
                           "dir",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
                           "getattr",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
                           "setattr",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
                           "hasattr",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
                           "int",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
                           "float",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
                           "str",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
                           "repr",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
                           "range",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
                           "len",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
                           "hash",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
                           "abs",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
                           "ord",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
                           "chr",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
                           "enumerate",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
                           "min",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
                           "max",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
                           "bool",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
                           "type",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
                           "isinstance")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
PYJSLIB_BUILTIN_CLASSES=("BaseException",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
                         "Exception",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
                         "StandardError",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
                         "StopIteration",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
                         "AttributeError",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
                         "TypeError",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
                         "KeyError",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
                         "LookupError",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
                         "list",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
                         "dict",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
                         "object",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
                         "tuple",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
                        )
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
def pyjs_builtin_remap(name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
    # XXX HACK!
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
    if name == 'list':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
        name = 'List'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
    if name == 'object':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
        name = '__Object'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
    if name == 'dict':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
        name = 'Dict'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
    if name == 'tuple':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
        name = 'Tuple'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
    return name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
# XXX: this is a hack: these should be dealt with another way
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
# however, console is currently the only global name which is causing
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
# problems.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   108
PYJS_GLOBAL_VARS=("console")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
# This is taken from the django project.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   111
# Escape every ASCII character with a value less than 32.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
JS_ESCAPES = (
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
    ('\\', r'\x5C'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
    ('\'', r'\x27'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
    ('"', r'\x22'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
    ('>', r'\x3E'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   117
    ('<', r'\x3C'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   118
    ('&', r'\x26'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
    (';', r'\x3B')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
    ) + tuple([('%c' % z, '\\x%02X' % z) for z in range(32)])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   121
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   122
def escapejs(value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   123
    """Hex encodes characters for use in JavaScript strings."""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
    for bad, good in JS_ESCAPES:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   125
        value = value.replace(bad, good)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
    return value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
def uuprefix(name, leave_alone=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   129
    name = name.split(".")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
    name = name[:leave_alone] + map(lambda x: "__%s" % x, name[leave_alone:])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   131
    return '.'.join(name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   132
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   133
class Klass:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
    klasses = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   136
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   137
    def __init__(self, name, name_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   138
        self.name = name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   139
        self.name_ = name_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   140
        self.klasses[name] = self
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   141
        self.functions = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   142
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   143
    def set_base(self, base_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   144
        self.base = self.klasses.get(base_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   145
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   146
    def add_function(self, function_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   147
        self.functions.add(function_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   148
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   149
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   150
class TranslationError(Exception):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   151
    def __init__(self, message, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   152
        self.message = "line %s:\n%s\n%s" % (node.lineno, message, node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   153
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   154
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   155
        return self.message
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   156
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   157
def strip_py(name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   158
    return name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   159
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   160
def mod_var_name_decl(raw_module_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   161
    """ function to get the last component of the module e.g.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   162
        pyjamas.ui.DOM into the "namespace".  i.e. doing
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   163
        "import pyjamas.ui.DOM" actually ends up with _two_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   164
        variables - one pyjamas.ui.DOM, the other just "DOM".
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   165
        but "DOM" is actually local, hence the "var" prefix.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   166
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   167
        for PyV8, this might end up causing problems - we'll have
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   168
        to see: gen_mod_import and mod_var_name_decl might have
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   169
        to end up in a library-specific module, somewhere.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   170
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   171
    name = raw_module_name.split(".")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   172
    if len(name) == 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   173
        return ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   174
    child_name = name[-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   175
    return "var %s = %s;\n" % (child_name, raw_module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   176
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   177
def gen_mod_import(parentName, importName, dynamic=1):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   178
    #pyjs_ajax_eval("%(n)s.cache.js", null, true);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   179
    return """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   180
    pyjslib.import_module(sys.loadpath, '%(p)s', '%(n)s', %(d)d, false);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   181
    """ % ({'p': parentName, 'd': dynamic, 'n': importName}) + \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   182
    mod_var_name_decl(importName)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   183
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   184
class Translator:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   185
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   186
    def __init__(self, mn, module_name, raw_module_name, src, debug, mod, output,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   187
                 dynamic=0, optimize=False,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   188
                 findFile=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   189
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   190
        if module_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   191
            self.module_prefix = module_name + "."
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   192
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
            self.module_prefix = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   194
        self.raw_module_name = raw_module_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   195
        src = src.replace("\r\n", "\n")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
        src = src.replace("\n\r", "\n")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   197
        src = src.replace("\r",   "\n")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
        self.src = src.split("\n")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
        self.debug = debug
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
        self.imported_modules = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
        self.imported_modules_as = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
        self.imported_js = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   203
        self.top_level_functions = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
        self.top_level_classes = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   205
        self.top_level_vars = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
        self.local_arg_stack = [[]]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
        self.output = output
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   208
        self.imported_classes = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   209
        self.method_imported_globals = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   210
        self.method_self = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   211
        self.nextTupleAssignID = 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
        self.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   213
        self.optimize = optimize
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   214
        self.findFile = findFile
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
        if module_name.find(".") >= 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
            vdec = ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   219
            vdec = 'var '
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
        print >>self.output, UU+"%s%s = function (__mod_name__) {" % (vdec, module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   221
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   222
        print >>self.output, "    if("+module_name+".__was_initialized__) return;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   223
        print >>self.output, "    "+UU+module_name+".__was_initialized__ = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   224
        print >>self.output, UU+"if (__mod_name__ == null) __mod_name__ = '%s';" % (mn)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   225
        print >>self.output, UU+"%s.__name__ = __mod_name__;" % (raw_module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   227
        decl = mod_var_name_decl(raw_module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   228
        if decl:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   229
            print >>self.output, decl
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   230
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   231
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   232
        if self.debug:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   233
            haltException = self.module_prefix + "HaltException"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   234
            print >>self.output, haltException + ' = function () {'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   235
            print >>self.output, '  this.message = "Program Halted";'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   236
            print >>self.output, '  this.name = "' + haltException + '";'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   237
            print >>self.output, '}'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   238
            print >>self.output, ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   239
            print >>self.output, haltException + ".prototype.__str__ = function()"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   240
            print >>self.output, '{'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   241
            print >>self.output, 'return this.message ;'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   242
            print >>self.output, '}'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   243
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   244
            print >>self.output, haltException + ".prototype.toString = function()"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   245
            print >>self.output, '{'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   246
            print >>self.output, 'return this.name + ": \\"" + this.message + "\\"";'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   247
            print >>self.output, '}'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   248
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   249
            isHaltFunction = self.module_prefix + "IsHaltException"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   250
            print >>self.output, """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   251
    %s = function (s) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   252
      var suffix="HaltException";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   253
      if (s.length < suffix.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   254
        //alert(s + " " + suffix);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   255
        return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   256
      } else {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   257
        var ss = s.substring(s.length, (s.length - suffix.length));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   258
        //alert(s + " " + suffix + " " + ss);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   259
        return ss == suffix;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   260
      }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   261
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   262
                """ % isHaltFunction
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   263
        for child in mod.node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   264
            if isinstance(child, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   265
                self.top_level_functions.add(child.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   266
            elif isinstance(child, ast.Class):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   267
                self.top_level_classes.add(child.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   268
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   269
        for child in mod.node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   270
            if isinstance(child, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   271
                self._function(child, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   272
            elif isinstance(child, ast.Class):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   273
                self._class(child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   274
            elif isinstance(child, ast.Import):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   275
                importName = child.names[0][0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   276
                if importName == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   277
                    pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   278
                elif importName.endswith('.js'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   279
                   self.imported_js.add(importName)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   280
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   281
                   self.add_imported_module(strip_py(importName))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   282
            elif isinstance(child, ast.From):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   283
                if child.modname == '__pyjamas__': # special module to help make pyjamas modules loadable in the python interpreter
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   284
                    pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   285
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   286
                    self.add_imported_module(child.modname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   287
                    self._from(child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   288
            elif isinstance(child, ast.Discard):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   289
                self._discard(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   290
            elif isinstance(child, ast.Assign):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   291
                self._assign(child, None, True)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   292
            elif isinstance(child, ast.AugAssign):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   293
                self._augassign(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   294
            elif isinstance(child, ast.If):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   295
                self._if(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   296
            elif isinstance(child, ast.For):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   297
                self._for(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   298
            elif isinstance(child, ast.While):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   299
                self._while(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   300
            elif isinstance(child, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   301
                self._subscript_stmt(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   302
            elif isinstance(child, ast.Global):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   303
                self._global(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   304
            elif isinstance(child, ast.Printnl):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   305
               self._print(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   306
            elif isinstance(child, ast.Print):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   307
               self._print(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   308
            elif isinstance(child, ast.TryExcept):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   309
                self._tryExcept(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   310
            elif isinstance(child, ast.Raise):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   311
                self._raise(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   312
            elif isinstance(child, ast.Stmt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   313
                self._stmt(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   314
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   315
                raise TranslationError("unsupported type (in __init__)", child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   316
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   317
        # Initialize all classes for this module
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   318
        #print >> self.output, "__"+self.modpfx()+\
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   319
        #          "classes_initialize = function() {\n"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   320
        #for className in self.top_level_classes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   321
        #    print >> self.output, "\t"+UU+self.modpfx()+"__"+className+"_initialize();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   322
        #print >> self.output, "};\n"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   323
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   324
        print >> self.output, "return this;\n"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   325
        print >> self.output, "}; /* end %s */ \n"  % module_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   326
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   327
    def module_imports(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   328
        return self.imported_modules + self.imported_modules_as
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   329
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   330
    def add_local_arg(self, varname):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   331
        local_vars = self.local_arg_stack[-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   332
        if varname not in local_vars:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   333
            local_vars.append(varname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   334
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   335
    def add_imported_module(self, importName):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   336
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   337
        if importName in self.imported_modules:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   338
            return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   339
        self.imported_modules.append(importName)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   340
        name = importName.split(".")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   341
        if len(name) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   342
            # add the name of the module to the namespace,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   343
            # but don't add the short name to imported_modules
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   344
            # because then the short name would be attempted to be
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   345
            # added to the dependencies, and it's half way up the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   346
            # module import directory structure!
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   347
            child_name = name[-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   348
            self.imported_modules_as.append(child_name) 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   349
        print >> self.output, gen_mod_import(self.raw_module_name,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   350
                                             strip_py(importName),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   351
                                             self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   352
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   353
    def _default_args_handler(self, node, arg_names, current_klass,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   354
                              output=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   355
        if len(node.defaults):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   356
            output = output or self.output
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   357
            default_pos = len(arg_names) - len(node.defaults)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   358
            if arg_names and arg_names[0] == self.method_self:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   359
                default_pos -= 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   360
            for default_node in node.defaults:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   361
                if isinstance(default_node, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   362
                    default_value = self._const(default_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   363
                elif isinstance(default_node, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   364
                    default_value = self._name(default_node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   365
                elif isinstance(default_node, ast.UnarySub):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   366
                    default_value = self._unarysub(default_node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   367
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   368
                    raise TranslationError("unsupported type (in _method)", default_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   369
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   370
                default_name = arg_names[default_pos]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   371
                default_pos += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   372
                print >> output, "    if (typeof %s == 'undefined') %s=%s;" % (default_name, default_name, default_value)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   373
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   374
    def _varargs_handler(self, node, varargname, arg_names, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   375
        print >>self.output, "    var", varargname, '= new pyjslib.Tuple();'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   376
        print >>self.output, "    for(var __va_arg="+str(len(arg_names))+"; __va_arg < arguments.length; __va_arg++) {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   377
        print >>self.output, "        var __arg = arguments[__va_arg];"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   378
        print >>self.output, "        "+varargname+".append(__arg);"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   379
        print >>self.output, "    }"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   380
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   381
    def _kwargs_parser(self, node, function_name, arg_names, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   382
        if len(node.defaults) or node.kwargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   383
            default_pos = len(arg_names) - len(node.defaults)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   384
            if arg_names and arg_names[0] == self.method_self:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   385
                default_pos -= 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   386
            print >>self.output, function_name+'.parse_kwargs = function (', ", ".join(["__kwargs"]+arg_names), ") {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   387
            for default_node in node.defaults:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   388
                default_value = self.expr(default_node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   389
#                if isinstance(default_node, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   390
#                    default_value = self._const(default_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   391
#                elif isinstance(default_node, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   392
#                    default_value = self._name(default_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   393
#                elif isinstance(default_node, ast.UnarySub):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   394
#                    default_value = self._unarysub(default_node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   395
#                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   396
#                    raise TranslationError("unsupported type (in _method)", default_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   397
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   398
                default_name = arg_names[default_pos]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   399
                print >>self.output, "    if (typeof %s == 'undefined')"%(default_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   400
                print >>self.output, "        %s=__kwargs.%s;"% (default_name, default_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   401
                default_pos += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   402
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   403
            #self._default_args_handler(node, arg_names, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   404
            if node.kwargs: arg_names += ["pyjslib.Dict(__kwargs)"]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   405
            print >>self.output, "    var __r = "+"".join(["[", ", ".join(arg_names), "]"])+";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   406
            if node.varargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   407
                self._varargs_handler(node, "__args", arg_names, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   408
                print >>self.output, "    __r.push.apply(__r, __args.getArray())"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   409
            print >>self.output, "    return __r;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   410
            print >>self.output, "};"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   411
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   412
    def _function(self, node, local=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   413
        if local:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   414
            function_name = node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   415
            self.add_local_arg(function_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   416
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   417
            function_name = UU + self.modpfx() + node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   418
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   419
        arg_names = list(node.argnames)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   420
        normal_arg_names = list(arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   421
        if node.kwargs: kwargname = normal_arg_names.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   422
        if node.varargs: varargname = normal_arg_names.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   423
        declared_arg_names = list(normal_arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   424
        if node.kwargs: declared_arg_names.append(kwargname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   425
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   426
        function_args = "(" + ", ".join(declared_arg_names) + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   427
        print >>self.output, "%s = function%s {" % (function_name, function_args)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   428
        self._default_args_handler(node, normal_arg_names, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   429
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   430
        local_arg_names = normal_arg_names + declared_arg_names 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   431
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   432
        if node.varargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   433
            self._varargs_handler(node, varargname, declared_arg_names, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   434
            local_arg_names.append(varargname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   435
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   436
        # stack of local variable names for this function call
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   437
        self.local_arg_stack.append(local_arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   438
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   439
        for child in node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   440
            self._stmt(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   441
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   442
        # remove the top local arg names
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   443
        self.local_arg_stack.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   444
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   445
        # we need to return null always, so it is not undefined
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   446
        lastStmt = [p for p in node.code][-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   447
        if not isinstance(lastStmt, ast.Return):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   448
            if not self._isNativeFunc(lastStmt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   449
                print >>self.output, "    return null;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   450
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   451
        print >>self.output, "};"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   452
        print >>self.output, "%s.__name__ = '%s';\n" % (function_name, node.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   453
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   454
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   455
        self._kwargs_parser(node, function_name, normal_arg_names, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   456
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   457
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   458
    def _return(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   459
        expr = self.expr(node.value, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   460
        # in python a function call always returns None, so we do it
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   461
        # here too
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   462
        print >>self.output, "    return " + expr + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   463
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   464
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   465
    def _break(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   466
        print >>self.output, "    break;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   467
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   468
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   469
    def _continue(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   470
        print >>self.output, "    continue;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   471
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   472
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   473
    def _callfunc(self, v, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   474
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   475
        if isinstance(v.node, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   476
            if v.node.name in self.top_level_functions:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   477
                call_name = self.modpfx() + v.node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   478
            elif v.node.name in self.top_level_classes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   479
                call_name = self.modpfx() + v.node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   480
            elif self.imported_classes.has_key(v.node.name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   481
                call_name = self.imported_classes[v.node.name] + '.' + v.node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   482
            elif v.node.name in PYJSLIB_BUILTIN_FUNCTIONS:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   483
                call_name = 'pyjslib.' + v.node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   484
            elif v.node.name in PYJSLIB_BUILTIN_CLASSES:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   485
                name = pyjs_builtin_remap(v.node.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   486
                call_name = 'pyjslib.' + name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   487
            elif v.node.name == "callable":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   488
                call_name = "pyjslib.isFunction"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   489
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   490
                call_name = v.node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   491
            call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   492
        elif isinstance(v.node, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   493
            attr_name = v.node.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   494
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   495
            if isinstance(v.node.expr, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   496
                call_name = self._name2(v.node.expr, current_klass, attr_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   497
                call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   498
            elif isinstance(v.node.expr, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   499
                call_name = self._getattr2(v.node.expr, current_klass, attr_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   500
                call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   501
            elif isinstance(v.node.expr, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   502
                call_name = self._callfunc(v.node.expr, current_klass) + "." + v.node.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   503
                call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   504
            elif isinstance(v.node.expr, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   505
                call_name = self._subscript(v.node.expr, current_klass) + "." + v.node.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   506
                call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   507
            elif isinstance(v.node.expr, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   508
                call_name = self.expr(v.node.expr, current_klass) + "." + v.node.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   509
                call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   510
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   511
                raise TranslationError("unsupported type (in _callfunc)", v.node.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   512
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   513
            raise TranslationError("unsupported type (in _callfunc)", v.node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   514
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   515
        call_name = strip_py(call_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   516
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   517
        kwargs = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   518
        star_arg_name = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   519
        if v.star_args:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   520
            star_arg_name = self.expr(v.star_args, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   521
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   522
        for ch4 in v.args:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   523
            if isinstance(ch4, ast.Keyword):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   524
                kwarg = ch4.name + ":" + self.expr(ch4.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   525
                kwargs.append(kwarg)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   526
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   527
                arg = self.expr(ch4, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   528
                call_args.append(arg)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   529
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   530
        if kwargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   531
            fn_args = ", ".join(['{' + ', '.join(kwargs) + '}']+call_args)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   532
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   533
            fn_args = ", ".join(call_args)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   534
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   535
        if kwargs or star_arg_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   536
            if not star_arg_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   537
                star_arg_name = 'null'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   538
            try: call_this, method_name = call_name.rsplit(".", 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   539
            except ValueError:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   540
                # Must be a function call ...
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   541
                return ("pyjs_kwargs_function_call("+call_name+", "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   542
                                  + star_arg_name 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   543
                                  + ", ["+fn_args+"]"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   544
                                  + ")" )
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   545
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   546
                return ("pyjs_kwargs_method_call("+call_this+", '"+method_name+"', "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   547
                                  + star_arg_name 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   548
                                  + ", ["+fn_args+"]"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   549
                                  + ")")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   550
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   551
            return call_name + "(" + ", ".join(call_args) + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   552
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   553
    def _print(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   554
        if self.optimize:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   555
            return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   556
        call_args = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   557
        for ch4 in node.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   558
            arg = self.expr(ch4, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   559
            call_args.append(arg)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   560
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   561
        print >>self.output, "pyjslib.printFunc([", ', '.join(call_args), "],", int(isinstance(node, ast.Printnl)), ");"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   562
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   563
    def _tryExcept(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   564
        if len(node.handlers) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   565
            raise TranslationError("except statements in this form are" +
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   566
                                   " not supported", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   567
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   568
        expr = node.handlers[0][0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   569
        as_ = node.handlers[0][1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   570
        if as_:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   571
            errName = as_.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   572
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   573
            errName = 'err'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   574
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   575
        # XXX TODO: check that this should instead be added as a _separate_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   576
        # local scope, temporary to the function.  oh dearie me.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   577
        self.add_local_arg(errName)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   578
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   579
        print >>self.output, "    try {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   580
        for stmt in node.body.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   581
            self._stmt(stmt, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   582
        print >> self.output, "    } catch(%s) {" % errName
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   583
        if expr:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   584
            l = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   585
            if isinstance(expr, ast.Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   586
                for x in expr.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   587
                    l.append("(%(err)s.__name__ == %(expr)s.__name__)" % dict (err=errName, expr=self.expr(x, current_klass)))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   588
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   589
                l = [ " (%(err)s.__name__ == %(expr)s.__name__) " % dict (err=errName, expr=self.expr(expr, current_klass)) ]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   590
            print >> self.output, "   if(%s) {" % '||\n\t\t'.join(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   591
        for stmt in node.handlers[0][2]:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   592
            self._stmt(stmt, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   593
        if expr:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   594
            #print >> self.output, "} else { throw(%s); } " % errName
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   595
            print >> self.output, "}"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   596
        if node.else_ != None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   597
            print >>self.output, "    } finally {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   598
            for stmt in node.else_:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   599
                self._stmt(stmt, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   600
        print >>self.output, "    }"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   601
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   602
    # XXX: change use_getattr to True to enable "strict" compilation
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   603
    # but incurring a 100% performance penalty. oops.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   604
    def _getattr(self, v, current_klass, use_getattr=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   605
        attr_name = v.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   606
        if isinstance(v.expr, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   607
            obj = self._name(v.expr, current_klass, return_none_for_module=True)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   608
            if obj == None and v.expr.name in self.module_imports():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   609
                # XXX TODO: distinguish between module import classes
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   610
                # and variables.  right now, this is a hack to get
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   611
                # the sys module working.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   612
                #if v.expr.name == 'sys':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   613
                return v.expr.name+'.'+attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   614
                #return v.expr.name+'.__'+attr_name+'.prototype.__class__'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   615
            if not use_getattr or attr_name == '__class__' or \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   616
                    attr_name == '__name__':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   617
                return obj + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   618
            return "pyjslib.getattr(%s, '%s')" % (obj, attr_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   619
        elif isinstance(v.expr, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   620
            return self._getattr(v.expr, current_klass) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   621
        elif isinstance(v.expr, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   622
            return self._subscript(v.expr, self.modpfx()) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   623
        elif isinstance(v.expr, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   624
            return self._callfunc(v.expr, self.modpfx()) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   625
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   626
            raise TranslationError("unsupported type (in _getattr)", v.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   627
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   628
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   629
    def modpfx(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   630
        return strip_py(self.module_prefix)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   631
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   632
    def _name(self, v, current_klass, top_level=False,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   633
                                      return_none_for_module=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   634
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   635
        if v.name == 'ilikesillynamesfornicedebugcode':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   636
            print top_level, current_klass, repr(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   637
            print self.top_level_vars
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   638
            print self.top_level_functions
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   639
            print self.local_arg_stack
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   640
            print "error..."
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   641
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   642
        local_var_names = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   643
        las = len(self.local_arg_stack)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   644
        if las > 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   645
            local_var_names = self.local_arg_stack[-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   646
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   647
        if v.name == "True":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   648
            return "true"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   649
        elif v.name == "False":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   650
            return "false"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   651
        elif v.name == "None":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   652
            return "null"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   653
        elif v.name == '__name__' and current_klass is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   654
            return self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   655
        elif v.name == self.method_self:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   656
            return "this"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   657
        elif v.name in self.top_level_functions:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   658
            return UU+self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   659
        elif v.name in self.method_imported_globals:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   660
            return UU+self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   661
        elif not current_klass and las == 1 and v.name in self.top_level_vars:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   662
            return UU+self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   663
        elif v.name in local_var_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   664
            return v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   665
        elif self.imported_classes.has_key(v.name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   666
            return UU+self.imported_classes[v.name] + '.__' + v.name + ".prototype.__class__"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   667
        elif v.name in self.top_level_classes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   668
            return UU+self.modpfx() + "__" + v.name + ".prototype.__class__"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   669
        elif v.name in self.module_imports() and return_none_for_module:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   670
            return None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   671
        elif v.name in PYJSLIB_BUILTIN_CLASSES:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   672
            return "pyjslib." + pyjs_builtin_remap( v.name )
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   673
        elif current_klass:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   674
            if v.name not in local_var_names and \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   675
               v.name not in self.top_level_vars and \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   676
               v.name not in PYJS_GLOBAL_VARS and \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   677
               v.name not in self.top_level_functions:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   678
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   679
                cls_name = current_klass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   680
                if hasattr(cls_name, "name"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   681
                    cls_name_ = cls_name.name_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   682
                    cls_name = cls_name.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   683
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   684
                    cls_name_ = current_klass + "_" # XXX ???
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   685
                name = UU+cls_name_ + ".prototype.__class__." \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   686
                                   + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   687
                if v.name == 'listener':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   688
                    name = 'listener+' + name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   689
                return name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   690
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   691
        return v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   692
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   693
    def _name2(self, v, current_klass, attr_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   694
        obj = v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   695
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   696
        if obj in self.method_imported_globals:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   697
            call_name = UU+self.modpfx() + obj + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   698
        elif self.imported_classes.has_key(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   699
            #attr_str = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   700
            #if attr_name != "__init__":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   701
            attr_str = ".prototype.__class__." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   702
            call_name = UU+self.imported_classes[obj] + '.__' + obj + attr_str
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   703
        elif obj in self.module_imports():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   704
            call_name = obj + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   705
        elif obj[0] == obj[0].upper(): # XXX HACK ALERT
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   706
            call_name = UU + self.modpfx() + "__" + obj + ".prototype.__class__." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   707
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   708
            call_name = UU+self._name(v, current_klass) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   709
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   710
        return call_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   711
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   712
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   713
    def _getattr2(self, v, current_klass, attr_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   714
        if isinstance(v.expr, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   715
            call_name = self._getattr2(v.expr, current_klass, v.attrname + "." + attr_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   716
        elif isinstance(v.expr, ast.Name) and v.expr.name in self.module_imports():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   717
            call_name = UU+v.expr.name + '.__' +v.attrname+".prototype.__class__."+attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   718
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   719
            obj = self.expr(v.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   720
            call_name = obj + "." + v.attrname + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   721
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   722
        return call_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   723
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   724
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   725
    def _class(self, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   726
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   727
        Handle a class definition.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   728
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   729
        In order to translate python semantics reasonably well, the following
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   730
        structure is used:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   731
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   732
        A special object is created for the class, which inherits attributes
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   733
        from the superclass, or Object if there's no superclass.  This is the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   734
        class object; the object which you refer to when specifying the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   735
        class by name.  Static, class, and unbound methods are copied
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   736
        from the superclass object.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   737
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   738
        A special constructor function is created with the same name as the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   739
        class, which is used to create instances of that class.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   740
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   741
        A javascript class (e.g. a function with a prototype attribute) is
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   742
        created which is the javascript class of created instances, and
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   743
        which inherits attributes from the class object. Bound methods are
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   744
        copied from the superclass into this class rather than inherited,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   745
        because the class object contains unbound, class, and static methods
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   746
        that we don't necessarily want to inherit.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   747
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   748
        The type of a method can now be determined by inspecting its
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   749
        static_method, unbound_method, class_method, or instance_method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   750
        attribute; only one of these should be true.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   751
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   752
        Much of this work is done in pyjs_extend, is pyjslib.py
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   753
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   754
        class_name = self.modpfx() + uuprefix(node.name, 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   755
        class_name_ = self.modpfx() + uuprefix(node.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   756
        current_klass = Klass(class_name, class_name_)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   757
        init_method = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   758
        for child in node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   759
            if isinstance(child, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   760
                current_klass.add_function(child.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   761
                if child.name == "__init__":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   762
                    init_method = child
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   763
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   764
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   765
        if len(node.bases) == 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   766
            base_class = "pyjslib.__Object"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   767
        elif len(node.bases) == 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   768
            if isinstance(node.bases[0], ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   769
                if self.imported_classes.has_key(node.bases[0].name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   770
                    base_class_ = self.imported_classes[node.bases[0].name] + '.__' + node.bases[0].name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   771
                    base_class = self.imported_classes[node.bases[0].name] + '.' + node.bases[0].name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   772
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   773
                    base_class_ = self.modpfx() + "__" + node.bases[0].name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   774
                    base_class = self.modpfx() + node.bases[0].name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   775
            elif isinstance(node.bases[0], ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   776
                # the bases are not in scope of the class so do not
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   777
                # pass our class to self._name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   778
                base_class_ = self._name(node.bases[0].expr, None) + \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   779
                             ".__" + node.bases[0].attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   780
                base_class = self._name(node.bases[0].expr, None) + \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   781
                             "." + node.bases[0].attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   782
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   783
                raise TranslationError("unsupported type (in _class)", node.bases[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   784
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   785
            current_klass.set_base(base_class)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   786
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   787
            raise TranslationError("more than one base (in _class)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   788
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   789
        print >>self.output, UU+class_name_ + " = function () {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   790
        # call superconstructor
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   791
        #if base_class:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   792
        #    print >>self.output, "    __" + base_class + ".call(this);"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   793
        print >>self.output, "}"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   794
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   795
        if not init_method:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   796
            init_method = ast.Function([], "__init__", ["self"], [], 0, None, [])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   797
            #self._method(init_method, current_klass, class_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   798
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   799
        # Generate a function which constructs the object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   800
        clsfunc = ast.Function([],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   801
           node.name,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   802
           init_method.argnames[1:],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   803
           init_method.defaults,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   804
           init_method.flags,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   805
           None,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   806
           [ast.Discard(ast.CallFunc(ast.Name("JS"), [ast.Const(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   807
#            I attempted lazy initialization, but then you can't access static class members
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   808
#            "    if(!__"+base_class+".__was_initialized__)"+
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   809
#            "        __" + class_name + "_initialize();\n" +
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   810
            "    var instance = new " + UU + class_name_ + "();\n" +
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   811
            "    if(instance.__init__) instance.__init__.apply(instance, arguments);\n" +
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   812
            "    return instance;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   813
            )]))])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   814
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   815
        self._function(clsfunc, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   816
        print >>self.output, UU+class_name_ + ".__initialize__ = function () {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   817
        print >>self.output, "    if("+UU+class_name_+".__was_initialized__) return;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   818
        print >>self.output, "    "+UU+class_name_+".__was_initialized__ = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   819
        cls_obj = UU+class_name_ + '.prototype.__class__'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   820
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   821
        if class_name == "pyjslib.__Object":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   822
            print >>self.output, "    "+cls_obj+" = {};"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   823
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   824
            if base_class and base_class not in ("object", "pyjslib.__Object"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   825
                print >>self.output, "    if(!"+UU+base_class_+".__was_initialized__)"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   826
                print >>self.output, "        "+UU+base_class_+".__initialize__();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   827
                print >>self.output, "    pyjs_extend(" + UU+class_name_ + ", "+UU+base_class_+");"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   828
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   829
                print >>self.output, "    pyjs_extend(" + UU+class_name_ + ", "+UU+"pyjslib.__Object);"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   830
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   831
        print >>self.output, "    "+cls_obj+".__new__ = "+UU+class_name+";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   832
        print >>self.output, "    "+cls_obj+".__name__ = '"+UU+node.name+"';"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   833
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   834
        for child in node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   835
            if isinstance(child, ast.Pass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   836
                pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   837
            elif isinstance(child, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   838
                self._method(child, current_klass, class_name, class_name_)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   839
            elif isinstance(child, ast.Assign):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   840
                self.classattr(child, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   841
            elif isinstance(child, ast.Discard) and isinstance(child.expr, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   842
                # Probably a docstring, turf it
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   843
                pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   844
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   845
                raise TranslationError("unsupported type (in _class)", child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   846
        print >>self.output, "}"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   847
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   848
        print >> self.output, class_name_+".__initialize__();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   849
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   850
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   851
    def classattr(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   852
        self._assign(node, current_klass, True)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   853
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   854
    def _raise(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   855
        if node.expr2:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   856
            raise TranslationError("More than one expression unsupported",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   857
                                   node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   858
        print >> self.output, "throw (%s);" % self.expr(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   859
            node.expr1, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   860
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   861
    def _method(self, node, current_klass, class_name, class_name_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   862
        # reset global var scope
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   863
        self.method_imported_globals = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   864
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   865
        arg_names = list(node.argnames)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   866
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   867
        classmethod = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   868
        staticmethod = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   869
        if node.decorators:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   870
            for d in node.decorators:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   871
                if d.name == "classmethod":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   872
                    classmethod = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   873
                elif d.name == "staticmethod":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   874
                    staticmethod = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   875
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   876
        if staticmethod:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   877
            staticfunc = ast.Function([], class_name_+"."+node.name, node.argnames, node.defaults, node.flags, node.doc, node.code, node.lineno)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   878
            self._function(staticfunc, True)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   879
            print >>self.output, "    " + UU+class_name_ + ".prototype.__class__." + node.name + " = " + class_name_+"."+node.name+";";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   880
            print >>self.output, "    " + UU+class_name_ + ".prototype.__class__." + node.name + ".static_method = true;";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   881
            return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   882
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   883
            if len(arg_names) == 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   884
                raise TranslationError("methods must take an argument 'self' (in _method)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   885
            self.method_self = arg_names[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   886
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   887
            #if not classmethod and arg_names[0] != "self":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   888
            #    raise TranslationError("first arg not 'self' (in _method)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   889
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   890
        normal_arg_names = arg_names[1:]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   891
        if node.kwargs: kwargname = normal_arg_names.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   892
        if node.varargs: varargname = normal_arg_names.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   893
        declared_arg_names = list(normal_arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   894
        if node.kwargs: declared_arg_names.append(kwargname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   895
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   896
        function_args = "(" + ", ".join(declared_arg_names) + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   897
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   898
        if classmethod:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   899
            fexpr = UU + class_name_ + ".prototype.__class__." + node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   900
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   901
            fexpr = UU + class_name_ + ".prototype." + node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   902
        print >>self.output, "    "+fexpr + " = function" + function_args + " {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   903
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   904
        # default arguments
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   905
        self._default_args_handler(node, normal_arg_names, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   906
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   907
        local_arg_names = normal_arg_names + declared_arg_names 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   908
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   909
        if node.varargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   910
            self._varargs_handler(node, varargname, declared_arg_names, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   911
            local_arg_names.append(varargname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   912
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   913
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   914
        # stack of local variable names for this function call
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   915
        self.local_arg_stack.append(local_arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   916
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   917
        for child in node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   918
            self._stmt(child, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   919
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   920
        # remove the top local arg names
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   921
        self.local_arg_stack.pop()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   922
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   923
        print >>self.output, "    };"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   924
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   925
        self._kwargs_parser(node, fexpr, normal_arg_names, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   926
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   927
        if classmethod:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   928
            # Have to create a version on the instances which automatically passes the
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   929
            # class as "self"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   930
            altexpr = UU + class_name_ + ".prototype." + node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   931
            print >>self.output, "    "+altexpr + " = function() {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   932
            print >>self.output, "        return " + fexpr + ".apply(this.__class__, arguments);"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   933
            print >>self.output, "    };"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   934
            print >>self.output, "    "+fexpr+".class_method = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   935
            print >>self.output, "    "+altexpr+".instance_method = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   936
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   937
            # For instance methods, we need an unbound version in the class object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   938
            altexpr = UU + class_name_ + ".prototype.__class__." + node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   939
            print >>self.output, "    "+altexpr + " = function() {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   940
            print >>self.output, "        return " + fexpr + ".call.apply("+fexpr+", arguments);"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   941
            print >>self.output, "    };"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   942
            print >>self.output, "    "+altexpr+".unbound_method = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   943
            print >>self.output, "    "+fexpr+".instance_method = true;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   944
            print >>self.output, "    "+altexpr+".__name__ = '%s';" % node.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   945
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   946
        print >>self.output, UU + class_name_ + ".prototype.%s.__name__ = '%s';" % \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   947
                (node.name, node.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   948
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   949
        if node.kwargs or len(node.defaults):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   950
            print >>self.output, "    "+altexpr + ".parse_kwargs = " + fexpr + ".parse_kwargs;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   951
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   952
        self.method_self = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   953
        self.method_imported_globals = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   954
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   955
    def _isNativeFunc(self, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   956
        if isinstance(node, ast.Discard):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   957
            if isinstance(node.expr, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   958
                if isinstance(node.expr.node, ast.Name) and \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   959
                       node.expr.node.name == NATIVE_JS_FUNC_NAME:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   960
                    return True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   961
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   962
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   963
    def _stmt(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   964
        debugStmt = self.debug and not self._isNativeFunc(node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   965
        if debugStmt:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   966
            print >>self.output, '  try {'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   967
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   968
        if isinstance(node, ast.Return):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   969
            self._return(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   970
        elif isinstance(node, ast.Break):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   971
            self._break(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   972
        elif isinstance(node, ast.Continue):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   973
            self._continue(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   974
        elif isinstance(node, ast.Assign):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   975
            self._assign(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   976
        elif isinstance(node, ast.AugAssign):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   977
            self._augassign(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   978
        elif isinstance(node, ast.Discard):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   979
            self._discard(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   980
        elif isinstance(node, ast.If):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   981
            self._if(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   982
        elif isinstance(node, ast.For):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   983
            self._for(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   984
        elif isinstance(node, ast.While):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   985
            self._while(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   986
        elif isinstance(node, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   987
            self._subscript_stmt(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   988
        elif isinstance(node, ast.Global):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   989
            self._global(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   990
        elif isinstance(node, ast.Pass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   991
            pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   992
        elif isinstance(node, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   993
            self._function(node, True)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   994
        elif isinstance(node, ast.Printnl):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   995
           self._print(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   996
        elif isinstance(node, ast.Print):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   997
           self._print(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   998
        elif isinstance(node, ast.TryExcept):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   999
            self._tryExcept(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1000
        elif isinstance(node, ast.Raise):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1001
            self._raise(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1002
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1003
            raise TranslationError("unsupported type (in _stmt)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1004
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1005
        if debugStmt:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1006
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1007
            lt = self.get_line_trace(node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1008
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1009
            haltException = self.module_prefix + "HaltException"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1010
            isHaltFunction = self.module_prefix + "IsHaltException"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1011
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1012
            print >>self.output, '  } catch (__err) {'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1013
            print >>self.output, '      if (' + isHaltFunction + '(__err.name)) {'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1014
            print >>self.output, '          throw __err;'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1015
            print >>self.output, '      } else {'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1016
            print >>self.output, "          st = sys.printstack() + "\
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1017
                                                + '"%s"' % lt + "+ '\\n' ;"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1018
            print >>self.output, '          alert("' + "Error in " \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1019
                                                + lt + '"' \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1020
                                                + '+"\\n"+__err.name+": "+__err.message'\
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1021
                                                + '+"\\n\\nStack trace:\\n"' \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1022
                                                + '+st' \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1023
                                                + ');'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1024
            print >>self.output, '          debugger;'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1025
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1026
            print >>self.output, '          throw new ' + self.module_prefix + "HaltException();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1027
            print >>self.output, '      }'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1028
            print >>self.output, '  }'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1029
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1030
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1031
    def get_line_trace(self, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1032
        lineNum = "Unknown"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1033
        srcLine = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1034
        if hasattr(node, "lineno"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1035
            if node.lineno != None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1036
                lineNum = node.lineno
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1037
                srcLine = self.src[min(lineNum, len(self.src))-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1038
                srcLine = srcLine.replace('\\', '\\\\')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1039
                srcLine = srcLine.replace('"', '\\"')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1040
                srcLine = srcLine.replace("'", "\\'")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1041
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1042
        return self.raw_module_name + ".py, line " \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1043
               + str(lineNum) + ":"\
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1044
               + "\\n" \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1045
               + "    " + srcLine
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1046
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1047
    def _augassign(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1048
        v = node.node
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1049
        if isinstance(v, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1050
            # XXX HACK!  don't allow += on return result of getattr.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1051
            # TODO: create a temporary variable or something.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1052
            lhs = self._getattr(v, current_klass, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1053
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1054
            lhs = self._name(node.node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1055
        op = node.op
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1056
        rhs = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1057
        print >>self.output, "    " + lhs + " " + op + " " + rhs + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1058
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1059
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1060
    def _assign(self, node, current_klass, top_level = False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1061
        if len(node.nodes) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1062
            tempvar = '__temp'+str(node.lineno)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1063
            tnode = ast.Assign([ast.AssName(tempvar, "OP_ASSIGN", node.lineno)], node.expr, node.lineno)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1064
            self._assign(tnode, current_klass, top_level)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1065
            for v in node.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1066
               tnode2 = ast.Assign([v], ast.Name(tempvar, node.lineno), node.lineno)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1067
               self._assign(tnode2, current_klass, top_level)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1068
            return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1069
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1070
        local_var_names = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1071
        if len(self.local_arg_stack) > 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1072
            local_var_names = self.local_arg_stack[-1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1073
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1074
        def _lhsFromAttr(v, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1075
            attr_name = v.attrname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1076
            if isinstance(v.expr, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1077
                obj = v.expr.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1078
                lhs = self._name(v.expr, current_klass) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1079
            elif isinstance(v.expr, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1080
                lhs = self._getattr(v, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1081
            elif isinstance(v.expr, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1082
                lhs = self._subscript(v.expr, current_klass) + "." + attr_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1083
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1084
                raise TranslationError("unsupported type (in _assign)", v.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1085
            return lhs
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1086
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1087
        def _lhsFromName(v, top_level, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1088
            if top_level:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1089
                if current_klass:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1090
                    lhs = UU+current_klass.name_ + ".prototype.__class__." \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1091
                               + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1092
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1093
                    self.top_level_vars.add(v.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1094
                    vname = self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1095
                    if not self.modpfx() and v.name not in\
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1096
                           self.method_imported_globals:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1097
                        lhs = "var " + vname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1098
                    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1099
                        lhs = UU + vname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1100
                    self.add_local_arg(v.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1101
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1102
                if v.name in local_var_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1103
                    lhs = v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1104
                elif v.name in self.method_imported_globals:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1105
                    lhs = self.modpfx() + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1106
                else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1107
                    lhs = "var " + v.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1108
                    self.add_local_arg(v.name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1109
            return lhs
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1110
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1111
        dbg = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1112
        v = node.nodes[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1113
        if isinstance(v, ast.AssAttr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1114
            lhs = _lhsFromAttr(v, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1115
            if v.flags == "OP_ASSIGN":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1116
                op = "="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1117
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1118
                raise TranslationError("unsupported flag (in _assign)", v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1119
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1120
        elif isinstance(v, ast.AssName):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1121
            lhs = _lhsFromName(v, top_level, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1122
            if v.flags == "OP_ASSIGN":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1123
                op = "="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1124
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1125
                raise TranslationError("unsupported flag (in _assign)", v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1126
        elif isinstance(v, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1127
            if v.flags == "OP_ASSIGN":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1128
                obj = self.expr(v.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1129
                if len(v.subs) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1130
                    raise TranslationError("must have one sub (in _assign)", v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1131
                idx = self.expr(v.subs[0], current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1132
                value = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1133
                print >>self.output, "    " + obj + ".__setitem__(" + idx + ", " + value + ");"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1134
                return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1135
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1136
                raise TranslationError("unsupported flag (in _assign)", v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1137
        elif isinstance(v, (ast.AssList, ast.AssTuple)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1138
            uniqueID = self.nextTupleAssignID
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1139
            self.nextTupleAssignID += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1140
            tempName = "__tupleassign" + str(uniqueID) + "__"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1141
            print >>self.output, "    var " + tempName + " = " + \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1142
                                 self.expr(node.expr, current_klass) + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1143
            for index,child in enumerate(v.getChildNodes()):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1144
                rhs = tempName + ".__getitem__(" + str(index) + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1145
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1146
                if isinstance(child, ast.AssAttr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1147
                    lhs = _lhsFromAttr(child, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1148
                elif isinstance(child, ast.AssName):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1149
                    lhs = _lhsFromName(child, top_level, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1150
                elif isinstance(child, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1151
                    if child.flags == "OP_ASSIGN":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1152
                        obj = self.expr(child.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1153
                        if len(child.subs) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1154
                            raise TranslationError("must have one sub " +
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1155
                                                   "(in _assign)", child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1156
                        idx = self.expr(child.subs[0], current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1157
                        value = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1158
                        print >>self.output, "    " + obj + ".__setitem__(" \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1159
                                           + idx + ", " + rhs + ");"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1160
                        continue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1161
                print >>self.output, "    " + lhs + " = " + rhs + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1162
            return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1163
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1164
            raise TranslationError("unsupported type (in _assign)", v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1165
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1166
        rhs = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1167
        if dbg:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1168
            print "b", repr(node.expr), rhs
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1169
        print >>self.output, "    " + lhs + " " + op + " " + rhs + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1170
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1171
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1172
    def _discard(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1173
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1174
        if isinstance(node.expr, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1175
            debugStmt = self.debug and not self._isNativeFunc(node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1176
            if debugStmt and isinstance(node.expr.node, ast.Name) and \
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1177
               node.expr.node.name == 'import_wait':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1178
               debugStmt = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1179
            if debugStmt:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1180
                st = self.get_line_trace(node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1181
                print >>self.output, "sys.addstack('%s');\n" % st
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1182
            if isinstance(node.expr.node, ast.Name) and node.expr.node.name == NATIVE_JS_FUNC_NAME:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1183
                if len(node.expr.args) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1184
                    raise TranslationError("native javascript function %s must have one arg" % NATIVE_JS_FUNC_NAME, node.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1185
                if not isinstance(node.expr.args[0], ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1186
                    raise TranslationError("native javascript function %s must have constant arg" % NATIVE_JS_FUNC_NAME, node.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1187
                raw_js = node.expr.args[0].value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1188
                print >>self.output, raw_js
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1189
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1190
                expr = self._callfunc(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1191
                print >>self.output, "    " + expr + ";"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1192
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1193
            if debugStmt:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1194
                print >>self.output, "sys.popstack();\n"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1195
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1196
        elif isinstance(node.expr, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1197
            if node.expr.value is not None: # Empty statements generate ignore None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1198
                print >>self.output, self._const(node.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1199
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1200
            raise TranslationError("unsupported type (in _discard)", node.expr)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1201
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1202
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1203
    def _if(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1204
        for i in range(len(node.tests)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1205
            test, consequence = node.tests[i]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1206
            if i == 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1207
                keyword = "if"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1208
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1209
                keyword = "else if"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1210
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1211
            self._if_test(keyword, test, consequence, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1212
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1213
        if node.else_:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1214
            keyword = "else"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1215
            test = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1216
            consequence = node.else_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1217
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1218
            self._if_test(keyword, test, consequence, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1219
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1220
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1221
    def _if_test(self, keyword, test, consequence, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1222
        if test:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1223
            expr = self.expr(test, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1224
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1225
            print >>self.output, "    " + keyword + " (pyjslib.bool(" + expr + ")) {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1226
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1227
            print >>self.output, "    " + keyword + " {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1228
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1229
        if isinstance(consequence, ast.Stmt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1230
            for child in consequence.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1231
                self._stmt(child, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1232
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1233
            raise TranslationError("unsupported type (in _if_test)", consequence)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1234
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1235
        print >>self.output, "    }"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1236
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1237
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1238
    def _from(self, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1239
        for name in node.names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1240
            # look up "hack" in AppTranslator as to how findFile gets here
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1241
            module_name = node.modname + "." + name[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1242
            try:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1243
                ff = self.findFile(module_name + ".py")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1244
            except Exception:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1245
                ff = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1246
            if ff:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1247
                self.add_imported_module(module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1248
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1249
                self.imported_classes[name[0]] = node.modname
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1250
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1251
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1252
    def _compare(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1253
        lhs = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1254
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1255
        if len(node.ops) != 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1256
            raise TranslationError("only one ops supported (in _compare)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1257
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1258
        op = node.ops[0][0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1259
        rhs_node = node.ops[0][1]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1260
        rhs = self.expr(rhs_node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1261
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1262
        if op == "==":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1263
            return "pyjslib.eq(%s, %s)" % (lhs, rhs)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1264
        if op == "in":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1265
            return rhs + ".__contains__(" + lhs + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1266
        elif op == "not in":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1267
            return "!" + rhs + ".__contains__(" + lhs + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1268
        elif op == "is":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1269
            op = "==="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1270
        elif op == "is not":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1271
            op = "!=="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1272
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1273
        return "(" + lhs + " " + op + " " + rhs + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1274
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1275
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1276
    def _not(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1277
        expr = self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1278
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1279
        return "!(" + expr + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1280
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1281
    def _or(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1282
        expr = "("+(") || (".join([self.expr(child, current_klass) for child in node.nodes]))+')'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1283
        return expr
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1284
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1285
    def _and(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1286
        expr = "("+(") && (".join([self.expr(child, current_klass) for child in node.nodes]))+")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1287
        return expr
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1288
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1289
    def _for(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1290
        assign_name = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1291
        assign_tuple = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1292
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1293
        # based on Bob Ippolito's Iteration in Javascript code
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1294
        if isinstance(node.assign, ast.AssName):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1295
            assign_name = node.assign.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1296
            self.add_local_arg(assign_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1297
            if node.assign.flags == "OP_ASSIGN":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1298
                op = "="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1299
        elif isinstance(node.assign, ast.AssTuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1300
            op = "="
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1301
            i = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1302
            for child in node.assign:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1303
                child_name = child.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1304
                if assign_name == "":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1305
                    assign_name = "temp_" + child_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1306
                self.add_local_arg(child_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1307
                assign_tuple += """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1308
                var %(child_name)s %(op)s %(assign_name)s.__getitem__(%(i)i);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1309
                """ % locals()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1310
                i += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1311
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1312
            raise TranslationError("unsupported type (in _for)", node.assign)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1313
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1314
        if isinstance(node.list, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1315
            list_expr = self._name(node.list, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1316
        elif isinstance(node.list, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1317
            list_expr = self._getattr(node.list, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1318
        elif isinstance(node.list, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1319
            list_expr = self._callfunc(node.list, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1320
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1321
            raise TranslationError("unsupported type (in _for)", node.list)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1322
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1323
        lhs = "var " + assign_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1324
        iterator_name = "__" + assign_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1325
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1326
        print >>self.output, """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1327
        var %(iterator_name)s = %(list_expr)s.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1328
        try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1329
            while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1330
                %(lhs)s %(op)s %(iterator_name)s.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1331
                %(assign_tuple)s
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1332
        """ % locals()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1333
        for node in node.body.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1334
            self._stmt(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1335
        print >>self.output, """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1336
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1337
        } catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1338
            if (e.__name__ != pyjslib.StopIteration.__name__) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1339
                throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1340
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1341
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1342
        """ % locals()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1343
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1344
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1345
    def _while(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1346
        test = self.expr(node.test, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1347
        print >>self.output, "    while (pyjslib.bool(" + test + ")) {"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1348
        if isinstance(node.body, ast.Stmt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1349
            for child in node.body.nodes:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1350
                self._stmt(child, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1351
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1352
            raise TranslationError("unsupported type (in _while)", node.body)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1353
        print >>self.output, "    }"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1354
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1355
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1356
    def _const(self, node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1357
        if isinstance(node.value, int):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1358
            return str(node.value)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1359
        elif isinstance(node.value, float):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1360
            return str(node.value)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1361
        elif isinstance(node.value, basestring):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1362
            v = node.value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1363
            if isinstance(node.value, unicode):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1364
                v = v.encode('utf-8')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1365
            return  "String('%s')" % escapejs(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1366
        elif node.value is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1367
            return "null"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1368
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1369
            raise TranslationError("unsupported type (in _const)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1370
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1371
    def _unaryadd(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1372
        return self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1373
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1374
    def _unarysub(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1375
        return "-" + self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1376
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1377
    def _add(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1378
        return self.expr(node.left, current_klass) + " + " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1379
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1380
    def _sub(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1381
        return self.expr(node.left, current_klass) + " - " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1382
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1383
    def _div(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1384
        return self.expr(node.left, current_klass) + " / " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1385
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1386
    def _mul(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1387
        return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1388
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1389
    def _mod(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1390
        if isinstance(node.left, ast.Const) and isinstance(node.left.value, StringType):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1391
           self.imported_js.add("sprintf.js") # Include the sprintf functionality if it is used
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1392
           return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1393
        return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1394
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1395
    def _invert(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1396
        return "~" + self.expr(node.expr, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1397
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1398
    def _bitand(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1399
        return " & ".join([self.expr(child, current_klass) for child in node.nodes])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1400
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1401
    def _bitshiftleft(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1402
        return self.expr(node.left, current_klass) + " << " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1403
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1404
    def _bitshiftright(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1405
        return self.expr(node.left, current_klass) + " >>> " + self.expr(node.right, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1406
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1407
    def _bitxor(self,node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1408
        return " ^ ".join([self.expr(child, current_klass) for child in node.nodes])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1409
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1410
    def _bitor(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1411
        return " | ".join([self.expr(child, current_klass) for child in node.nodes])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1412
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1413
    def _subscript(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1414
        if node.flags == "OP_APPLY":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1415
            if len(node.subs) == 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1416
                return self.expr(node.expr, current_klass) + ".__getitem__(" + self.expr(node.subs[0], current_klass) + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1417
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1418
                raise TranslationError("must have one sub (in _subscript)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1419
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1420
            raise TranslationError("unsupported flag (in _subscript)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1421
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1422
    def _subscript_stmt(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1423
        if node.flags == "OP_DELETE":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1424
            print >>self.output, "    " + self.expr(node.expr, current_klass) + ".__delitem__(" + self.expr(node.subs[0], current_klass) + ");"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1425
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1426
            raise TranslationError("unsupported flag (in _subscript)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1427
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1428
    def _list(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1429
        return "new pyjslib.List([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1430
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1431
    def _dict(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1432
        items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1433
        for x in node.items:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1434
            key = self.expr(x[0], current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1435
            value = self.expr(x[1], current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1436
            items.append("[" + key + ", " + value + "]")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1437
        return "new pyjslib.Dict([" + ", ".join(items) + "])"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1438
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1439
    def _tuple(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1440
        return "new pyjslib.Tuple([" + ", ".join([self.expr(x, current_klass) for x in node.nodes]) + "])"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1441
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1442
    def _lambda(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1443
        if node.varargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1444
            raise TranslationError("varargs are not supported in Lambdas", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1445
        if node.kwargs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1446
            raise TranslationError("kwargs are not supported in Lambdas", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1447
        res = cStringIO.StringIO()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1448
        arg_names = list(node.argnames)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1449
        function_args = ", ".join(arg_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1450
        for child in node.getChildNodes():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1451
            expr = self.expr(child, None)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1452
        print >> res, "function (%s){" % function_args
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1453
        self._default_args_handler(node, arg_names, None,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1454
                                   output=res)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1455
        print >> res, 'return %s;}' % expr
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1456
        return res.getvalue()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1457
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1458
    def _slice(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1459
        if node.flags == "OP_APPLY":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1460
            lower = "null"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1461
            upper = "null"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1462
            if node.lower != None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1463
                lower = self.expr(node.lower, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1464
            if node.upper != None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1465
                upper = self.expr(node.upper, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1466
            return  "pyjslib.slice(" + self.expr(node.expr, current_klass) + ", " + lower + ", " + upper + ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1467
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1468
            raise TranslationError("unsupported flag (in _slice)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1469
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1470
    def _global(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1471
        for name in node.names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1472
            self.method_imported_globals.add(name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1473
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1474
    def expr(self, node, current_klass):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1475
        if isinstance(node, ast.Const):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1476
            return self._const(node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1477
        # @@@ not sure if the parentheses should be here or in individual operator functions - JKT
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1478
        elif isinstance(node, ast.Mul):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1479
            return " ( " + self._mul(node, current_klass) + " ) "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1480
        elif isinstance(node, ast.Add):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1481
            return " ( " + self._add(node, current_klass) + " ) "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1482
        elif isinstance(node, ast.Sub):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1483
            return " ( " + self._sub(node, current_klass) + " ) "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1484
        elif isinstance(node, ast.Div):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1485
            return " ( " + self._div(node, current_klass) + " ) "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1486
        elif isinstance(node, ast.Mod):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1487
            return self._mod(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1488
        elif isinstance(node, ast.UnaryAdd):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1489
            return self._unaryadd(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1490
        elif isinstance(node, ast.UnarySub):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1491
            return self._unarysub(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1492
        elif isinstance(node, ast.Not):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1493
            return self._not(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1494
        elif isinstance(node, ast.Or):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1495
            return self._or(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1496
        elif isinstance(node, ast.And):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1497
            return self._and(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1498
        elif isinstance(node, ast.Invert):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1499
            return self._invert(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1500
        elif isinstance(node, ast.Bitand):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1501
            return "("+self._bitand(node, current_klass)+")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1502
        elif isinstance(node,ast.LeftShift):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1503
            return self._bitshiftleft(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1504
        elif isinstance(node, ast.RightShift):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1505
            return self._bitshiftright(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1506
        elif isinstance(node, ast.Bitxor):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1507
            return "("+self._bitxor(node, current_klass)+")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1508
        elif isinstance(node, ast.Bitor):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1509
            return "("+self._bitor(node, current_klass)+")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1510
        elif isinstance(node, ast.Compare):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1511
            return self._compare(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1512
        elif isinstance(node, ast.CallFunc):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1513
            return self._callfunc(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1514
        elif isinstance(node, ast.Name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1515
            return self._name(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1516
        elif isinstance(node, ast.Subscript):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1517
            return self._subscript(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1518
        elif isinstance(node, ast.Getattr):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1519
            return self._getattr(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1520
        elif isinstance(node, ast.List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1521
            return self._list(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1522
        elif isinstance(node, ast.Dict):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1523
            return self._dict(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1524
        elif isinstance(node, ast.Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1525
            return self._tuple(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1526
        elif isinstance(node, ast.Slice):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1527
            return self._slice(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1528
        elif isinstance(node, ast.Lambda):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1529
            return self._lambda(node, current_klass)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1530
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1531
            raise TranslationError("unsupported type (in expr)", node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1532
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1533
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1534
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1535
import cStringIO
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1536
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1537
def translate(file_name, module_name, debug=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1538
    f = file(file_name, "r")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1539
    src = f.read()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1540
    f.close()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1541
    output = cStringIO.StringIO()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1542
    mod = compiler.parseFile(file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1543
    t = Translator(module_name, module_name, module_name, src, debug, mod, output)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1544
    return output.getvalue()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1545
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1546
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1547
class PlatformParser:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1548
    def __init__(self, platform_dir = "", verbose=True):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1549
        self.platform_dir = platform_dir
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1550
        self.parse_cache = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1551
        self.platform = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1552
        self.verbose = verbose
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1553
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1554
    def setPlatform(self, platform):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1555
        self.platform = platform
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1556
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1557
    def parseModule(self, module_name, file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1558
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1559
        importing = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1560
        if not self.parse_cache.has_key(file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1561
            importing = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1562
            mod = compiler.parseFile(file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1563
            self.parse_cache[file_name] = mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1564
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1565
            mod = self.parse_cache[file_name]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1566
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1567
        override = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1568
        platform_file_name = self.generatePlatformFilename(file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1569
        if self.platform and os.path.isfile(platform_file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1570
            mod = copy.deepcopy(mod)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1571
            mod_override = compiler.parseFile(platform_file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1572
            self.merge(mod, mod_override)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1573
            override = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1574
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1575
        if self.verbose:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1576
            if override:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1577
                print "Importing %s (Platform %s)" % (module_name, self.platform)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1578
            elif importing:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1579
                print "Importing %s" % (module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1580
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1581
        return mod, override
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1582
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1583
    def generatePlatformFilename(self, file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1584
        (module_name, extension) = os.path.splitext(os.path.basename(file_name))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1585
        platform_file_name = module_name + self.platform + extension
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1586
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1587
        return os.path.join(os.path.dirname(file_name), self.platform_dir, platform_file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1588
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1589
    def merge(self, tree1, tree2):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1590
        for child in tree2.node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1591
            if isinstance(child, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1592
                self.replaceFunction(tree1, child.name, child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1593
            elif isinstance(child, ast.Class):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1594
                self.replaceClassMethods(tree1, child.name, child)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1595
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1596
        return tree1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1597
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1598
    def replaceFunction(self, tree, function_name, function_node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1599
        # find function to replace
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1600
        for child in tree.node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1601
            if isinstance(child, ast.Function) and child.name == function_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1602
                self.copyFunction(child, function_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1603
                return
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1604
        raise TranslationError("function not found: " + function_name, function_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1605
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1606
    def replaceClassMethods(self, tree, class_name, class_node):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1607
        # find class to replace
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1608
        old_class_node = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1609
        for child in tree.node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1610
            if isinstance(child, ast.Class) and child.name == class_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1611
                old_class_node = child
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1612
                break
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1613
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1614
        if not old_class_node:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1615
            raise TranslationError("class not found: " + class_name, class_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1616
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1617
        # replace methods
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1618
        for function_node in class_node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1619
            if isinstance(function_node, ast.Function):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1620
                found = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1621
                for child in old_class_node.code:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1622
                    if isinstance(child, ast.Function) and child.name == function_node.name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1623
                        found = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1624
                        self.copyFunction(child, function_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1625
                        break
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1626
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1627
                if not found:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1628
                    raise TranslationError("class method not found: " + class_name + "." + function_node.name, function_node)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1629
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1630
    def copyFunction(self, target, source):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1631
        target.code = source.code
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1632
        target.argnames = source.argnames
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1633
        target.defaults = source.defaults
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1634
        target.doc = source.doc # @@@ not sure we need to do this any more
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1635
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1636
def dotreplace(fname):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1637
    path, ext = os.path.splitext(fname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1638
    return path.replace(".", "/") + ext
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1639
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1640
class AppTranslator:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1641
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1642
    def __init__(self, library_dirs=[], parser=None, dynamic=False,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1643
                 optimize=False, verbose=True):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1644
        self.extension = ".py"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1645
        self.optimize = optimize
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1646
        self.library_modules = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1647
        self.overrides = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1648
        self.library_dirs = path + library_dirs
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1649
        self.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1650
        self.verbose = verbose
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1651
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1652
        if not parser:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1653
            self.parser = PlatformParser()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1654
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1655
            self.parser = parser
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1656
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1657
        self.parser.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1658
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1659
    def findFile(self, file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1660
        if os.path.isfile(file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1661
            return file_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1662
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1663
        for library_dir in self.library_dirs:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1664
            file_name = dotreplace(file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1665
            full_file_name = os.path.join(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1666
                os.path.abspath(os.path.dirname(__file__)), library_dir, file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1667
            if os.path.isfile(full_file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1668
                return full_file_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1669
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1670
            fnameinit, ext = os.path.splitext(file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1671
            fnameinit = fnameinit + "/__init__.py"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1672
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1673
            full_file_name = os.path.join(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1674
                os.path.abspath(os.path.dirname(__file__)), library_dir, fnameinit)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1675
            if os.path.isfile(full_file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1676
                return full_file_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1677
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1678
        raise Exception("file not found: " + file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1679
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1680
    def _translate(self, module_name, is_app=True, debug=False,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1681
                   imported_js=set()):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1682
        if module_name not in self.library_modules:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1683
            self.library_modules.append(module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1684
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1685
        file_name = self.findFile(module_name + self.extension)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1686
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1687
        output = cStringIO.StringIO()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1688
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1689
        f = file(file_name, "r")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1690
        src = f.read()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1691
        f.close()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1692
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1693
        mod, override = self.parser.parseModule(module_name, file_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1694
        if override:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1695
            override_name = "%s.%s" % (self.parser.platform.lower(),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1696
                                           module_name)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1697
            self.overrides[override_name] = override_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1698
        if is_app:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1699
            mn = '__main__'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1700
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1701
            mn = module_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1702
        t = Translator(mn, module_name, module_name,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1703
                       src, debug, mod, output, self.dynamic, self.optimize,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1704
                       self.findFile)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1705
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1706
        module_str = output.getvalue()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1707
        imported_js.update(set(t.imported_js))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1708
        imported_modules_str = ""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1709
        for module in t.imported_modules:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1710
            if module not in self.library_modules:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1711
                self.library_modules.append(module)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1712
                #imported_js.update(set(t.imported_js))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1713
                #imported_modules_str += self._translate(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1714
                #    module, False, debug=debug, imported_js=imported_js)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1715
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1716
        return imported_modules_str + module_str
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1717
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1718
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1719
    def translate(self, module_name, is_app=True, debug=False,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1720
                  library_modules=[]):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1721
        app_code = cStringIO.StringIO()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1722
        lib_code = cStringIO.StringIO()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1723
        imported_js = set()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1724
        self.library_modules = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1725
        self.overrides = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1726
        for library in library_modules:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1727
            if library.endswith(".js"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1728
                imported_js.add(library)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1729
                continue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1730
            self.library_modules.append(library)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1731
            if self.verbose:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1732
                print 'Including LIB', library
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1733
            print >> lib_code, '\n//\n// BEGIN LIB '+library+'\n//\n'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1734
            print >> lib_code, self._translate(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1735
                library, False, debug=debug, imported_js=imported_js)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1736
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1737
            print >> lib_code, "/* initialize static library */"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1738
            print >> lib_code, "%s%s();\n" % (UU, library)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1739
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1740
            print >> lib_code, '\n//\n// END LIB '+library+'\n//\n'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1741
        if module_name:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1742
            print >> app_code, self._translate(
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1743
                module_name, is_app, debug=debug, imported_js=imported_js)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1744
        for js in imported_js:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1745
           path = self.findFile(js)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1746
           if os.path.isfile(path):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1747
              if self.verbose:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1748
                  print 'Including JS', js
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1749
              print >> lib_code,  '\n//\n// BEGIN JS '+js+'\n//\n'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1750
              print >> lib_code, file(path).read()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1751
              print >> lib_code,  '\n//\n// END JS '+js+'\n//\n'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1752
           else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1753
              print >>sys.stderr, 'Warning: Unable to find imported javascript:', js
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1754
        return lib_code.getvalue(), app_code.getvalue()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1755
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1756
usage = """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1757
  usage: %s file_name [module_name]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1758
"""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1759
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1760
def main():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1761
    import sys
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1762
    if len(sys.argv)<2:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1763
        print >> sys.stderr, usage % sys.argv[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1764
        sys.exit(1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1765
    file_name = os.path.abspath(sys.argv[1])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1766
    if not os.path.isfile(file_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1767
        print >> sys.stderr, "File not found %s" % file_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1768
        sys.exit(1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1769
    if len(sys.argv) > 2:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1770
        module_name = sys.argv[2]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1771
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1772
        module_name = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1773
    print translate(file_name, module_name),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1774
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1775
if __name__ == "__main__":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1776
    main()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1777