1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
|
# Copyright 2006 James Tauber and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# iteration from Bob Ippolito's Iteration in JavaScript
# must declare import _before_ importing sys
# FIXME: dynamic=1, async=False, init=True are useless here (?)
def import_module(path, parent_module, module_name, dynamic=1, async=False, init=True):
JS("""
module = $pyjs.modules_hash[module_name];
if (typeof module == 'function' && module.__was_initialized__ == true) {
return null;
}
if (module_name == 'sys' || module_name == 'pyjslib') {
module();
return null;
}
""")
module = None
names = module_name.split(".")
importName = ''
# Import all modules in the chain (import a.b.c)
for name in names:
importName += name
JS("""module = $pyjs.modules_hash[importName];""")
if isUndefined(module):
raise ImportError("No module named " + importName)
if JS("module.__was_initialized__ != true"):
# Module wasn't initialized
module()
importName += '.'
return None
# FIXME: dynamic=1, async=False are useless here (?). Only dynamic modules
# are loaded with load_module and it's always "async"
@noSourceTracking
def load_module(path, parent_module, module_name, dynamic=1, async=False):
"""
"""
JS("""
var cache_file;
var module = $pyjs.modules_hash[module_name];
if (typeof module == 'function') {
return true;
}
if (!dynamic) {
// There's no way we can load a none dynamic module
return false;
}
if (path == null)
{
path = './';
}
var override_name = sys.platform + "." + module_name;
if (((sys.overrides != null) &&
(sys.overrides.has_key(override_name))))
{
cache_file = sys.overrides.__getitem__(override_name) ;
}
else
{
cache_file = module_name ;
}
cache_file = (path + cache_file + '.cache.js' ) ;
//alert("cache " + cache_file + " " + module_name + " " + parent_module);
onload_fn = '';
// this one tacks the script onto the end of the DOM
pyjs_load_script(cache_file, onload_fn, async);
try {
loaded = (typeof $pyjs.modules_hash[module_name] == 'function')
} catch ( e ) {
}
if (loaded) {
return true;
}
return false;
""")
@noSourceTracking
def load_module_wait(proceed_fn, parent_mod, module_list, dynamic):
module_list = module_list.getArray()
JS("""
var wait_count = 0;
//var data = '';
//var element = $doc.createElement("div");
//element.innerHTML = '';
//$doc.body.appendChild(element);
//function write_dom(txt) {
// element.innerHTML += txt;
//}
var timeoutperiod = 1;
if (dynamic)
var timeoutperiod = 1;
var wait = function() {
wait_count++;
//write_dom(".");
var loaded = true;
for (var i in module_list) {
if (typeof $pyjs.modules_hash[module_list[i]] != 'function') {
loaded = false;
break;
}
}
if (!loaded) {
setTimeout(wait, timeoutperiod);
} else {
if (proceed_fn.importDone)
proceed_fn.importDone(proceed_fn);
else
proceed_fn();
//$doc.body.removeChild(element);
}
}
//write_dom("Loading modules ");
wait();
""")
class Modload:
# All to-be-imported module names are in app_modlist
# Since we're only _loading_ the modules here, we can do that in almost
# any order. There's one limitation: a child/sub module cannot be loaded
# unless its parent is loaded. It has to be chained in the module list.
# (1) $pyjs.modules.pyjamas
# (2) $pyjs.modules.pyjamas.ui
# (3) $pyjs.modules.pyjamas.ui.Widget
# Therefore, all modules are collected and sorted on the depth (i.e. the
# number of dots in it)
# As long as we don't move on to the next depth unless all modules of the
# previous depth are loaded, we won't trun into unchainable modules
# The execution of the module code is done when the import statement is
# reached, or after loading the modules for the main module.
@noSourceTracking
def __init__(self, path, app_modlist, app_imported_fn, dynamic,
parent_mod):
self.app_modlist = app_modlist
self.app_imported_fn = app_imported_fn
self.path = path
self.dynamic = dynamic
self.parent_mod = parent_mod
self.modules = {}
for modlist in self.app_modlist:
for mod in modlist:
depth = len(mod.split('.'))
if not self.modules.has_key(depth):
self.modules[depth] = []
self.modules[depth].append(mod)
self.depths = self.modules.keys()
self.depths.sort()
self.depths.reverse()
@noSourceTracking
def next(self):
if not self.dynamic:
# All modules are static. Just start the main module.
self.app_imported_fn()
return
depth = self.depths.pop()
# Initiate the loading of the modules.
for app in self.modules[depth]:
load_module(self.path, self.parent_mod, app, self.dynamic, True);
if len(self.depths) == 0:
# This is the last depth. Start the main module after loading these
# modules.
load_module_wait(self.app_imported_fn, self.parent_mod, self.modules[depth], self.dynamic)
else:
# After loading the modules, to the next depth.
load_module_wait(getattr(self, "next"), self.parent_mod, self.modules[depth], self.dynamic)
def get_module(module_name):
ev = "__mod = %s;" % module_name
JS("pyjs_eval(ev);")
return __mod
def preload_app_modules(path, app_modnames, app_imported_fn, dynamic,
parent_mod=None):
loader = Modload(path, app_modnames, app_imported_fn, dynamic, parent_mod)
loader.next()
class BaseException:
message = ''
def __init__(self, *args):
self.args = args
if len(args) == 1:
self.message = args[0]
def __getitem__(self, index):
return self.args.__getitem__(index)
def __str__(self):
if len(self.args) is 0:
return ''
elif len(self.args) is 1:
return str(self.message)
return repr(self.args)
def __repr__(self):
return self.__name__ + repr(self.args)
def toString(self):
return str(self)
class Exception(BaseException):
pass
class StandardError(Exception):
pass
class TypeError(StandardError):
pass
class AttributeError(StandardError):
def toString(self):
return "AttributeError: %s of %s" % (self.args[1], self.args[0])
class NameError(StandardError):
pass
class ValueError(StandardError):
pass
class ImportError(StandardError):
pass
class LookupError(StandardError):
def toString(self):
return self.__name__ + ": " + self.args[0]
class KeyError(LookupError):
def __str__(self):
if len(self.args) is 0:
return ''
elif len(self.args) is 1:
return repr(self.message)
return repr(self.args)
class IndexError(LookupError):
pass
# There seems to be an bug in Chrome with accessing the message
# property, on which an error is thrown
# Hence the declaration of 'var message' and the wrapping in try..catch
def init():
JS("""
pyjslib._errorMapping = function(err) {
if (err instanceof(ReferenceError) || err instanceof(TypeError)) {
var message = ''
try {
message = err.message;
} catch ( e) {
}
return pyjslib.AttributeError(message);
}
return err
}
pyjslib.TryElse = function () { };
pyjslib.TryElse.prototype = new Error();
pyjslib.TryElse.__name__ = 'TryElse';
pyjslib.TryElse.message = 'TryElse';
pyjslib.StopIteration = function () { };
pyjslib.StopIteration.prototype = new Error();
pyjslib.StopIteration.__name__ = 'StopIteration';
pyjslib.StopIteration.message = 'StopIteration';
pyjslib.String_find = function(sub, start, end) {
var pos=this.indexOf(sub, start);
if (pyjslib.isUndefined(end)) return pos;
if (pos + sub.length>end) return -1;
return pos;
}
pyjslib.String_join = function(data) {
var text="";
if (pyjslib.isArray(data)) {
return data.join(this);
}
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
try {
text+=iter.next();
while (true) {
var item=iter.next();
text+=this + item;
}
}
catch (e) {
if (e.__name__ != 'StopIteration') throw e;
}
}
return text;
}
pyjslib.String_isdigit = function() {
return (this.match(/^\d+$/g) != null);
}
pyjslib.String_replace = function(old, replace, count) {
var do_max=false;
var start=0;
var new_str="";
var pos=0;
if (!pyjslib.isString(old)) return this.__replace(old, replace);
if (!pyjslib.isUndefined(count)) do_max=true;
while (start<this.length) {
if (do_max && !count--) break;
pos=this.indexOf(old, start);
if (pos<0) break;
new_str+=this.substring(start, pos) + replace;
start=pos+old.length;
}
if (start<this.length) new_str+=this.substring(start);
return new_str;
}
pyjslib.String_split = function(sep, maxsplit) {
var items=new pyjslib.List();
var do_max=false;
var subject=this;
var start=0;
var pos=0;
if (pyjslib.isUndefined(sep) || pyjslib.isNull(sep)) {
sep=" ";
subject=subject.strip();
subject=subject.replace(/\s+/g, sep);
}
else if (!pyjslib.isUndefined(maxsplit)) do_max=true;
if (subject.length == 0) {
return items;
}
while (start<subject.length) {
if (do_max && !maxsplit--) break;
pos=subject.indexOf(sep, start);
if (pos<0) break;
items.append(subject.substring(start, pos));
start=pos+sep.length;
}
if (start<=subject.length) items.append(subject.substring(start));
return items;
}
pyjslib.String___iter__ = function() {
var i = 0;
var s = this;
return {
'next': function() {
if (i >= s.length) {
throw pyjslib.StopIteration;
}
return s.substring(i++, i, 1);
},
'__iter__': function() {
return this;
}
};
}
pyjslib.String_strip = function(chars) {
return this.lstrip(chars).rstrip(chars);
}
pyjslib.String_lstrip = function(chars) {
if (pyjslib.isUndefined(chars)) return this.replace(/^\s+/, "");
return this.replace(new RegExp("^[" + chars + "]+"), "");
}
pyjslib.String_rstrip = function(chars) {
if (pyjslib.isUndefined(chars)) return this.replace(/\s+$/, "");
return this.replace(new RegExp("[" + chars + "]+$"), "");
}
pyjslib.String_startswith = function(prefix, start, end) {
// FIXME: accept tuples as suffix (since 2.5)
if (pyjslib.isUndefined(start)) start = 0;
if (pyjslib.isUndefined(end)) end = this.length;
if ((end - start) < prefix.length) return false
if (this.substr(start, prefix.length) == prefix) return true;
return false;
}
pyjslib.String_endswith = function(suffix, start, end) {
// FIXME: accept tuples as suffix (since 2.5)
if (pyjslib.isUndefined(start)) start = 0;
if (pyjslib.isUndefined(end)) end = this.length;
if ((end - start) < suffix.length) return false
if (this.substr(end - suffix.length, suffix.length) == suffix) return true;
return false;
}
pyjslib.String_ljust = function(width, fillchar) {
if (typeof(width) != 'number' ||
parseInt(width) != width) {
throw (pyjslib.TypeError("an integer is required"));
}
if (pyjslib.isUndefined(fillchar)) fillchar = ' ';
if (typeof(fillchar) != 'string' ||
fillchar.length != 1) {
throw (pyjslib.TypeError("ljust() argument 2 must be char, not " + typeof(fillchar)));
}
if (this.length >= width) return this;
return this + new Array(width+1 - this.length).join(fillchar);
}
pyjslib.String_rjust = function(width, fillchar) {
if (typeof(width) != 'number' ||
parseInt(width) != width) {
throw (pyjslib.TypeError("an integer is required"));
}
if (pyjslib.isUndefined(fillchar)) fillchar = ' ';
if (typeof(fillchar) != 'string' ||
fillchar.length != 1) {
throw (pyjslib.TypeError("rjust() argument 2 must be char, not " + typeof(fillchar)));
}
if (this.length >= width) return this;
return new Array(width + 1 - this.length).join(fillchar) + this;
}
pyjslib.String_center = function(width, fillchar) {
if (typeof(width) != 'number' ||
parseInt(width) != width) {
throw (pyjslib.TypeError("an integer is required"));
}
if (pyjslib.isUndefined(fillchar)) fillchar = ' ';
if (typeof(fillchar) != 'string' ||
fillchar.length != 1) {
throw (pyjslib.TypeError("center() argument 2 must be char, not " + typeof(fillchar)));
}
if (this.length >= width) return this;
padlen = width - this.length
right = Math.ceil(padlen / 2);
left = padlen - right;
return new Array(left+1).join(fillchar) + this + new Array(right+1).join(fillchar);
}
pyjslib.abs = Math.abs;
""")
class Class:
def __init__(self, name):
self.name = name
def __str___(self):
return self.name
@noSourceTracking
def eq(a,b):
# All 'python' classes and types are implemented as objects/functions.
# So, for speed, do a typeof X / X.__cmp__ on a/b.
# Checking for the existance of .__cmp__ is expensive...
JS("""
if (a === null) {
if (b === null) return true;
return false;
}
if (b === null) {
return false;
}
if ((typeof a == 'object' || typeof a == 'function') && typeof a.__cmp__ == 'function') {
return a.__cmp__(b) == 0;
} else if ((typeof b == 'object' || typeof b == 'function') && typeof b.__cmp__ == 'function') {
return b.__cmp__(a) == 0;
}
return a == b;
""")
@noSourceTracking
def cmp(a,b):
JS("""
if (a === null) {
if (b === null) return 0;
return -1;
}
if (b === null) {
return 1;
}
if ((typeof a == 'object' || typeof a == 'function') && typeof a.__cmp__ == 'function') {
return a.__cmp__(b);
} else if ((typeof b == 'object' || typeof b == 'function') && typeof b.__cmp__ == 'function') {
return -b.__cmp__(a);
}
if (a > b) return 1;
if (b > a) return -1;
return 0;
""")
# For list.sort()
__cmp = cmp
@noSourceTracking
def bool(v):
# this needs to stay in native code without any dependencies here,
# because this is used by if and while, we need to prevent
# recursion
JS("""
if (!v) return false;
switch(typeof v){
case 'boolean':
return v;
case 'object':
if (v.__nonzero__){
return v.__nonzero__();
}else if (v.__len__){
return v.__len__()>0;
}
return true;
}
return Boolean(v);
""")
class List:
@noSourceTracking
def __init__(self, data=None):
JS("""
this.l = [];
this.extend(data);
""")
@noSourceTracking
def append(self, item):
JS(""" this.l[this.l.length] = item;""")
@noSourceTracking
def extend(self, data):
JS("""
if (pyjslib.isArray(data)) {
n = this.l.length;
for (var i=0; i < data.length; i++) {
this.l[n+i]=data[i];
}
}
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
var i=this.l.length;
try {
while (true) {
var item=iter.next();
this.l[i++]=item;
}
}
catch (e) {
if (e.__name__ != 'StopIteration') throw e;
}
}
""")
@noSourceTracking
def remove(self, value):
JS("""
var index=this.index(value);
if (index<0) return false;
this.l.splice(index, 1);
return true;
""")
@noSourceTracking
def index(self, value, start=0):
JS("""
var length=this.l.length;
for (var i=start; i<length; i++) {
if (this.l[i]==value) {
return i;
}
}
return -1;
""")
@noSourceTracking
def insert(self, index, value):
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
@noSourceTracking
def pop(self, index = -1):
JS("""
if (index<0) index = this.l.length + index;
var a = this.l[index];
this.l.splice(index, 1);
return a;
""")
@noSourceTracking
def __cmp__(self, l):
if not isinstance(l, List):
return -1
ll = len(self) - len(l)
if ll != 0:
return ll
for x in range(len(l)):
ll = cmp(self.__getitem__(x), l[x])
if ll != 0:
return ll
return 0
@noSourceTracking
def slice(self, lower, upper):
JS("""
if (upper==null) return pyjslib.List(this.l.slice(lower));
return pyjslib.List(this.l.slice(lower, upper));
""")
@noSourceTracking
def __getitem__(self, index):
JS("""
if (index<0) index = this.l.length + index;
return this.l[index];
""")
@noSourceTracking
def __setitem__(self, index, value):
JS(""" this.l[index]=value;""")
@noSourceTracking
def __delitem__(self, index):
JS(""" this.l.splice(index, 1);""")
@noSourceTracking
def __len__(self):
JS(""" return this.l.length;""")
@noSourceTracking
def __contains__(self, value):
return self.index(value) >= 0
@noSourceTracking
def __iter__(self):
JS("""
var i = 0;
var l = this.l;
return {
'next': function() {
if (i >= l.length) {
throw pyjslib.StopIteration;
}
return l[i++];
},
'__iter__': function() {
return this;
}
};
""")
@noSourceTracking
def reverse(self):
JS(""" this.l.reverse();""")
def sort(self, cmp=None, key=None, reverse=False):
if not cmp:
cmp = __cmp
if key and reverse:
def thisSort1(a,b):
return -cmp(key(a), key(b))
self.l.sort(thisSort1)
elif key:
def thisSort2(a,b):
return cmp(key(a), key(b))
self.l.sort(thisSort2)
elif reverse:
def thisSort3(a,b):
return -cmp(a, b)
self.l.sort(thisSort3)
else:
self.l.sort(cmp)
@noSourceTracking
def getArray(self):
"""
Access the javascript Array that is used internally by this list
"""
return self.l
@noSourceTracking
def __str__(self):
return self.__repr__()
@noSourceTracking
def toString(self):
return self.__repr__()
def __repr__(self):
#r = []
#for item in self:
# r.append(repr(item))
#return '[' + ', '.join(r) + ']'
JS("""
var s = "[";
for (var i=0; i < self.l.length; i++) {
s += pyjslib.repr(self.l[i]);
if (i < self.l.length - 1)
s += ", ";
};
s += "]"
return s;
""")
class Tuple:
@noSourceTracking
def __init__(self, data=None):
JS("""
this.l = [];
this.extend(data);
""")
@noSourceTracking
def append(self, item):
JS(""" this.l[this.l.length] = item;""")
@noSourceTracking
def extend(self, data):
JS("""
if (pyjslib.isArray(data)) {
n = this.l.length;
for (var i=0; i < data.length; i++) {
this.l[n+i]=data[i];
}
}
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
var i=this.l.length;
try {
while (true) {
var item=iter.next();
this.l[i++]=item;
}
}
catch (e) {
if (e.__name__ != 'StopIteration') throw e;
}
}
""")
@noSourceTracking
def remove(self, value):
JS("""
var index=this.index(value);
if (index<0) return false;
this.l.splice(index, 1);
return true;
""")
@noSourceTracking
def index(self, value, start=0):
JS("""
var length=this.l.length;
for (var i=start; i<length; i++) {
if (this.l[i]==value) {
return i;
}
}
return -1;
""")
@noSourceTracking
def insert(self, index, value):
JS(""" var a = this.l; this.l=a.slice(0, index).concat(value, a.slice(index));""")
@noSourceTracking
def pop(self, index = -1):
JS("""
if (index<0) index = this.l.length + index;
var a = this.l[index];
this.l.splice(index, 1);
return a;
""")
@noSourceTracking
def __cmp__(self, l):
if not isinstance(l, Tuple):
return 1
ll = len(self) - len(l)
if ll != 0:
return ll
for x in range(len(l)):
ll = cmp(self.__getitem__(x), l[x])
if ll != 0:
return ll
return 0
@noSourceTracking
def slice(self, lower, upper):
JS("""
if (upper==null) return pyjslib.Tuple(this.l.slice(lower));
return pyjslib.Tuple(this.l.slice(lower, upper));
""")
@noSourceTracking
def __getitem__(self, index):
JS("""
if (index<0) index = this.l.length + index;
return this.l[index];
""")
@noSourceTracking
def __setitem__(self, index, value):
JS(""" this.l[index]=value;""")
@noSourceTracking
def __delitem__(self, index):
JS(""" this.l.splice(index, 1);""")
@noSourceTracking
def __len__(self):
JS(""" return this.l.length;""")
@noSourceTracking
def __contains__(self, value):
return self.index(value) >= 0
@noSourceTracking
def __iter__(self):
JS("""
var i = 0;
var l = this.l;
return {
'next': function() {
if (i >= l.length) {
throw pyjslib.StopIteration;
}
return l[i++];
},
'__iter__': function() {
return this;
}
};
""")
@noSourceTracking
def reverse(self):
JS(""" this.l.reverse();""")
def sort(self, cmp=None, key=None, reverse=False):
if not cmp:
cmp = cmp
if key and reverse:
def thisSort1(a,b):
return -cmp(key(a), key(b))
self.l.sort(thisSort1)
elif key:
def thisSort2(a,b):
return cmp(key(a), key(b))
self.l.sort(thisSort2)
elif reverse:
def thisSort3(a,b):
return -cmp(a, b)
self.l.sort(thisSort3)
else:
self.l.sort(cmp)
@noSourceTracking
def getArray(self):
"""
Access the javascript Array that is used internally by this list
"""
return self.l
@noSourceTracking
def __str__(self):
return self.__repr__()
@noSourceTracking
def toString(self):
return self.__repr__()
def __repr__(self):
#r = []
#for item in self:
# r.append(repr(item))
#if len(r) == 1:
# return '(' + ', '.join(r) + ',)'
#return '(' + ', '.join(r) + ')'
JS("""
var s = "(";
for (var i=0; i < self.l.length; i++) {
s += pyjslib.repr(self.l[i]);
if (i < self.l.length - 1)
s += ", ";
};
if (self.l.length == 1)
s += ",";
s += ")"
return s;
""")
class Dict:
@noSourceTracking
def __init__(self, data=None):
JS("""
this.d = {};
if (pyjslib.isArray(data)) {
for (var i in data) {
var item=data[i];
this.__setitem__(item[0], item[1]);
//var sKey=pyjslib.hash(item[0]);
//this.d[sKey]=item[1];
}
}
else if (pyjslib.isIteratable(data)) {
var iter=data.__iter__();
try {
while (true) {
var item=iter.next();
this.__setitem__(item.__getitem__(0), item.__getitem__(1));
}
}
catch (e) {
if (e.__name__ != 'StopIteration') throw e;
}
}
else if (pyjslib.isObject(data)) {
for (var key in data) {
this.__setitem__(key, data[key]);
}
}
""")
@noSourceTracking
def __setitem__(self, key, value):
JS("""
var sKey = pyjslib.hash(key);
this.d[sKey]=[key, value];
""")
@noSourceTracking
def __getitem__(self, key):
JS("""
var sKey = pyjslib.hash(key);
var value=this.d[sKey];
if (pyjslib.isUndefined(value)){
throw pyjslib.KeyError(key);
}
return value[1];
""")
@noSourceTracking
def __nonzero__(self):
JS("""
for (var i in this.d){
return true;
}
return false;
""")
@noSourceTracking
def __len__(self):
JS("""
var size=0;
for (var i in this.d) size++;
return size;
""")
@noSourceTracking
def has_key(self, key):
return self.__contains__(key)
@noSourceTracking
def __delitem__(self, key):
JS("""
var sKey = pyjslib.hash(key);
delete this.d[sKey];
""")
@noSourceTracking
def __contains__(self, key):
JS("""
var sKey = pyjslib.hash(key);
return (pyjslib.isUndefined(this.d[sKey])) ? false : true;
""")
@noSourceTracking
def keys(self):
JS("""
var keys=new pyjslib.List();
for (var key in this.d) {
keys.append(this.d[key][0]);
}
return keys;
""")
@noSourceTracking
def values(self):
JS("""
var values=new pyjslib.List();
for (var key in this.d) values.append(this.d[key][1]);
return values;
""")
@noSourceTracking
def items(self):
JS("""
var items = new pyjslib.List();
for (var key in this.d) {
var kv = this.d[key];
items.append(new pyjslib.List(kv))
}
return items;
""")
@noSourceTracking
def __iter__(self):
return self.keys().__iter__()
@noSourceTracking
def iterkeys(self):
return self.__iter__()
@noSourceTracking
def itervalues(self):
return self.values().__iter__();
@noSourceTracking
def iteritems(self):
return self.items().__iter__();
@noSourceTracking
def setdefault(self, key, default_value):
if not self.has_key(key):
self[key] = default_value
return self[key]
@noSourceTracking
def get(self, key, default_value=None):
if not self.has_key(key):
return default_value
return self[key]
@noSourceTracking
def update(self, d):
for k,v in d.iteritems():
self[k] = v
@noSourceTracking
def getObject(self):
"""
Return the javascript Object which this class uses to store
dictionary keys and values
"""
return self.d
@noSourceTracking
def copy(self):
return Dict(self.items())
@noSourceTracking
def __str__(self):
return self.__repr__()
@noSourceTracking
def toString(self):
return self.__repr__()
def __repr__(self):
#r = []
#for item in self:
# r.append(repr(item) + ': ' + repr(self[item]))
#return '{' + ', '.join(r) + '}'
JS("""
var keys = new Array();
for (var key in self.d)
keys.push(key);
var s = "{";
for (var i=0; i<keys.length; i++) {
var v = self.d[keys[i]]
s += pyjslib.repr(v[0]) + ": " + pyjslib.repr(v[1]);
if (i < keys.length-1)
s += ", "
};
s += "}";
return s;
""")
# IE6 doesn't like pyjslib.super
@noSourceTracking
def _super(type_, object_or_type = None):
# This is a partially implementation: only super(type, object)
if not _issubtype(object_or_type, type_):
raise TypeError("super(type, obj): obj must be an instance or subtype of type")
JS("""
var fn = pyjs_type('super', type_.__mro__.slice(1), {})
fn.__new__ = fn.__mro__[1].__new__;
fn.__init__ = fn.__mro__[1].__init__;
if (object_or_type.__is_instance__ === false) {
return fn;
}
var obj = new Object();
function wrapper(obj, name) {
var fnwrap = function() {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return obj[name].apply(object_or_type,args);
}
fnwrap.__name__ = name;
fnwrap.parse_kwargs = obj.parse_kwargs;
return fnwrap;
}
for (var m in fn) {
if (typeof fn[m] == 'function') {
obj[m] = wrapper(fn, m);
}
}
return obj;
""")
# taken from mochikit: range( [start,] stop[, step] )
@noSourceTracking
def range(start, stop = None, step = 1):
if stop is None:
stop = start
start = 0
JS("""
/*
var start = 0;
var stop = 0;
var step = 1;
if (arguments.length == 2) {
start = arguments[0];
stop = arguments[1];
}
else if (arguments.length == 3) {
start = arguments[0];
stop = arguments[1];
step = arguments[2];
}
else if (arguments.length>0) stop = arguments[0];
*/
return {
'next': function() {
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) throw pyjslib.StopIteration;
var rval = start;
start += step;
return rval;
},
'__iter__': function() {
return this;
}
}
""")
@noSourceTracking
def slice(object, lower, upper):
JS("""
if (pyjslib.isString(object)) {
if (lower < 0) {
lower = object.length + lower;
}
if (upper < 0) {
upper = object.length + upper;
}
if (pyjslib.isNull(upper)) upper=object.length;
return object.substring(lower, upper);
}
if (pyjslib.isObject(object) && object.slice)
return object.slice(lower, upper);
return null;
""")
@noSourceTracking
def str(text):
JS("""
if (pyjslib.hasattr(text,"__str__")) {
return text.__str__();
}
return String(text);
""")
@noSourceTracking
def ord(x):
if(isString(x) and len(x) is 1):
JS("""
return x.charCodeAt(0);
""")
else:
JS("""
throw pyjslib.TypeError();
""")
return None
@noSourceTracking
def chr(x):
JS("""
return String.fromCharCode(x)
""")
@noSourceTracking
def is_basetype(x):
JS("""
var t = typeof(x);
return t == 'boolean' ||
t == 'function' ||
t == 'number' ||
t == 'string' ||
t == 'undefined'
;
""")
@noSourceTracking
def get_pyjs_classtype(x):
JS("""
if (pyjslib.hasattr(x, "__is_instance__")) {
var src = x.__name__;
return src;
}
return null;
""")
@noSourceTracking
def repr(x):
""" Return the string representation of 'x'.
"""
if hasattr(x, '__repr__'):
return x.__repr__()
JS("""
if (x === null)
return "null";
if (x === undefined)
return "undefined";
var t = typeof(x);
//alert("repr typeof " + t + " : " + x);
if (t == "boolean")
return x.toString();
if (t == "function")
return "<function " + x.toString() + ">";
if (t == "number")
return x.toString();
if (t == "string") {
if (x.indexOf("'") == -1)
return "'" + x + "'";
if (x.indexOf('"') == -1)
return '"' + x + '"';
var s = x.replace(new RegExp('"', "g"), '\\\\"');
return '"' + s + '"';
};
if (t == "undefined")
return "undefined";
// If we get here, x is an object. See if it's a Pyjamas class.
if (!pyjslib.hasattr(x, "__init__"))
return "<" + x.toString() + ">";
// Handle the common Pyjamas data types.
var constructor = "UNKNOWN";
constructor = pyjslib.get_pyjs_classtype(x);
//alert("repr constructor: " + constructor);
// If we get here, the class isn't one we know -> return the class name.
// Note that we replace underscores with dots so that the name will
// (hopefully!) look like the original Python name.
//var s = constructor.replace(new RegExp('_', "g"), '.');
return "<" + constructor + " object>";
""")
@noSourceTracking
def float(text):
JS("""
return parseFloat(text);
""")
@noSourceTracking
def int(text, radix=0):
JS("""
var i = parseInt(text, radix);
if (!isNaN(i)) {
return i;
}
""")
if radix == 0:
radix = 10
raise ValueError("invalid literal for int() with base %d: '%s'" % (radix, text))
@noSourceTracking
def len(object):
JS("""
if (object==null) return 0;
if (pyjslib.isObject(object) && object.__len__) return object.__len__();
return object.length;
""")
@noSourceTracking
def isinstance(object_, classinfo):
if pyjslib.isUndefined(object_):
return False
JS("""if (classinfo.__name__ == 'int') {
return pyjslib.isNumber(object_); /* XXX TODO: check rounded? */
}
""")
JS("""if (classinfo.__name__ == 'str') {
return pyjslib.isString(object_);
}
""")
if not pyjslib.isObject(object_):
return False
if _isinstance(classinfo, Tuple):
for ci in classinfo:
if isinstance(object_, ci):
return True
return False
else:
return _isinstance(object_, classinfo)
@noSourceTracking
def _isinstance(object_, classinfo):
JS("""
if (object_.__is_instance__ !== true) {
return false;
}
for (var c in object_.__mro__) {
if (object_.__mro__[c].__md5__ == classinfo.prototype.__md5__) return true;
}
return false;
""")
@noSourceTracking
def _issubtype(object_, classinfo):
JS("""
if (object_.__is_instance__ == null || classinfo.__is_instance__ == null) {
return false;
}
for (var c in object_.__mro__) {
if (object_.__mro__[c] == classinfo.prototype) return true;
}
return false;
""")
@noSourceTracking
def getattr(obj, name, default_value=None):
JS("""
if ((!pyjslib.isObject(obj))||(pyjslib.isUndefined(obj[name]))){
if (arguments.length != 3){
throw pyjslib.AttributeError(obj, name);
}else{
return default_value;
}
}
if (!pyjslib.isFunction(obj[name])) return obj[name];
var method = obj[name];
var fnwrap = function() {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return method.apply(obj,args);
}
fnwrap.__name__ = name;
fnwrap.parse_kwargs = obj.parse_kwargs;
return fnwrap;
""")
@noSourceTracking
def delattr(obj, name):
JS("""
if (!pyjslib.isObject(obj)) {
throw pyjslib.AttributeError("'"+typeof(obj)+"' object has no attribute '"+name+"%s'")
}
if ((pyjslib.isUndefined(obj[name])) ||(typeof(obj[name]) == "function") ){
throw pyjslib.AttributeError(obj.__name__+" instance has no attribute '"+ name+"'");
}
delete obj[name];
""")
@noSourceTracking
def setattr(obj, name, value):
JS("""
if (!pyjslib.isObject(obj)) return null;
obj[name] = value;
""")
@noSourceTracking
def hasattr(obj, name):
JS("""
if (!pyjslib.isObject(obj)) return false;
if (pyjslib.isUndefined(obj[name])) return false;
return true;
""")
@noSourceTracking
def dir(obj):
JS("""
var properties=new pyjslib.List();
for (property in obj) properties.append(property);
return properties;
""")
@noSourceTracking
def filter(obj, method, sequence=None):
# object context is LOST when a method is passed, hence object must be passed separately
# to emulate python behaviour, should generate this code inline rather than as a function call
items = []
if sequence is None:
sequence = method
method = obj
for item in sequence:
if method(item):
items.append(item)
else:
for item in sequence:
if method.call(obj, item):
items.append(item)
return items
@noSourceTracking
def map(obj, method, sequence=None):
items = []
if sequence is None:
sequence = method
method = obj
for item in sequence:
items.append(method(item))
else:
for item in sequence:
items.append(method.call(obj, item))
return items
def enumerate(sequence):
enumeration = []
nextIndex = 0
for item in sequence:
enumeration.append([nextIndex, item])
nextIndex = nextIndex + 1
return enumeration
def min(*sequence):
if len(sequence) == 1:
sequence = sequence[0]
minValue = None
for item in sequence:
if minValue is None:
minValue = item
elif cmp(item, minValue) == -1:
minValue = item
return minValue
def max(*sequence):
if len(sequence) == 1:
sequence = sequence[0]
maxValue = None
for item in sequence:
if maxValue is None:
maxValue = item
elif cmp(item, maxValue) == 1:
maxValue = item
return maxValue
@noSourceTracking
def hash(obj):
JS("""
if (obj == null) return null;
if (obj.$H) return obj.$H;
if (obj.__hash__) return obj.__hash__();
if (obj.constructor == String || obj.constructor == Number || obj.constructor == Date) return obj;
obj.$H = ++pyjslib.next_hash_id;
return obj.$H;
""")
# type functions from Douglas Crockford's Remedial Javascript: http://www.crockford.com/javascript/remedial.html
@noSourceTracking
def isObject(a):
JS("""
return (a != null && (typeof a == 'object')) || pyjslib.isFunction(a);
""")
@noSourceTracking
def isFunction(a):
JS("""
return typeof a == 'function';
""")
@noSourceTracking
def isString(a):
JS("""
return typeof a == 'string';
""")
@noSourceTracking
def isNull(a):
JS("""
return typeof a == 'object' && !a;
""")
@noSourceTracking
def isArray(a):
JS("""
return pyjslib.isObject(a) && a.constructor == Array;
""")
@noSourceTracking
def isUndefined(a):
JS("""
return typeof a == 'undefined';
""")
@noSourceTracking
def isIteratable(a):
JS("""
return pyjslib.isString(a) || (pyjslib.isObject(a) && a.__iter__);
""")
@noSourceTracking
def isNumber(a):
JS("""
return typeof a == 'number' && isFinite(a);
""")
@noSourceTracking
def toJSObjects(x):
"""
Convert the pyjs pythonic List and Dict objects into javascript Object and Array
objects, recursively.
"""
if isArray(x):
JS("""
var result = [];
for(var k=0; k < x.length; k++) {
var v = x[k];
var tv = pyjslib.toJSObjects(v);
result.push(tv);
}
return result;
""")
if isObject(x):
if isinstance(x, Dict):
JS("""
var o = x.getObject();
var result = {};
for (var i in o) {
result[o[i][0].toString()] = o[i][1];
}
return pyjslib.toJSObjects(result)
""")
elif isinstance(x, List):
return toJSObjects(x.l)
elif hasattr(x, '__class__'):
# we do not have a special implementation for custom
# classes, just pass it on
return x
if isObject(x):
JS("""
var result = {};
for(var k in x) {
var v = x[k];
var tv = pyjslib.toJSObjects(v)
result[k] = tv;
}
return result;
""")
return x
@noSourceTracking
def sprintf(strng, args):
# See http://docs.python.org/library/stdtypes.html
constructor = get_pyjs_classtype(args)
JS("""
var re_dict = /([^%]*)%[(]([^)]+)[)]([#0\x20\0x2B-]*)(\d+)?(\.\d+)?[hlL]?(.)((.|\\n)*)/;
var re_list = /([^%]*)%([#0\x20\x2B-]*)(\*|(\d+))?(\.\d+)?[hlL]?(.)((.|\\n)*)/;
var re_exp = /(.*)([+-])(.*)/;
""")
strlen = len(strng)
argidx = 0
nargs = 0
result = []
remainder = strng
def next_arg():
if argidx == nargs:
raise TypeError("not enough arguments for format string")
arg = args[argidx]
argidx += 1
return arg
def formatarg(flags, minlen, precision, conversion, param):
subst = ''
numeric = True
if not minlen:
minlen=0
else:
minlen = int(minlen)
if not precision:
precision = None
else:
precision = int(precision)
left_padding = 1
if flags.find('-') >= 0:
left_padding = 0
if conversion == '%':
numeric = False
subst = '%'
elif conversion == 'c':
numeric = False
subst = chr(int(param))
elif conversion == 'd' or conversion == 'i' or conversion == 'u':
subst = str(int(param))
elif conversion == 'e':
if precision is None:
precision = 6
JS("""
subst = re_exp.exec(String(param.toExponential(precision)));
if (subst[3].length == 1) {
subst = subst[1] + subst[2] + '0' + subst[3];
} else {
subst = subst[1] + subst[2] + subst[3];
}""")
elif conversion == 'E':
if precision is None:
precision = 6
JS("""
subst = re_exp.exec(String(param.toExponential(precision)).toUpperCase());
if (subst[3].length == 1) {
subst = subst[1] + subst[2] + '0' + subst[3];
} else {
subst = subst[1] + subst[2] + subst[3];
}""")
elif conversion == 'f':
if precision is None:
precision = 6
JS("""
subst = String(parseFloat(param).toFixed(precision));""")
elif conversion == 'F':
if precision is None:
precision = 6
JS("""
subst = String(parseFloat(param).toFixed(precision)).toUpperCase();""")
elif conversion == 'g':
if flags.find('#') >= 0:
if precision is None:
precision = 6
if param >= 1E6 or param < 1E-5:
JS("""
subst = String(precision == null ? param.toExponential() : param.toExponential().toPrecision(precision));""")
else:
JS("""
subst = String(precision == null ? parseFloat(param) : parseFloat(param).toPrecision(precision));""")
elif conversion == 'G':
if flags.find('#') >= 0:
if precision is None:
precision = 6
if param >= 1E6 or param < 1E-5:
JS("""
subst = String(precision == null ? param.toExponential() : param.toExponential().toPrecision(precision)).toUpperCase();""")
else:
JS("""
subst = String(precision == null ? parseFloat(param) : parseFloat(param).toPrecision(precision)).toUpperCase().toUpperCase();""")
elif conversion == 'r':
numeric = False
subst = repr(param)
elif conversion == 's':
numeric = False
subst = str(param)
elif conversion == 'o':
param = int(param)
JS("""
subst = param.toString(8);""")
if flags.find('#') >= 0 and subst != '0':
subst = '0' + subst
elif conversion == 'x':
param = int(param)
JS("""
subst = param.toString(16);""")
if flags.find('#') >= 0:
if left_padding:
subst = subst.rjust(minlen - 2, '0')
subst = '0x' + subst
elif conversion == 'X':
param = int(param)
JS("""
subst = param.toString(16).toUpperCase();""")
if flags.find('#') >= 0:
if left_padding:
subst = subst.rjust(minlen - 2, '0')
subst = '0X' + subst
else:
raise ValueError("unsupported format character '" + conversion + "' ("+hex(ord(conversion))+") at index " + (strlen - len(remainder) - 1))
if minlen and len(subst) < minlen:
padchar = ' '
if numeric and left_padding and flags.find('0') >= 0:
padchar = '0'
if left_padding:
subst = subst.rjust(minlen, padchar)
else:
subst = subst.ljust(minlen, padchar)
return subst
def sprintf_list(strng, args):
while remainder:
JS("""
var a = re_list.exec(remainder);""")
if a is None:
result.append(remainder)
break;
JS("""
var left = a[1], flags = a[2];
var minlen = a[3], precision = a[5], conversion = a[6];
remainder = a[7];
if (typeof minlen == 'undefined') minlen = null;
if (typeof precision == 'undefined') precision = null;
if (typeof conversion == 'undefined') conversion = null;
""")
result.append(left)
if minlen == '*':
minlen = next_arg()
JS("var minlen_type = typeof(minlen);")
if minlen_type != 'number' or \
int(minlen) != minlen:
raise TypeError('* wants int')
if conversion != '%':
param = next_arg()
result.append(formatarg(flags, minlen, precision, conversion, param))
def sprintf_dict(strng, args):
arg = args
argidx += 1
while remainder:
JS("""
var a = re_dict.exec(remainder);""")
if a is None:
result.append(remainder)
break;
JS("""
var left = a[1], key = a[2], flags = a[3];
var minlen = a[4], precision = a[5], conversion = a[6];
remainder = a[7];
if (typeof minlen == 'undefined') minlen = null;
if (typeof precision == 'undefined') precision = null;
if (typeof conversion == 'undefined') conversion = null;
""")
result.append(left)
if not arg.has_key(key):
raise KeyError(key)
else:
param = arg[key]
result.append(formatarg(flags, minlen, precision, conversion, param))
JS("""
var a = re_dict.exec(strng);
""")
if a is None:
if constructor != "Tuple":
args = (args,)
nargs = len(args)
sprintf_list(strng, args)
if argidx != nargs:
raise TypeError('not all arguments converted during string formatting')
else:
if constructor != "Dict":
raise TypeError("format requires a mapping")
sprintf_dict(strng, args)
return ''.join(result)
@noSourceTracking
def printFunc(objs, newline):
JS("""
if ($wnd.console==undefined) return;
var s = "";
for(var i=0; i < objs.length; i++) {
if(s != "") s += " ";
s += objs[i];
}
console.debug(s)
""")
@noSourceTracking
def type(clsname, bases=None, methods=None):
""" creates a class, derived from bases, with methods and variables
"""
JS(" var mths = {}; ")
if methods:
for k in methods.keys():
mth = methods[k]
JS(" mths[k] = mth; ")
JS(" var bss = null; ")
if bases:
JS("bss = bases.l;")
JS(" return pyjs_type(clsname, bss, mths); ")
def pow(x, y, z = None):
JS("p = Math.pow(x, y);")
if z is None:
return float(p)
return float(p % z)
def hex(x):
if int(x) != x:
raise TypeError("hex() argument can't be converted to hex")
JS("r = '0x'+x.toString(16);")
return str(r)
def oct(x):
if int(x) != x:
raise TypeError("oct() argument can't be converted to oct")
JS("r = '0'+x.toString(8);")
return str(r)
def round(x, n = 0):
n = pow(10, n)
JS("r = Math.round(n*x)/n;")
return float(r)
def divmod(x, y):
if int(x) == x and int(y) == y:
return (int(x / y), int(x % y))
JS("f = Math.floor(x / y);")
f = float(f)
return (f, x - f * y)
def all(iterable):
for element in iterable:
if not element:
return False
return True
def any(iterable):
for element in iterable:
if element:
return True
return False
|