svgui/pyjs/lib/pyjslib.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Wed, 11 Oct 2017 11:54:55 +0300
changeset 1867 418777c1fbc7
parent 1865 0bd5b3099144
child 1870 4d070115b552
permissions -rw-r--r--
fix pylint error "(no-name-in-module) No name 'X' in module 'Y'"
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    22
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
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
    24
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
        var cache_file;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
        if (module_name == "sys" || module_name == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
            /*module_load_request[module_name] = 1;*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
        if (path == null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
            path = './';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
        }
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
        var override_name = sys.platform + "." + module_name;
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    40
        if (((sys.overrides != null) &&
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
             (sys.overrides.has_key(override_name))))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
            cache_file =  sys.overrides.__getitem__(override_name) ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
            cache_file =  module_name ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
        }
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
        cache_file = (path + cache_file + '.cache.js' ) ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
        //alert("cache " + cache_file + " " + module_name + " " + parent_module);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
        /* already loaded? */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
        if (module_load_request[module_name])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
            if (module_load_request[module_name] >= 3 && parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
                //onload_fn = parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
                //pyjs_eval(onload_fn); /* set up the parent-module namespace */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
        if (typeof (module_load_request[module_name]) == 'undefined')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
            module_load_request[module_name] = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    69
        /* following a load, this first executes the script
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
         * "preparation" function MODULENAME_loaded_fn()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
         * and then sets up the loaded module in the namespace
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
         * of the parent.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
         */
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
        onload_fn = ''; // module_name + "_loaded_fn();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
        if (parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
            //onload_fn += parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
            /*pmod = parent_module + '.' + module_name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
            onload_fn += 'alert("' + pmod + '"+' + pmod+');';*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
        }
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
        if (dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
            /* this one tacks the script onto the end of the DOM
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
             */
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
            pyjs_load_script(cache_file, onload_fn, async);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
            /* this one actually RUNS the script (eval) into the page.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
               my feeling is that this would be better for non-async
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
               but i can't get it to work entirely yet.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
             */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
            /*pyjs_ajax_eval(cache_file, onload_fn, async);*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
            if (module_name != "pyjslib" &&
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
                module_name != "sys")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
                pyjs_eval(onload_fn);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
        }
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
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   107
371
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   187
1851
1b8b5324506c fix pylint warning '(old-style-class) Old-style class defined.'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   188
# pylint: disable=C1001
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   189
class Object:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   190
    pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   191
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   192
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
object = Object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   194
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   195
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
class Modload:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   197
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
    def __init__(self, path, app_modlist, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
                 parent_mod):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
        self.app_modlist = app_modlist
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
        self.app_imported_fn = app_imported_fn
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
        self.path = path
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   203
        self.idx = 0
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
        self.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   205
        self.parent_mod = parent_mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
    def next(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
   208
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   209
        for i in range(len(self.app_modlist[self.idx])):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   210
            app = self.app_modlist[self.idx][i]
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   211
            import_module(self.path, self.parent_mod, app, self.dynamic, True)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
        self.idx += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   213
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   214
        if self.idx >= len(self.app_modlist):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
            import_wait(self.app_imported_fn, self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
            import_wait(getattr(self, "next"), self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   219
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
def get_module(module_name):
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1841
diff changeset
   221
    _ev = "__mod = %s;" % module_name
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1841
diff changeset
   222
    JS("pyjs_eval(_ev);")
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   223
    return __mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   224
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   225
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   227
                        parent_mod=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   228
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   229
    loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   230
    loader.next()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   231
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   232
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   233
# as comment on line 20 says
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   234
# import sys should be below
1859
1df77c809257 fix pylint warning '(wrong-import-position) Import "import X" should be placed at the top of the module'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1854
diff changeset
   235
import sys  # noqa # pylint: disable=wrong-import-order,unused-import,wrong-import-position
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   236
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   237
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   238
class BaseException:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   239
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   240
    name = "BaseException"
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 __init__(self, *args):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   243
        self.args = args
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
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   246
        if len(self.args) is 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   247
            return ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   248
        elif len(self.args) is 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   249
            return repr(self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   250
        return repr(self.args)
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
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   253
        return str(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   254
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   255
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   256
class Exception(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   257
    name = "Exception"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   258
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   259
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   260
class TypeError(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   261
    name = "TypeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   262
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   263
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   264
class StandardError(Exception):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   265
    name = "StandardError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   266
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   267
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   268
class LookupError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   269
    name = "LookupError"
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
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   272
        return self.name + ": " + self.args[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   273
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   274
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   275
class KeyError(LookupError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   276
    name = "KeyError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   277
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   278
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   279
class AttributeError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   280
    name = "AttributeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   281
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   282
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   283
        return "AttributeError: %s of %s" % (self.args[1], self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   284
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   285
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   286
JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   287
pyjslib.StopIteration = function () { };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   288
pyjslib.StopIteration.prototype = new Error();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   289
pyjslib.StopIteration.name = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   290
pyjslib.StopIteration.message = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   291
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   292
pyjslib.String_find = function(sub, start, end) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   293
    var pos=this.indexOf(sub, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   294
    if (pyjslib.isUndefined(end)) return pos;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   295
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   296
    if (pos + sub.length>end) return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   297
    return pos;
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
pyjslib.String_join = function(data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   301
    var text="";
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
    if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   304
        return data.join(this);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   305
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   306
    else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   307
        var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   308
        try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   309
            text+=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   310
            while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   311
                var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   312
                text+=this + item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   313
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   314
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   315
        catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   316
            if (e != pyjslib.StopIteration) throw e;
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
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   319
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   320
    return text;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   321
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   322
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   323
pyjslib.String_isdigit = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   324
    return (this.match(/^\d+$/g) != null);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   325
}
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
pyjslib.String_replace = function(old, replace, count) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   328
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   329
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   330
    var new_str="";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   331
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   332
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   333
    if (!pyjslib.isString(old)) return this.__replace(old, replace);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   334
    if (!pyjslib.isUndefined(count)) do_max=true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   335
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   336
    while (start<this.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   337
        if (do_max && !count--) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   338
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   339
        pos=this.indexOf(old, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   340
        if (pos<0) break;
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
        new_str+=this.substring(start, pos) + replace;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   343
        start=pos+old.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   344
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   345
    if (start<this.length) new_str+=this.substring(start);
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
    return new_str;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   350
pyjslib.String_split = function(sep, maxsplit) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   351
    var items=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   352
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   353
    var subject=this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   354
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   355
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   356
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   357
    if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   358
        sep=" ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   359
        subject=subject.strip();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   360
        subject=subject.replace(/\s+/g, sep);
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
    else if (!pyjslib.isUndefined(maxsplit)) do_max=true;
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
    if (subject.length == 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   365
        return items;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   368
    while (start<subject.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   369
        if (do_max && !maxsplit--) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   370
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   371
        pos=subject.indexOf(sep, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   372
        if (pos<0) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   373
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   374
        items.append(subject.substring(start, pos));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   375
        start=pos+sep.length;
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
    if (start<=subject.length) items.append(subject.substring(start));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   378
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   379
    return items;
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
pyjslib.String___iter__ = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   383
    var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   384
    var s = this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   385
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   386
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   387
            if (i >= s.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   388
                throw pyjslib.StopIteration;
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 s.substring(i++, i, 1);
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
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   393
            return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   394
        }
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
}
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
pyjslib.String_strip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   399
    return this.lstrip(chars).rstrip(chars);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   400
}
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
pyjslib.String_lstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   403
    if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, "");
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
    return this.replace(new RegExp("^[" + chars + "]+"), "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   406
}
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
pyjslib.String_rstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   409
    if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   410
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   411
    return this.replace(new RegExp("[" + chars + "]+$"), "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   412
}
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
pyjslib.String_startswith = function(prefix, start) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   415
    if (pyjslib.isUndefined(start)) start = 0;
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
    if (this.substring(start, prefix.length) == prefix) return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   418
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   419
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   420
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   421
pyjslib.abs = Math.abs;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   422
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   425
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   426
class Class:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   427
    def __init__(self, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   428
        self.name = name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   429
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   430
    def __str___(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   431
        return self.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   432
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   433
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   434
def eq(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   435
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   436
    if (pyjslib.hasattr(a, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   437
        return a.__cmp__(b) == 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   438
    } else if (pyjslib.hasattr(b, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   439
        return b.__cmp__(a) == 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   440
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   441
    return a == b;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   442
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   443
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   444
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   445
def cmp(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   446
    if hasattr(a, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   447
        return a.__cmp__(b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   448
    elif hasattr(b, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   449
        return -b.__cmp__(a)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   450
    if a > b:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   451
        return 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   452
    elif b > a:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   453
        return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   454
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   455
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   456
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   457
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   458
def bool(v):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   459
    # this needs to stay in native code without any dependencies here,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   460
    # 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
   461
    # recursion
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   462
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   463
    if (!v) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   464
    switch(typeof v){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   465
    case 'boolean':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   466
        return v;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   467
    case 'object':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   468
        if (v.__nonzero__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   469
            return v.__nonzero__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   470
        }else if (v.__len__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   471
            return v.__len__()>0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   472
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   473
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   474
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   475
    return Boolean(v);
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   478
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   479
class List:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   480
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   481
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   482
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   483
        this.extend(data);
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
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   487
        JS("""    this.l[this.l.length] = item;""")
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
    def extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   490
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   491
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   492
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   493
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   494
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   495
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   496
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   497
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   498
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   499
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   500
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   501
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   502
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   503
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   504
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   505
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   506
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   507
                if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   508
                }
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   512
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   513
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   514
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   515
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   516
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   517
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   518
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   519
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   520
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   521
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   522
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   523
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   524
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   525
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   526
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   527
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   528
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   529
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   530
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   531
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   532
        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
   533
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   534
    def pop(self, index=-1):
371
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 (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   537
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   538
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   539
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   540
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   541
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   542
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   543
        if not isinstance(l, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   544
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   545
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   546
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   547
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   548
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   549
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   550
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   551
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   552
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   553
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   554
    def slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   555
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   556
        if (upper==null) return pyjslib.List(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   557
        return pyjslib.List(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   558
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   559
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   560
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   561
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   562
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   563
        return this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   564
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   565
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   566
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   567
        JS("""    this.l[index]=value;""")
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
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   570
        JS("""    this.l.splice(index, 1);""")
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
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   573
        JS("""    return this.l.length;""")
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 __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   576
        return self.index(value) >= 0
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 __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   579
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   580
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   581
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   582
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   583
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   584
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   585
                    throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   586
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   587
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   588
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   589
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   590
                return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   591
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   592
        };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   593
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   594
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   595
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   596
        JS("""    this.l.reverse();""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   597
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   598
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   599
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   600
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   601
        if keyFunc and reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   602
            def thisSort1(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   603
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   604
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   605
        elif keyFunc:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   606
            def thisSort2(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   607
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   608
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   609
        elif reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   610
            def thisSort3(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   611
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   612
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   613
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   614
            self.l.sort(compareFunc)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   615
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   616
    def getArray(self):
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
        Access the javascript Array that is used internally by this list
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   619
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   620
        return self.l
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   621
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   622
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   623
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   624
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   625
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   626
list = List
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   627
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   628
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   629
class Tuple:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   630
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   631
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   632
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   633
        this.extend(data);
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   636
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   637
        JS("""    this.l[this.l.length] = item;""")
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
    def extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   640
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   641
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   642
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   643
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   644
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   645
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   646
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   647
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   648
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   649
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   650
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   651
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   652
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   653
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   654
                    }
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
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   657
                if (e != pyjslib.StopIteration) throw e;
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   661
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   662
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   663
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   664
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   665
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   666
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   667
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   668
        """)
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
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   671
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   672
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   673
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   674
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   675
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   676
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   677
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   678
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   679
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   680
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   681
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   682
        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
   683
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   684
    def pop(self, index=-1):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   685
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   686
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   687
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   688
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   689
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   690
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   691
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   692
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   693
        if not isinstance(l, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   694
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   695
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   696
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   697
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   698
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   699
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   700
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   701
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   702
        return 0
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 slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   705
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   706
        if (upper==null) return pyjslib.Tuple(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   707
        return pyjslib.Tuple(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   708
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   709
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   710
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   711
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   712
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   713
        return this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   714
        """)
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
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   717
        JS("""    this.l[index]=value;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   718
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   719
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   720
        JS("""    this.l.splice(index, 1);""")
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
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   723
        JS("""    return this.l.length;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   724
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   725
    def __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   726
        return self.index(value) >= 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   727
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   728
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   729
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   730
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   731
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   732
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   733
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   734
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   735
                    throw pyjslib.StopIteration;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   736
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   737
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   738
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   739
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   740
                return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   741
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   742
        };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   743
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   744
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   745
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   746
        JS("""    this.l.reverse();""")
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
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   749
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   750
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   751
        if keyFunc and reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   752
            def thisSort1(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   753
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   754
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   755
        elif keyFunc:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   756
            def thisSort2(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   757
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   758
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   759
        elif reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   760
            def thisSort3(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   761
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   762
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   763
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   764
            self.l.sort(compareFunc)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   765
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   766
    def getArray(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   767
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   768
        Access the javascript Array that is used internally by this list
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
        return self.l
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   771
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   772
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   773
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   774
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   775
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   776
tuple = Tuple
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
class Dict:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   780
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   781
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   782
        this.d = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   783
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   784
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   785
            for (var i in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   786
                var item=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   787
                this.__setitem__(item[0], item[1]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   788
                //var sKey=pyjslib.hash(item[0]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   789
                //this.d[sKey]=item[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   790
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   791
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   792
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   793
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   794
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   795
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   796
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   797
                    this.__setitem__(item.__getitem__(0), item.__getitem__(1));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   798
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   799
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   800
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   801
                if (e != pyjslib.StopIteration) throw e;
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
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   804
        else if (pyjslib.isObject(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   805
            for (var key in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   806
                this.__setitem__(key, data[key]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   807
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   808
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   809
        """)
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
    def __setitem__(self, key, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   812
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   813
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   814
        this.d[sKey]=[key, value];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   815
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   816
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   817
    def __getitem__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   818
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   819
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   820
        var value=this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   821
        if (pyjslib.isUndefined(value)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   822
            throw pyjslib.KeyError(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
        return value[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   825
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   826
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   827
    def __nonzero__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   828
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   829
        for (var i in this.d){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   830
            return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   831
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   832
        return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   833
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   834
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   835
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   836
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   837
        var size=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   838
        for (var i in this.d) size++;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   839
        return size;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   840
        """)
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
    def has_key(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   843
        return self.__contains__(key)
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 __delitem__(self, key):
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 sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   848
        delete this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   849
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   850
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   851
    def __contains__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   852
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   853
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   854
        return (pyjslib.isUndefined(this.d[sKey])) ? false : true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   855
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   856
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   857
    def keys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   858
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   859
        var keys=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   860
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   861
            keys.append(this.d[key][0]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   862
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   863
        return keys;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   866
    def values(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   867
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   868
        var values=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   869
        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
   870
        return values;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   871
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   872
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   873
    def items(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   874
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   875
        var items = new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   876
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   877
          var kv = this.d[key];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   878
          items.append(new pyjslib.List(kv))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   879
          }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   880
          return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   881
        """)
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 __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   884
        return self.keys().__iter__()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   885
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   886
    def iterkeys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   887
        return self.__iter__()
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
    def itervalues(self):
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   890
        return self.values().__iter__()
371
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
    def iteritems(self):
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   893
        return self.items().__iter__()
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   894
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   895
    def setdefault(self, key, default_value):
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   896
        if key not in self:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   897
            self[key] = default_value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   898
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   899
    def get(self, key, default_=None):
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   900
        if key not in self:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   901
            return default_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   902
        return self[key]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   903
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   904
    def update(self, d):
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   905
        for k, v in d.iteritems():
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   906
            self[k] = v
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   907
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   908
    def getObject(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   909
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   910
        Return the javascript Object which this class uses to store
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   911
        dictionary keys and values
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
        return self.d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   914
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   915
    def copy(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   916
        return Dict(self.items())
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
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   919
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   920
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   921
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   922
dict = Dict
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   923
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   924
# taken from mochikit: range( [start,] stop[, step] )
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   925
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   926
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   927
def range():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   928
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   929
    var start = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   930
    var stop = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   931
    var step = 1;
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
    if (arguments.length == 2) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   934
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   935
        stop = arguments[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   936
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   937
    else if (arguments.length == 3) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   938
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   939
        stop = arguments[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   940
        step = arguments[2];
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
    else if (arguments.length>0) stop = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   943
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   944
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   945
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   946
            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
   947
            var rval = start;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   948
            start += step;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   949
            return rval;
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
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   952
            return this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   953
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   954
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   955
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   956
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   957
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   958
def slice(object, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   959
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   960
    if (pyjslib.isString(object)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   961
        if (lower < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   962
           lower = object.length + lower;
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
        if (upper < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   965
           upper = object.length + upper;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   966
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   967
        if (pyjslib.isNull(upper)) upper=object.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   968
        return object.substring(lower, upper);
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
    if (pyjslib.isObject(object) && object.slice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   971
        return object.slice(lower, upper);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   972
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   973
    return null;
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   976
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   977
def str(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   978
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   979
    if (pyjslib.hasattr(text,"__str__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   980
        return text.__str__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   981
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   982
    return String(text);
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   985
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   986
def ord(x):
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
   987
    if isString(x) and len(x) is 1:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   988
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   989
            return x.charCodeAt(0);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   990
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   991
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   992
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   993
            throw pyjslib.TypeError();
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
    return None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   996
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   997
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   998
def chr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   999
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1000
        return String.fromCharCode(x)
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1003
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1004
def is_basetype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1005
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1006
       var t = typeof(x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1007
       return t == 'boolean' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1008
       t == 'function' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1009
       t == 'number' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1010
       t == 'string' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1011
       t == 'undefined'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1012
       ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1013
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1014
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1015
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1016
def get_pyjs_classtype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1017
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1018
       if (pyjslib.hasattr(x, "__class__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1019
           if (pyjslib.hasattr(x.__class__, "__new__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1020
               var src = x.__class__.__name__;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1021
               return src;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1022
       return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1023
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1024
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1025
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1026
def repr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1027
    """ Return the string representation of 'x'.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1028
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1029
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1030
       if (x === null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1031
           return "null";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1032
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1033
       if (x === undefined)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1034
           return "undefined";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1035
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1036
       var t = typeof(x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1037
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1038
        //alert("repr typeof " + t + " : " + x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1039
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1040
       if (t == "boolean")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1041
           return x.toString();
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 (t == "function")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1044
           return "<function " + x.toString() + ">";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1045
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1046
       if (t == "number")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1047
           return x.toString();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1048
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1049
       if (t == "string") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1050
           if (x.indexOf("'") == -1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1051
               return "'" + x + "'";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1052
           if (x.indexOf('"') == -1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1053
               return '"' + x + '"';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1054
           var s = x.replace(new RegExp('"', "g"), '\\\\"');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1055
           return '"' + s + '"';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1056
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1057
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1058
       if (t == "undefined")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1059
           return "undefined";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1060
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1061
       // 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
  1062
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1063
       if (!pyjslib.hasattr(x, "__init__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1064
           return "<" + x.toString() + ">";
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
       // Handle the common Pyjamas data types.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1067
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1068
       var constructor = "UNKNOWN";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1069
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1070
       constructor = pyjslib.get_pyjs_classtype(x);
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
        //alert("repr constructor: " + constructor);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1073
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1074
       if (constructor == "Tuple") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1075
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1076
           var s = "(";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1077
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1078
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1079
               if (i < contents.length - 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1080
                   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
           s += ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1083
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1084
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1085
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1086
       if (constructor == "List") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1087
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1088
           var s = "[";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1089
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1090
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1091
               if (i < contents.length - 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1092
                   s += ", ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1093
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1094
           s += "]"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1095
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1096
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1097
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1098
       if (constructor == "Dict") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1099
           var keys = new Array();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1100
           for (var key in x.d)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1101
               keys.push(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1102
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1103
           var s = "{";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1104
           for (var i=0; i<keys.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1105
               var key = keys[i]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1106
               s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1107
               if (i < keys.length-1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1108
                   s += ", "
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1109
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1110
           s += "}";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1111
           return s;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1114
       // 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
  1115
       // 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
  1116
       // (hopefully!) look like the original Python name.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1117
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1118
       //var s = constructor.replace(new RegExp('_', "g"), '.');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1119
       return "<" + constructor + " object>";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1120
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1121
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1122
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1123
def float(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1124
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1125
    return parseFloat(text);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1126
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1127
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1128
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1129
def int(text, radix=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1130
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1131
    return parseInt(text, radix);
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1134
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1135
def len(object):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1136
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1137
    if (object==null) return 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1138
    if (pyjslib.isObject(object) && object.__len__) return object.__len__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1139
    return object.length;
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1142
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1143
def isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1144
    if pyjslib.isUndefined(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1145
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1146
    if not pyjslib.isObject(object_):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
  1147
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1148
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1149
    if _isinstance(classinfo, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1150
        for ci in classinfo:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1151
            if isinstance(object_, ci):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1152
                return True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1153
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1154
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1155
        return _isinstance(object_, classinfo)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1156
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1157
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1158
def _isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1159
    if not pyjslib.isObject(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1160
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1161
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1162
    if (object_.__class__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1163
        var res =  object_ instanceof classinfo.constructor;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1164
        return res;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1165
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1166
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1167
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1168
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1169
1865
0bd5b3099144 fix pylint warning "(no-value-for-parameter) No value for argument 'X' in function call"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1859
diff changeset
  1170
def getattr(obj, name, default_=None):
371
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
    if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1173
        if (pyjslib.isUndefined(default_)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1174
            throw pyjslib.AttributeError(obj, name);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1175
        }else{
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1176
        return default_;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1177
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1178
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1179
    if (!pyjslib.isFunction(obj[name])) return obj[name];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1180
    var fnwrap = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1181
        var args = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1182
        for (var i = 0; i < arguments.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1183
          args.push(arguments[i]);
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
        return obj[name].apply(obj,args);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1186
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1187
    fnwrap.__name__ = name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1188
    return fnwrap;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1189
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1190
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1191
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1192
def setattr(obj, name, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1193
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1194
    if (!pyjslib.isObject(obj)) return null;
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
    obj[name] = value;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1197
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
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1200
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1201
def hasattr(obj, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1202
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1203
    if (!pyjslib.isObject(obj)) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1204
    if (pyjslib.isUndefined(obj[name])) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1205
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1206
    return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1207
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1208
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1209
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1210
def dir(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1211
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1212
    var properties=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1213
    for (property in obj) properties.append(property);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1214
    return properties;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1215
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1216
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1217
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1218
def filter(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1219
    # 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
  1220
    # 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
  1221
    items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1222
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1223
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1224
        method = obj
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1225
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1226
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1227
            if method(item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1228
                items.append(item)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1229
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1230
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1231
            if method.call(obj, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1232
                items.append(item)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1233
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1234
    return items
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1235
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1236
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1237
def map(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1238
    items = []
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
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1241
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1242
        method = obj
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1243
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1244
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1245
            items.append(method(item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1246
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1247
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1248
            items.append(method.call(obj, item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1249
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1250
    return items
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1251
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1252
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1253
def enumerate(sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1254
    enumeration = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1255
    nextIndex = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1256
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1257
        enumeration.append([nextIndex, item])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1258
        nextIndex = nextIndex + 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1259
    return enumeration
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 min(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1263
    minValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1264
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1265
        if minValue is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1266
            minValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1267
        elif item < minValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1268
            minValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1269
    return minValue
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 max(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1273
    maxValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1274
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1275
        if maxValue is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1276
            maxValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1277
        elif item > maxValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1278
            maxValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1279
    return maxValue
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
next_hash_id = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1283
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1284
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1285
def hash(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1286
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1287
    if (obj == null) return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1288
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1289
    if (obj.$H) return obj.$H;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1290
    if (obj.__hash__) return obj.__hash__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1291
    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
  1292
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1293
    obj.$H = ++pyjslib.next_hash_id;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1294
    return obj.$H;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1298
# 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
  1299
def isObject(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1300
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1301
    return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1302
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1303
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1304
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1305
def isFunction(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1306
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1307
    return typeof a == 'function';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1308
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1309
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1310
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1311
def isString(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1312
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1313
    return typeof a == 'string';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1314
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1315
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1316
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1317
def isNull(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1318
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1319
    return typeof a == 'object' && !a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1320
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1321
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1322
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1323
def isArray(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1324
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1325
    return pyjslib.isObject(a) && a.constructor == Array;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1326
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1327
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1328
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1329
def isUndefined(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1330
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1331
    return typeof a == 'undefined';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1332
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1333
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1334
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1335
def isIteratable(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1336
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1337
    return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1338
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1339
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1340
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1341
def isNumber(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1342
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1343
    return typeof a == 'number' && isFinite(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1344
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1345
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1346
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1347
def toJSObjects(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1348
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1349
       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
  1350
       objects, recursively.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1351
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1352
    if isArray(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1353
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1354
        var result = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1355
        for(var k=0; k < x.length; k++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1356
           var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1357
           var tv = pyjslib.toJSObjects(v);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1358
           result.push(tv);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1359
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1360
        return result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1361
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1362
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1363
        if isinstance(x, Dict):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1364
            JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1365
            var o = x.getObject();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1366
            var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1367
            for (var i in o) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1368
               result[o[i][0].toString()] = o[i][1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1369
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1370
            return pyjslib.toJSObjects(result)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1371
            """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1372
        elif isinstance(x, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1373
            return toJSObjects(x.l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1374
        elif hasattr(x, '__class__'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1375
            # we do not have a special implementation for custom
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1376
            # classes, just pass it on
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1377
            return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1378
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1379
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1380
        var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1381
        for(var k in x) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1382
            var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1383
            var tv = pyjslib.toJSObjects(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1384
            result[k] = tv;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1385
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1386
            return result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1387
         """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1388
    return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1389
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1390
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1391
def printFunc(objs):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1392
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1393
    if ($wnd.console==undefined)  return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1394
    var s = "";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1395
    for(var i=0; i < objs.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1396
        if(s != "") s += " ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1397
        s += objs[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1398
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1399
    console.debug(s)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1400
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1401
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1402
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1403
def type(clsname, bases=None, methods=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1404
    """ creates a class, derived from bases, with methods and variables
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1405
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1406
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1407
    JS(" var mths = {}; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1408
    if methods:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1409
        for k in methods.keys():
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1841
diff changeset
  1410
            _mth = methods[k]
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1841
diff changeset
  1411
            JS(" mths[k] = _mth; ")
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1412
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1413
    JS(" var bss = null; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1414
    if bases:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1415
        JS("bss = bases.l;")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1416
    JS(" return pyjs_type(clsname, bss, mths); ")