/usr/share/pyshared/traits/trait_types.py is in python-traits 4.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 | #------------------------------------------------------------------------------
#
# Copyright (c) 2007, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 03/22/2007
#
#------------------------------------------------------------------------------
""" Core Trait definitions.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
import sys
import re
import datetime
from weakref import ref
from os.path import isfile, isdir
from types import FunctionType, MethodType, ClassType, InstanceType, ModuleType
from . import trait_handlers
from .protocols.api import adapt
from .trait_base import (strx, get_module_name, class_of, SequenceTypes, TypeTypes,
ClassTypes, Undefined, Missing, TraitsCache, python_version)
from .trait_handlers import (TraitType, TraitInstance, TraitListObject,
TraitSetObject, TraitSetEvent, TraitDictObject, TraitDictEvent,
ThisClass, items_event, RangeTypes)
from .traits import (Trait, trait_from, _TraitMaker, _InstanceArgs, code_editor,
html_editor, password_editor, shell_editor, date_editor, time_editor)
from .trait_errors import TraitError
#-------------------------------------------------------------------------------
# Constants:
#-------------------------------------------------------------------------------
MutableTypes = ( list, dict )
SetTypes = SequenceTypes + ( set, )
#-------------------------------------------------------------------------------
# Numeric type fast validator definitions:
#-------------------------------------------------------------------------------
if sys.modules.get( 'numpy' ) is not None:
# The numpy enhanced definitions:
from numpy import integer, floating, complexfloating, bool_
int_fast_validate = ( 11, int, integer )
long_fast_validate = ( 11, long, None, int, integer )
float_fast_validate = ( 11, float, floating, None, int, integer )
complex_fast_validate = ( 11, complex, complexfloating, None,
float, floating, int, integer )
bool_fast_validate = ( 11, bool, bool_ )
else:
# The standard python definitions (without numpy):
int_fast_validate = ( 11, int )
long_fast_validate = ( 11, long, None, int )
float_fast_validate = ( 11, float, None, int )
complex_fast_validate = ( 11, complex, None, float, int )
bool_fast_validate = ( 11, bool )
#-------------------------------------------------------------------------------
# Returns a default text editor:
#-------------------------------------------------------------------------------
def default_text_editor ( trait, type = None ):
auto_set = trait.auto_set
if auto_set is None:
auto_set = True
enter_set = trait.enter_set or False
from traitsui.api import TextEditor
if type is None:
return TextEditor( auto_set = auto_set, enter_set = enter_set )
return TextEditor( auto_set = auto_set,
enter_set = enter_set,
evaluate = type )
#-------------------------------------------------------------------------------
# 'Any' trait:
#-------------------------------------------------------------------------------
class Any ( TraitType ):
""" Defines a trait whose value can be anything.
"""
# The default value for the trait:
default_value = None
#: A description of the type of value this trait accepts:
info_text = 'any value'
#-------------------------------------------------------------------------------
# 'Generic' trait:
#-------------------------------------------------------------------------------
class Generic ( Any ):
""" Defines a trait whose value can be anything and whose definition can
be redefined via assignment using a TraitValue object.
"""
# The standard metadata for the trait:
metadata = { 'trait_value': True }
#-------------------------------------------------------------------------------
# 'BaseInt' and 'Int' traits:
#-------------------------------------------------------------------------------
class BaseInt ( TraitType ):
""" Defines a trait whose value must be a Python int.
"""
#: The function to use for evaluating strings to this type:
evaluate = int
#: The default value for the trait:
default_value = 0
#: A description of the type of value this trait accepts:
info_text = 'an integer'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, int ):
return value
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
return default_text_editor( self, int )
class Int ( BaseInt ):
""" Defines a trait whose value must be a Python int using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = int_fast_validate
#-------------------------------------------------------------------------------
# 'BaseLong' and 'Long' traits:
#-------------------------------------------------------------------------------
class BaseLong ( TraitType ):
""" Defines a trait whose value must be a Python long.
"""
#: The function to use for evaluating strings to this type:
evaluate = long
#: The default value for the trait:
default_value = 0L
#: A description of the type of value this trait accepts:
info_text = 'a long'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, long ):
return value
if isinstance( value, int ):
return long( value )
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
return default_text_editor( self, long )
class Long ( BaseLong ):
""" Defines a trait whose value must be a Python long using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = long_fast_validate
#-------------------------------------------------------------------------------
# 'BaseFloat' and 'Float' traits:
#-------------------------------------------------------------------------------
class BaseFloat ( TraitType ):
""" Defines a trait whose value must be a Python float.
"""
#: The function to use for evaluating strings to this type:
evaluate = float
#: The default value for the trait:
default_value = 0.0
#: A description of the type of value this trait accepts:
info_text = 'a float'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, float ):
return value
if isinstance( value, int ):
return float( value )
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
return default_text_editor( self, float )
class Float ( BaseFloat ):
""" Defines a trait whose value must be a Python float using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = float_fast_validate
#-------------------------------------------------------------------------------
# 'BaseComplex' and 'Complex' traits:
#-------------------------------------------------------------------------------
class BaseComplex ( TraitType ):
""" Defines a trait whose value must be a Python complex.
"""
#: The function to use for evaluating strings to this type:
evaluate = complex
#: The default value for the trait:
default_value = 0.0 + 0.0j
#: A description of the type of value this trait accepts:
info_text = 'a complex number'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, complex ):
return value
if isinstance( value, ( float, int ) ):
return complex( value )
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
return default_text_editor( self, complex )
class Complex ( BaseComplex ):
""" Defines a trait whose value must be a Python complex using a C-level
fast validator.
"""
#: The C-level fast validator to use:
fast_validate = complex_fast_validate
#-------------------------------------------------------------------------------
# 'BaseStr' and 'Str' traits:
#-------------------------------------------------------------------------------
class BaseStr ( TraitType ):
""" Defines a trait whose value must be a Python string.
"""
#: The default value for the trait:
default_value = ''
#: A description of the type of value this trait accepts:
info_text = 'a string'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, basestring ):
return value
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
from .traits import multi_line_text_editor
auto_set = self.auto_set
if auto_set is None:
auto_set = True
enter_set = self.enter_set or False
return multi_line_text_editor(auto_set, enter_set)
class Str ( BaseStr ):
""" Defines a trait whose value must be a Python string using a C-level
fast validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 11, basestring )
class Title ( Str ):
""" Defines a string type which by default uses the traits ui TitleEditor
when used in a View.
"""
def create_editor ( self ):
""" Returns the default traits UI editor to use for a trait.
"""
from traitsui.api import TitleEditor
if hasattr(self, 'allow_selection'):
return TitleEditor(allow_selection=self.allow_selection)
else:
return TitleEditor()
#-------------------------------------------------------------------------------
# 'BaseUnicode' and 'Unicode' traits:
#-------------------------------------------------------------------------------
class BaseUnicode ( TraitType ):
""" Defines a trait whose value must be a Python unicode string.
"""
#: The default value for the trait:
default_value = u''
#: A description of the type of value this trait accepts:
info_text = 'a unicode string'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, unicode ):
return value
if isinstance( value, str ):
return unicode( value )
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
from .traits import multi_line_text_editor
auto_set = self.auto_set
if auto_set is None:
auto_set = True
enter_set = self.enter_set or False
return multi_line_text_editor(auto_set, enter_set)
class Unicode ( BaseUnicode ):
""" Defines a trait whose value must be a Python unicode string using a
C-level fast validator.
"""
# The C-level fast validator to use:
fast_validate = ( 11, unicode, None, str )
#-------------------------------------------------------------------------------
# 'BaseBool' and 'Bool' traits:
#-------------------------------------------------------------------------------
class BaseBool ( TraitType ):
""" Defines a trait whose value must be a Python boolean.
"""
#: The function to use for evaluating strings to this type:
evaluate = bool
#: The default value for the trait:
default_value = False
#: A description of the type of value this trait accepts:
info_text = 'a boolean'
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if isinstance( value, bool ):
return value
self.error( object, name, value )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
from traitsui.api import BooleanEditor
return BooleanEditor()
class Bool ( BaseBool ):
""" Defines a trait whose value must be a Python boolean using a C-level
fast validator.
"""
#: The C-level fast validator to use:
fast_validate = bool_fast_validate
#-------------------------------------------------------------------------------
# 'BaseCInt' and 'CInt' traits:
#-------------------------------------------------------------------------------
class BaseCInt ( BaseInt ):
""" Defines a trait whose value must be a Python int and which supports
coercions of non-int values to int.
"""
#: The function to use for evaluating strings to this type:
evaluate = int
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return int( value )
except:
self.error( object, name, value )
class CInt ( BaseCInt ):
""" Defines a trait whose value must be a Python int and which supports
coercions of non-int values to int using a C-level fast validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, int )
#-------------------------------------------------------------------------------
# 'BaseCLong' and 'CLong' traits:
#-------------------------------------------------------------------------------
class BaseCLong ( BaseLong ):
""" Defines a trait whose value must be a Python long and which supports
coercions of non-long values to long.
"""
#: The function to use for evaluating strings to this type:
evaluate = long
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return long( value )
except:
self.error( object, name, value )
class CLong ( BaseCLong ):
""" Defines a trait whose value must be a Python long and which supports
coercions of non-long values to long using a C-level fast validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, long )
#-------------------------------------------------------------------------------
# 'BaseCFloat' and 'CFloat' traits:
#-------------------------------------------------------------------------------
class BaseCFloat ( BaseFloat ):
""" Defines a trait whose value must be a Python float and which supports
coercions of non-float values to float.
"""
#: The function to use for evaluating strings to this type:
evaluate = float
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return float( value )
except:
self.error( object, name, value )
class CFloat ( BaseCFloat ):
""" Defines a trait whose value must be a Python float and which supports
coercions of non-float values to float using a C-level fast validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, float )
#-------------------------------------------------------------------------------
# 'BaseCComplex' and 'CComplex' traits:
#-------------------------------------------------------------------------------
class BaseCComplex ( BaseComplex ):
""" Defines a trait whose value must be a Python complex and which supports
coercions of non-complex values to complex.
"""
#: The function to use for evaluating strings to this type:
evaluate = complex
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return complex( value )
except:
self.error( object, name, value )
class CComplex ( BaseCComplex ):
""" Defines a trait whose value must be a Python complex and which supports
coercions of non-complex values to complex using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, complex )
#-------------------------------------------------------------------------------
# 'BaseCStr' and 'CStr' traits:
#-------------------------------------------------------------------------------
class BaseCStr ( BaseStr ):
""" Defines a trait whose value must be a Python string and which supports
coercions of non-string values to string.
"""
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return str( value )
except:
try:
return unicode( value )
except:
self.error( object, name, value )
class CStr ( BaseCStr ):
""" Defines a trait whose value must be a Python string and which supports
coercions of non-string values to string using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 7, ( ( 12, str ), ( 12, unicode ) ) )
#-------------------------------------------------------------------------------
# 'BaseCUnicode' and 'CUnicode' traits:
#-------------------------------------------------------------------------------
class BaseCUnicode ( BaseUnicode ):
""" Defines a trait whose value must be a Python unicode string and which
supports coercions of non-unicode values to unicode.
"""
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return unicode( value )
except:
self.error( object, name, value )
class CUnicode ( BaseCUnicode ):
""" Defines a trait whose value must be a Python unicode string and which
supports coercions of non-unicode values to unicode using a C-level
fast validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, unicode )
#-------------------------------------------------------------------------------
# 'BaseCBool' and 'CBool' traits:
#-------------------------------------------------------------------------------
class BaseCBool ( BaseBool ):
""" Defines a trait whose value must be a Python boolean and which supports
coercions of non-boolean values to boolean.
"""
#: The function to use for evaluating strings to this type:
evaluate = bool
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
try:
return bool( value )
except:
self.error( object, name, value )
class CBool ( BaseCBool ):
""" Defines a trait whose value must be a Python boolean and which supports
coercions of non-boolean values to boolean using a C-level fast
validator.
"""
#: The C-level fast validator to use:
fast_validate = ( 12, bool )
#-------------------------------------------------------------------------------
# 'String' trait:
#-------------------------------------------------------------------------------
class String ( TraitType ):
""" Defines a trait whose value must be a Python string whose length is
optionally in a specified range, and which optionally matches a
specified regular expression.
"""
def __init__ ( self, value = '', minlen = 0, maxlen = sys.maxint,
regex = '', **metadata ):
""" Creates a String trait.
Parameters
----------
value : string
The default value for the string
minlen : integer
The minimum length allowed for the string
maxlen : integer
The maximum length allowed for the string
regex : string
A Python regular expression that the string must match
"""
super( String, self ).__init__( value, **metadata )
self.minlen = max( 0, minlen )
self.maxlen = max( self.minlen, maxlen )
self.regex = regex
self._init()
def _init ( self ):
""" Completes initialization of the trait at construction or unpickling
time.
"""
self._validate = 'validate_all'
if self.regex != '':
self.match = re.compile( self.regex ).match
if (self.minlen == 0) and (self.maxlen == sys.maxint):
self._validate = 'validate_regex'
elif (self.minlen == 0) and (self.maxlen == sys.maxint):
self._validate = 'validate_str'
else:
self._validate = 'validate_len'
def validate ( self, object, name, value ):
""" Validates that the value is a valid string.
"""
return getattr( self, self._validate )( object, name, value )
def validate_all ( self, object, name, value ):
""" Validates that the value is a valid string in the specified length
range which matches the specified regular expression.
"""
try:
value = strx( value )
if ((self.minlen <= len( value ) <= self.maxlen) and
(self.match( value ) is not None)):
return value
except:
pass
self.error( object, name, value )
def validate_str ( self, object, name, value ):
""" Validates that the value is a valid string.
"""
try:
return strx( value )
except:
pass
self.error( object, name, value )
def validate_len ( self, object, name, value ):
""" Validates that the value is a valid string in the specified length
range.
"""
try:
value = strx( value )
if self.minlen <= len( value ) <= self.maxlen:
return value
except:
pass
self.error( object, name, value )
def validate_regex ( self, object, name, value ):
""" Validates that the value is a valid string which matches the
specified regular expression.
"""
try:
value = strx( value )
if self.match( value ) is not None:
return value
except:
pass
self.error( object, name, value )
def info ( self ):
""" Returns a description of the trait.
"""
msg = ''
if (self.minlen != 0) and (self.maxlen != sys.maxint):
msg = ' between %d and %d characters long' % (
self.minlen, self.maxlen )
elif self.maxlen != sys.maxint:
msg = ' <= %d characters long' % self.maxlen
elif self.minlen != 0:
msg = ' >= %d characters long' % self.minlen
if self.regex != '':
if msg != '':
msg += ' and'
msg += (" matching the pattern '%s'" % self.regex)
return 'a string' + msg
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
return default_text_editor( self )
def __getstate__ ( self ):
""" Returns the current state of the trait.
"""
result = self.__dict__.copy()
for name in [ 'validate', 'match' ]:
if name in result:
del result[ name ]
return result
def __setstate__ ( self, state ):
""" Sets the current state of the trait.
"""
self.__dict__.update( state )
self._init()
#-------------------------------------------------------------------------------
# 'Regex' trait:
#-------------------------------------------------------------------------------
class Regex ( String ):
""" Defines a trait whose value is a Python string that matches a specified
regular expression.
"""
def __init__ ( self, value = '', regex = '.*', **metadata ):
""" Creates a Regex trait.
Parameters
----------
value : string
The default value of the trait
regex : string
The regular expression that the trait value must match.
Default Value
-------------
*value* or ''
"""
super( Regex, self ).__init__( value = value, regex = regex,
**metadata )
#-------------------------------------------------------------------------------
# 'Code' trait:
#-------------------------------------------------------------------------------
class Code ( String ):
""" Defines a trait whose value is a Python string that represents source
code in some language.
"""
#: The standard metadata for the trait:
metadata = { 'editor': code_editor }
#-------------------------------------------------------------------------------
# 'HTML' trait:
#-------------------------------------------------------------------------------
class HTML ( String ):
""" Defines a trait whose value must be a string that is interpreted as
being HTML. By default the value is parsed and displayed as HTML in
TraitsUI views. The validation of the value does not enforce HTML syntax.
"""
#: The standard metadata for the trait:
metadata = { 'editor': html_editor }
#-------------------------------------------------------------------------------
# 'Password' trait:
#-------------------------------------------------------------------------------
class Password ( String ):
""" Defines a trait whose value must be a string, optionally of constrained
length or matching a regular expression.
The trait is identical to a String trait except that by default it uses a
PasswordEditor in TraitsUI views, which obscures text entered by the user.
"""
#: The standard metadata for the trait:
metadata = { 'editor': password_editor }
#-------------------------------------------------------------------------------
# 'Callable' trait:
#-------------------------------------------------------------------------------
class Callable ( TraitType ):
""" Defines a trait whose value must be a Python callable.
"""
#: The standard metadata for the trait:
metadata = { 'copy': 'ref' }
#: The default value for the trait:
default_value = None
#: A description of the type of value this trait accepts:
info_text = 'a callable value'
def validate ( self, object, name, value ):
""" Validates that the value is a Python callable.
"""
if (value is None) or callable( value ):
return value
self.error( object, name, value )
#-------------------------------------------------------------------------------
# 'BaseType' base class:
#-------------------------------------------------------------------------------
class BaseType ( TraitType ):
""" Defines a trait whose value must be an instance of a simple Python type.
"""
def validate ( self, object, name, value ):
""" Validates that the value is a Python callable.
"""
if isinstance( value, self.fast_validate[1:] ):
return value
self.error( object, name, value )
class This ( BaseType ):
""" Defines a trait whose value must be an instance of the defining class.
"""
#: The C-level fast validator to use:
fast_validate = ( 2, )
#: A description of the type of value this trait accepts:
info_text = 'an instance of the same type as the receiver'
def __init__ ( self, value = None, allow_none = True, **metadata ):
super( This, self ).__init__( value, **metadata )
if allow_none:
self.fast_validate = ( 2, None )
self.validate = self.validate_none
self.info = self.info_none
def validate ( self, object, name, value ):
if isinstance( value, object.__class__ ):
return value
self.validate_failed( object, name, value )
def validate_none ( self, object, name, value ):
if isinstance( value, object.__class__ ) or (value is None):
return value
self.validate_failed( object, name, value )
def info ( self ):
return 'an instance of the same type as the receiver'
def info_none ( self ):
return 'an instance of the same type as the receiver or None'
def validate_failed ( self, object, name, value ):
kind = type( value )
if kind is InstanceType:
msg = 'class %s' % value.__class__.__name__
else:
msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) )
self.error( object, name, msg )
class self ( This ):
""" Defines a trait whose value must be an instance of the defining class
and whose default value is the object containing the trait.
"""
#: The default value type to use (i.e. 'self'):
default_value_type = 2
class Function ( TraitType ):
""" Defines a trait whose value must be a Python function.
"""
#: The C-level fast validator to use:
fast_validate = ( 11, FunctionType )
#: A description of the type of value this trait accepts:
info_text = 'a function'
class Method ( TraitType ):
""" Defines a trait whose value must be a Python method.
"""
#: The C-level fast validator to use:
fast_validate = ( 11, MethodType )
#: A description of the type of value this trait accepts:
info_text = 'a method'
class Class ( TraitType ):
""" Defines a trait whose value must be an old-style Python class.
"""
#: The C-level fast validator to use:
fast_validate = ( 11, ClassType )
#: A description of the type of value this trait accepts:
info_text = 'an old-style class'
class Module ( TraitType ):
""" Defines a trait whose value must be a Python module.
"""
#: The C-level fast validator to use:
fast_validate = ( 11, ModuleType )
#: A description of the type of value this trait accepts:
info_text = 'a module'
#-------------------------------------------------------------------------------
# 'Python' trait:
#-------------------------------------------------------------------------------
class Python ( TraitType ):
""" Defines a trait that provides behavior identical to a standard Python
attribute. That is, it allows any value to be assigned, and raises an
ValueError if an attempt is made to get the value before one has been
assigned. It has no default value. This trait is most often used in
conjunction with wildcard naming. See the *Traits User Manual* for
details on wildcards.
"""
#: The standard metadata for the trait:
metadata = { 'type': 'python' }
#: The default value for the trait:
default_value = Undefined
#-------------------------------------------------------------------------------
# 'ReadOnly' trait:
#-------------------------------------------------------------------------------
class ReadOnly ( TraitType ):
""" Defines a trait that is write-once, and then read-only.
The initial value of the attribute is the special, singleton object
Undefined. The trait allows any value to be assigned to the attribute
if the current value is the Undefined object. Once any other value is
assigned, no further assignment is allowed. Normally, the initial
assignment to the attribute is performed in the class constructor,
based on information passed to the constructor. If the read-only value
is known in advance of run time, use the Constant() function instead of
ReadOnly to define the trait.
"""
# Defines the CTrait type to use for this trait:
ctrait_type = 6
#: The default value for the trait:
default_value = Undefined
# Create a singleton instance as the trait:
ReadOnly = ReadOnly()
#-------------------------------------------------------------------------------
# 'Disallow' trait:
#-------------------------------------------------------------------------------
class Disallow ( TraitType ):
""" Defines a trait that prevents any value from being assigned or read.
That is, any attempt to get or set the value of the trait attribute
raises an exception. This trait is most often used in conjunction with
wildcard naming, for example, to catch spelling mistakes in attribute
names. See the *Traits User Manual* for details on wildcards.
"""
#: Defines the CTrait type to use for this trait:
ctrait_type = 5
# Create a singleton instance as the trait:
Disallow = Disallow()
#-------------------------------------------------------------------------------
# 'missing' trait:
#-------------------------------------------------------------------------------
class missing ( TraitType ):
""" Defines a trait used to indicate that a parameter is missing from a
type-checked method signature. Allows any value to be assigned; no
type-checking is performed; default value is the singleton Missing
object.
See the **traits.has_traits.method()**.
"""
#: The default value for the trait:
default_value = Missing
# Create a singleton instance as the trait:
missing = missing()
#-------------------------------------------------------------------------------
# 'Constant' trait:
#-------------------------------------------------------------------------------
class Constant ( TraitType ):
""" Defines a trait whose value is a constant.
"""
#: Defines the CTrait type to use for this trait:
ctrait_type = 7
#: The standard metadata for the trait:
metadata = { 'type': 'constant', 'transient': True }
def __init__ ( self, value, **metadata ):
""" Returns a constant, read-only trait whose value is *value*.
Parameters
----------
value : any type except a list or dictionary
The default value for the trait
Default Value
-------------
*value*
Description
-----------
Traits of this type are very space efficient (and fast) because
*value* is not stored in each instance using the trait, but only in
the trait object itself. The *value* cannot be a list or dictionary,
because those types have mutable values.
"""
if type( value ) in MutableTypes:
raise TraitError, \
"Cannot define a constant using a mutable list or dictionary"
super( Constant, self ).__init__( value, **metadata )
#-------------------------------------------------------------------------------
# 'Delegate' trait:
#-------------------------------------------------------------------------------
class Delegate ( TraitType ):
""" Defines a trait whose value is delegated to a trait on another object.
"""
#: Defines the CTrait type to use for this trait:
ctrait_type = 3
#: The standard metadata for the trait:
metadata = { 'type': 'delegate', 'transient': False }
def __init__ ( self, delegate, prefix = '', modify = False,
listenable = True, **metadata ):
""" Creates a Delegate trait.
"""
if prefix == '':
prefix_type = 0
elif prefix[-1:] != '*':
prefix_type = 1
else:
prefix = prefix[:-1]
if prefix != '':
prefix_type = 2
else:
prefix_type = 3
metadata[ '_delegate' ] = delegate
metadata[ '_prefix' ] = prefix
metadata[ '_listenable' ] = listenable
super( Delegate, self ).__init__( **metadata )
self.delegate = delegate
self.prefix = prefix
self.prefix_type = prefix_type
self.modify = modify
def as_ctrait ( self ):
""" Returns a CTrait corresponding to the trait defined by this class.
"""
trait = super( Delegate, self ).as_ctrait()
trait.delegate( self.delegate, self.prefix, self.prefix_type,
self.modify )
return trait
#-------------------------------------------------------------------------------
# 'DelegatesTo' trait:
#-------------------------------------------------------------------------------
class DelegatesTo ( Delegate ):
""" Defines a trait delegate that matches the standard 'delegate' design
pattern.
"""
def __init__ ( self, delegate, prefix = '', listenable = True, **metadata ):
""" Creates a "delegator" trait, whose definition and default value are
delegated to a *delegate* trait attribute on another object.
Parameters
----------
delegate : string
Name of the attribute on the current object which references the
object that is the trait's delegate
prefix : string
A prefix or substitution applied to the original attribute when
looking up the delegated attribute
listenable : Boolean
Indicates whether a listener can be attached to this attribute
such that changes to the delagate attribute will trigger it
Description
-----------
An object containing a delegator trait attribute must contain a
second attribute that references the object containing the delegate
trait attribute. The name of this second attribute is passed as the
*delegate* argument to the DelegatesTo() function.
The following rules govern the application of the prefix parameter:
* If *prefix* is empty or omitted, the delegation is to an attribute
of the delegate object with the same name as the delegator
attribute.
* If *prefix* is a valid Python attribute name, then the delegation
is to an attribute whose name is the value of *prefix*.
* If *prefix* ends with an asterisk ('*') and is longer than one
character, then the delegation is to an attribute whose name is
the value of *prefix*, minus the trailing asterisk, prepended to
the delegator attribute name.
* If *prefix* is equal to a single asterisk, the delegation is to an
attribute whose name is the value of the delegator object's
__prefix__ attribute prepended to delegator attribute name.
Note that any changes to the delegator attribute are actually
applied to the corresponding attribute on the delegate object. The
original object containing the delegator trait is not modified.
"""
super( DelegatesTo, self ).__init__( delegate,
prefix = prefix,
modify = True,
listenable = listenable,
**metadata )
#-------------------------------------------------------------------------------
# 'PrototypedFrom' trait:
#-------------------------------------------------------------------------------
class PrototypedFrom ( Delegate ):
""" Defines a trait delegate that matches the standard 'prototype' design
pattern.
"""
def __init__ ( self, prototype, prefix = '', listenable = True,
**metadata ):
""" Creates a "prototyped" trait, whose definition and default value are
obtained from a trait attribute on another object.
Parameters
----------
prototype : string
Name of the attribute on the current object which references the
object that is the trait's prototype
prefix : string
A prefix or substitution applied to the original attribute when
looking up the prototyped attribute
listenable : Boolean
Indicates whether a listener can be attached to this attribute
such that changes to the corresponding attribute on the
prototype object will trigger it
Description
-----------
An object containing a prototyped trait attribute must contain a
second attribute that references the object containing the prototype
trait attribute. The name of this second attribute is passed as the
*prototype* argument to the PrototypedFrom() function.
The following rules govern the application of the prefix parameter:
* If *prefix* is empty or omitted, the prototype delegation is to an
attribute of the prototype object with the same name as the
prototyped attribute.
* If *prefix* is a valid Python attribute name, then the prototype
delegation is to an attribute whose name is the value of *prefix*.
* If *prefix* ends with an asterisk ('*') and is longer than one
character, then the prototype delegation is to an attribute whose
name is the value of *prefix*, minus the trailing asterisk,
prepended to the prototyped attribute name.
* If *prefix* is equal to a single asterisk, the prototype
delegation is to an attribute whose name is the value of the
prototype object's __prefix__ attribute prepended to the
prototyped attribute name.
Note that any changes to the prototyped attribute are made to the
original object, not the prototype object. The prototype object is
only used to define to trait type and default value.
"""
super( PrototypedFrom, self ).__init__( prototype,
prefix = prefix,
modify = False,
listenable = listenable,
**metadata )
#-------------------------------------------------------------------------------
# 'Expression' class:
#-------------------------------------------------------------------------------
class Expression ( TraitType ):
""" Defines a trait whose value must be a valid Python expression. The
compiled form of a valid expression is stored as the mapped value of
the trait.
"""
#: The default value for the trait:
default_value = '0'
#: A description of the type of value this trait accepts:
info_text = 'a valid Python expression'
#: Indicate that this is a mapped trait:
is_mapped = True
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
"""
try:
return compile( value, '<string>', 'eval' )
except:
self.error( object, name, value )
def post_setattr ( self, object, name, value ):
""" Performs additional post-assignment processing.
"""
object.__dict__[ name + '_' ] = value
def mapped_value ( self, value ):
""" Returns the 'mapped' value for the specified **value**.
"""
return compile( value, '<string>', 'eval' )
def as_ctrait ( self ):
""" Returns a CTrait corresponding to the trait defined by this class.
"""
# Tell the C code that 'setattr' should store the original, unadapted
# value passed to it:
return super( Expression, self
).as_ctrait().setattr_original_value( True )
#-------------------------------------------------------------------------------
# 'PythonValue' trait:
#-------------------------------------------------------------------------------
class PythonValue ( Any ):
""" Defines a trait whose value can be of any type, and whose default
editor is a Python shell.
"""
#: The standard metadata for the trait:
metadata = { 'editor': shell_editor }
#-------------------------------------------------------------------------------
# 'BaseFile' and 'File' traits:
#-------------------------------------------------------------------------------
class BaseFile ( BaseStr ):
""" Defines a trait whose value must be the name of a file.
"""
#: A description of the type of value this trait accepts:
info_text = 'a file name'
def __init__ ( self, value = '', filter = None, auto_set = False,
entries = 0, exists = False, **metadata ):
""" Creates a File trait.
Parameters
----------
value : string
The default value for the trait
filter : string
A wildcard string to filter filenames in the file dialog box used by
the attribute trait editor.
auto_set : boolean
Indicates whether the file editor updates the trait value after
every key stroke.
exists : boolean
Indicates whether the trait value must be an existing file or
not.
Default Value
-------------
*value* or ''
"""
self.filter = filter
self.auto_set = auto_set
self.entries = entries
self.exists = exists
super( BaseFile, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
validated_value = super( BaseFile, self ).validate( object, name, value )
if not self.exists:
return validated_value
elif isfile( value ):
return validated_value
self.error( object, name, value )
def create_editor(self):
from traitsui.editors.file_editor import FileEditor
editor = FileEditor(
filter = self.filter or [],
auto_set = self.auto_set,
entries = self.entries,
)
return editor
class File ( BaseFile ):
""" Defines a trait whose value must be the name of a file using a C-level
fast validator.
"""
def __init__ ( self, value = '', filter = None, auto_set = False,
entries = 0, exists = False, **metadata ):
""" Creates a File trait.
Parameters
----------
value : string
The default value for the trait
filter : string
A wildcard string to filter filenames in the file dialog box used by
the attribute trait editor.
auto_set : boolean
Indicates whether the file editor updates the trait value after
every key stroke.
exists : boolean
Indicates whether the trait value must be an existing file or
not.
Default Value
-------------
*value* or ''
"""
if not exists:
# Define the C-level fast validator to use:
fast_validate = ( 11, basestring )
super( File, self ).__init__( value, filter, auto_set, entries, exists,
**metadata )
#-------------------------------------------------------------------------------
# 'BaseDirectory' and 'Directory' traits:
#-------------------------------------------------------------------------------
class BaseDirectory ( BaseStr ):
""" Defines a trait whose value must be the name of a directory.
"""
#: A description of the type of value this trait accepts:
info_text = 'a directory name'
def __init__ ( self, value = '', auto_set = False, entries = 0,
exists = False, **metadata ):
""" Creates a BaseDirectory trait.
Parameters
----------
value : string
The default value for the trait
auto_set : boolean
Indicates whether the directory editor updates the trait value
after every key stroke.
exists : boolean
Indicates whether the trait value must be an existing directory or
not.
Default Value
-------------
*value* or ''
"""
self.entries = entries
self.auto_set = auto_set
self.exists = exists
super( BaseDirectory, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
Note: The 'fast validator' version performs this check in C.
"""
if not self.exists:
return super( BaseDirectory, self ).validate( object, name, value )
if isdir( value ):
return value
self.error( object, name, value )
def create_editor(self):
from traitsui.editors.directory_editor import DirectoryEditor
editor = DirectoryEditor(
auto_set = self.auto_set,
entries = self.entries,
)
return editor
class Directory ( BaseDirectory ):
""" Defines a trait whose value must be the name of a directory using a
C-level fast validator.
"""
def __init__ ( self, value = '', auto_set = False, entries = 0,
exists = False, **metadata ):
""" Creates a Directory trait.
Parameters
----------
value : string
The default value for the trait
auto_set : boolean
Indicates whether the directory editor updates the trait value
after every key stroke.
exists : boolean
Indicates whether the trait value must be an existing directory or
not.
Default Value
-------------
*value* or ''
"""
# Define the C-level fast validator to use if the directory existence
#: test is not required:
if not exists:
self.fast_validate = ( 11, basestring )
super( Directory, self ).__init__( value, auto_set, entries, exists,
**metadata )
#-------------------------------------------------------------------------------
# 'BaseRange' and 'Range' traits:
#-------------------------------------------------------------------------------
class BaseRange ( TraitType ):
""" Defines a trait whose numeric value must be in a specified range.
"""
def __init__ ( self, low = None, high = None, value = None,
exclude_low = False, exclude_high = False,
**metadata ):
""" Creates a Range trait.
Parameters
----------
low : integer, float or string (i.e. extended trait name)
The low end of the range.
high : integer, float or string (i.e. extended trait name)
The high end of the range.
value : integer, float or string (i.e. extended trait name)
The default value of the trait
exclude_low : Boolean
Indicates whether the low end of the range is exclusive.
exclude_high : Boolean
Indicates whether the high end of the range is exclusive.
The *low*, *high*, and *value* arguments must be of the same type
(integer or float), except in the case where either *low* or *high* is
a string (i.e. extended trait name).
Default Value
-------------
*value*; if *value* is None or omitted, the default value is *low*,
unless *low* is None or omitted, in which case the default value is
*high*.
"""
if value is None:
if low is not None:
value = low
else:
value = high
super( BaseRange, self ).__init__( value, **metadata )
vtype = type( high )
if ((low is not None) and
(not issubclass( vtype, ( float, basestring ) ))):
vtype = type( low )
is_static = (not issubclass( vtype, basestring ))
if is_static and (vtype not in RangeTypes):
raise TraitError, ("Range can only be use for int, long or float "
"values, but a value of type %s was specified." %
vtype)
self._low_name = self._high_name = ''
self._vtype = Undefined
if vtype is float:
self._validate = 'float_validate'
kind = 4
self._type_desc = 'a floating point number'
if low is not None:
low = float( low )
if high is not None:
high = float( high )
elif vtype is long:
self._validate = 'long_validate'
self._type_desc = 'a long integer'
if low is not None:
low = long( low )
if high is not None:
high = long( high )
elif vtype is int:
self._validate = 'int_validate'
kind = 3
self._type_desc = 'an integer'
if low is not None:
low = int( low )
if high is not None:
high = int( high )
else:
self.get, self.set, self.validate = self._get, self._set, None
self._vtype = None
self._type_desc = 'a number'
if isinstance( high, basestring ):
self._high_name = high = 'object.' + high
else:
self._vtype = type( high )
high = compile( str( high ), '<string>', 'eval' )
if isinstance( low, basestring ):
self._low_name = low = 'object.' + low
else:
self._vtype = type( low )
low = compile( str( low ), '<string>', 'eval' )
if isinstance( value, basestring ):
value = 'object.' + value
self._value = compile( str( value ), '<string>', 'eval' )
self.default_value_type = 8
self.default_value = self._get_default_value
exclude_mask = 0
if exclude_low:
exclude_mask |= 1
if exclude_high:
exclude_mask |= 2
if is_static and (vtype is not long):
self.init_fast_validator( kind, low, high, exclude_mask )
#: Assign type-corrected arguments to handler attributes:
self._low = low
self._high = high
self._exclude_low = exclude_low
self._exclude_high = exclude_high
def init_fast_validator ( self, *args ):
""" Does nothing for the BaseRange class. Used in the Range class to
set up the fast validator.
"""
pass
def validate ( self, object, name, value ):
""" Validate that the value is in the specified range.
"""
return getattr( self, self._validate )( object, name, value )
def float_validate ( self, object, name, value ):
""" Validate that the value is a float value in the specified range.
"""
try:
if (isinstance( value, RangeTypes ) and
((self._low is None) or
(self._exclude_low and (self._low < value)) or
((not self._exclude_low) and (self._low <= value))) and
((self._high is None) or
(self._exclude_high and (self._high > value)) or
((not self._exclude_high) and (self._high >= value)))):
return float( value )
except:
pass
self.error( object, name, value )
def int_validate ( self, object, name, value ):
""" Validate that the value is an int value in the specified range.
"""
try:
if (isinstance( value, int_fast_validate[1:]) and
((self._low is None) or
(self._exclude_low and (self._low < value)) or
((not self._exclude_low) and (self._low <= value))) and
((self._high is None) or
(self._exclude_high and (self._high > value)) or
((not self._exclude_high) and (self._high >= value)))):
return value
except:
pass
self.error( object, name, value )
def long_validate ( self, object, name, value ):
""" Validate that the value is a long value in the specified range.
"""
try:
valid_types = list(long_fast_validate[1:])
valid_types.remove(None)
if (isinstance( value, tuple(valid_types) ) and
((self._low is None) or
(self._exclude_low and (self._low < value)) or
((not self._exclude_low) and (self._low <= value))) and
((self._high is None) or
(self._exclude_high and (self._high > value)) or
((not self._exclude_high) and (self._high >= value)))):
return value
except:
pass
self.error( object, name, value )
def _get_default_value ( self, object ):
""" Returns the default value of the range.
"""
return eval( self._value )
def _get ( self, object, name, trait ):
""" Returns the current value of a dynamic range trait.
"""
cname = '_traits_cache_' + name
value = object.__dict__.get( cname, Undefined )
if value is Undefined:
object.__dict__[ cname ] = value = eval( self._value )
low = eval( self._low )
high = eval( self._high )
if (low is not None) and (value < low):
value = low
elif (high is not None) and (value > high):
value = high
return self._typed_value( value, low, high )
def _set ( self, object, name, value ):
""" Sets the current value of a dynamic range trait.
"""
if not isinstance( value, basestring ):
try:
low = eval( self._low )
high = eval( self._high )
if (low is None) and (high is None):
if isinstance( value, RangeTypes ):
self._set_value( object, name, value )
return
else:
new_value = self._typed_value( value, low, high )
if (((low is None) or
(self._exclude_low and (low < new_value)) or
((not self._exclude_low) and (low <= new_value))) and
((high is None) or
(self._exclude_high and (high > new_value)) or
((not self._exclude_high) and (high >= new_value)))):
self._set_value( object, name, new_value )
return
except:
pass
self.error( object, name, value )
def _typed_value ( self, value, low, high ):
""" Returns the specified value with the correct type for the current
dynamic range.
"""
vtype = self._vtype
if vtype is None:
if low is not None:
vtype = type( low )
elif high is not None:
vtype = type( high )
else:
vtype = lambda x: x
return vtype( value )
def _set_value ( self, object, name, value ):
""" Sets the specified value as the value of the dynamic range.
"""
cname = '_traits_cache_' + name
old = object.__dict__.get( cname, Undefined )
if old is Undefined:
old = eval( self._value )
object.__dict__[ cname ] = value
if value != old:
object.trait_property_changed( name, old, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
if self._vtype is not Undefined:
low = eval( self._low )
high = eval( self._high )
low, high = ( self._typed_value( low, low, high ),
self._typed_value( high, low, high ) )
else:
low = self._low
high = self._high
if low is None:
if high is None:
return self._type_desc
return '%s <%s %s' % (
self._type_desc, '='[ self._exclude_high: ], high )
elif high is None:
return '%s >%s %s' % (
self._type_desc, '='[ self._exclude_low: ], low )
return '%s <%s %s <%s %s' % (
low, '='[ self._exclude_low: ], self._type_desc,
'='[ self._exclude_high: ], high )
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
# fixme: Needs to support a dynamic range editor.
auto_set = self.auto_set
if auto_set is None:
auto_set = True
from traitsui.api import RangeEditor
return RangeEditor( self,
mode = self.mode or 'auto',
cols = self.cols or 3,
auto_set = auto_set,
enter_set = self.enter_set or False,
low_label = self.low or '',
high_label = self.high or '',
low_name = self._low_name,
high_name = self._high_name )
class Range ( BaseRange ):
""" Defines a trait whose numeric value must be in a specified range using
a C-level fast validator.
"""
def init_fast_validator ( self, *args ):
""" Set up the C-level fast validator.
"""
self.fast_validate = args
#-------------------------------------------------------------------------------
# 'BaseEnum' and 'Enum' traits:
#-------------------------------------------------------------------------------
class BaseEnum ( TraitType ):
""" Defines a trait whose value must be one of a specified set of values.
"""
def __init__ ( self, *args, **metadata ):
""" Returns an Enum trait.
Parameters
----------
values : list or tuple
The enumeration of all legal values for the trait
Default Value
-------------
values[0]
"""
values = metadata.pop( 'values', None )
if isinstance( values, basestring ):
n = len( args )
if n == 0:
default_value = None
elif n == 1:
default_value = args[0]
else:
raise TraitError( "Incorrect number of arguments specified "
"when using the 'values' keyword" )
self.name = values
self.values = compile( 'object.' + values, '<string>', 'eval' )
self.get, self.set, self.validate = self._get, self._set, None
else:
default_value = args[0]
if (len( args ) == 1) and isinstance( default_value, SequenceTypes):
args = default_value
default_value = args[0]
elif (len( args ) == 2) and isinstance( args[1], SequenceTypes ):
args = args[1]
self.name = ''
self.values = tuple( args )
self.init_fast_validator( 5, self.values )
super( BaseEnum, self ).__init__( default_value, **metadata )
def init_fast_validator ( self, *args ):
""" Does nothing for the BaseEnum class. Used in the Enum class to set
up the fast validator.
"""
pass
def validate ( self, object, name, value ):
""" Validates that the value is one of the enumerated set of valid
values.
"""
if value in self.values:
return value
self.error( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
if self.name == '':
values = self.values
else:
values = eval( self.values )
return ' or '.join( [ repr( x ) for x in values ] )
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
from traitsui.api import EnumEditor
values = self
if self.name != '':
values = None
return EnumEditor( values = values,
name = self.name,
cols = self.cols or 3,
evaluate = self.evaluate,
mode = self.mode or 'radio' )
def _get ( self, object, name, trait ):
""" Returns the current value of a dynamic enum trait.
"""
value = self.get_value( object, name, trait )
values = eval( self.values )
if value not in values:
value = None
if len( values ) > 0:
value = values[0]
return value
def _set ( self, object, name, value ):
""" Sets the current value of a dynamic range trait.
"""
if value in eval( self.values ):
self.set_value( object, name, value )
else:
self.error( object, name, value )
class Enum ( BaseEnum ):
""" Defines a trait whose value must be one of a specified set of values
using a C-level fast validator.
"""
def init_fast_validator ( self, *args ):
""" Set up the C-level fast validator.
"""
self.fast_validate = args
#-------------------------------------------------------------------------------
# 'BaseTuple' and 'Tuple' traits:
#-------------------------------------------------------------------------------
class BaseTuple ( TraitType ):
""" Defines a trait whose value must be a tuple of specified trait types.
"""
def __init__ ( self, *types, **metadata ):
""" Returns a Tuple trait.
Parameters
----------
types : zero or more arguments
Definition of the default and allowed tuples. If the first item of
*types* is a tuple, it is used as the default value.
The remaining argument list is used to form a tuple that constrains
the values assigned to the returned trait. The trait's value must
be a tuple of the same length as the remaining argument list, whose
elements must match the types specified by the corresponding items
of the remaining argument list.
Default Value
-------------
1. If no arguments are specified, the default value is ().
2. If a tuple is specified as the first argument, it is the default
value.
3. If a tuple is not specified as the first argument, the default
value is a tuple whose length is the length of the argument list,
and whose values are the default values for the corresponding trait
types.
Example for case #2::
mytuple = Tuple(('Fred', 'Betty', 5))
The trait's value must be a 3-element tuple whose first and second
elements are strings, and whose third element is an integer. The
default value is ('Fred', 'Betty', 5).
Example for case #3::
mytuple = Tuple('Fred', 'Betty', 5)
The trait's value must be a 3-element tuple whose first and second
elements are strings, and whose third element is an integer. The
default value is ('','',0).
"""
if len( types ) == 0:
self.init_fast_validator( 11, tuple, None, list )
super( BaseTuple, self ).__init__( (), **metadata )
return
default_value = None
if isinstance( types[0], tuple ):
default_value, types = types[0], types[1:]
if len( types ) == 0:
types = [ Trait( element ) for element in default_value ]
self.types = tuple( [ trait_from( type ) for type in types ] )
self.init_fast_validator( 9, self.types )
if default_value is None:
default_value = tuple( [ type.default_value()[1]
for type in self.types ] )
super( BaseTuple, self ).__init__( default_value, **metadata )
def init_fast_validator ( self, *args ):
""" Saves the validation parameters.
"""
self.no_type_check = (args[0] == 11)
def validate ( self, object, name, value ):
""" Validates that the value is a valid tuple.
"""
if self.no_type_check:
if isinstance( value, tuple ):
return value
if isinstance( value, list ):
return tuple( value )
self.error( object, name, value )
try:
if isinstance( value, list ):
value = tuple( value )
if isinstance( value, tuple ):
types = self.types
if len( value ) == len( types ):
values = []
for i, type in enumerate( types ):
values.append( type.validate( object, name, value[i] ) )
return tuple( values )
except:
pass
self.error( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
if self.no_type_check:
return 'a tuple'
return 'a tuple of the form: (%s)' % (', '.join(
[ type.full_info( object, name, value )
for type in self.types ] ))
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
from traitsui.api import TupleEditor
auto_set = self.auto_set
if auto_set is None:
auto_set = True
enter_set = self.enter_set or False
return TupleEditor( types = self.types,
labels = self.labels or [],
cols = self.cols or 1,
auto_set = auto_set,
enter_set = enter_set )
class Tuple ( BaseTuple ):
""" Defines a trait whose value must be a tuple of specified trait types
using a C-level fast validator.
"""
def init_fast_validator ( self, *args ):
""" Set up the C-level fast validator.
"""
super( Tuple, self ).init_fast_validator( *args )
self.fast_validate = args
#-------------------------------------------------------------------------------
# 'List' trait:
#-------------------------------------------------------------------------------
class List ( TraitType ):
""" Defines a trait whose value must be a list whose items are of the
specified trait type.
"""
info_trait = None
default_value_type = 5
_items_event = None
def __init__ ( self, trait = None, value = None, minlen = 0,
maxlen = sys.maxint, items = True, **metadata ):
""" Returns a List trait.
Parameters
----------
trait : a trait or value that can be converted to a trait using Trait()
The type of item that the list contains. If not specified, the list
can contain items of any type.
value :
Default value for the list
minlen : integer
The minimum length of a list that can be assigned to the trait.
maxlen : integer
The maximum length of a list that can be assigned to the trait.
The length of the list assigned to the trait must be such that::
minlen <= len(list) <= maxlen
Default Value
-------------
*value* or None
"""
metadata.setdefault( 'copy', 'deep' )
if isinstance( trait, SequenceTypes ):
trait, value = value, list( trait )
if value is None:
value = []
self.item_trait = trait_from( trait )
self.minlen = max( 0, minlen )
self.maxlen = max( minlen, maxlen )
self.has_items = items
if self.item_trait.instance_handler == '_instance_changed_handler':
metadata.setdefault( 'instance_handler', '_list_changed_handler' )
super( List, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the values is a valid list.
"""
if (isinstance( value, list ) and
(self.minlen <= len( value ) <= self.maxlen)):
if object is None:
return value
return TraitListObject( self, object, name, value )
self.error( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
if self.minlen == 0:
if self.maxlen == sys.maxint:
size = 'items'
else:
size = 'at most %d items' % self.maxlen
else:
if self.maxlen == sys.maxint:
size = 'at least %d items' % self.minlen
else:
size = 'from %s to %s items' % (
self.minlen, self.maxlen )
return 'a list of %s which are %s' % (
size, self.item_trait.full_info( object, name, value ) )
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
handler = self.item_trait.handler
if isinstance( handler, TraitInstance ) and (self.mode != 'list'):
from .api import HasTraits
if issubclass( handler.aClass, HasTraits ):
from traitsui.api import TableEditor
return TableEditor()
from traitsui.api import ListEditor
return ListEditor( trait_handler = self,
rows = self.rows or 5,
use_notebook = self.use_notebook is True,
page_name = self.page_name or '' )
def inner_traits ( self ):
""" Returns the *inner trait* (or traits) for this trait.
"""
return ( self.item_trait, )
#-- Private Methods --------------------------------------------------------
def items_event ( self ):
return items_event()
#-------------------------------------------------------------------------------
# 'CList' trait:
#-------------------------------------------------------------------------------
class CList ( List ):
""" Defines a trait whose values must be a list whose items are of the
specified trait type or which can be coerced to a list whose values are
of the specified trait type.
"""
def validate ( self, object, name, value ):
""" Validates that the values is a valid list.
"""
if not isinstance( value, list ):
try:
# Should work for all iterables as well as strings (which do
# not define an __iter__ method)
value = list( value )
except (ValueError, TypeError):
value = [ value ]
return super( CList, self ).validate( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
return '%s or %s' % (
self.item_trait.full_info( object, name, value),
super( CList, self ).full_info( object, name, value ) )
#-------------------------------------------------------------------------------
# 'Set' trait:
#-------------------------------------------------------------------------------
class Set ( TraitType ):
""" Defines a trait whose value must be a set whose items are of the
specified trait type.
"""
info_trait = None
default_value_type = 9
_items_event = None
def __init__ ( self, trait = None, value = None, items = True, **metadata ):
""" Returns a Set trait.
Parameters
----------
trait : a trait or value that can be converted to a trait using Trait()
The type of item that the list contains. If not specified, the list
can contain items of any type.
value :
Default value for the set
Default Value
-------------
*value* or None
"""
metadata.setdefault( 'copy', 'deep' )
if isinstance( trait, SetTypes ):
trait, value = value, set( trait )
if value is None:
value = set()
self.item_trait = trait_from( trait )
self.has_items = items
super( Set, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the values is a valid set.
"""
if isinstance( value, set ):
if object is None:
return value
return TraitSetObject( self, object, name, value )
self.error( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
return 'a set of %s' % self.item_trait.full_info( object, name, value )
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
# fixme: Needs to be customized for sets.
handler = self.item_trait.handler
if isinstance( handler, TraitInstance ) and (self.mode != 'list'):
from .api import HasTraits
if issubclass( handler.aClass, HasTraits ):
try:
object = handler.aClass()
from traitsui.table_column import ObjectColumn
from traitsui.table_filter import (EvalFilterTemplate,
RuleFilterTemplate, MenuFilterTemplate, EvalTableFilter)
from traitsui.api import TableEditor
return TableEditor(
columns = [ ObjectColumn( name = name )
for name in object.editable_traits() ],
filters = [ RuleFilterTemplate,
MenuFilterTemplate,
EvalFilterTemplate ],
edit_view = '',
orientation = 'vertical',
search = EvalTableFilter(),
deletable = True,
row_factory = handler.aClass )
except:
pass
from traitsui.api import ListEditor
return ListEditor( trait_handler = self,
rows = self.rows or 5,
use_notebook = self.use_notebook is True,
page_name = self.page_name or '' )
def inner_traits ( self ):
""" Returns the *inner trait* (or traits) for this trait.
"""
return ( self.item_trait, )
#-- Private Methods --------------------------------------------------------
def items_event ( self ):
if self.__class__._items_event is None:
self.__class__._items_event = \
Event( TraitSetEvent, is_base = False ).as_ctrait()
return self.__class__._items_event
#-------------------------------------------------------------------------------
# 'CSet' trait:
#-------------------------------------------------------------------------------
class CSet ( Set ):
""" Defines a trait whose values must be a set whose items are of the
specified trait type or which can be coerced to a set whose values are
of the specified trait type.
"""
def validate ( self, object, name, value ):
""" Validates that the values is a valid list.
"""
if not isinstance( value, set ):
try:
# Should work for all iterables as well as strings (which do
# not define an __iter__ method)
value = set( value )
except ( ValueError, TypeError ):
value = set( [ value ] )
return super( CList, self ).validate( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
return '%s or %s' % (
self.item_trait.full_info( object, name, value),
super( CSet, self ).full_info( object, name, value ) )
#-------------------------------------------------------------------------------
# 'Dict' trait:
#-------------------------------------------------------------------------------
class Dict ( TraitType ):
""" Defines a trait whose value must be a dictionary, optionally with
specified types for keys and values.
"""
info_trait = None
default_value_type = 6
_items_event = None
def __init__ ( self, key_trait = None, value_trait = None, value = None,
items = True, **metadata ):
""" Returns a Dict trait.
Parameters
----------
key_trait : a trait or value that can convert to a trait using Trait()
The trait type for keys in the dictionary; if not specified, any
values can be used as keys.
value_trait : a trait or value that can convert to a trait using Trait()
The trait type for values in the dictionary; if not specified, any
values can be used as dictionary values.
value : a dictionary
The default value for the returned trait
items : Boolean
Indicates whether the value contains items
Default Value
-------------
*value* or {}
"""
if isinstance( key_trait, dict ):
key_trait, value_trait, value = value_trait, value, key_trait
if value is None:
value = {}
self.key_trait = trait_from( key_trait )
self.value_trait = trait_from( value_trait )
self.has_items = items
handler = self.value_trait.handler
if (handler is not None) and handler.has_items:
handler = handler.clone()
handler.has_items = False
self.value_handler = handler
super( Dict, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the value is a valid dictionary.
"""
if isinstance( value, dict ):
if value is None:
return value
return TraitDictObject( self, object, name, value )
self.error( object, name, value )
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
return ('a dictionary with keys which are %s and with values which '
'are %s') % (
self.key_trait.full_info( object, name, value ),
self.value_trait.full_info( object, name, value ) )
def create_editor ( self ):
""" Returns the default UI editor for the trait.
"""
from traitsui.api import TextEditor
return TextEditor( evaluate = eval )
def inner_traits ( self ):
""" Returns the *inner trait* (or traits) for this trait.
"""
return ( self.key_trait, self.value_trait )
#-- Private Methods --------------------------------------------------------
def items_event ( self ):
cls = self.__class__
if cls._items_event is None:
cls._items_event = \
Event( TraitDictEvent, is_base = False ).as_ctrait()
return cls._items_event
#-------------------------------------------------------------------------------
# 'BaseInstance' and 'Instance' traits:
#-------------------------------------------------------------------------------
# Allowed values and mappings for the 'adapt' keyword:
AdaptMap = {
'no': 0,
'yes': 1,
'default': 2
}
class BaseClass ( TraitType ):
""" Base class for types which have an associated class which can be
determined dynamically by specifying a string name for the class (e.g.
'package1.package2.module.class'.
Any subclass must define instances with 'klass' and 'module' attributes
that contain the string name of the class (or actual class object) and
the module name that contained the original trait definition (used for
resolving local class names (e.g. 'LocalClass')).
This is an abstract class that only provides helper methods used to
resolve the class name into an actual class object.
"""
def resolve_class ( self, object, name, value ):
klass = self.validate_class( self.find_class( self.klass ) )
if klass is None:
self.validate_failed( object, name, value )
self.klass = klass
def validate_class ( self, klass ):
return klass
def find_class ( self, klass ):
module = self.module
col = klass.rfind( '.' )
if col >= 0:
module = klass[ : col ]
klass = klass[ col + 1: ]
theClass = getattr( sys.modules.get( module ), klass, None )
if (theClass is None) and (col >= 0):
try:
mod = __import__( module )
for component in module.split( '.' )[1:]:
mod = getattr( mod, component )
theClass = getattr( mod, klass, None )
except:
pass
return theClass
def validate_failed ( self, object, name, value ):
self.error( object, name, value )
def validate_implements ( value, klass, unused = None ):
""" Checks to see if a specified value implements the instance class
interface (if it is an interface).
"""
from .has_traits import Interface
from .interface_checker import check_implements
from .protocols.api import declareImplementation
rc = (issubclass( klass, Interface) and
check_implements( value.__class__, klass ))
if rc:
declareImplementation( value.__class__, instancesProvide = [ klass ] )
return rc
#: Tell the C-base code about the 'validate_implements' function (used by the
#: 'fast_validate' code for Instance types):
from . import ctraits
ctraits._validate_implements( validate_implements )
class BaseInstance ( BaseClass ):
""" Defines a trait whose value must be an instance of a specified class,
or one of its subclasses.
"""
adapt_default = 'no'
def __init__ ( self, klass = None, factory = None, args = None, kw = None,
allow_none = True, adapt = None, module = None,
**metadata ):
""" Returns an Instance trait.
Parameters
----------
klass : class or instance
The object that forms the basis for the trait; if it is an
instance, then trait values must be instances of the same class or
a subclass. This object is not the default value, even if it is an
instance.
factory : callable
A callable, typically a class, that when called with *args* and
*kw*, returns the default value for the trait. If not specified,
or *None*, *klass* is used as the factory.
args : tuple
Positional arguments for generating the default value.
kw : dictionary
Keyword arguments for generating the default value.
allow_none : boolean
Indicates whether None is allowed as a value.
adapt : string
A string specifying how adaptation should be applied. The possible
values are:
- 'no': Adaptation is not allowed.
- 'yes': Adaptation is allowed. If adaptation fails, an
exception should be raised.
- 'default': Adapation is allowed. If adaptation fails, the
default value for the trait should be used.
Default Value
-------------
**None** if *klass* is an instance or if it is a class and *args* and
*kw* are not specified. Otherwise, the default value is the instance
obtained by calling ``klass(*args, **kw)``. Note that the constructor
call is performed each time a default value is assigned, so each
default value assigned is a unique instance.
"""
if klass is None:
raise TraitError( 'A %s trait must have a class specified.' %
self.__class__.__name__ )
metadata.setdefault( 'copy', 'deep' )
metadata.setdefault( 'instance_handler', '_instance_changed_handler' )
adapt = adapt or self.adapt_default
if adapt not in AdaptMap:
raise TraitError( "'adapt' must be 'yes', 'no' or 'default'." )
if isinstance( factory, tuple ):
if args is None:
args, factory = factory, klass
elif isinstance( args, dict ):
factory, args, kw = klass, factory, args
elif (kw is None) and isinstance( factory, dict ):
kw, factory = factory, klass
elif ((args is not None) or (kw is not None)) and (factory is None):
factory = klass
self._allow_none = allow_none
self.adapt = AdaptMap[ adapt ]
self.module = module or get_module_name()
if isinstance( klass, basestring ):
self.klass = klass
else:
if not isinstance( klass, ClassTypes ):
klass = klass.__class__
self.klass = klass
self.init_fast_validate()
value = factory
if factory is not None:
if args is None:
args = ()
if kw is None:
if isinstance( args, dict ):
kw = args
args = ()
else:
kw = {}
elif not isinstance( kw, dict ):
raise TraitError( "The 'kw' argument must be a dictionary." )
if ((not callable( factory )) and
(not isinstance( factory, basestring ))):
if (len( args ) > 0) or (len( kw ) > 0):
raise TraitError( "'factory' must be callable" )
else:
value = _InstanceArgs( factory, args, kw )
self.default_value = value
super( BaseInstance, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the value is a valid object instance.
"""
if value is None:
if self._allow_none:
return value
self.validate_failed( object, name, value )
if isinstance( self.klass, basestring ):
self.resolve_class( object, name, value )
if self.adapt == 0:
try:
if value is adapt( value, self.klass ):
return value
except:
if validate_implements( value, self.klass ):
return value
elif self.adapt == 1:
try:
return adapt( value, self.klass )
except:
if validate_implements( value, self.klass ):
return value
else:
result = adapt( value, self.klass, None )
if result is None:
if validate_implements( value, self.klass ):
return value
result = self.default_value
if isinstance( result, _InstanceArgs ):
result = result[0]( *result[1], **result[2] )
return result
self.validate_failed( object, name, value )
def info ( self ):
""" Returns a description of the trait.
"""
klass = self.klass
if not isinstance( klass, basestring ):
klass = klass.__name__
if self.adapt == 0:
result = class_of( klass )
else:
result = ('an implementor of, or can be adapted to implement, %s' %
klass)
if self._allow_none:
return result + ' or None'
return result
def get_default_value ( self ):
""" Returns a tuple of the form: ( default_value_type, default_value )
which describes the default value for this trait.
"""
dv = self.default_value
dvt = self.default_value_type
if dvt < 0:
if not isinstance( dv, _InstanceArgs ):
return super( BaseInstance, self ).get_default_value()
self.default_value_type = dvt = 7
self.default_value = dv = ( self.create_default_value,
dv.args, dv.kw )
return ( dvt, dv )
def create_editor ( self ):
""" Returns the default traits UI editor for this type of trait.
"""
from traitsui.api import InstanceEditor
return InstanceEditor( label = self.label or '',
view = self.view or '',
kind = self.kind or 'live' )
#-- Private Methods --------------------------------------------------------
def create_default_value ( self, *args, **kw ):
klass = args[0]
if isinstance( klass, basestring ):
klass = self.validate_class( self.find_class( klass ) )
if klass is None:
raise TraitError, 'Unable to locate class: ' + args[0]
return klass( *args[1:], **kw )
#: fixme: Do we still need this method using the new style?...
def allow_none ( self ):
self._allow_none = True
self.init_fast_validate()
def init_fast_validate ( self ):
""" Does nothing for the BaseInstance' class. Used by the 'Instance',
'AdaptedTo' and 'AdaptsTo' classes to set up the C-level fast
validator.
"""
pass
def resolve_class ( self, object, name, value ):
super( BaseInstance, self ).resolve_class( object, name, value )
#: fixme: The following is quite ugly, because it wants to try and fix
#: the trait referencing this handler to use the 'fast path' now that the
#: actual class has been resolved. The problem is finding the trait,
# especially in the case of List(Instance('foo')), where the
# object.base_trait(...) value is the List trait, not the Instance
# trait, so we need to check for this and pull out the List
# 'item_trait'. Obviously this does not extend well to other traits
# containing nested trait references (Dict?)...
self.init_fast_validate()
trait = object.base_trait( name )
handler = trait.handler
if handler is not self:
set_validate = getattr( handler, 'set_validate', None )
if set_validate is not None:
# The outer trait is a TraitCompound. Recompute its
# fast_validate table now that we have updated ours.
# FIXME: there are probably still issues if the TraitCompound is
# further nested.
set_validate()
else:
item_trait = getattr( handler, 'item_trait', None )
if item_trait is not None and item_trait.handler is self:
# The outer trait is a List trait.
trait = item_trait
handler = self
else:
return
if handler.fast_validate is not None:
trait.set_validate( handler.fast_validate )
class Instance ( BaseInstance ):
""" Defines a trait whose value must be an instance of a specified class,
or one of its subclasses using a C-level fast validator.
"""
def init_fast_validate ( self ):
""" Sets up the C-level fast validator.
"""
from .has_traits import Interface
if (self.adapt == 0) and (not issubclass( self.klass, Interface )):
fast_validate = [ 1, self.klass ]
if self._allow_none:
fast_validate = [ 1, None, self.klass ]
if self.klass in TypeTypes:
fast_validate[0] = 0
self.fast_validate = tuple( fast_validate )
else:
self.fast_validate = ( 19, self.klass, self.adapt,
self._allow_none )
class AdaptedTo ( Instance ):
adapt_default = 'yes'
def post_setattr ( self, object, name, value ):
""" Performs additional post-assignment processing.
"""
# Save the original, unadapted value in the mapped trait:
object.__dict__[ name + '_' ] = value
def as_ctrait ( self ):
""" Returns a CTrait corresponding to the trait defined by this class.
"""
return self.modify_ctrait( super( AdaptedTo, self ).as_ctrait() )
def modify_ctrait ( self, ctrait ):
# Tell the C code that the 'post_setattr' method wants the original,
# unadapted value passed to 'setattr':
return ctrait.post_setattr_original_value( True )
class AdaptsTo ( AdaptedTo ):
def modify_ctrait ( self, ctrait ):
# Tell the C code that 'setattr' should store the original, unadapted
# value passed to it:
return ctrait.setattr_original_value( True )
#-------------------------------------------------------------------------------
# 'Type' trait:
#-------------------------------------------------------------------------------
class Type ( BaseClass ):
""" Defines a trait whose value must be a subclass of a specified class.
"""
def __init__ ( self, value = None, klass = None, allow_none = True,
**metadata ):
""" Returns an Type trait.
Parameters
----------
value : class or None
klass : class or None
allow_none : boolean
Indicates whether None is allowed as an assignable value. Even if
**False**, the default *value* may be **None**.
Default Value
-------------
**None** if *klass* is an instance or if it is a class and *args* and
*kw* are not specified. Otherwise, the default value is the instance
obtained by calling ``klass(*args, **kw)``. Note that the constructor
call is performed each time a default value is assigned, so each
default value assigned is a unique instance.
"""
if value is None:
if klass is None:
klass = object
elif klass is None:
klass = value
if isinstance( klass, basestring ):
self.validate = self.resolve
elif not isinstance( klass, ClassTypes ):
raise TraitError( "A Type trait must specify a class." )
self.klass = klass
self._allow_none = allow_none
self.module = get_module_name()
super( Type, self ).__init__( value, **metadata )
def validate ( self, object, name, value ):
""" Validates that the value is a valid object instance.
"""
try:
if issubclass( value, self.klass ):
return value
except:
if (value is None) and (self._allow_none):
return value
self.error( object, name, value )
def resolve ( self, object, name, value ):
""" Resolves a class originally specified as a string into an actual
class, then resets the trait so that future calls will be handled by
the normal validate method.
"""
if isinstance( self.klass, basestring ):
self.resolve_class( object, name, value )
del self.validate
return self.validate( object, name, value )
def info ( self ):
""" Returns a description of the trait.
"""
klass = self.klass
if not isinstance( klass, basestring ):
klass = klass.__name__
result = 'a subclass of ' + klass
if self._allow_none:
return result + ' or None'
return result
def get_default_value ( self ):
""" Returns a tuple of the form: ( default_value_type, default_value )
which describes the default value for this trait.
"""
if not isinstance( self.default_value, basestring ):
return super( Type, self ).get_default_value()
return ( 7, ( self.resolve_default_value, (), None ) )
def resolve_default_value ( self ):
""" Resolves a class name into a class so that it can be used to
return the class as the default value of the trait.
"""
if isinstance( self.klass, basestring ):
try:
self.resolve_class( None, None, None )
del self.validate
except:
raise TraitError( 'Could not resolve %s into a valid class' %
self.klass )
return self.klass
#-------------------------------------------------------------------------------
# 'Event' trait:
#-------------------------------------------------------------------------------
class Event ( TraitType ):
def __init__ ( self, trait = None, **metadata ):
metadata[ 'type' ] = 'event'
metadata[ 'transient' ] = True
super( Event, self ).__init__( **metadata )
self.trait = None
if trait is not None:
self.trait = trait_from( trait )
validate = self.trait.get_validate()
if validate is not None:
self.fast_validate = validate
def full_info ( self, object, name, value ):
""" Returns a description of the trait.
"""
trait = self.trait
if trait is None:
return 'any value'
return trait.full_info( object, name, value )
# Handle circular module dependencies:
trait_handlers.Event = Event
#-------------------------------------------------------------------------------
# 'Button' trait:
#-------------------------------------------------------------------------------
class Button ( Event ):
""" Defines a trait whose UI editor is a button.
"""
def __init__ ( self, label = '', image = None, values_trait = None, style = 'button',
orientation = 'vertical', width_padding = 7,
height_padding = 5, view = None, **metadata ):
""" Returns a trait event whose editor is a button.
Parameters
----------
label : string
The label for the button
image : pyface.ImageResource
An image to display on the button
style : one of: 'button', 'radio', 'toolbar', 'checkbox'
The style of button to display
values_trait : string
For a "button" or "toolbar" style, the name of an enum
trait whose values will populate a drop-down menu on the button.
The selected value will replace the label on the button.
orientation : one of: 'horizontal', 'vertical'
The orientation of the label relative to the image
width_padding : integer between 0 and 31
Extra padding (in pixels) added to the left and right sides of
the button
height_padding : integer between 0 and 31
Extra padding (in pixels) added to the top and bottom of the
button
Default Value
-------------
No default value because events do not store values.
"""
self.label = label
self.values_trait = values_trait
self.image = image
self.style = style
self.orientation = orientation
self.width_padding = width_padding
self.height_padding = height_padding
self.view = view
super( Button, self ).__init__( **metadata )
def create_editor(self):
from traitsui.api import ButtonEditor
editor = ButtonEditor(
label = self.label,
values_trait = self.values_trait,
image = self.image,
style = self.style,
orientation = self.orientation,
width_padding = self.width_padding,
height_padding = self.height_padding,
view = self.view,
)
return editor
#-------------------------------------------------------------------------------
# 'ToolbarButton' trait:
#-------------------------------------------------------------------------------
class ToolbarButton ( Button ):
""" Defines a trait whose UI editor is a button that can be used on a
toolbar.
"""
def __init__ ( self, label = '', image = None, style = 'toolbar',
orientation = 'vertical', width_padding = 2,
height_padding = 2, **metadata ):
""" Returns a trait event whose editor is a toolbar button.
Parameters
----------
label : string
The label for the button
image : pyface.ImageResource
An image to display on the button
style : one of: 'button', 'radio', 'toolbar', 'checkbox'
The style of button to display
orientation : one of: 'horizontal', 'vertical'
The orientation of the label relative to the image
width_padding : integer between 0 and 31
Extra padding (in pixels) added to the left and right sides of
the button
height_padding : integer between 0 and 31
Extra padding (in pixels) added to the top and bottom of the
button
Default Value
-------------
No default value because events do not store values.
"""
super( ToolbarButton, self ).__init__( label,
image=image,
style=style,
orientation=orientation,
width_padding=width_padding,
height_padding=height_padding,
**metadata )
#-------------------------------------------------------------------------------
# 'Either' trait:
#-------------------------------------------------------------------------------
class Either ( TraitType ):
""" Defines a trait whose value can be any of of a specified list of traits.
"""
def __init__ ( self, *traits, **metadata ):
""" Creates a trait whose value can be any of of a specified list of
traits.
"""
self.trait_maker = _TraitMaker( metadata.pop( 'default', None ),
*traits, **metadata )
def as_ctrait ( self ):
""" Returns a CTrait corresponding to the trait defined by this class.
"""
return self.trait_maker.as_ctrait()
#-------------------------------------------------------------------------------
# 'Symbol' trait:
#-------------------------------------------------------------------------------
class Symbol ( TraitType ):
#: A description of the type of value this trait accepts:
info_text = ("an object or a string of the form "
"'[package.package...package.]module[:symbol[([arg1,...,argn])]]' "
"specifying where to locate the object")
def get ( self, object, name ):
value = object.__dict__.get( name, Undefined )
if value is Undefined:
cache = TraitsCache + name
ref = object.__dict__.get( cache )
if ref is None:
object.__dict__[ cache ] = ref = \
object.trait( name ).default_value_for( object, name )
if isinstance( ref, basestring ):
object.__dict__[ name ] = value = self._resolve( ref )
return value
def set ( self, object, name, value ):
dict = object.__dict__
old = dict.get( name, Undefined )
if isinstance( value, basestring ):
dict.pop( name, None )
dict[ TraitsCache + name ] = value
object.trait_property_changed( name, old )
else:
dict[ name ] = value
object.trait_property_changed( name, old, value )
def _resolve ( self, ref ):
try:
path = ref.split( ':', 1 )
module = __import__( path[0] )
for component in path[0].split( '.' )[1:]:
module = getattr( module, component )
if len( path ) == 1:
return module
elements = path[1].split( '(', 1 )
symbol = getattr( module, elements[0] )
if len( elements ) == 1:
return symbol
args = eval( '(' + elements[1] )
if not isinstance( args, tuple ):
args = ( args, )
return symbol( *args )
except:
raise TraitError( "Could not resolve '%s' into a valid symbol." %
ref )
if python_version >= 2.5:
import uuid
#---------------------------------------------------------------------------
# 'UUID' trait:
#---------------------------------------------------------------------------
class UUID ( TraitType ):
""" Defines a trait whose value is a globally unique UUID (type 4).
"""
#: A description of the type of value this trait accepts:
info_text = 'a read-only UUID'
def __init__ ( self, **metadata ):
""" Returns a UUID trait.
"""
super( UUID, self ).__init__( None, **metadata )
def validate ( self, object, name, value ):
""" Raises an error, since no values can be assigned to the trait.
"""
raise TraitError( "The '%s' trait of %s instance is a read-only "
"UUID." % ( name, class_of( object ) ) )
def get_default_value ( self ):
return ( 7, ( self._create_uuid, (), None ) )
#-- Private Methods ---------------------------------------------------
def _create_uuid ( self ):
return uuid.uuid4()
#-------------------------------------------------------------------------------
# 'WeakRef' trait:
#-------------------------------------------------------------------------------
class WeakRef ( Instance ):
""" Returns a trait whose value must be an instance of the same type
(or a subclass) of the specified *klass*, which can be a class or an
instance. Note that the trait only maintains a weak reference to the
assigned value.
"""
def __init__ ( self, klass = 'traits.has_traits.HasTraits',
allow_none = False, adapt = 'yes', **metadata ):
""" Returns a WeakRef trait.
Only a weak reference is maintained to any object assigned to a WeakRef
trait. If no other references exist to the assigned value, the value
may be garbage collected, in which case the value of the trait becomes
None. In all other cases, the value returned by the trait is the
original object.
Parameters
----------
klass : class or instance
The object that forms the basis for the trait. If *klass* is
omitted, then values must be an instance of HasTraits.
allow_none : boolean
Indicates whether None can be assigned
Default Value
-------------
**None** (even if allow_none==False)
"""
metadata.setdefault( 'copy', 'ref' )
super( WeakRef, self ).__init__( klass, allow_none = allow_none,
adapt = adapt, module = get_module_name(), **metadata )
def get ( self, object, name ):
value = getattr( object, name + '_', None )
if value is not None:
return value.value()
return None
def set ( self, object, name, value ):
old = self.get( object, name )
if value is None:
object.__dict__[ name + '_' ] = None
else:
object.__dict__[ name + '_' ] = HandleWeakRef( object, name, value )
if value is not old:
object.trait_property_changed( name, old, value )
def resolve_class ( self, object, name, value ):
# fixme: We have to override this method to prevent the 'fast validate'
# from being set up, since the trait using this is a 'property' style
# trait which is not currently compatible with the 'fast_validate'
# style (causes internal Python SystemError messages).
klass = self.find_class( self.klass )
if klass is None:
self.validate_failed( object, name, value )
self.klass = klass
#-- Private Class --------------------------------------------------------------
class HandleWeakRef ( object ):
def __init__ ( self, object, name, value ):
self.object = ref( object )
self.name = name
self.value = ref( value, self._value_freed )
def _value_freed ( self, ref ):
object = self.object()
if object is not None:
object.trait_property_changed( self.name, Undefined, None )
#-- Date Trait definition ----------------------------------------------------
Date = BaseInstance(datetime.date, editor=date_editor)
#-- Time Trait definition ----------------------------------------------------
Time = BaseInstance(datetime.time, editor=time_editor)
#-------------------------------------------------------------------------------
# Create predefined, reusable trait instances:
#-------------------------------------------------------------------------------
# Synonym for Bool; default value is False.
false = Bool
# Boolean values only; default value is True.
true = Bool( True )
# Allows any value to be assigned; no type-checking is performed.
# Default value is Undefined.
undefined = Any( Undefined )
#-- List Traits ----------------------------------------------------------------
#: List of integer values; default value is [].
ListInt = List( int )
#: List of float values; default value is [].
ListFloat = List( float )
#: List of string values; default value is [].
ListStr = List( str )
#: List of Unicode string values; default value is [].
ListUnicode = List( unicode )
#: List of complex values; default value is [].
ListComplex = List( complex )
#: List of Boolean values; default value is [].
ListBool = List( bool )
#: List of function values; default value is [].
ListFunction = List( FunctionType )
#: List of method values; default value is [].
ListMethod = List( MethodType )
#: List of class values; default value is [].
ListClass = List( ClassType )
#: List of instance values; default value is [].
ListInstance = List( InstanceType )
#: List of container type values; default value is [].
ListThis = List( ThisClass )
#-- Dictionary Traits ----------------------------------------------------------
#: Only a dictionary of string:Any values can be assigned; only string keys can
#: be inserted. The default value is {}.
DictStrAny = Dict( str, Any )
#: Only a dictionary of string:string values can be assigned; only string keys
#: with string values can be inserted. The default value is {}.
DictStrStr = Dict( str, str )
#: Only a dictionary of string:integer values can be assigned; only string keys
#: with integer values can be inserted. The default value is {}.
DictStrInt = Dict( str, int )
#: Only a dictionary of string:long-integer values can be assigned; only string
#: keys with long-integer values can be inserted. The default value is {}.
DictStrLong = Dict( str, long )
#: Only a dictionary of string:float values can be assigned; only string keys
#: with float values can be inserted. The default value is {}.
DictStrFloat = Dict( str, float )
#: Only a dictionary of string:Boolean values can be assigned; only string keys
#: with Boolean values can be inserted. The default value is {}.
DictStrBool = Dict( str, bool )
#: Only a dictionary of string:list values can be assigned; only string keys
#: with list values can be assigned. The default value is {}.
DictStrList = Dict( str, list )
|