svgui/pyjs/lib/pyjslib.py
author Edouard Tisserant
Fri, 28 Aug 2020 11:31:18 +0200
branchsvghmi
changeset 3047 c113904f0e62
parent 2455 80b225e9a6b6
permissions -rw-r--r--
Merged
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
2455
80b225e9a6b6 python3 support: pylint, W1653 # (next-method-defined) next method defined
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2454
diff changeset
    15
# pylint: disable=too-many-function-args,undefined-variable,no-absolute-import,assign-to-new-keyword,nonzero-method,next-method-called,next-method-defined
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    16
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
# iteration from Bob Ippolito's Iteration in JavaScript
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
from __pyjamas__ import JS
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
# must declare import _before_ importing sys
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    23
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
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
    25
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
        var cache_file;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
        if (module_name == "sys" || module_name == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
            /*module_load_request[module_name] = 1;*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
        if (path == null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
            path = './';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
        var override_name = sys.platform + "." + module_name;
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    41
        if (((sys.overrides != null) &&
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
             (sys.overrides.has_key(override_name))))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
            cache_file =  sys.overrides.__getitem__(override_name) ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
            cache_file =  module_name ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
        cache_file = (path + cache_file + '.cache.js' ) ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
        //alert("cache " + cache_file + " " + module_name + " " + parent_module);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
        /* already loaded? */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
        if (module_load_request[module_name])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
            if (module_load_request[module_name] >= 3 && parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
                //onload_fn = parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
                //pyjs_eval(onload_fn); /* set up the parent-module namespace */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
            return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
        if (typeof (module_load_request[module_name]) == 'undefined')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
            module_load_request[module_name] = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    70
        /* following a load, this first executes the script
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
         * "preparation" function MODULENAME_loaded_fn()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
         * and then sets up the loaded module in the namespace
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
         * of the parent.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
         */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
        onload_fn = ''; // module_name + "_loaded_fn();"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
        if (parent_module != null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
            //onload_fn += parent_module + '.' + module_name + ' = ' + module_name + ';';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
            /*pmod = parent_module + '.' + module_name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
            onload_fn += 'alert("' + pmod + '"+' + pmod+');';*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
        if (dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
            /* this one tacks the script onto the end of the DOM
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
             */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
            pyjs_load_script(cache_file, onload_fn, async);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
            /* this one actually RUNS the script (eval) into the page.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
               my feeling is that this would be better for non-async
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
               but i can't get it to work entirely yet.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
             */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
            /*pyjs_ajax_eval(cache_file, onload_fn, async);*/
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
            if (module_name != "pyjslib" &&
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
                module_name != "sys")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
                pyjs_eval(onload_fn);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
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
   108
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
function import_wait(proceed_fn, parent_mod, dynamic) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   111
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
    var data = '';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
    var element = $doc.createElement("div");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
    $doc.body.appendChild(element);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
    function write_dom(txt) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
        element.innerHTML = txt + '<br />';
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
    var timeoutperiod = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
    if (dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   121
        var timeoutperiod = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   122
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   123
    var wait = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   125
        var status = '';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
        for (l in module_load_request)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
            var m = module_load_request[l];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   129
            if (l == "sys" || l == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
                continue;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   131
            status += l + m + " ";
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
        //write_dom( " import wait " + wait_count + " " + status + " parent_mod " + parent_mod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
        wait_count += 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   136
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   137
        if (status == '')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   138
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   139
            setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   140
            return;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   143
        for (l in module_load_request)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   144
        {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   145
            var m = module_load_request[l];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   146
            if (l == "sys" || l == 'pyjslib')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   147
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   148
                module_load_request[l] = 4;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   149
                continue;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   150
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   151
            if ((parent_mod != null) && (l == parent_mod))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   152
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   153
                if (m == 1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   154
                {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   155
                    setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   156
                    return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   157
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   158
                if (m == 2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   159
                {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   160
                    /* cheat and move app on to next stage */
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   161
                    module_load_request[l] = 3;
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
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   164
            if (m == 1 || m == 2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   165
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   166
                setTimeout(wait, timeoutperiod);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   167
                return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   168
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   169
            if (m == 3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   170
            {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   171
                //alert("waited for module " + l + ": loaded");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   172
                module_load_request[l] = 4;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   173
                mod_fn = modules[l];
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
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   176
        //alert("module wait done");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   177
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   178
        if (proceed_fn.importDone)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   179
            proceed_fn.importDone(proceed_fn);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   180
        else
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   181
            proceed_fn();
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   184
    wait();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   185
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   186
""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   187
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   188
1873
e69d7e5aff00 fix pylint warning '(no-init) Class has no __init__ method'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1872
diff changeset
   189
# pylint: disable=old-style-class
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   190
class Object:
1873
e69d7e5aff00 fix pylint warning '(no-init) Class has no __init__ method'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1872
diff changeset
   191
    def __init__(self):
e69d7e5aff00 fix pylint warning '(no-init) Class has no __init__ method'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1872
diff changeset
   192
        pass
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
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
   194
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   195
object = Object
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   197
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
class Modload:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
    def __init__(self, path, app_modlist, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
                 parent_mod):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
        self.app_modlist = app_modlist
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   203
        self.app_imported_fn = app_imported_fn
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
        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
   205
        self.idx = 0
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
        self.dynamic = dynamic
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
        self.parent_mod = parent_mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   208
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   209
    def next(self):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
   210
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   211
        for i in range(len(self.app_modlist[self.idx])):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
            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
   213
            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
   214
        self.idx += 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
        if self.idx >= len(self.app_modlist):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
            import_wait(self.app_imported_fn, self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   219
            import_wait(getattr(self, "next"), self.parent_mod, self.dynamic)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   221
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   222
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
   223
    _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
   224
    JS("pyjs_eval(_ev);")
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   225
    return __mod
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   227
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   228
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   229
                        parent_mod=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   230
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   231
    loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   232
    loader.next()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   233
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
   234
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
   235
# 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
   236
# 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
   237
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
   238
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   239
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   240
class 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
    name = "BaseException"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   243
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   244
    def __init__(self, *args):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   245
        self.args = args
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   246
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   247
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   248
        if len(self.args) is 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   249
            return ''
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   250
        elif len(self.args) is 1:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   251
            return repr(self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   252
        return repr(self.args)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   253
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   254
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   255
        return str(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   256
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   257
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   258
class Exception(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   259
    name = "Exception"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   260
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   261
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   262
class TypeError(BaseException):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   263
    name = "TypeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   264
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   265
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   266
class StandardError(Exception):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   267
    name = "StandardError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   268
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   269
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   270
class LookupError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   271
    name = "LookupError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   272
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   273
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   274
        return self.name + ": " + self.args[0]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   275
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   276
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   277
class KeyError(LookupError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   278
    name = "KeyError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   279
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   280
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   281
class AttributeError(StandardError):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   282
    name = "AttributeError"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   283
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   284
    def toString(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   285
        return "AttributeError: %s of %s" % (self.args[1], self.args[0])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   286
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
   287
2439
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2411
diff changeset
   288
JS(r"""
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   289
pyjslib.StopIteration = function () { };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   290
pyjslib.StopIteration.prototype = new Error();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   291
pyjslib.StopIteration.name = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   292
pyjslib.StopIteration.message = 'StopIteration';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   293
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   294
pyjslib.String_find = function(sub, start, end) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   295
    var pos=this.indexOf(sub, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   296
    if (pyjslib.isUndefined(end)) return pos;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   297
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   298
    if (pos + sub.length>end) return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   299
    return pos;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   300
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   301
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   302
pyjslib.String_join = function(data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   303
    var text="";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   304
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   305
    if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   306
        return data.join(this);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   307
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   308
    else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   309
        var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   310
        try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   311
            text+=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   312
            while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   313
                var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   314
                text+=this + item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   315
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   316
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   317
        catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   318
            if (e != pyjslib.StopIteration) throw e;
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
    }
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
    return text;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   323
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   324
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   325
pyjslib.String_isdigit = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   326
    return (this.match(/^\d+$/g) != null);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   327
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   328
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   329
pyjslib.String_replace = function(old, replace, count) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   330
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   331
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   332
    var new_str="";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   333
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   334
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   335
    if (!pyjslib.isString(old)) return this.__replace(old, replace);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   336
    if (!pyjslib.isUndefined(count)) do_max=true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   337
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   338
    while (start<this.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   339
        if (do_max && !count--) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   340
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   341
        pos=this.indexOf(old, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   342
        if (pos<0) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   343
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   344
        new_str+=this.substring(start, pos) + replace;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   345
        start=pos+old.length;
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
    if (start<this.length) new_str+=this.substring(start);
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
    return new_str;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   350
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   351
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   352
pyjslib.String_split = function(sep, maxsplit) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   353
    var items=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   354
    var do_max=false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   355
    var subject=this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   356
    var start=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   357
    var pos=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   358
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   359
    if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   360
        sep=" ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   361
        subject=subject.strip();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   362
        subject=subject.replace(/\s+/g, sep);
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
    else if (!pyjslib.isUndefined(maxsplit)) do_max=true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   365
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   366
    if (subject.length == 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   367
        return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   368
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   369
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   370
    while (start<subject.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   371
        if (do_max && !maxsplit--) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   372
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   373
        pos=subject.indexOf(sep, start);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   374
        if (pos<0) break;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   375
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   376
        items.append(subject.substring(start, pos));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   377
        start=pos+sep.length;
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
    if (start<=subject.length) items.append(subject.substring(start));
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
    return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   382
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   383
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   384
pyjslib.String___iter__ = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   385
    var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   386
    var s = this;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   387
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   388
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   389
            if (i >= s.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   390
                throw pyjslib.StopIteration;
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
            return s.substring(i++, i, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   393
        },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   394
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   395
            return this;
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
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   399
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   400
pyjslib.String_strip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   401
    return this.lstrip(chars).rstrip(chars);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   402
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   403
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   404
pyjslib.String_lstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   405
    if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, "");
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
    return this.replace(new RegExp("^[" + chars + "]+"), "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   408
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   409
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   410
pyjslib.String_rstrip = function(chars) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   411
    if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, "");
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
    return this.replace(new RegExp("[" + chars + "]+$"), "");
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   414
}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   415
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   416
pyjslib.String_startswith = function(prefix, start) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   417
    if (pyjslib.isUndefined(start)) start = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   418
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   419
    if (this.substring(start, prefix.length) == prefix) return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   420
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   421
}
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
pyjslib.abs = Math.abs;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   424
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   425
""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   426
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   427
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   428
class Class:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   429
    def __init__(self, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   430
        self.name = name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   431
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   432
    def __str___(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   433
        return self.name
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   434
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   435
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   436
def eq(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   437
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   438
    if (pyjslib.hasattr(a, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   439
        return a.__cmp__(b) == 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   440
    } else if (pyjslib.hasattr(b, "__cmp__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   441
        return b.__cmp__(a) == 0;
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
    return a == b;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   444
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   445
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   446
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   447
def cmp(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   448
    if hasattr(a, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   449
        return a.__cmp__(b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   450
    elif hasattr(b, "__cmp__"):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   451
        return -b.__cmp__(a)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   452
    if a > b:
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
    elif b > a:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   455
        return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   456
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   457
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   458
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   459
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   460
def bool(v):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   461
    # this needs to stay in native code without any dependencies here,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   462
    # 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
   463
    # recursion
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   464
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   465
    if (!v) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   466
    switch(typeof v){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   467
    case 'boolean':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   468
        return v;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   469
    case 'object':
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   470
        if (v.__nonzero__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   471
            return v.__nonzero__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   472
        }else if (v.__len__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   473
            return v.__len__()>0;
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 true;
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
    return Boolean(v);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   478
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   479
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   480
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   481
class List:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   482
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   483
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   484
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   485
        this.extend(data);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   486
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   487
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   488
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   489
        JS("""    this.l[this.l.length] = item;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   490
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   491
    def extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   492
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   493
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   494
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   495
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   496
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   497
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   498
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   499
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   500
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   501
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   502
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   503
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   504
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   505
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   506
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   507
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   508
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   509
                if (e != pyjslib.StopIteration) throw e;
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   513
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   514
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   515
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   516
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   517
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   518
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   519
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   520
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   521
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   522
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   523
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   524
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   525
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   526
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   527
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   528
                }
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
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   531
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   532
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   533
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   534
        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
   535
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   536
    def pop(self, index=-1):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   537
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   538
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   539
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   540
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   541
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   542
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   543
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   544
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   545
        if not isinstance(l, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   546
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   547
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   548
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   549
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   550
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   551
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   552
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   553
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   554
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   555
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   556
    def slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   557
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   558
        if (upper==null) return pyjslib.List(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   559
        return pyjslib.List(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   560
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   561
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   562
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   563
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   564
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   565
        return this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   566
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   567
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   568
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   569
        JS("""    this.l[index]=value;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   570
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   571
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   572
        JS("""    this.l.splice(index, 1);""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   573
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   574
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   575
        JS("""    return this.l.length;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   576
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   577
    def __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   578
        return self.index(value) >= 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   579
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   580
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   581
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   582
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   583
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   584
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   585
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   586
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   587
                    throw pyjslib.StopIteration;
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
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   590
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   591
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   592
                return this;
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   596
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   597
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   598
        JS("""    this.l.reverse();""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   599
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   600
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   601
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   602
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   603
        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
   604
            def thisSort1(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   605
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   606
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   607
        elif keyFunc:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   608
            def thisSort2(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   609
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   610
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   611
        elif reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   612
            def thisSort3(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   613
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   614
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   615
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   616
            self.l.sort(compareFunc)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   617
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   618
    def getArray(self):
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
        Access the javascript Array that is used internally by this list
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
        return self.l
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   623
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   624
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   625
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   626
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
   627
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   628
list = List
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   629
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   630
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   631
class Tuple:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   632
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   633
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   634
        this.l = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   635
        this.extend(data);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   636
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   637
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   638
    def append(self, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   639
        JS("""    this.l[this.l.length] = item;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   640
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   641
    def extend(self, data):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   642
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   643
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   644
            n = this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   645
            for (var i=0; i < data.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   646
                this.l[n+i]=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   647
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   648
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   649
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   650
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   651
            var i=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   652
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   653
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   654
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   655
                    this.l[i++]=item;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   656
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   657
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   658
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   659
                if (e != pyjslib.StopIteration) throw e;
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   663
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   664
    def remove(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   665
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   666
        var index=this.index(value);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   667
        if (index<0) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   668
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   669
        return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   670
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   671
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   672
    def index(self, value, start=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   673
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   674
        var length=this.l.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   675
        for (var i=start; i<length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   676
            if (this.l[i]==value) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   677
                return i;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   678
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   679
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   680
        return -1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   681
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   682
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   683
    def insert(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   684
        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
   685
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   686
    def pop(self, index=-1):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   687
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   688
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   689
        var a = this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   690
        this.l.splice(index, 1);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   691
        return a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   692
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   693
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   694
    def __cmp__(self, l):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   695
        if not isinstance(l, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   696
            return -1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   697
        ll = len(self) - len(l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   698
        if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   699
            return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   700
        for x in range(len(l)):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   701
            ll = cmp(self.__getitem__(x), l[x])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   702
            if ll != 0:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   703
                return ll
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   704
        return 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   705
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   706
    def slice(self, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   707
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   708
        if (upper==null) return pyjslib.Tuple(this.l.slice(lower));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   709
        return pyjslib.Tuple(this.l.slice(lower, upper));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   710
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   711
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   712
    def __getitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   713
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   714
        if (index<0) index = this.l.length + index;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   715
        return this.l[index];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   716
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   717
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   718
    def __setitem__(self, index, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   719
        JS("""    this.l[index]=value;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   720
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   721
    def __delitem__(self, index):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   722
        JS("""    this.l.splice(index, 1);""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   723
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   724
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   725
        JS("""    return this.l.length;""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   726
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   727
    def __contains__(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   728
        return self.index(value) >= 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   729
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   730
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   731
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   732
        var i = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   733
        var l = this.l;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   734
        return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   735
            'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   736
                if (i >= l.length) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   737
                    throw pyjslib.StopIteration;
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
                return l[i++];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   740
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   741
            '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   742
                return this;
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   746
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   747
    def reverse(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   748
        JS("""    this.l.reverse();""")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   749
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   750
    def sort(self, compareFunc=None, keyFunc=None, reverse=False):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   751
        if not compareFunc:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   752
            compareFunc = cmp
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   753
        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
   754
            def thisSort1(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   755
                return -compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   756
            self.l.sort(thisSort1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   757
        elif keyFunc:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   758
            def thisSort2(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   759
                return compareFunc(keyFunc(a), keyFunc(b))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   760
            self.l.sort(thisSort2)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   761
        elif reverse:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   762
            def thisSort3(a, b):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   763
                return -compareFunc(a, b)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   764
            self.l.sort(thisSort3)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   765
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   766
            self.l.sort(compareFunc)
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
    def getArray(self):
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
        Access the javascript Array that is used internally by this list
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
        return self.l
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   773
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   774
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   775
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   776
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
   777
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   778
tuple = Tuple
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   779
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   780
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   781
class Dict:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   782
    def __init__(self, data=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   783
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   784
        this.d = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   785
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   786
        if (pyjslib.isArray(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   787
            for (var i in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   788
                var item=data[i];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   789
                this.__setitem__(item[0], item[1]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   790
                //var sKey=pyjslib.hash(item[0]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   791
                //this.d[sKey]=item[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   792
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   793
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   794
        else if (pyjslib.isIteratable(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   795
            var iter=data.__iter__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   796
            try {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   797
                while (true) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   798
                    var item=iter.next();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   799
                    this.__setitem__(item.__getitem__(0), item.__getitem__(1));
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   800
                    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   801
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   802
            catch (e) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   803
                if (e != pyjslib.StopIteration) throw e;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   804
                }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   805
            }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   806
        else if (pyjslib.isObject(data)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   807
            for (var key in data) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   808
                this.__setitem__(key, data[key]);
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
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   812
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   813
    def __setitem__(self, key, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   814
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   815
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   816
        this.d[sKey]=[key, value];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   817
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   818
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   819
    def __getitem__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   820
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   821
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   822
        var value=this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   823
        if (pyjslib.isUndefined(value)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   824
            throw pyjslib.KeyError(key);
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
        return value[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   827
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   828
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   829
    def __nonzero__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   830
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   831
        for (var i in this.d){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   832
            return true;
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
        return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   835
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   836
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   837
    def __len__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   838
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   839
        var size=0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   840
        for (var i in this.d) size++;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   841
        return size;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   842
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   843
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   844
    def has_key(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   845
        return self.__contains__(key)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   846
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   847
    def __delitem__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   848
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   849
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   850
        delete this.d[sKey];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   851
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   852
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   853
    def __contains__(self, key):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   854
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   855
        var sKey = pyjslib.hash(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   856
        return (pyjslib.isUndefined(this.d[sKey])) ? false : true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   857
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   858
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   859
    def keys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   860
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   861
        var keys=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   862
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   863
            keys.append(this.d[key][0]);
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
        return keys;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   866
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   867
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   868
    def values(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   869
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   870
        var values=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   871
        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
   872
        return values;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   873
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   874
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   875
    def items(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   876
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   877
        var items = new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   878
        for (var key in this.d) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   879
          var kv = this.d[key];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   880
          items.append(new pyjslib.List(kv))
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
          return items;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   883
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   884
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   885
    def __iter__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   886
        return self.keys().__iter__()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   887
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   888
    def iterkeys(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   889
        return self.__iter__()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   890
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   891
    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
   892
        return self.values().__iter__()
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   893
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   894
    def iteritems(self):
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   895
        return self.items().__iter__()
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   896
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   897
    def 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
   898
        if key not in self:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   899
            self[key] = default_value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   900
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   901
    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
   902
        if key not in self:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   903
            return default_
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   904
        return self[key]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   905
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   906
    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
   907
        for k, v in d.iteritems():
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   908
            self[k] = v
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
    def getObject(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   911
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   912
        Return the javascript Object which this class uses to store
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   913
        dictionary keys and values
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
        return self.d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   916
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   917
    def copy(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   918
        return Dict(self.items())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   919
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   920
    def __str__(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   921
        return repr(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   922
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
   923
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   924
dict = Dict
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   925
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   926
# 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
   927
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   928
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   929
def range():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   930
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   931
    var start = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   932
    var stop = 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   933
    var step = 1;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   934
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   935
    if (arguments.length == 2) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   936
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   937
        stop = arguments[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   938
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   939
    else if (arguments.length == 3) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   940
        start = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   941
        stop = arguments[1];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   942
        step = arguments[2];
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
    else if (arguments.length>0) stop = arguments[0];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   945
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   946
    return {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   947
        'next': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   948
            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
   949
            var rval = start;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   950
            start += step;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   951
            return rval;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   952
            },
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   953
        '__iter__': function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   954
            return this;
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
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   957
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   958
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   959
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   960
def slice(object, lower, upper):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   961
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   962
    if (pyjslib.isString(object)) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   963
        if (lower < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   964
           lower = object.length + lower;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   965
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   966
        if (upper < 0) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   967
           upper = object.length + upper;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   968
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   969
        if (pyjslib.isNull(upper)) upper=object.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   970
        return object.substring(lower, upper);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   971
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   972
    if (pyjslib.isObject(object) && object.slice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   973
        return object.slice(lower, upper);
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
    return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   976
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   977
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   978
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   979
def str(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   980
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   981
    if (pyjslib.hasattr(text,"__str__")) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   982
        return text.__str__();
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
    return String(text);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   985
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   986
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   987
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   988
def ord(x):
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
   989
    if isString(x) and len(x) is 1:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   990
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   991
            return x.charCodeAt(0);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   992
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   993
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   994
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   995
            throw pyjslib.TypeError();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   996
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   997
    return None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   998
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   999
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1000
def chr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1001
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1002
        return String.fromCharCode(x)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1003
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1004
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1005
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1006
def is_basetype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1007
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1008
       var t = typeof(x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1009
       return t == 'boolean' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1010
       t == 'function' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1011
       t == 'number' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1012
       t == 'string' ||
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1013
       t == 'undefined'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1014
       ;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1015
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1016
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1017
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1018
def get_pyjs_classtype(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1019
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1020
       if (pyjslib.hasattr(x, "__class__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1021
           if (pyjslib.hasattr(x.__class__, "__new__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1022
               var src = x.__class__.__name__;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1023
               return src;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1024
       return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1025
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1026
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1027
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1028
def repr(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1029
    """ Return the string representation of 'x'.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1030
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1031
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1032
       if (x === null)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1033
           return "null";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1034
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1035
       if (x === undefined)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1036
           return "undefined";
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
       var t = typeof(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
        //alert("repr typeof " + t + " : " + x);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1041
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1042
       if (t == "boolean")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1043
           return x.toString();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1044
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1045
       if (t == "function")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1046
           return "<function " + x.toString() + ">";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1047
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1048
       if (t == "number")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1049
           return x.toString();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1050
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1051
       if (t == "string") {
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
           if (x.indexOf('"') == -1)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1055
               return '"' + x + '"';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1056
           var s = x.replace(new RegExp('"', "g"), '\\\\"');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1057
           return '"' + s + '"';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1058
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1059
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1060
       if (t == "undefined")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1061
           return "undefined";
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 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
  1064
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1065
       if (!pyjslib.hasattr(x, "__init__"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1066
           return "<" + x.toString() + ">";
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
       // Handle the common Pyjamas data types.
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
       var constructor = "UNKNOWN";
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
       constructor = pyjslib.get_pyjs_classtype(x);
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
        //alert("repr constructor: " + constructor);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1075
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1076
       if (constructor == "Tuple") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1077
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1078
           var s = "(";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1079
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1080
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1081
               if (i < contents.length - 1)
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
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1084
           s += ")"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1085
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1086
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1087
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1088
       if (constructor == "List") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1089
           var contents = x.getArray();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1090
           var s = "[";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1091
           for (var i=0; i < contents.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1092
               s += pyjslib.repr(contents[i]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1093
               if (i < contents.length - 1)
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
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1096
           s += "]"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1097
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1098
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1099
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1100
       if (constructor == "Dict") {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1101
           var keys = new Array();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1102
           for (var key in x.d)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1103
               keys.push(key);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1104
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1105
           var s = "{";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1106
           for (var i=0; i<keys.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1107
               var key = keys[i]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1108
               s += pyjslib.repr(key) + ": " + pyjslib.repr(x.d[key]);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1109
               if (i < keys.length-1)
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
           };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1112
           s += "}";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1113
           return s;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1114
       };
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1115
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1116
       // 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
  1117
       // 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
  1118
       // (hopefully!) look like the original Python name.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1119
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1120
       //var s = constructor.replace(new RegExp('_', "g"), '.');
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1121
       return "<" + constructor + " object>";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1122
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1123
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1124
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1125
def float(text):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1126
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1127
    return parseFloat(text);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1128
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1129
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1130
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1131
def int(text, radix=0):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1132
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1133
    return parseInt(text, radix);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1134
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1135
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1136
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1137
def len(object):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1138
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1139
    if (object==null) return 0;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1140
    if (pyjslib.isObject(object) && object.__len__) return object.__len__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1141
    return object.length;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1142
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1143
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1144
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1145
def isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1146
    if pyjslib.isUndefined(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1147
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1148
    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
  1149
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1150
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1151
    if _isinstance(classinfo, Tuple):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1152
        for ci in classinfo:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1153
            if isinstance(object_, ci):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1154
                return True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1155
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1156
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1157
        return _isinstance(object_, classinfo)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1158
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1159
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1160
def _isinstance(object_, classinfo):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1161
    if not pyjslib.isObject(object_):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1162
        return False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1163
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1164
    if (object_.__class__){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1165
        var res =  object_ instanceof classinfo.constructor;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1166
        return res;
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
    return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1169
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1170
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1171
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
  1172
def getattr(obj, name, default_=None):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1173
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1174
    if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1175
        if (pyjslib.isUndefined(default_)){
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1176
            throw pyjslib.AttributeError(obj, name);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1177
        }else{
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1178
        return default_;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1179
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1180
    }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1181
    if (!pyjslib.isFunction(obj[name])) return obj[name];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1182
    var fnwrap = function() {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1183
        var args = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1184
        for (var i = 0; i < arguments.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1185
          args.push(arguments[i]);
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
        return obj[name].apply(obj,args);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1188
        }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1189
    fnwrap.__name__ = name;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1190
    return fnwrap;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1191
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1192
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1193
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1194
def setattr(obj, name, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1195
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1196
    if (!pyjslib.isObject(obj)) return null;
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
    obj[name] = value;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1199
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1200
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1201
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1202
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1203
def hasattr(obj, name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1204
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1205
    if (!pyjslib.isObject(obj)) return false;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1206
    if (pyjslib.isUndefined(obj[name])) return false;
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
    return true;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1209
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1210
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1211
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1212
def dir(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1213
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1214
    var properties=new pyjslib.List();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1215
    for (property in obj) properties.append(property);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1216
    return properties;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1217
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1218
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1219
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1220
def filter(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1221
    # 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
  1222
    # 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
  1223
    items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1224
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1225
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1226
        method = obj
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1227
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1228
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1229
            if method(item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1230
                items.append(item)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1231
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1232
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1233
            if method.call(obj, item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1234
                items.append(item)
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
    return items
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1237
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1238
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1239
def map(obj, method, sequence=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1240
    items = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1241
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1242
    if sequence is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1243
        sequence = method
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1244
        method = obj
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1245
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1246
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1247
            items.append(method(item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1248
    else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1249
        for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1250
            items.append(method.call(obj, item))
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
    return items
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1253
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1254
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1255
def enumerate(sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1256
    enumeration = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1257
    nextIndex = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1258
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1259
        enumeration.append([nextIndex, item])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1260
        nextIndex = nextIndex + 1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1261
    return enumeration
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1262
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1263
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1264
def min(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1265
    minValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1266
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1267
        if minValue is None:
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
        elif item < minValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1270
            minValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1271
    return minValue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1272
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1273
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1274
def max(*sequence):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1275
    maxValue = None
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1276
    for item in sequence:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1277
        if maxValue is None:
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
        elif item > maxValue:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1280
            maxValue = item
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1281
    return maxValue
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1282
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1283
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1284
next_hash_id = 0
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1285
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1286
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1287
def hash(obj):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1288
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1289
    if (obj == null) return null;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1290
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1291
    if (obj.$H) return obj.$H;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1292
    if (obj.__hash__) return obj.__hash__();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1293
    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
  1294
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1295
    obj.$H = ++pyjslib.next_hash_id;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1296
    return obj.$H;
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
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1299
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1300
# 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
  1301
def isObject(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1302
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1303
    return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1304
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1305
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1306
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1307
def isFunction(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1308
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1309
    return typeof a == 'function';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1310
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1311
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1312
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1313
def isString(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1314
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1315
    return typeof a == 'string';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1316
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1317
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1318
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1319
def isNull(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1320
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1321
    return typeof a == 'object' && !a;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1322
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1323
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1324
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1325
def isArray(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1326
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1327
    return pyjslib.isObject(a) && a.constructor == Array;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1328
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1329
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1330
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1331
def isUndefined(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1332
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1333
    return typeof a == 'undefined';
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1334
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1335
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1336
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1337
def isIteratable(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1338
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1339
    return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1340
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1341
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1342
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1343
def isNumber(a):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1344
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1345
    return typeof a == 'number' && isFinite(a);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1346
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1347
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1348
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1349
def toJSObjects(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1350
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1351
       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
  1352
       objects, recursively.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1353
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1354
    if isArray(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1355
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1356
        var result = [];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1357
        for(var k=0; k < x.length; k++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1358
           var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1359
           var tv = pyjslib.toJSObjects(v);
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1360
           result.push(tv);
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
        return result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1363
        """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1364
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1365
        if isinstance(x, Dict):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1366
            JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1367
            var o = x.getObject();
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1368
            var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1369
            for (var i in o) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1370
               result[o[i][0].toString()] = o[i][1];
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
            return pyjslib.toJSObjects(result)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1373
            """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1374
        elif isinstance(x, List):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1375
            return toJSObjects(x.l)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1376
        elif hasattr(x, '__class__'):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1377
            # we do not have a special implementation for custom
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1378
            # classes, just pass it on
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1379
            return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1380
    if isObject(x):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1381
        JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1382
        var result = {};
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1383
        for(var k in x) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1384
            var v = x[k];
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1385
            var tv = pyjslib.toJSObjects(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1386
            result[k] = tv;
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 result;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1389
         """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1390
    return x
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1391
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1392
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1393
def printFunc(objs):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1394
    JS("""
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1395
    if ($wnd.console==undefined)  return;
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1396
    var s = "";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1397
    for(var i=0; i < objs.length; i++) {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1398
        if(s != "") s += " ";
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1399
        s += objs[i];
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
    console.debug(s)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1402
    """)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1403
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1404
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1405
def type(clsname, bases=None, methods=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1406
    """ creates a class, derived from bases, with methods and variables
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1407
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1408
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1409
    JS(" var mths = {}; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1410
    if methods:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1411
        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
  1412
            _mth = methods[k]
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1841
diff changeset
  1413
            JS(" mths[k] = _mth; ")
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1414
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1415
    JS(" var bss = null; ")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1416
    if bases:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1417
        JS("bss = bases.l;")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
  1418
    JS(" return pyjs_type(clsname, bss, mths); ")