svgui/pyjs/lib/pyjslib.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Sat, 19 Aug 2017 18:12:20 +0300
changeset 1780 c52d1460cea8
parent 1775 b45f2768fab1
child 1783 3311eea28d56
permissions -rw-r--r--
clean-up: fix PEP8 E722 do not use bare except'

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