plugins/python/modules/svgui/pyjs/lib/pyjslib.py
author edouard
Fri, 04 Dec 2009 15:14:32 +0100
changeset 451 73b3913b6d9c
parent 371 b7cb57a2da08
permissions -rw-r--r--
Fixed automatic console rising on w32
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
# Copyright 2006 James Tauber and contributors
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
# Licensed under the Apache License, Version 2.0 (the "License");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
# 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
     5
# You may obtain a copy of the License at
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     6
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
#     http://www.apache.org/licenses/LICENSE-2.0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
# Unless required by applicable law or agreed to in writing, software
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
# distributed under the License is distributed on an "AS IS" BASIS,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    12
# See the License for the specific language governing permissions and
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    13
# limitations under the License.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    14
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
# iteration from Bob Ippolito's Iteration in JavaScript
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
from __pyjamas__ import JS
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
# must declare import _before_ importing sys
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
def import_module(path, parent_module, module_name, dynamic=1, async=False):
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
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
        var cache_file;
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
        if (module_name == "sys" || module_name == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
            /*module_load_request[module_name] = 1;*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
            return;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
        if (path == null)
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
            path = './';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
        var override_name = sys.platform + "." + module_name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
        if (((sys.overrides != null) && 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
             (sys.overrides.has_key(override_name))))
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
            cache_file =  sys.overrides.__getitem__(override_name) ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
            cache_file =  module_name ;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
        cache_file = (path + cache_file + '.cache.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
        //alert("cache " + cache_file + " " + module_name + " " + parent_module);
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
        /* already loaded? */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
        if (module_load_request[module_name])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
            if (module_load_request[module_name] >= 3 && parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
                //onload_fn = parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
                //pyjs_eval(onload_fn); /* set up the parent-module namespace */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
        if (typeof (module_load_request[module_name]) == 'undefined')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
            module_load_request[module_name] = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
        /* following a load, this first executes the script 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
         * "preparation" function MODULENAME_loaded_fn()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
         * and then sets up the loaded module in the namespace
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
         * of the parent.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
         */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
        onload_fn = ''; // module_name + "_loaded_fn();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
        if (parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
            //onload_fn += parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
            /*pmod = parent_module + '.' + module_name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
            onload_fn += 'alert("' + pmod + '"+' + pmod+');';*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
        if (dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
            /* this one tacks the script onto the end of the DOM
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
             */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
            pyjs_load_script(cache_file, onload_fn, async);
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
            /* this one actually RUNS the script (eval) into the page.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
               my feeling is that this would be better for non-async
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
               but i can't get it to work entirely yet.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
             */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
            /*pyjs_ajax_eval(cache_file, onload_fn, async);*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
            if (module_name != "pyjslib" &&
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
                module_name != "sys")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
                pyjs_eval(onload_fn);
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   108
JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
function import_wait(proceed_fn, parent_mod, dynamic) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   111
    var data = '';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
    var element = $doc.createElement("div");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
    $doc.body.appendChild(element);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
    function write_dom(txt) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
        element.innerHTML = txt + '<br />';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   117
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   118
    var timeoutperiod = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
    if (dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
        var timeoutperiod = 1;
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
    var wait = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   123
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
        var status = '';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   125
        for (l in module_load_request)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
            var m = module_load_request[l];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
            if (l == "sys" || l == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   129
                continue;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
            status += l + m + " ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   131
        }
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
        //write_dom( " import wait " + wait_count + " " + status + " parent_mod " + parent_mod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
        wait_count += 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   136
        if (status == '')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   137
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   138
            setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   139
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   140
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   141
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   142
        for (l in module_load_request)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   143
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   144
            var m = module_load_request[l];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   145
            if (l == "sys" || l == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   146
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   147
                module_load_request[l] = 4;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   148
                continue;
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
            if ((parent_mod != null) && (l == parent_mod))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   151
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   152
                if (m == 1)
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
                    setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   155
                    return;
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
                if (m == 2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   158
                {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   159
                    /* cheat and move app on to next stage */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   160
                    module_load_request[l] = 3;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   161
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   162
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   163
            if (m == 1 || m == 2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   164
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   165
                setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   166
                return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   167
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   168
            if (m == 3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   169
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   170
                //alert("waited for module " + l + ": loaded");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   171
                module_load_request[l] = 4;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   172
                mod_fn = modules[l];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   173
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   174
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   175
        //alert("module wait done");
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
        if (proceed_fn.importDone)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   178
            proceed_fn.importDone(proceed_fn);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   179
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   180
            proceed_fn();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   181
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   182
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   183
    wait();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   184
}
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   187
class Object:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   188
    pass
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
object = Object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   191
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   192
class Modload:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   194
    def __init__(self, path, app_modlist, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   195
                 parent_mod):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
        self.app_modlist = app_modlist
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   197
        self.app_imported_fn = app_imported_fn
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
        self.path = path
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
        self.idx = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
        self.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
        self.parent_mod = parent_mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   203
    def next(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   205
        for i in range(len(self.app_modlist[self.idx])):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
            app = self.app_modlist[self.idx][i]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
            import_module(self.path, self.parent_mod, app, self.dynamic, True);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   208
        self.idx += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   209
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   210
        if self.idx >= len(self.app_modlist):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   211
            import_wait(self.app_imported_fn, self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   213
            import_wait(getattr(self, "next"), self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   214
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
def get_module(module_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
    ev = "__mod = %s;" % module_name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
    JS("pyjs_eval(ev);")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
    return __mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   219
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   221
                        parent_mod=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   222
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   223
    loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   224
    loader.next()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   225
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
import sys
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   227
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   228
class BaseException:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   229
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   230
    name = "BaseException"
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
    def __init__(self, *args):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   233
        self.args = args
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   234
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   235
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   236
        if len(self.args) is 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   237
            return ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   238
        elif len(self.args) is 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   239
            return repr(self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   240
        return repr(self.args)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   241
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   242
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   243
        return str(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   244
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   245
class Exception(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   246
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   247
    name = "Exception"
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
class TypeError(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   250
    name = "TypeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   251
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   252
class StandardError(Exception):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   253
    name = "StandardError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   254
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   255
class LookupError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   256
    name = "LookupError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   257
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   258
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   259
        return self.name + ": " + self.args[0]
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
class KeyError(LookupError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   262
    name = "KeyError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   263
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   264
class AttributeError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   265
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   266
    name = "AttributeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   267
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   268
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   269
        return "AttributeError: %s of %s" % (self.args[1], self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   270
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   271
JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   272
pyjslib.StopIteration = function () { };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   273
pyjslib.StopIteration.prototype = new Error();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   274
pyjslib.StopIteration.name = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   275
pyjslib.StopIteration.message = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   276
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   277
pyjslib.String_find = function(sub, start, end) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   278
    var pos=this.indexOf(sub, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   279
    if (pyjslib.isUndefined(end)) return pos;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   280
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   281
    if (pos + sub.length>end) return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   282
    return pos;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   283
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   284
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   285
pyjslib.String_join = function(data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   286
    var text="";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   287
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   288
    if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   289
        return data.join(this);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   290
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   291
    else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   292
        var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   293
        try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   294
            text+=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   295
            while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   296
                var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   297
                text+=this + item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   298
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   299
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   300
        catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   301
            if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   302
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   303
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   304
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   305
    return text;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   306
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   307
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   308
pyjslib.String_isdigit = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   309
    return (this.match(/^\d+$/g) != null);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   310
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   311
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   312
pyjslib.String_replace = function(old, replace, count) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   313
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   314
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   315
    var new_str="";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   316
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   317
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   318
    if (!pyjslib.isString(old)) return this.__replace(old, replace);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   319
    if (!pyjslib.isUndefined(count)) do_max=true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   320
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   321
    while (start<this.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   322
        if (do_max && !count--) break;
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
        pos=this.indexOf(old, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   325
        if (pos<0) break;
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
        new_str+=this.substring(start, pos) + replace;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   328
        start=pos+old.length;
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
    if (start<this.length) new_str+=this.substring(start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   331
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   332
    return new_str;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   333
}
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
pyjslib.String_split = function(sep, maxsplit) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   336
    var items=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   337
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   338
    var subject=this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   339
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   340
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   341
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   342
    if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   343
        sep=" ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   344
        subject=subject.strip();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   345
        subject=subject.replace(/\s+/g, sep);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   346
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   347
    else if (!pyjslib.isUndefined(maxsplit)) do_max=true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   348
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   349
    if (subject.length == 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   350
        return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   351
    }
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
    while (start<subject.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   354
        if (do_max && !maxsplit--) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   355
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   356
        pos=subject.indexOf(sep, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   357
        if (pos<0) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   358
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   359
        items.append(subject.substring(start, pos));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   360
        start=pos+sep.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   361
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   362
    if (start<=subject.length) items.append(subject.substring(start));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   363
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   364
    return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   365
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   366
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   367
pyjslib.String___iter__ = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   368
    var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   369
    var s = this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   370
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   371
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   372
            if (i >= s.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   373
                throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   374
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   375
            return s.substring(i++, i, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   376
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   377
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   378
            return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   379
        }
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
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   382
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   383
pyjslib.String_strip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   384
    return this.lstrip(chars).rstrip(chars);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   385
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   386
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   387
pyjslib.String_lstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   388
    if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   389
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   390
    return this.replace(new RegExp("^[" + chars + "]+"), "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   391
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   392
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   393
pyjslib.String_rstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   394
    if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   395
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   396
    return this.replace(new RegExp("[" + chars + "]+$"), "");
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   399
pyjslib.String_startswith = function(prefix, start) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   400
    if (pyjslib.isUndefined(start)) start = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   401
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   402
    if (this.substring(start, prefix.length) == prefix) return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   403
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   404
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   405
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   406
pyjslib.abs = Math.abs;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   407
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   408
""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   409
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   410
class Class:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   411
    def __init__(self, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   412
        self.name = name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   413
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   414
    def __str___(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   415
        return self.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   416
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   417
def eq(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   418
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   419
    if (pyjslib.hasattr(a, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   420
        return a.__cmp__(b) == 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   421
    } else if (pyjslib.hasattr(b, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   422
        return b.__cmp__(a) == 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   423
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   424
    return a == b;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   427
def cmp(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   428
    if hasattr(a, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   429
        return a.__cmp__(b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   430
    elif hasattr(b, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   431
        return -b.__cmp__(a)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   432
    if a > b:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   433
        return 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   434
    elif b > a:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   435
        return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   436
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   437
        return 0
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
def bool(v):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   440
    # this needs to stay in native code without any dependencies here,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   441
    # because this is used by if and while, we need to prevent
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   442
    # recursion
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   443
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   444
    if (!v) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   445
    switch(typeof v){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   446
    case 'boolean':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   447
        return v;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   448
    case 'object':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   449
        if (v.__nonzero__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   450
            return v.__nonzero__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   451
        }else if (v.__len__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   452
            return v.__len__()>0;
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
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   455
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   456
    return Boolean(v);
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   459
class List:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   460
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   461
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   462
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   463
        this.extend(data);
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   466
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   467
        JS("""    this.l[this.l.length] = item;""")
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 extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   470
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   471
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   472
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   473
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   474
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   475
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   476
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   477
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   478
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   479
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   480
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   481
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   482
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   483
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   484
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   485
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   486
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   487
                if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   488
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   489
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   490
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   491
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   492
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   493
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   494
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   495
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   496
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   497
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   498
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   499
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   500
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   501
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   502
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   503
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   504
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   505
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   506
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   507
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   508
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   509
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   510
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   511
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   512
        JS("""    var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   513
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   514
    def pop(self, index = -1):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   515
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   516
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   517
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   518
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   519
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   520
        """)
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
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   523
        if not isinstance(l, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   524
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   525
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   526
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   527
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   528
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   529
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   530
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   531
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   532
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   533
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   534
    def slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   535
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   536
        if (upper==null) return pyjslib.List(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   537
        return pyjslib.List(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   538
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   539
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   540
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   541
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   542
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   543
        return this.l[index];
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   546
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   547
        JS("""    this.l[index]=value;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   548
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   549
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   550
        JS("""    this.l.splice(index, 1);""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   551
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   552
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   553
        JS("""    return this.l.length;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   554
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   555
    def __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   556
        return self.index(value) >= 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   557
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   558
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   559
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   560
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   561
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   562
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   563
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   564
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   565
                    throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   566
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   567
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   568
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   569
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   570
                return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   571
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   572
        };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   573
        """)
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
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   576
        JS("""    this.l.reverse();""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   577
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   578
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   579
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   580
            global cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   581
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   582
        if keyFunc and reverse:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   583
            def thisSort1(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   584
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   585
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   586
        elif keyFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   587
            def thisSort2(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   588
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   589
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   590
        elif reverse:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   591
            def thisSort3(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   592
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   593
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   594
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   595
            self.l.sort(compareFunc)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   596
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   597
    def getArray(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   598
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   599
        Access the javascript Array that is used internally by this list
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   600
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   601
        return self.l
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   602
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   603
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   604
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   605
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   606
list = List
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   607
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   608
class Tuple:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   609
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   610
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   611
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   612
        this.extend(data);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   613
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   614
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   615
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   616
        JS("""    this.l[this.l.length] = item;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   617
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   618
    def extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   619
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   620
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   621
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   622
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   623
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   624
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   625
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   626
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   627
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   628
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   629
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   630
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   631
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   632
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   633
                    }
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
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   636
                if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   637
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   638
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   639
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   640
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   641
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   642
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   643
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   644
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   645
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   646
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   647
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   648
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   649
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   650
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   651
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   652
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   653
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   654
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   655
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   656
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   657
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   658
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   659
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   660
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   661
        JS("""    var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   662
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   663
    def pop(self, index = -1):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   664
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   665
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   666
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   667
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   668
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   669
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   670
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   671
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   672
        if not isinstance(l, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   673
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   674
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   675
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   676
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   677
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   678
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   679
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   680
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   681
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   682
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   683
    def slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   684
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   685
        if (upper==null) return pyjslib.Tuple(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   686
        return pyjslib.Tuple(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   687
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   688
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   689
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   690
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   691
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   692
        return this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   693
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   694
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   695
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   696
        JS("""    this.l[index]=value;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   697
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   698
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   699
        JS("""    this.l.splice(index, 1);""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   700
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   701
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   702
        JS("""    return this.l.length;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   703
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   704
    def __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   705
        return self.index(value) >= 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   706
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   707
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   708
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   709
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   710
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   711
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   712
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   713
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   714
                    throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   715
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   716
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   717
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   718
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   719
                return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   720
            }
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
        """)
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
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   725
        JS("""    this.l.reverse();""")
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
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   728
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   729
            global cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   730
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   731
        if keyFunc and reverse:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   732
            def thisSort1(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   733
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   734
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   735
        elif keyFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   736
            def thisSort2(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   737
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   738
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   739
        elif reverse:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   740
            def thisSort3(a,b):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   741
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   742
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   743
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   744
            self.l.sort(compareFunc)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   745
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   746
    def getArray(self):
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
        Access the javascript Array that is used internally by this list
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   749
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   750
        return self.l
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
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   753
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   754
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   755
tuple = Tuple
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   756
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   757
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   758
class Dict:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   759
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   760
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   761
        this.d = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   762
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   763
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   764
            for (var i in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   765
                var item=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   766
                this.__setitem__(item[0], item[1]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   767
                //var sKey=pyjslib.hash(item[0]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   768
                //this.d[sKey]=item[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   769
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   770
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   771
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   772
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   773
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   774
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   775
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   776
                    this.__setitem__(item.__getitem__(0), item.__getitem__(1));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   777
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   778
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   779
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   780
                if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   781
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   782
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   783
        else if (pyjslib.isObject(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   784
            for (var key in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   785
                this.__setitem__(key, data[key]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   786
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   787
            }
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   790
    def __setitem__(self, key, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   791
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   792
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   793
        this.d[sKey]=[key, value];
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   796
    def __getitem__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   797
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   798
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   799
        var value=this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   800
        if (pyjslib.isUndefined(value)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   801
            throw pyjslib.KeyError(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   802
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   803
        return value[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   804
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   805
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   806
    def __nonzero__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   807
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   808
        for (var i in this.d){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   809
            return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   810
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   811
        return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   812
        """)
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
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   815
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   816
        var size=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   817
        for (var i in this.d) size++;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   818
        return size;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   819
        """)
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
    def has_key(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   822
        return self.__contains__(key)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   823
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   824
    def __delitem__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   825
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   826
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   827
        delete this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   828
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   829
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   830
    def __contains__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   831
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   832
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   833
        return (pyjslib.isUndefined(this.d[sKey])) ? false : true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   834
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   835
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   836
    def keys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   837
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   838
        var keys=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   839
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   840
            keys.append(this.d[key][0]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   841
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   842
        return keys;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   843
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   844
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   845
    def values(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   846
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   847
        var values=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   848
        for (var key in this.d) values.append(this.d[key][1]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   849
        return values;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   852
    def items(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   853
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   854
        var items = new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   855
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   856
          var kv = this.d[key];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   857
          items.append(new pyjslib.List(kv))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   858
          }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   859
          return items;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   862
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   863
        return self.keys().__iter__()
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
    def iterkeys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   866
        return self.__iter__()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   867
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   868
    def itervalues(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   869
        return self.values().__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   870
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   871
    def iteritems(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   872
        return self.items().__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   873
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   874
    def setdefault(self, key, default_value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   875
        if not self.has_key(key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   876
            self[key] = default_value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   877
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   878
    def get(self, key, default_=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   879
        if not self.has_key(key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   880
            return default_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   881
        return self[key]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   882
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   883
    def update(self, d):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   884
        for k,v in d.iteritems():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   885
            self[k] = v
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
    def getObject(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   888
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   889
        Return the javascript Object which this class uses to store
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   890
        dictionary keys and values
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   891
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   892
        return self.d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   893
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   894
    def copy(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   895
        return Dict(self.items())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   896
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   897
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   898
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   899
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   900
dict = Dict
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   901
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   902
# taken from mochikit: range( [start,] stop[, step] )
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   903
def range():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   904
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   905
    var start = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   906
    var stop = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   907
    var step = 1;
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 (arguments.length == 2) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   910
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   911
        stop = arguments[1];
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
    else if (arguments.length == 3) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   914
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   915
        stop = arguments[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   916
        step = arguments[2];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   917
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   918
    else if (arguments.length>0) stop = arguments[0];
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
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   921
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   922
            if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   923
            var rval = start;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   924
            start += step;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   925
            return rval;
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
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   928
            return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   929
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   930
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   931
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   932
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   933
def slice(object, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   934
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   935
    if (pyjslib.isString(object)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   936
        if (lower < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   937
           lower = object.length + lower;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   938
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   939
        if (upper < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   940
           upper = object.length + upper;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   941
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   942
        if (pyjslib.isNull(upper)) upper=object.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   943
        return object.substring(lower, upper);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   944
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   945
    if (pyjslib.isObject(object) && object.slice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   946
        return object.slice(lower, upper);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   947
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   948
    return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   949
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   950
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   951
def str(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   952
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   953
    if (pyjslib.hasattr(text,"__str__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   954
        return text.__str__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   955
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   956
    return String(text);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   957
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   958
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   959
def ord(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   960
    if(isString(x) and len(x) is 1):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   961
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   962
            return x.charCodeAt(0);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   963
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   964
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   965
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   966
            throw pyjslib.TypeError();
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
    return None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   969
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   970
def chr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   971
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   972
        return String.fromCharCode(x)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   973
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   974
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   975
def is_basetype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   976
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   977
       var t = typeof(x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   978
       return t == 'boolean' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   979
       t == 'function' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   980
       t == 'number' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   981
       t == 'string' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   982
       t == 'undefined'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   983
       ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   984
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   985
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   986
def get_pyjs_classtype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   987
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   988
       if (pyjslib.hasattr(x, "__class__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   989
           if (pyjslib.hasattr(x.__class__, "__new__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   990
               var src = x.__class__.__name__;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   991
               return src;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   992
       return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   993
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   994
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   995
def repr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   996
    """ Return the string representation of 'x'.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   997
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   998
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   999
       if (x === null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1000
           return "null";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1001
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1002
       if (x === undefined)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1003
           return "undefined";
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
       var t = typeof(x);
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
        //alert("repr typeof " + t + " : " + x);
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
       if (t == "boolean")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1010
           return x.toString();
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
       if (t == "function")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1013
           return "<function " + x.toString() + ">";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1014
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1015
       if (t == "number")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1016
           return x.toString();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1017
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1018
       if (t == "string") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1019
           if (x.indexOf("'") == -1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1020
               return "'" + x + "'";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1021
           if (x.indexOf('"') == -1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1022
               return '"' + x + '"';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1023
           var s = x.replace(new RegExp('"', "g"), '\\\\"');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1024
           return '"' + s + '"';
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1027
       if (t == "undefined")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1028
           return "undefined";
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
       // If we get here, x is an object.  See if it's a Pyjamas class.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1031
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1032
       if (!pyjslib.hasattr(x, "__init__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1033
           return "<" + x.toString() + ">";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1034
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1035
       // Handle the common Pyjamas data types.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1036
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1037
       var constructor = "UNKNOWN";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1038
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1039
       constructor = pyjslib.get_pyjs_classtype(x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1040
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1041
        //alert("repr constructor: " + constructor);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1042
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1043
       if (constructor == "Tuple") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1044
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1045
           var s = "(";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1046
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1047
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1048
               if (i < contents.length - 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1049
                   s += ", ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1050
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1051
           s += ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1052
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1053
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1054
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1055
       if (constructor == "List") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1056
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1057
           var s = "[";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1058
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1059
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1060
               if (i < contents.length - 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1061
                   s += ", ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1062
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1063
           s += "]"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1064
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1065
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1066
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1067
       if (constructor == "Dict") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1068
           var keys = new Array();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1069
           for (var key in x.d)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1070
               keys.push(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1071
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1072
           var s = "{";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1073
           for (var i=0; i<keys.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1074
               var key = keys[i]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1075
               s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1076
               if (i < keys.length-1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1077
                   s += ", "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1078
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1079
           s += "}";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1080
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1081
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1082
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1083
       // If we get here, the class isn't one we know -> return the class name.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1084
       // Note that we replace underscores with dots so that the name will
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1085
       // (hopefully!) look like the original Python name.
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
       //var s = constructor.replace(new RegExp('_', "g"), '.');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1088
       return "<" + constructor + " object>";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1089
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1090
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1091
def float(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1092
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1093
    return parseFloat(text);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1094
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1095
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1096
def int(text, radix=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1097
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1098
    return parseInt(text, radix);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1099
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1100
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1101
def len(object):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1102
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1103
    if (object==null) return 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1104
    if (pyjslib.isObject(object) && object.__len__) return object.__len__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1105
    return object.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1106
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1107
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1108
def isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1109
    if pyjslib.isUndefined(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1110
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1111
    if not pyjslib.isObject(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1112
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1113
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1114
    if _isinstance(classinfo, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1115
        for ci in classinfo:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1116
            if isinstance(object_, ci):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1117
                return True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1118
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1119
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1120
        return _isinstance(object_, classinfo)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1121
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1122
def _isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1123
    if not pyjslib.isObject(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1124
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1125
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1126
    if (object_.__class__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1127
        var res =  object_ instanceof classinfo.constructor;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1128
        return res;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1129
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1130
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1131
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1132
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1133
def getattr(obj, name, default_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1134
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1135
    if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1136
        if (pyjslib.isUndefined(default_)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1137
            throw pyjslib.AttributeError(obj, name);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1138
        }else{
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1139
        return default_;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1140
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1141
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1142
    if (!pyjslib.isFunction(obj[name])) return obj[name];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1143
    var fnwrap = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1144
        var args = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1145
        for (var i = 0; i < arguments.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1146
          args.push(arguments[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1147
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1148
        return obj[name].apply(obj,args);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1149
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1150
    fnwrap.__name__ = name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1151
    return fnwrap;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1152
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1153
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1154
def setattr(obj, name, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1155
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1156
    if (!pyjslib.isObject(obj)) return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1157
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1158
    obj[name] = value;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1159
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1160
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1161
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1162
def hasattr(obj, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1163
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1164
    if (!pyjslib.isObject(obj)) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1165
    if (pyjslib.isUndefined(obj[name])) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1166
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1167
    return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1168
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1169
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1170
def dir(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1171
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1172
    var properties=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1173
    for (property in obj) properties.append(property);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1174
    return properties;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1175
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1176
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1177
def filter(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1178
    # object context is LOST when a method is passed, hence object must be passed separately
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1179
    # to emulate python behaviour, should generate this code inline rather than as a function call
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1180
    items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1181
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1182
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1183
        method = obj
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1184
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1185
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1186
            if method(item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1187
                items.append(item)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1188
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1189
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1190
            if method.call(obj, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1191
                items.append(item)
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
    return items
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1194
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
def map(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1197
    items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1198
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1199
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1200
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1201
        method = obj
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
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1204
            items.append(method(item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1205
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1206
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1207
            items.append(method.call(obj, item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1208
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1209
    return items
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1212
def enumerate(sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1213
    enumeration = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1214
    nextIndex = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1215
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1216
        enumeration.append([nextIndex, item])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1217
        nextIndex = nextIndex + 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1218
    return enumeration
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 min(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1222
    minValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1223
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1224
        if minValue is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1225
            minValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1226
        elif item < minValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1227
            minValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1228
    return minValue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1229
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1230
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1231
def max(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1232
    maxValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1233
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1234
        if maxValue is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1235
            maxValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1236
        elif item > maxValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1237
            maxValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1238
    return maxValue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1239
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1240
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1241
next_hash_id = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1242
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1243
def hash(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1244
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1245
    if (obj == null) return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1246
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1247
    if (obj.$H) return obj.$H;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1248
    if (obj.__hash__) return obj.__hash__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1249
    if (obj.constructor == String || obj.constructor == Number || obj.constructor == Date) return obj;
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
    obj.$H = ++pyjslib.next_hash_id;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1252
    return obj.$H;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1253
    """)
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1256
# type functions from Douglas Crockford's Remedial Javascript: http://www.crockford.com/javascript/remedial.html
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1257
def isObject(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1258
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1259
    return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1260
    """)
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
def isFunction(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1263
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1264
    return typeof a == 'function';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1265
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1266
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1267
def isString(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1268
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1269
    return typeof a == 'string';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1270
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1271
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1272
def isNull(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1273
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1274
    return typeof a == 'object' && !a;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1277
def isArray(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1278
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1279
    return pyjslib.isObject(a) && a.constructor == Array;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1282
def isUndefined(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1283
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1284
    return typeof a == 'undefined';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1285
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1286
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1287
def isIteratable(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1288
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1289
    return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1290
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1291
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1292
def isNumber(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1293
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1294
    return typeof a == 'number' && isFinite(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1295
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1296
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1297
def toJSObjects(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1298
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1299
       Convert the pyjs pythonic List and Dict objects into javascript Object and Array
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1300
       objects, recursively.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1301
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1302
    if isArray(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1303
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1304
        var result = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1305
        for(var k=0; k < x.length; k++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1306
           var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1307
           var tv = pyjslib.toJSObjects(v);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1308
           result.push(tv);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1309
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1310
        return result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1311
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1312
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1313
        if isinstance(x, Dict):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1314
            JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1315
            var o = x.getObject();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1316
            var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1317
            for (var i in o) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1318
               result[o[i][0].toString()] = o[i][1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1319
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1320
            return pyjslib.toJSObjects(result)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1321
            """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1322
        elif isinstance(x, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1323
            return toJSObjects(x.l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1324
        elif hasattr(x, '__class__'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1325
            # we do not have a special implementation for custom
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1326
            # classes, just pass it on
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1327
            return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1328
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1329
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1330
        var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1331
        for(var k in x) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1332
            var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1333
            var tv = pyjslib.toJSObjects(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1334
            result[k] = tv;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1335
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1336
            return result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1337
         """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1338
    return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1339
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1340
def printFunc(objs):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1341
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1342
    if ($wnd.console==undefined)  return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1343
    var s = "";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1344
    for(var i=0; i < objs.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1345
        if(s != "") s += " ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1346
        s += objs[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1347
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1348
    console.debug(s)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1349
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1350
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1351
def type(clsname, bases=None, methods=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1352
    """ creates a class, derived from bases, with methods and variables
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1353
    """
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
    JS(" var mths = {}; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1356
    if methods:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1357
        for k in methods.keys():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1358
            mth = methods[k]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1359
            JS(" mths[k] = mth; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1360
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1361
    JS(" var bss = null; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1362
    if bases:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1363
        JS("bss = bases.l;")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1364
    JS(" return pyjs_type(clsname, bss, mths); ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1365