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
|
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2016-2023 German Aerospace Center (DLR) and others.
# SUMOPy module
# Copyright (C) 2012-2021 University of Bologna - DICAM
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file classman.py
# @author Joerg Schweizer
# @date 2012
# python classman.py
# TODO:
# - store old values in attrcons and recover with undo
# To be or not to be. -- Shakespeare
# To do is to be. -- Nietzsche
# To be is to do. -- Sartre
# Do be do be do. -- Sinatra
# save with is saved flag
# xml mixin
# different attrconfig classe (numbers, strings, lists, colors,...)
import types
import os
import pickle
import sys
import string
import math
from collections import OrderedDict
from datetime import datetime
#import numpy as np
import xmlman as xm
from logger import Logger
from misc import get_inversemap
##########
# event triggers
# plugtype plugcode
EVTDEL = 0 # delete attribute
EVTSET = 1 # set attribute
EVTGET = 2 # get attribute
EVTADD = 3 # add/create attribute
EVTDELITEM = 20 # delete attribute
EVTSETITEM = 21 # set attribute
EVTGETITEM = 22 # get attribute
EVTADDITEM = 23 # add/create attribute
ATTRS_NOSAVE = ('value', 'plugin', '_obj', '_manager', 'get', 'set', 'add',
'del', 'delete', 'childs', 'parent', '_attrconfig_id_tab')
ATTRS_SAVE = ('ident', '_name', 'managertype', '_info', 'xmltag', '_attrs_nosave')
ATTRS_SAVE_TABLE = ('_is_localvalue', 'attrname', '_colconfigs', '_ids', '_inds', '_attrconfigs',
'_groups', 'plugin', '_is_indexing', '_index_to_id', '_id_to_index')
STRUCTS_COL = ('odict', 'array')
STRUCTS_SCALAR = ('scalar', 'list', 'matrix', 'scalar.func')
NUMERICTYPES = (types.BooleanType, types.FloatType, types.IntType, types.LongType, types.ComplexType)
STRINGTYPES = (types.StringType, types.UnicodeType)
NODATATYPES = (types.FunctionType, types.InstanceType, types.LambdaType)
def save_obj(obj, filename, is_not_save_parent=False):
"""
Saves python object to a file with filename.
Filename may also include absolute or relative path.
If operation fails a False is returned and True otherwise.
"""
# print 'save_obj',is_not_save_parent,filename,obj.parent
try:
f = open(filename, 'wb')
except:
print('WARNING in save: could not open', filename)
return False
if is_not_save_parent:
parent = obj.parent
obj.parent = None
# print ' before',is_not_save_parent,parent,obj.parent
pickle.dump(obj, f, protocol=2)
f.close()
# set all objects and attrubutes to unsaved again
# obj.set_unsaved()
# no, decided to eliminate _is_saved restriction
# print ' after',is_not_save_parent,parent,obj.parent
if is_not_save_parent:
obj.parent = parent
return True
def load_obj(filename, parent=None, is_throw_error=False):
"""
Reads python object from a file with filename and returns object.
Filename may also include absolute or relative path.
If operation fails a None object is returned.
"""
print('load_obj', filename)
if is_throw_error:
f = open(filename, 'rb')
else:
try:
f = open(filename, 'rb')
except:
print('WARNING in load_obj: could not open', filename)
return None
# try:
# print ' pickle.load...'
obj = pickle.load(f)
f.close()
# init_postload_internal is to restore INTERNAL states from INTERNAL states
# print 'load_obj->init_postload_internal',obj.ident
obj.init_postload_internal(parent)
# print ' after init_postload_internal obj.parent',obj.parent
# init_postload_external is to restore INTERNAL states from EXTERNAL states
# such as linking
#obj.init_postload_external(is_root = True)
obj.init_postload_external()
# _init4_ is to do misc stuff when everything is set
# obj._init4_config()
return obj
# class ObjXmlMixin:
# class AttrXmlMixin:
class Plugin:
def __init__(self, obj, is_enabled=True):
self._obj = obj # this can be either attrconf or main object
self._events = {}
self._has_events = False
self._is_enabled = is_enabled
def get_obj(self):
return self._obj
def unplug(self):
del self._events
self._events = {}
self._has_events = False
def add_event(self, trigger, function):
"""
Standard plug types are automatically set but the system:
"""
if not self._events.has_key(trigger):
self._events[trigger] = []
self._events[trigger].append(function)
self._has_events = True
def del_event(self, trigger):
del self._events[trigger]
if len(self._events) == 0:
self._has_events = False
def enable(self, is_enabled=True):
self._is_enabled = is_enabled
def exec_events(self, trigger):
if self._has_events & self._is_enabled:
# print '**PuginMixin.exec_events',trigger,(EVTGETITEM,EVTGET)
# if trigger!=EVTGET:
# print ' call set_modified',self._obj
# self._obj.set_modified(True)
for function in self._events.get(trigger, []):
function(self._obj)
def exec_events_attr(self, trigger, attrconfig):
if self._has_events & self._is_enabled:
# print '**PuginMixin.exec_events',trigger,(EVTGETITEM,EVTGET)
# if trigger!=EVTGET:
# print ' call set_modified',self._obj
# self._obj.set_modified(True)
for function in self._events.get(trigger, []):
function(self._obj, attrconfig)
def exec_events_ids(self, trigger, ids):
"""
Executes all functions assigned for this trigger for multiple ids.
"""
if self._has_events & self._is_enabled:
# print '**ArrayConf._execute_events_keys',trigger,ids
# print ' _events',self._events
for function in self._events.get(trigger, []):
function(self._obj, ids)
class AttrConf:
"""
Contains additional information on the object's attribute.
"""
def __init__(self, attrname, default,
groupnames=[], perm='rw',
is_save=True,
# is_link = False, # define link class
is_copy=True,
name='', info='',
unit='',
xmltag=None,
xmlsep=' ',
xmlmap=None,
is_plugin=False,
struct='scalar',
metatype='',
is_returnval=True,
**attrs):
# if struct == 'None':
# if hasattr(default, '__iter__'):
# struct = 'scalar'
# else:
# struct = 'list'
# these states will be saved and reloaded
self.attrname = attrname
self.groupnames = groupnames
self.metatype = metatype
self.struct = struct
self._default = default
self._is_save = is_save # will be set properly in set_manager
self._is_copy = is_copy
self._is_localvalue = True # value stored locally, set in set_manager
self._is_returnval = is_returnval
self._unit = unit
self._info = info
self._name = name
self._perm = perm
# states below need to be resored after load
self._manager = None # set later by attrsman , necessary?
self._obj = None # parent object, set later by attrsman
self._is_modified = False
self._is_saved = False
self.init_plugin(is_plugin)
# self._init_xml(xmltag)
self.set_xmltag(xmltag, xmlsep, xmlmap)
# set rest of attributes passed as keyword args
# no matter what they are used for
for attr, value in attrs.iteritems():
setattr(self, attr, value)
def is_save(self):
return self._is_save
def set_save(self, is_save):
if is_save:
self._manager.do_save_attr(self.attrname)
else:
self._manager.do_not_save_attr(self.attrname)
def add_groupnames(self, groupnames):
self.groupnames = list(set(self.groupnames+groupnames))
self._manager.insert_groupnames(self)
def del_groupname(self, groupname):
if groupname in self.groupnames:
self.groupnames.remove(groupname)
# do this update in any case to be sure
# it disappears in the centralized database
self._manager.del_groupname(self)
def has_group(self, groupname):
return groupname in self.groupnames
def enable_plugin(self, is_enabled=True):
if self.plugin is not None:
self.plugin.enable(is_enabled)
def get_metatype(self):
return self.metatype
def init_plugin(self, is_plugin):
if is_plugin:
self.plugin = Plugin(self)
self.set = self.set_plugin
self.get = self.get_plugin
else:
self.plugin = None
# def _init_xml(self,xmltag=None):
# if xmltag is not None:
# self.xmltag = xmltag
# else:
# self.xmltag = self.attrname
def set_xmltag(self, xmltag, xmlsep=' ', xmlmap=None):
self.xmltag = xmltag
self.xmlsep = xmlsep
self.xmlmap = xmlmap
def get_value_from_xmlattr(self, xmlattrs):
"""
Return a value of the correct data type
from the xml attribute object
If this configuration is not found in xmlattrs
then None is returned.
"""
if (self.xmltag is not None):
if xmlattrs.has_key(self.xmltag):
return self.get_value_from_string(xmlattrs[self.xmltag])
else:
return None
def get_value_from_string(self, s, sep=','):
"""
Returns the attribute value from a string in the correct type.
"""
if self.metatype == 'color':
return xm.parse_color(s, sep)
# TODO: allow arrays
# elif hasattr(val, '__iter__'):
# if len(val)>0:
# if hasattr(val[0], '__iter__'):
# # matrix
# fd.write(xm.mat(self.xmltag,val))
# else:
# # list
# fd.write(xm.arr(self.xmltag,val,self.xmlsep))
# else:
# # empty list
# #fd.write(xm.arr(self.xmltag,val))
# # don't even write empty lists
# pass
elif self.xmlmap is not None:
imap = get_inversemap(self.xmlmap)
# print 'get_value_from_string',s,imap
if imap.has_key(s):
return imap[s]
else:
return self.get_numvalue_from_string(s)
else:
return self.get_numvalue_from_string(s)
def get_numvalue_from_string(self, s, valtype=None):
if valtype is None:
t = type(self._default)
else:
t = valtype
if t in (types.UnicodeType, types.StringType):
return s
elif t in (types.UnicodeType, types.StringType):
return s
elif t in (types.LongType, types.IntType):
return int(s)
elif t in (types.FloatType, types.ComplexType):
return float(s)
elif t == types.BooleanType: # use default and hope it is no a numpy bool!!!
if s in ('1', 'True'):
return True
else:
return False
# 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType'
else:
return None # unsuccessful
def write_xml(self, fd):
if self.xmltag is not None:
self._write_xml_value(self.get_value(), fd)
def _write_xml_value(self, val, fd):
# print 'write_xml',self.xmltag,'is array',hasattr(val, '__iter__'),'xmlmap',self.xmlmap
if self.metatype == 'color':
fd.write(xm.color(self.xmltag, val))
elif hasattr(val, '__iter__'):
if len(val) > 0:
if hasattr(val[0], '__iter__'):
# matrix
fd.write(xm.mat(self.xmltag, val))
else:
# list
fd.write(xm.arr(self.xmltag, val, self.xmlsep))
else:
# empty list
# fd.write(xm.arr(self.xmltag,val))
# don't even write empty lists
pass
elif self.xmlmap is not None:
if self.xmlmap.has_key(val):
fd.write(xm.num(self.xmltag, self.xmlmap[val]))
else:
fd.write(xm.num(self.xmltag, val))
elif hasattr(self, 'choices'):
if type(self.choices) == types.ListType:
fd.write(xm.num(self.xmltag, val))
else:
# print '_write_xml_value',self.attrname
# print ' val,self.choices.values()',val,self.choices.values()
i = self.choices.values().index(val)
fd.write(xm.num(self.xmltag, self.choices.keys()[i]))
elif type(self._default) == types.BooleanType: # use default and hope it is no a numpy bool!!!
if val:
fd.write(xm.num(self.xmltag, 1))
else:
fd.write(xm.num(self.xmltag, 0))
elif type(self._default) in (types.UnicodeType, types.StringType):
if len(val) > 0:
fd.write(xm.num(self.xmltag, val))
else:
# scalar number or string
fd.write(xm.num(self.xmltag, val))
def get_name(self):
return self._name
def is_modified(self):
# print 'is_modified', self.attrname, self._is_modified
return self._is_modified
def set_modified(self, is_modified):
self._is_modified = is_modified
def set_manager(self, manager):
"""
Method to set manager to attribute configuration object.
This is either attribute manager or table manager.
Used by add method of AttrManager
"""
self._manager = manager
self._is_localvalue = manager.is_localvalue()
self.set_save(self._is_save)
def get_manager(self):
"""
Method to get manager to attribute configuration object.
"""
return self._manager
def set_obj(self, obj):
"""
Method to set instance of managed object.
Used by add method of AttrManager
"""
self._obj = obj
def get_obj(self):
return self._obj
def get(self):
# return attribute, overridden with indexing for array and dict struct
return self.get_value()
def set(self, value):
# set attribute, overridden with indexing for array and dict struct
if value != self.get_value():
self.set_value(value)
self._is_modified = True
return value
def get_plugin(self):
"""
Default get method with plugin for scalar attrs
"""
# return attribute, overridden with indexing for array and dict struct
self.plugin.exec_events(EVTGET)
return self.get_value()
def set_plugin(self, value):
"""
Default set method with plugin for scalar attrs
"""
# set attribute, overridden with indexing for array and dict struct
if value != self.get_value():
self.set_value(value)
self._is_modified = True
self.plugin.exec_events(EVTSET)
return value
def set_default(self, value):
self._default = value
def get_default(self):
return self._default
def get_init(self):
"""
Returns initialization of attribute.
Usually same as get_default for scalars.
Overridden by table configuration classes
"""
value = self.get_default()
# store locally if required
if self._is_localvalue:
self.value = value
return value
def reset(self):
if self._is_localvalue:
self.value = self.get_default()
else:
setattr(self._obj, self.attrname, self.get_default())
def clear(self):
self.reset()
# def is_tableattr(self):
# return self.struct in ('dict','array','list')
def set_perm(self, perm):
self._perm = perm
def get_perm(self):
return self._perm
def is_returnval(self):
if hasattr(self, '_is_returnval'): # for back compatibility
return self._is_returnval
else:
return True
def is_readonly(self):
return 'w' not in self._perm
def is_writable(self):
return 'w' in self._perm
def is_editable(self):
"""Can attribute be edited """
return 'e' in self._perm
def has_unit(self):
return self._unit != ''
def has_info(self):
return self.get_info() is not None
def is_colattr(self):
return hasattr(self, '__getitem__')
def get_info(self):
if self._info is None:
return self.__doc__
else:
return self._info
def format_unit(self, show_parentesis=False):
if self._unit in ('', None):
return ''
if show_parentesis:
return '[%s]' % self._unit
else:
return '%s' % self._unit
def format_value(self, show_unit=False, show_parentesis=False):
if show_unit:
unit = ' '+self.format_unit(show_parentesis)
else:
unit = ''
# return repr(self.get_value())+unit
v = self.get_value()
# print 'format_value',self.attrname,v,type(v)
if not hasattr(v, '__iter__'):
# print ' ',hasattr(self, 'digits_fraction'),(not math.isnan(v)),(not math.isinf(v))
if hasattr(self, 'digits_fraction'):
if (not math.isnan(v)) & (not math.isinf(v)):
printformat = "%."+str(self.digits_fraction)+"f"
return printformat % v+unit
else:
return str(v)+unit
else:
return str(v)+unit
else:
return str(v)+unit
def format_symbol(self, show_parentesis=True):
if hasattr(self, 'symbol'):
symbol = self.symbol
else:
symbol = self._name
return symbol+' '+self.format_unit(show_parentesis=show_parentesis)
####
def get_value(self):
# always return attribute, no indexing, no plugin
if self._is_localvalue:
return self.value
else:
return getattr(self._obj, self.attrname)
def set_value(self, value):
# set entire attribute, no indexing, no plugin
# print 'AttrConf.set_value',self.attrname, self._is_localvalue, value, type(value)
if self._is_localvalue:
self.value = value
else:
return setattr(self._obj, self.attrname, value)
def predelete(self):
"""
Cleanup operations before deleting
"""
if self._is_localvalue:
del self.value # delete value
else:
del self._obj.__dict__[self.attrname] # delete value
# def init_presave_internal(self, man, obj):
# pass
# not a good idea to delete links, plugins here
# def save_value(self, state):
# """
# Save attribute value of managed object to state dict.
#
# move this into __getstate__
#
# restore value in _obj during postllad_external
#
# make _getstate_ for speecific save
# """
# #print 'save_value',self.attrname,self._is_save, self._is_localvalue,
# #
# # Attention can be called fron __getstate__ of obj if _is_localvalue=False
# # or from __getstate__ of attribute config if _is_localvalue=True
def _getstate_specific(self, state):
"""
Called by __getstate__ to add/change specific states,
before returning states.
To be overridden.
"""
pass
def __getstate__(self):
# print 'AttrConf.__getstate__',self.get_obj().format_ident_abs(),self.attrname
# print ' self.__dict__=\n',self.__dict__.keys()
if self._is_saved:
# this message indicates a loop!!
print('WARNING in __getstate__: Attribute already saved:', self.get_obj().format_ident_abs(), self.attrname)
state = {}
for attr in self.__dict__.keys():
if attr == 'plugin':
plugin = self.__dict__[attr]
if plugin is not None:
state[attr] = True
else:
state[attr] = False
elif attr not in ATTRS_NOSAVE:
state[attr] = self.__dict__[attr]
if self._is_save:
self._is_modified = False
state['value'] = self.get_value()
self._getstate_specific(state)
# print ' state',state
return state
def __setstate__(self, state):
# print '__setstate__',self
# this is always required, but will not be saved
self.plugins = {}
for attr in state.keys():
# print ' state key',attr, state[attr]
# done in init_postload_internal...
# if attr=='plugin':
# if state[attr]==True:
# self.__dict__[attr] = Plugin(self)
# else:
# self.__dict__[attr]= None
# else:
self.__dict__[attr] = state[attr]
def init_postload_internal(self, man, obj):
# print 'AttrConf.init_postload_internal',self.attrname,hasattr(self,'value'),self._is_save,self._is_localvalue,'obj:',obj.ident
self.set_manager(man)
self.set_obj(obj)
self.init_plugin(self.plugin)
# set initial values for unsafed attributes
if not self._is_save:
self.set_value(self.get_init())
else:
if self._is_localvalue:
# OK self.value already set in __setstate__
pass
else:
setattr(self._obj, self.attrname, self.value) # TODO: could be made nicer with method
del self.value # no longer needed
# this attribute became compulsory
if not hasattr(self, 'xmlmap'):
self.xmlmap = None
# print ' check',hasattr(self,'value')
# print ' value=',self.get_value()
self._is_saved = False
def init_postload_external(self):
pass
class NumConf(AttrConf):
"""
Contains additional information on the object's attribute.
Here specific number related attributes are defined.
"""
def __init__(self, attrname, default,
digits_integer=None, digits_fraction=None,
minval=None, maxval=None,
**kwargs):
self.min = minval
self.max = maxval
self.digits_integer = digits_integer
self.digits_fraction = digits_fraction
AttrConf.__init__(self, attrname, default, metatype='number',
**kwargs
)
class ListConf(AttrConf):
"""
Specifies a list of objects.
Objects may be editable or selectable in the GUI
"""
def __init__(self, attrname, default,
sep=',', valtype=None, is_fixedlength=False,
perm='rw', **kwargs):
#self._is_child = is_child
if valtype is None:
if len(default) > 0:
valtype = type(default[0])
else:
valtype = types.UnicodeType
AttrConf.__init__(self, attrname, default,
struct='scalar',
metatype='list',
sep=sep,
valtype=valtype,
is_fixedlength=is_fixedlength,
perm=perm,
**kwargs
)
def get_value_from_string(self, s, sep=None):
"""
Returns the attribute value from a string in the correct type.
"""
value = []
for val in s.split(self.sep):
value.append(self.get_numvalue_from_string(val, valtype=self.valtype))
return value
class ObjConf(AttrConf):
"""
Contains additional information on the object's attribute.
Configures Pointer to another object .
This other object must have an ident.
it can be either a child (then it will be saved)
or a link (then only the id will saved)
If it is a child the is_child = True (default value)
"""
def __init__(self, valueobj, is_child=True, **kwargs):
attrname = valueobj.get_ident()
self._is_child = is_child
AttrConf.__init__(self, attrname, valueobj,
struct='scalar',
metatype='obj',
perm='r',
**kwargs
)
def set_obj(self, obj):
"""
Method to set instance of managed object.
Used by add method of AttrManager
"""
# print 'ObjConf.set_obj',self.attrname,obj.ident
AttrConf.set_obj(self, obj)
if self._is_child:
# tricky: during first initialization
# child instance is stored in default
obj.set_child(self)
def predelete(self):
AttrConf.predelete(self)
if self._is_child:
self.get_obj().del_child(self.attrname)
def reset(self):
if self._is_child:
self.get_value().reset()
def clear(self):
if self._is_child:
self.get_value().clear()
def is_child(self):
return self._is_child
def _getstate_specific(self, state):
"""
Called by __getstate__ to add/change specific states,
before returning states.
To be overridden.
"""
# print 'ObjConf._getstate_specific',self.attrname,self._is_child,self._is_save
if self._is_save:
if self._is_child:
# OK self.value already set in
pass
else:
# print ' remove object reference from value and create ident'
state['value'] = None
if self.get_value() is not None:
# print ' found object',self.get_value().get_ident_abs()
state['_ident_value'] = self.get_value().get_ident_abs()
else:
print('WARNING in ObjConf._getstate_specific', self.attrname, 'lost linked object')
state['_ident_value'] = []
# print ' ',
def is_modified(self):
# if self._is_child
#is_modified = self.get_value().is_modified()
# print 'is_modified', self.attrname, is_modified
return self.get_value().is_modified()
def set_modified(self, is_modified):
if self._is_child:
self.get_value().set_modified(is_modified)
def write_xml(self, fd):
"""
Objects are not written here, but in write_xml of the parent obj.
"""
pass
def init_postload_internal(self, man, obj):
# print 'ObjConf.init_postload_internal',self.attrname,hasattr(self,'value'),self._is_save,self._is_localvalue,'parent obj:',obj.ident
AttrConf.init_postload_internal(self, man, obj)
if self._is_child:
# print ' make sure children get initialized'
# self.get_value().init_postload_internal(obj)
# print ' call init_postload_internal of',self.get_value().ident,self.get_value(),self.get_value().__class__,self.get_value().init_postload_internal
self.get_value().init_postload_internal(obj)
def init_postload_external(self):
# print 'ObjConf.init_postload_external',self.attrname,self._is_child
if self._is_child:
# restore normally
AttrConf.init_postload_external(self)
self.get_value().init_postload_external()
else:
# Substitute absolute ident with link object.
# Called from init_postload_external of attrsman during load_obj
#
ident_abs = self._ident_value
if len(ident_abs) > 0:
# print 'reset_linkobj',self.attrname,ident_abs
obj = self.get_obj()
rootobj = obj.get_root()
# print ' rootobj',rootobj.ident
linkobj = rootobj.get_obj_from_ident(ident_abs)
# print ' linkobj',linkobj.ident
self.set_value(linkobj)
else:
print('WARNING in ObjConf._getstate_specific', self.attrname, 'lost linked object')
self.set_value(BaseObjman('lost_object'))
# def get_valueobj(self):
# """
# This is called by get_childobj to retrive the child instance.
# """
# return self.get_value()
def get_name(self):
return self.get_value().get_name()
def get_info(self):
return self.get_value().__doc__
def format_value(self, show_unit=False, show_parentesis=False):
return repr(self.get_value())
class FuncConf(AttrConf):
"""
Configures a function.
The function with name funcname must be a method of the object.
Default value is used to specify the type of output.
"""
def __init__(self, attrname, funcname, exampleoutput, struct='scalar.func', perm='r', **kwargs):
self.funcname = funcname
AttrConf.__init__(self, attrname, exampleoutput,
struct=struct,
perm=perm,
is_save=False,
**kwargs
)
def set_obj(self, obj):
AttrConf.set_obj(self, obj)
if self._info == '':
self._info = getattr(self._obj, self.funcname).__doc__
def get_value(self):
# print 'get_value',self.attrname
# always return attribute, no indexing, no plugin
return getattr(self._obj, self.funcname)()
# if self._is_localvalue:
# return self.value
# else:
# return getattr(self._obj, self.attrname)
def get_function(self):
# print 'get_function',self.attrname
return getattr(self._obj, self.funcname)
def set_value(self, value):
# set entire attribute, no indexing, no plugin
# print 'AttrConf.set_value',self.attrname, self._is_localvalue, value, type(value)
# this will run the function and return a value
return self.get_function()
def is_modified(self):
return False
def reset(self):
pass
def clear(self):
pass
class Indexing:
"""
Mixing to allow any column attribute to be used as index.
"""
def _init_indexing(self):
"""
Init Indexing management attributes.
"""
self._index_to_id = {} # OrderedDict()
# this updates index if values already exist
if hasattr(self, 'value'):
ids = self.get_obj().get_ids()
self.add_indices(ids, self[ids])
def reset_index(self):
self._init_indexing()
def get_indexmap(self):
return self._index_to_id
def get_id_from_index(self, index):
return self._index_to_id[index]
def has_indices(self, indices):
ans = len(indices)*[False]
for i in range(len(indices)):
if self._index_to_id.has_key(indices[i]):
ans[i] = True
return ans
def has_index(self, index):
return self._index_to_id.has_key(index)
def get_ids_from_indices(self, indices):
ids = len(indices)*[0]
for i in range(len(indices)):
# if not self._index_to_id.has_key(indices[i]):
# print 'WARNING from get_ids_from_indices: no index',indices[i]
# print self._index_to_id
ids[i] = self._index_to_id[indices[i]]
return ids
def get_ids_from_indices_save(self, indices):
ids = len(indices)*[0]
for i in range(len(indices)):
if not self._index_to_id.has_key(indices[i]):
ids[i] = -1
else:
ids[i] = self._index_to_id[indices[i]]
return ids
# use set instead of add
def add_indices(self, ids, indices):
for _id, index in zip(ids, indices):
self.add_index(_id, index)
def add_index(self, _id, index):
self._index_to_id[index] = _id
def rebuild_indices(self):
for idx in self._index_to_id.keys():
del self._index_to_id[idx]
ids = self.get_obj().get_ids()
self.add_indices(ids, self[ids])
def del_indices(self, ids):
for _id in ids:
self.del_index(_id)
def set_index(self, _id, index):
# print 'set_index',self.attrname,_id, index
# print ' B_index_to_id',self._index_to_id
self.del_index(_id)
self.add_index(_id, index)
# print ' A_index_to_id',self._index_to_id
def set_indices(self, ids, indices):
self.del_indices(ids)
self.add_indices(ids, indices)
def del_index(self, _id):
index = self[_id]
# when index is added (with set) no previous index value exists
if self._index_to_id.has_key(index):
del self._index_to_id[index]
def get_ids_sorted(self):
# print 'get_ids_sorted',self.value
# print ' _index_to_id',self._index_to_id
# print ' sorted',sorted(self._index_to_id.iteritems())
return OrderedDict(sorted(self._index_to_id.iteritems())).values()
# def indexset(self, indices, values):
# no! set made with respective attribute
# print 'indexset',indices
# print ' ids=',self.get_ids_from_indices(indices)
# print ' values=',values
# self[self.get_ids_from_indices(indices)] = values
class ColConf(Indexing, AttrConf):
"""
Basic column configuration.
Here an ordered dictionary is used to represent the data.
#>>> from collections import OrderedDict
#>>> spam = OrderedDict([('s',(1,2)),('p',(3,4)),('a',(5,6)),('m',(7,8))])
>>> spam.values()
"""
# def __init__(self, **attrs):
# print 'ColConf',attrs
def __init__(self, attrname, default, is_index=False, **attrs):
# print 'ColConf',attrs
self._is_index = is_index
AttrConf.__init__(self, attrname, default,
struct='odict',
**attrs)
if is_index:
self._init_indexing()
def is_index(self):
return self._is_index
def get_defaults(self, ids):
# create a list, should work for all types and dimensions
# default can be scalar or an array of any dimension
# print '\n\nget_defaults',self.attrname,ids,self.get_default()
values = []
for _id in ids:
values.append(self.get_default())
# len(ids)*self.get_default() # makes links, not copies
return values
def get_init(self):
"""
Returns initialization of attribute.
Usually same as get_default for scalars.
Overridden by table configuration classes
"""
ids = self._manager.get_ids()
# print '\n\nget_init',self.attrname,ids
values = self.get_defaults(ids)
i = 0
odict = OrderedDict()
for _id in ids:
odict[_id] = values[i]
i += 1
# store locally if required
if self._is_localvalue:
self.value = odict
# pass on to calling instance
# in this cas the data is stored under self._obj
return odict
def reset(self):
# this reset works also for np arrays!
odict = self.get_init()
if not self._is_localvalue:
setattr(self._obj, self.attrname, odict)
if self._is_index:
self.reset_index()
def init_plugin(self, is_plugin):
if is_plugin:
self.plugin = Plugin(self)
self.set = self.set_plugin
self.get = self.get_plugin
self.add = self.add_plugin
self.delete = self.delete_plugin
else:
self.plugin = None
def write_xml(self, fd, _id):
if self.xmltag is not None:
self._write_xml_value(self[_id], fd)
def __delitem__(self, ids):
# print ' before=\n',self.__dict__[attr]
#attr = self.attrconf.get_attr()
if hasattr(ids, '__iter__'):
if self._is_index:
self.del_indices(ids)
array = self.get_value()
for _id in ids:
del array[_id]
else:
if self._is_index:
self.del_index(ids)
del self.get_value()[ids]
def delete_item(self, _id):
# print ' before=\n',self.__dict__[attr]
#attr = self.attrconf.get_attr()
del self.get_value()[_id]
def __getitem__(self, ids):
# print '__getitem__',key
if hasattr(ids, '__iter__'):
items = len(ids)*[None]
i = 0
array = self.get_value()
for _id in ids:
items[i] = array[_id]
i += 1
return items
else:
return self.get_value()[ids]
def __setitem__(self, ids, values):
# print '__setitem__',ids,values,type(self.get_value())
if hasattr(ids, '__iter__'):
if self._is_index:
self.set_indices(ids, values) # must be set before setting new value
i = 0
array = self.get_value()
for _id in ids:
array[_id] = values[i]
i += 1
# if self._is_index:
# self.add_indices(ids, values)
else:
if self._is_index:
self.set_index(ids, values) # must be set before setting new value
self.get_value()[ids] = values
# if self._is_index:
# self.add_index(ids, values)
def add(self, ids, values=None):
if not hasattr(ids, '__iter__'):
_ids = [ids]
if values is not None:
_values = [values]
else:
_ids = ids
_values = values
if values is None:
_values = self.get_defaults(_ids)
# print 'add ids, _values',ids, _values
# trick to prevent updating index before value is added
if self._is_index:
is_index_backup = True
self._is_index = False
else:
is_index_backup = False
self[_ids] = _values
if is_index_backup:
self._is_index = True
self.add_indices(_ids, _values)
self._is_modified = True
def add_plugin(self, ids, values=None):
if not hasattr(ids, '__iter__'):
_ids = [ids]
if values is not None:
_values = [values]
else:
_ids = ids
_values = values
# print 'add ids, _values',ids, _values
if values is None:
_values = self.get_defaults(_ids)
# trick to prevent updating index before value is added
if self._is_index:
is_index_backup = True
self._is_index = False
else:
is_index_backup = False
self[_ids] = _values
if is_index_backup:
self._is_index = True
self.add_indices(_ids, _values)
self._is_modified = True
if self.plugin:
self.plugin.exec_events_ids(EVTADDITEM, _ids)
def get(self, ids):
"""
Central function to get the attribute value associated with ids.
should be overridden by specific array configuration classes
"""
return self[ids]
def get_plugin(self, ids):
"""
Central function to get the attribute value associated with ids.
should be overridden by specific array configuration classes
"""
if self._plugin:
if not hasattr(ids, '__iter__'):
self.plugin.exec_events_ids(EVTGETITEM, [ids])
else:
self.plugin.exec_events_ids(EVTGETITEM, ids)
return self[ids]
def set(self, ids, values):
"""
Returns value of array element for all ids.
"""
self[ids] = values
self._is_modified = True
# print 'set',self.attrname
if self._is_index:
self.set_indices(ids, values)
def set_plugin(self, ids, values):
"""
Returns value of array element for all ids.
"""
self[ids] = values
self._is_modified = True
# print 'set',self.attrname
if self.plugin:
if not hasattr(ids, '__iter__'):
self.plugin.exec_events_ids(EVTSETITEM, [ids])
else:
self.plugin.exec_events_ids(EVTSETITEM, ids)
if self._is_index:
self.set_indices(ids, values)
def delete(self, ids):
"""
removes key from array structure
To be overridden
"""
del self[ids]
self._is_modified = True
def delete_plugin(self, ids):
"""
removes key from array structure
To be overridden
"""
if self.plugin:
if not hasattr(ids, '__iter__'):
self.plugin.exec_events_ids(EVTGETITEM, [ids])
else:
self.plugin.exec_events_ids(EVTGETITEM, ids)
del self[ids]
self._is_modified = True
def format_value(self, _id, show_unit=False, show_parentesis=False):
if show_unit:
unit = ' '+self.format_unit(show_parentesis)
else:
unit = ''
# return repr(self[_id])+unit
#self.min = minval
#self.max = maxval
#self.digits_integer = digits_integer
#self.digits_fraction = digits_fraction
val = self[_id]
tt = type(val)
if tt in (types.LongType, types.IntType):
return str(val)+unit
elif tt in (types.FloatType, types.ComplexType):
if hasattr(attrconf, 'digits_fraction'):
digits_fraction = self.digits_fraction
else:
digits_fraction = 3
return "%."+str(digits_fraction)+"f" % (val)+unit
else:
return str(val)+unit
# return str(self[_id])+unit
def format(self, ids=None):
# TODO: incredibly slow when calling format_value for each value
text = ''
if ids is None:
ids = self._manager.get_ids()
if not hasattr(ids, '__iter__'):
ids = [ids]
#unit = self.format_unit()
attrname = self.attrname
for id in ids:
text += '%s[%d] = %s\n' % (attrname, id, self.format_value(id, show_unit=True))
return text[:-1] # remove last newline
class NumcolConf(ColConf):
def __init__(self, attrname, default,
digits_integer=None, digits_fraction=None,
minval=None, maxval=None,
**attrs):
self.min = minval
self.max = maxval
self.digits_integer = digits_integer
self.digits_fraction = digits_fraction
ColConf.__init__(self, attrname, default, **attrs)
class IdsConf(ColConf):
"""
Column, where each entry is the id of a single Table.
"""
def __init__(self, attrname, tab, id_default=-1, is_index=False, perm='r', **kwargs):
self._is_index = is_index
self._tab = tab
AttrConf.__init__(self, attrname,
id_default, # default id
struct='odict',
metatype='id',
perm=perm,
**kwargs
)
self.init_xml()
# print 'IdsConf.__init__',attrname
# print ' ',self._tab.xmltag,self._attrconfig_id_tab
def set_linktab(self, tab):
self._tab = tab
def get_linktab(self):
return self._tab
def init_xml(self):
# print 'init_xml',self.attrname
if self._tab.xmltag is not None:
# necessary?? see ObjMan.write_xml
xmltag_tab, xmltag_item_tab, attrname_id_tab = self._tab.xmltag
if (attrname_id_tab is None) | (attrname_id_tab == ''):
self._attrconfig_id_tab = None
else:
self._attrconfig_id_tab = getattr(self._tab, attrname_id_tab) # tab = tabman !
if not hasattr(self, 'is_xml_include_tab'):
# this means that entire table rows will be included
self.is_xml_include_tab = False
# print ' xmltag_tab, xmltag_item_tab, attrname_id_tab',xmltag_tab, xmltag_item_tab, attrname_id_tab,self.is_xml_include_tab
else:
self._attrconfig_id_tab = None
self.is_xml_include_tab = False
def write_xml(self, fd, _id, indent=0):
if (self.xmltag is not None) & (self[_id] >= 0):
if self._attrconfig_id_tab is None:
self._write_xml_value(self[_id], fd)
elif self.is_xml_include_tab:
# this means that entire table rows will be included
self._tab.write_xml(fd, indent, ids=self[_id], is_print_begin_end=False)
else:
self._write_xml_value(self._attrconfig_id_tab[self[_id]], fd)
def _write_xml_value(self, val, fd):
# print 'write_xml',self.xmltag,hasattr(val, '__iter__')
if hasattr(val, '__iter__'):
if len(val) > 0:
if hasattr(val[0], '__iter__'):
# matrix
fd.write(xm.mat(self.xmltag, val))
else:
# list
fd.write(xm.arr(self.xmltag, val, self.xmlsep))
else:
# empty list
# fd.write(xm.arr(self.xmltag,val))
# don't even write empty lists
pass
elif type(self._default) in (types.UnicodeType, types.StringType):
if len(val) > 0:
fd.write(xm.num(self.xmltag, val))
else:
# scalar number or string
fd.write(xm.num(self.xmltag, val))
def get_defaults(self, ids):
# create a list, should work for all types and dimensions
# default can be scalar or an array of any dimension
# print '\n\nget_defaults',self.attrname,ids,self.get_default()
return len(ids)*[self.get_default()]
def _getstate_specific(self, state):
"""
Called by __getstate__ to add/change specific states,
before returning states.
To be overridden.
"""
if self._is_save:
# if self._is_child:
# # OK self.value already set in
# pass
# else:
# # remove table reference and create ident
# print '_getstate_specific',self._tab.get_ident_abs()
state['_tab'] = None
state['_ident_tab'] = self._tab.get_ident_abs()
def init_postload_internal(self, man, obj):
# print 'IdsConf.init_postload_internal',self.attrname,hasattr(self,'value'),self._is_save,self._is_localvalue,'obj:',obj.ident
AttrConf.init_postload_internal(self, man, obj)
# if self._is_child:
# print ' make sure children get initialized'
# print ' call init_postload_internal of',self._tab.ident
# self._tab.init_postload_internal(obj)
def init_postload_external(self):
# if self._is_child:
# # restore normally
# AttrConf.init_postload_external(self)
# self._tab.init_postload_external()
# else:
# Substitute absolute ident with link object.
# Called from init_postload_external of attrsman during load_obj
#
ident_abs = self._ident_tab
# print 'reset_linkobj',self.attrname,ident_abs
obj = self.get_obj()
rootobj = obj.get_root()
# print ' rootobj',rootobj.ident
linkobj = rootobj.get_obj_from_ident(ident_abs)
# print ' linkobj',linkobj.ident
self._tab = linkobj
self.init_xml()
def is_modified(self):
return False
class TabIdsConf(ColConf):
"""
Column, where each entry contains a tuple with table object and id.
"""
def __init__(self, attrname, is_index=False, **kwargs):
self._is_index = is_index
AttrConf.__init__(self, attrname,
-1, # default id
struct='odict',
metatype='tabids',
**kwargs
)
def get_defaults(self, ids):
# create a list, should work for all types and dimensions
# default can be scalar or an array of any dimension
# print '\n\nget_defaults',self.attrname,ids,self.get_default()
return len(ids)*[(None, -1)]
def reset(self):
# TODO: this will reset all the tables
# instead should reset only the specified ids
if self._is_child:
for tab, ids in self.get_value():
tab.reset()
def clear(self):
self.reset()
# necessary? because tbles have been cleared from manager
# if self._is_child:
# for tab, ids in self.get_value():
# tab.clear()
def _getstate_specific(self, state):
"""
Called by __getstate__ to add/change specific states,
before returning states.
To be overridden.
"""
if self._is_save:
n = len(state['value'])
state['value'] = None
_tabids_save = n*[None]
i = 0
for tab, ids in self.get_value():
_tabids_save[i] = [tab.get_ident_abs(), ids]
i += 1
state['_tabids_save'] = _tabids_save
def init_postload_internal(self, man, obj):
# print 'IdsConf.init_postload_internal',self.attrname,hasattr(self,'value'),self._is_save,self._is_localvalue,'obj:',obj.ident
AttrConf.init_postload_internal(self, man, obj)
# if self._is_child:
# print ' make sure children get initialized'
# print ' call init_postload_internal of',self._tab.ident
# self._tab.init_postload_internal(obj)
def init_postload_external(self):
# if self._is_child:
# # restore normally
# AttrConf.init_postload_external(self)
# self._tab.init_postload_external()
# else:
# Substitute absolute ident with link object.
# Called from init_postload_external of attrsman during load_obj
#
#ident_abs = self._ident_tab
# print 'reset_linkobj',self.attrname,ident_abs
#obj = self.get_obj()
#rootobj = obj.get_root()
# print ' rootobj',rootobj.ident
#linkobj = rootobj.get_obj_from_ident(ident_abs)
# print ' linkobj',linkobj.ident
#self._tab = linkobj
# Substitute absolute ident with link object.
# Called from init_postload_external of attrsman during load_obj
#
_tabids_save = self._tabids_save
#ident_abs = self._ident_value
# print 'init_postload_external',self.attrname,_tabids_save
obj = self.get_obj()
rootobj = obj.get_root()
# print ' rootobj',rootobj.ident
tabids = len(self._tabids_save)*[None]
i = 0
for tabident, ids in self._tabids_save:
tab = rootobj.get_obj_from_ident(tabident)
# print ' ',tab.get_ident_abs(), ids
tabids[i] = [tab, ids]
i += 1
self.set_value(tabids)
def is_modified(self):
return False
class ObjsConf(ColConf):
"""
Column, where each entry is an object of class objclass with
ident= (attrname, id).
"""
# TODO:
# there is a problems with objects that are stored here
# in particular if objects are Table. What is their correct ident
# or absolute ident. Currently it is not correct.
# This leads to incorrect referencing when linked from elsewhere
# for example within TabIdListArrayConf
# .get_ident_abs() needs to to be correct, such that
# get_obj_from_ident can locate them
# maybe it is not an issue of ObjsConf itself,
# but the Objects stored must have a special getident method
def __init__(self, attrname, is_index=False, **kwargs):
self._is_index = is_index
self._is_child = True # at the moment no links possible
AttrConf.__init__(self, attrname,
None, # BaseObjman('empty'), # default id
struct='odict',
metatype='obj',
perm='r',
**kwargs
)
def set_obj(self, obj):
"""
Method to set instance of managed object.
Used by add method of AttrManager
"""
# print 'set_obj',self.attrname,obj.ident
AttrConf.set_obj(self, obj)
# if self._is_child:
obj.set_child(self)
# def get_valueobj(self, id = None):
# """
# This is called by get_childobj to retrive the child instance.
# Here this is just the table.
# """
# return self._tab
def predelete(self):
AttrConf.predelete(self)
# if self._is_child:
self.get_obj().del_child(self.attrname)
# def _getstate_specific(self, state):
# """
# Called by __getstate__ to add/change specific states,
# before returning states.
# To be overridden.
# """
# if self._is_save:
# if self._is_child:
# # OK self.value already set in
# pass
# else:
# # remove column reference and create column with idents
# state['value']= None
# idents_obj = OrderedDict()
# linkobjs = self.get_value()
# for _id in self.get_ids():
# idents_obj[_id] = linkobjs[_id].get_ident_abs()
# state['_idents_obj'] = idents_obj
def init_postload_internal(self, man, obj):
# print 'ObjsConf.init_postload_internal',self.attrname,hasattr(self,'value'),self._is_save,self._is_localvalue,'obj:',obj.ident
AttrConf.init_postload_internal(self, man, obj)
# if self._is_child:
# make sure all children in column get initialized
# print ' make sure childrenS get initialized'
childobjs = self.get_value()
obj = self.get_obj()
# print 'init_postload_internal',self.attrname,obj,obj.ident
for _id in obj.get_ids():
# print ' call init_postload_internal of',childobjs[_id].ident
childobjs[_id].init_postload_internal(obj) # attention obj is the parent object!
def reset(self):
# print 'ObjsConf.reset',self.get_value(),len(self.get_obj().get_ids())
#obj = self.get_obj()
# print 'init_postload_internal',self.attrname,obj,obj.ident
childobjs = self.get_value()
for _id in self.get_obj().get_ids():
# print ' call reset of',childobjs[_id].ident,_id
childobjs[_id].reset()
def clear(self):
odict = self.get_init()
if not self._is_localvalue:
setattr(self._obj, self.attrname, odict)
if self._is_index:
self.reset_index()
def is_modified(self):
# if self._is_child
#is_modified = self.get_value().is_modified()
# print 'is_modified', self.attrname, is_modified
childobjs = self.get_value()
#obj = self.get_obj()
# print 'init_postload_internal',self.attrname,obj,obj.ident
for _id in self.get_obj().get_ids():
# print ' call init_postload_internal of',childobjs[_id].ident
if childobjs[_id].is_modified():
return True
def set_modified(self, is_modified):
childobjs = self.get_value()
obj = self.get_obj()
# print 'init_postload_internal',self.attrname,obj,obj.ident
for _id in self.get_obj().get_ids():
# print ' call init_postload_internal of',childobjs[_id].ident
childobjs[_id].set_modified(is_modified)
def init_postload_external(self):
# if self._is_child:
# restore normally
AttrConf.init_postload_external(self)
childobjs = self.get_value()
for _id in self.get_obj().get_ids():
childobjs[_id].init_postload_external()
# def get_name(self):
# return self.'Table ID for '+self._tab.get_name()
#
# def get_info(self):
# return 'ID for Table:\n'+self._tab.get_info()
class Attrsman:
"""
Manages all attributes of an object
if argument obj is specified with an instance
then attributes are stored under this instance.
The values of attrname is then directly accessible with
obj.attrname
If nothing is specified, then column attribute will be stored under
the respective config instance of this attrsman (self).
The values of attrname is then directly accessible with
self.attrname.value
"""
def __init__(self, obj, attrname='attrsman', is_plugin=False):
if obj is None:
# this means that column data will be stored
# in value attribute of attrconfigs
obj = self
self._is_localvalue = True
else:
# this means that column data will be stored under obj
self._is_localvalue = False
self._obj = obj # managed object
self._attrconfigs = [] # managed attribute config instances
self.attrname = attrname # the manager's attribute name in the obj instance
self._attrs_nosave = set(ATTRS_NOSAVE)
# groupes of attributes
# key=groupname, value = list of attribute config instances
self._groups = {}
self.init_plugin(is_plugin)
def init_plugin(self, is_plugin):
if is_plugin:
self.plugin = Plugin(self)
else:
self.plugin = None
def enable_plugin(self, is_enabled=True):
if self.plugin is not None:
self.plugin.enable(is_enabled)
def is_localvalue(self):
return self._is_localvalue
def has_attrname(self, attrname):
# attention this is a trick, exploiting the fact that the
# attribute object with all the attr info is an attribute
# of the attr manager (=self)
return hasattr(self, attrname)
def is_modified(self):
for attrconf in self._attrconfigs:
# TODO: not very clean
if hasattr(attrconf, 'is_child'):
if attrconf.is_child():
if attrconf.is_modified():
return True
else:
if attrconf.is_modified():
return True
return False
def set_modified(self, is_modified=True):
for attrconf in self._attrconfigs:
attrconf.set_modified(is_modified)
def get_modified(self):
# returns a list of modified attributes
modified = []
for attrconf in self._attrconfigs:
if attrconf.is_modified():
modified.append(attrconf)
return modified
def __getitem__(self, attrname):
return getattr(self, attrname).get_value()
def get_config(self, attrname):
return getattr(self, attrname) # a bit risky
def get_configs(self, is_all=False, structs=None, filtergroupnames=None, is_private=False):
# print 'get_configs',self,self._obj.ident,structs,filtergroupnames,len(self._attrconfigs)
if is_all:
return self._attrconfigs
else:
attrconfigs = []
for attrconf in self._attrconfigs:
# print ' found',attrconf.attrname,attrconf.struct,'groupnames',attrconf.groupnames
is_check = True
if (structs is not None):
if (attrconf.struct not in structs):
is_check = False
if is_check:
# print ' **is_check',is_check
if len(attrconf.groupnames) > 0:
if ('_private' not in attrconf.groupnames) | is_private:
# print ' not private'
if filtergroupnames is not None:
# print ' apply filtergroupnames',filtergroupnames,attrconf.groupnames
if not set(filtergroupnames).isdisjoint(attrconf.groupnames):
# print ' append',attrconf.attrname
attrconfigs.append(attrconf)
else:
# print ' no filtergroupnames'
attrconfigs.append(attrconf)
else:
if filtergroupnames is None:
attrconfigs.append(attrconf)
return attrconfigs
# def get_colconfigs(self, is_all = False):
# return []
def get_obj(self):
return self._obj
def add(self, attrconf, is_overwrite=False, is_prepend=False):
"""
Add a one or several new attributes to be managed.
kwargs has attribute name as key and Attribute configuration object
as value.
"""
attrname = attrconf.attrname
# print '\n\nAttrsman.add',self.get_obj().ident,'add',attrname,self.has_attrname(attrname)
# dir(self._obj)
if (not self.has_attrname(attrname)) | is_overwrite:
attrconf.set_obj(self._obj)
attrconf.set_manager(self)
# set configuration object as attribute of AttrManager
setattr(self, attrname, attrconf)
# append also to the list of managed objects
if is_prepend:
self._attrconfigs.insert(0, attrconf)
else:
self._attrconfigs.append(attrconf)
# insert in groups
self.insert_groupnames(attrconf)
if self.plugin:
self.plugin.exec_events_attr(EVTADD, attrconf)
# return default value as attribute of managed object
if (attrconf.struct in STRUCTS_SCALAR) & (attrconf.is_returnval()): # == 'scalar':
return attrconf.get_init()
else:
return None # table configs do their own init
else:
# print ' attribute with this name already exists',attrname,type(attrconf)
# TODO: here we could do some intelligent updating
del attrconf
attrconf = getattr(self, attrname)
# print ' existing',attrconf,type(attrconf)
if (attrconf.struct in STRUCTS_SCALAR) & (attrconf.is_returnval()): # == 'scalar':
return attrconf.get_value()
else:
return None # table configs do their own init
def do_not_save_attr(self, attrname):
self._attrs_nosave.add(attrname)
def do_save_attr(self, attrname):
if attrname in self._attrs_nosave:
self._attrs_nosave.remove(attrname)
def do_not_save_attrs(self, attrnames):
self._attrs_nosave.update(attrnames)
def insert_groupnames(self, attrconf):
if len(attrconf.groupnames) > 0:
for groupname in attrconf.groupnames:
if not self._groups.has_key(groupname):
self._groups[groupname] = []
if attrconf not in self._groups[groupname]:
self._groups[groupname].append(attrconf)
def del_groupname(self, attrconf):
if len(attrconf.groupnames) > 0:
for groupname in self._groups.keys():
attrconfigs = self._groups[groupname]
if attrconf in attrconfigs:
if groupname not in attrconf.groupnames:
attrconfigs.remove(attrconf)
def get_groups(self):
return self._groups
def get_groupnames(self):
return self._groups.keys()
def has_group(self, groupname):
return self._groups.has_key(groupname)
def get_group(self, name):
"""
Returns a list with attributes that belong to that group name.
"""
# print 'get_group self._groups=\n',self._groups.keys()
return self._groups.get(name, [])
def get_group_attrs(self, name):
"""
Returns a dictionary with all attributes of a group.
Key is attribute name and value is attribute value.
"""
# print 'get_group_attrs', self._groups
attrs = OrderedDict()
if not self._groups.has_key(name):
return attrs
for attrconf in self._groups[name]:
# print ' attrconf.attrname',attrconf.attrname
attrs[attrconf.attrname] = getattr(self._obj, attrconf.attrname)
# print ' attrs',attrs
return attrs
def write_csv(self, fd, sep=',',
show_parentesis=False, attrconfigs=None,
groupnames=None,
is_export_not_save=True):
# print 'Attrsman.write_csv attrconfigs'#,attrconfigs,'groupnames',groupnames,
if attrconfigs is None:
attrconfigs = self.get_configs(is_all=False, structs=STRUCTS_SCALAR, filtergroupnames=groupnames)
for attrconf in attrconfigs:
# print ' attrconfig', attrconf.attrname,attrconf.struct,attrconf.metatype
if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in STRUCTS_SCALAR):
mt = attrconf.metatype
if mt == 'id':
fd.write('%s %s%s%s\n' % (attrconf.attrname, attrconf.format_unit(show_parentesis),
sep, attrconf.get_linktab().format_ids([attrconf.get_value()])))
else:
fd.write('%s %s%s%s\n' % (attrconf.attrname, attrconf.format_unit(
show_parentesis), sep, attrconf.format_value()))
def print_attrs(self, show_unit=True, show_parentesis=False, attrconfigs=None):
print('Attributes of', self._obj._name, 'ident_abs=', self._obj.get_ident_abs())
if attrconfigs is None:
attrconfigs = self.get_configs(structs=STRUCTS_SCALAR)
# for attrconf in attrconfigs:
# print ' %s =\t %s'%(attrconf.attrname, attrconf.format_value(show_unit=True))
def save_values(self, state):
"""
Called by the managed object during save to save the
attribute values.
"""
for attrconfig in self.get_configs():
attrconfig.save_value(state)
def delete(self, attrname):
"""
Delete attribute with respective name
"""
# print '.__delitem__','attrname=',attrname
# if hasattr(self,attrname):
attrconf = getattr(self, attrname)
if attrconf in self._attrconfigs:
if self.plugin:
self.plugin.exec_events_attr(EVTDEL, attrconf)
for groupname in attrconf.groupnames:
self._groups[groupname].remove(attrconf)
self._attrconfigs.remove(attrconf)
attrconf.predelete() # this will remove also the value attribute
#attrname = attrconf.attrname
del self.__dict__[attrname] # delete config
return True
return False # attribute not managed
# return False # attribute not existant
def __getstate__(self):
# if hasattr(self,'attrname'):
# print 'Attrsman.__getstate__ of',self.attrname,' of obj=',self._obj.ident,'id',id(self),'id obj',id(self._obj)
#
# else:
# print 'WARNING in Attrsman.__getstate__',self,'attrname missing','id',id(self),'id obj',id(self._obj)
if not hasattr(self, '_obj'):
print('WARNING: unknown obj in attrman', self, type(self))
# print ' dir',dir(self)
# if hasattr(self,'attrname'):
# print ' No attrman but attribute',self.attrname
# for attrconf in self.get_configs(is_all=True):
# print ' attrname=',attrconf.attrname
return {}
# if not hasattr(self,'_attrs_nosave'):
# print 'WARNING: in __getstate__ of',self.attrname#,'obj',self._obj,'has no attr _attrs_nosave'
# #self.print_attrs()
# print 'dict=\n',self.__dict__
# print ' self.__dict__=\n',self.__dict__.keys()
state = {}
for attr in self.__dict__.keys():
# print ' attr',attr,self.__dict__[attr]
# TODO: optimize and put this at the end
if attr == 'plugin':
plugin = self.__dict__[attr]
if plugin is not None:
state[attr] = True
else:
state[attr] = False
elif attr == '_attrconfigs':
attrconfigs_save = []
for attrconfig in self._attrconfigs:
if attrconfig.is_save():
# print ' save',attrconfig.attrname
# if attrconfig.struct == 'array':
# print ' size =',len(self)
attrconfigs_save.append(attrconfig)
state[attr] = attrconfigs_save
elif attr not in self._attrs_nosave:
state[attr] = self.__dict__[attr]
# print ' _attrs_nosave=', self._attrs_nosave
# print ' state=', state
return state
def __setstate__(self, state):
# print '__setstate__',self
# this is always required, but will not be saved
# self.plugins={}
for attr in state.keys():
# print ' set state',attr
# plugin set in init_postload_internal
# if attr=='plugin':
# if state[attr]==True:
# self.__dict__[attr] = Plugin(self)
# else:
# self.__dict__[attr]= None
# else:
self.__dict__[attr] = state[attr]
def init_postload_internal(self, obj):
"""
Called after set state.
Link internal states.
"""
# print 'Attrsman.init_postload_internal of obj:',obj.ident
if not hasattr(self, '_attrs_nosave'):
self._attrs_nosave = set(ATTRS_NOSAVE)
# if not hasattr(self,'_attrs_nosave'):
# print 'WARNING: in init_postload_internal of',self.attrname,'obj',obj,'has no attr _attrs_nosave'
self._obj = obj
self.init_plugin(self.plugin)
for attrconfig in self.get_configs(is_all=True):
# print ' call init_postload_internal of',attrconfig.attrname
attrconfig.init_postload_internal(self, obj)
def init_postload_external(self):
"""
Called after set state.
Link external states.
"""
# print 'Attrsman.init_postload_external',self._obj.get_ident()
for attrconfig in self.get_configs(is_all=True):
# print ' call',attrconfig.attrname,attrconfig.metatype
attrconfig.init_postload_external()
class Tabman(Attrsman):
"""
Manages all table attributes of an object.
if argument obj is specified with an instance
then column attributes are stored under this instance.
The values of attrname is then directly accessible with
obj.attrname
If nothing is specified, then column attribute will be stored under
the respective config instance of this tab man (self).
The values of attrname is then directly accessible with
self.attrname.value
"""
def __init__(self, obj=None, **kwargs):
Attrsman.__init__(self, obj, **kwargs)
self._colconfigs = []
self._ids = []
def add_col(self, attrconf):
# print 'add_col',attrconf.attrname,attrconf.is_index()
attrname = attrconf.attrname
if not self.has_attrname(attrname):
Attrsman.add(self, attrconf) # insert in common attrs database
self._colconfigs.append(attrconf)
# returns initial array and also create local array if self._is_localvalue == True
return attrconf.get_init()
else:
return getattr(self, attrname).get_value()
def delete(self, attrname):
"""
Delete attribute with respective name
"""
# print '.__delitem__','attrname=',attrname
if hasattr(self, attrname):
attrconf = getattr(self, attrname)
if self.plugin:
self.plugin.exec_events_attr(EVTDEL, attrconf)
if Attrsman.delete(self, attrname):
if attrconf in self._colconfigs:
self._colconfigs.remove(attrconf)
def get_colconfigs(self, is_all=False, filtergroupnames=None):
if is_all:
return self._colconfigs
else:
colconfigs = []
if filtergroupnames is not None:
filtergroupnameset = set(filtergroupnames)
for colconfig in self._colconfigs:
if len(colconfig.groupnames) > 0:
if '_private' not in colconfig.groupnames:
if filtergroupnames is not None:
if not filtergroupnameset.isdisjoint(colconfig.groupnames):
colconfigs.append(colconfig)
else:
colconfigs.append(colconfig)
else:
if filtergroupnames is None:
colconfigs.append(colconfig)
return colconfigs
def get_ids(self):
return self._ids
def __len__(self):
"""
Determine current array length (same for all arrays)
"""
return len(self._ids)
def __contains__(self, _id):
return _id in self._ids
def select_ids(self, mask):
ids_mask = []
i = 0
for _id in self.get_ids():
if mask[i]:
ids_mask.append(_id)
i += 1
return ids_mask
def suggest_id(self, is_zeroid=False):
"""
Returns a an availlable id.
Options:
is_zeroid=True allows id to be zero.
"""
if is_zeroid:
id0 = 0
else:
id0 = 1
id_set = set(self.get_ids())
if len(id_set) == 0:
id_max = 0
else:
id_max = max(id_set)
# print 'suggest_id',id0,
return list(id_set.symmetric_difference(xrange(id0, id_max+id0+1)))[0]
def suggest_ids(self, n, is_zeroid=False):
"""
Returns a list of n availlable ids.
It returns even a list for n=1.
Options:
is_zeroid=True allows id to be zero.
"""
if is_zeroid:
id0 = 0
else:
id0 = 1
id_set = set(self.get_ids())
if len(id_set) == 0:
id_max = 0
else:
id_max = max(id_set)
return list(id_set.symmetric_difference(xrange(id0, id_max+id0+n)))[:n]
def add_rows(self, n=None, ids=[], **attrs):
if n is not None:
ids = self.suggest_ids(n)
elif len(ids) == 0:
# get number of rows from any valye vector provided
ids = self.suggest_ids(len(attrs.values()[0]))
else:
# ids already given , no ids to create
pass
self._ids += ids
# print 'add_rows ids', ids
for colconfig in self._colconfigs:
colconfig.add(ids, values=attrs.get(colconfig.attrname, None))
if self.plugin:
self.plugin.exec_events_ids(EVTADDITEM, ids)
return ids
def add_row(self, _id=None, **attrs):
if _id is None:
_id = self.suggest_id()
self._ids += [_id, ]
for colconfig in self._colconfigs:
colconfig.add(_id, values=attrs.get(colconfig.attrname, None))
if self.plugin:
self.plugin.exec_events_ids(EVTADDITEM, [_id])
return _id
def set_row(self, _id, **attrs):
for colconfig in self._colconfigs:
colconfig.set(_id, values=attrs.get(colconfig.attrname, None))
if self.plugin:
self.plugin.exec_events_ids(EVTSETITEM, [_id])
def set_rows(self, ids, **attrs):
# print 'add_rows ids', ids
for colconfig in self._colconfigs:
colconfig.set(ids, values=attrs.get(colconfig.attrname, None))
if self.plugin:
self.plugin.exec_events_ids(SETSETITEM, ids)
def get_row(self, _id):
attrvalues = {}
if self.plugin:
self.plugin.exec_events_ids(EVTGETITEM, [_id])
for attrconfig in self._colconfigs:
attrvalues[attrconfig.attrname] = attrconfig[_id]
return attrvalues
def del_rows(self, ids):
if self.plugin:
self.plugin.exec_events_ids(EVTDELITEM, ids)
for colconfig in self._colconfigs:
del colconfig[ids]
for _id in ids:
self._ids.remove(_id)
def del_row(self, _id):
if self.plugin:
self.plugin.exec_events_ids(EVTDELITEM, [_id])
for colconfig in self._colconfigs:
del colconfig[_id]
self._ids.remove(_id)
def __delitem__(self, ids):
"""
remove rows correspondent to the given ids from all array and dict
attributes
"""
if hasattr(ids, '__iter__'):
self.del_rows(ids)
else:
self.del_row(ids)
def print_attrs(self, ids=None, **kwargs):
# print 'Attributes of',self._obj._name,'(ident=%s)'%self._obj.ident
Attrsman.print_attrs(self, attrconfigs=self.get_configs(structs=['scalar']), **kwargs)
# print ' ids=',self._ids
if ids is None:
ids = self.get_ids()
for _id in ids:
for attrconf in self.get_configs(structs=STRUCTS_COL):
print(' %s[%d] =\t %s' % (attrconf.attrname, _id, attrconf.format_value(_id, show_unit=True)))
def write_csv(self, fd, sep=',', ids=None,
attrconfigs=None,
groupnames=None,
show_parentesis=True,
is_export_not_save=True, # export attr, also non save
name_id='ID',
):
# if attrconfigs is None:
# attrconfigs = self.get_configs()
# print 'Tabman.write_csv attrconfigs is_export_not_save',is_export_not_save
Attrsman.write_csv(self, fd, sep=sep,
show_parentesis=show_parentesis,
groupnames=groupnames,
attrconfigs=attrconfigs,
#is_export_private = False,
is_export_not_save=is_export_not_save, # export attr, also non save
)
# fd.write('\n')
# for attrconf in attrconfigs:
# #print ' %s =\t %s'%(attrconf.attrname, attrconf.format_value(show_unit=True))
# fd.write( '%s %s%s%s\n'%(attrconf.attrname,format_unit(show_parentesis), sep, attrconf.format_value()))
if ids is None:
ids = self.get_ids()
if groupnames is not None:
#attrconfigs = self.get_group(groupname)
attrconfigs = self.get_configs(is_all=False, filtergroupnames=groupnames)
is_exportall = False
elif attrconfigs is None:
attrconfigs = self.get_colconfigs(is_all=False)
#is_exportall = False
# check if attributes are all column attribute and indicted for save
attrconfigs_checked = []
for attrconf in attrconfigs:
if (attrconf.is_save() | is_export_not_save) & (attrconf.struct in STRUCTS_COL):
attrconfigs_checked.append(attrconf)
# first table row
row = name_id
for attrconf in attrconfigs_checked:
# print ' attrconfig', attrconf.attrname,attrconf.metatype
row += sep+self._clean_csv(attrconf.format_symbol(show_parentesis=show_parentesis), sep)
fd.write(row+'\n')
# rest
for _id in ids:
row = str(_id)
row = self._clean_csv(row, sep)
for attrconf in attrconfigs:
mt = attrconf.metatype
# print ' attrconf,',attrconf.attrname,mt,attrconf[_id]
if mt == 'id':
# print ' ',
row += sep+self._clean_csv(attrconf.get_linktab().format_ids([attrconf[_id]]), sep)
else:
row += sep+self._clean_csv('%s' % (attrconf.format_value(_id, show_unit=False)), sep)
# make sure there is no CR in the row!!
# print row
fd.write(row+'\n')
class BaseObjman:
"""
Object management base methods to be inherited by all object managers.
"""
def __init__(self, ident, is_plugin=False, **kwargs):
# print 'BaseObjman.__init__',ident#,kwargs
self._init_objman(ident, **kwargs)
self.set_attrsman(Attrsman(self, is_plugin=is_plugin))
# print 'BaseObjman.__init__',self.format_ident(),'parent=',self.parent
self._init_attributes()
self._init_constants()
def set_attrsman(self, attrsman):
self._attrsman = attrsman
return attrsman
def _init_objman(self, ident='no_ident', parent=None, name=None,
managertype='basic', info=None, logger=None,
xmltag=None, version=0.0):
# print 'BaseObjman._init_objman',ident,logger,parent
self.managertype = managertype
self.ident = ident
self.set_version(version)
self.set_logger(logger)
#self._is_root = False
self.parent = parent
self.childs = {} # dict with attrname as key and child instance as value
self._info = info
self._is_saved = False
if name is None:
self._name = self.format_ident()
else:
self._name = name
self.set_xmltag(xmltag)
# print ' self.parent',self.parent
# must be called explicitely during __init__
# self._init_attributes()
# self._init_constants()
def _init_attributes(self):
"""
This is the place to add all attributes.
This method will be called to initialize
and after loading a saved object.
Use this method also to update a version.
"""
pass
def _init_constants(self):
"""
This is the place to init any costants that are outside the management.
Constants are not saved.
This method will be called to initialize and after loading a saved object.
"""
pass
def set_version(self, version):
self._version = version
def get_version(self):
if hasattr(self, '_version'):
return self._version
else:
# for compatibility
return 0.0
# def _upgrade_version(self):
# pass
# def _init_xml(self,xmltag=None):
# if xmltag is not None:
# self.xmltag = xmltag
# else:
# self.xmltag = self.get_ident()
def reset(self):
"""
Resets all attributes to default values
"""
# print 'reset'
for attrconfig in self.get_attrsman().get_configs(is_all=True):
# print ' reset',attrconfig.attrname
attrconfig.reset()
def clear(self):
"""
Clear tables and reset scalars.
"""
for attrconfig in self.get_attrsman().get_configs(is_all=True):
attrconfig.clear()
self._init_constants()
def unplug(self):
if self.plugin:
self.plugin.unplug()
def set_xmltag(self, xmltag, xmlsep=' '):
self.xmltag = xmltag
self.xmlsep = xmlsep
def write_xml(self, fd, ident):
if self.xmltag is not None:
# figure out scalar attributes and child objects
attrconfigs = []
objconfigs = []
for attrconfig in self.get_attrsman().get_configs(structs=STRUCTS_SCALAR):
if (attrconfig.metatype == 'obj'): # better use self.childs
if (attrconfig.get_value().xmltag is not None) & attrconfig.is_child():
objconfigs.append(attrconfig)
elif attrconfig.xmltag is not None:
attrconfigs.append(attrconfig)
# start writing
if len(attrconfigs) > 0:
# there are scalar attributes
fd.write(xm.start(self.xmltag, ident))
for attrconfig in attrconfigs:
attrconfig.write_xml(fd)
# are there child objects to write
if len(objconfigs) > 0:
fd.write(xm.stop())
for attrconfig in objconfigs:
attrconfig.get_value().write_xml(fd, ident+2)
fd.write(xm.end(self.xmltag, ident))
else:
fd.write(xm.stopit())
else:
# no scalars
fd.write(xm.begin(self.xmltag, ident))
if len(objconfigs) > 0:
for attrconfig in objconfigs:
attrconfig.get_value().write_xml(fd, ident+2)
fd.write(xm.end(self.xmltag, ident))
def get_logger(self):
# print 'get_logger',self.ident,self._logger,self.parent
if self._logger is not None:
return self._logger
else:
return self.parent.get_logger()
def set_logger(self, logger):
# print 'set_logger',self.ident,logger
self._logger = logger
def __repr__(self):
# return '|'+self._name+'|'
return self.format_ident()
def is_modified(self):
return self._attrsman.is_modified()
def set_modified(self, is_modified=True):
self._attrsman.set_modified(is_modified)
def get_name(self):
return self._name
def get_info(self):
if self._info is None:
return self.__doc__
else:
return self._info
def get_ident(self):
return self.ident
def _format_ident(self, ident):
if hasattr(ident, '__iter__'):
return str(ident[0])+'#'+str(ident[1])
else:
return str(ident)
def format_ident(self):
return self._format_ident(self.ident)
def format_ident_abs(self):
s = ''
# print 'format_ident_abs',self.get_ident_abs()
for ident in self.get_ident_abs():
s += self._format_ident(ident)+'.'
return s[:-1]
def export_csv(self, filepath, sep=',',
attrconfigs=None, groupnames=None,
is_header=True, is_ident=False, is_timestamp=True,
show_parentesis=True):
"""
Export scalars to file feed in csv format.
"""
print('BaseObjman.export_csv', filepath, "*"+sep+"*", 'attrconfigs', attrconfigs, self.get_attrsman())
fd = open(filepath, 'w')
# header
if is_header:
row = self._clean_csv(self.get_name(), sep)
if is_ident:
row += sep+'(ident=%s)' % self.format_ident_abs()
fd.write(row+'\n')
if is_timestamp:
now = datetime.now()
fd.write(self._clean_csv(now.isoformat(), sep)+'\n')
fd.write('\n\n')
self.get_attrsman().write_csv(fd, sep=sep,
show_parentesis=show_parentesis, groupnames=groupnames,
attrconfigs=attrconfigs)
fd.close()
def _clean_csv(self, row, sep):
row = row.replace('\n', ' ')
#row=row.replace('\b',' ')
row = row.replace('\r', ' ')
#row=row.replace('\f',' ')
#row=row.replace('\newline',' ')
row = row.replace(sep, ' ')
return row
def get_root(self):
# if hasattr(self,'_is_root'):
# print 'get_root',self.ident,'is_root',self._is_root
# if self._is_root:
# return self
if self.parent is not None:
return self.parent.get_root()
else:
return self
def get_ident_abs(self):
"""
Returns absolute identity.
This is the ident of this object in the global tree of objects.
If there is a parent objecty it must also be managed by the
object manager.
"""
# print 'obj.get_ident_abs',self.ident,self.parent, type(self.parent)
# if hasattr(self,'_is_root'):
# print 'get_ident_abs',self.ident,'is_root',self._is_root
# if self._is_root:
# return (self.get_ident(),)# always return tuple
if self.parent is not None:
return self.parent.get_ident_abs()+(self.ident,)
else:
return (self.get_ident(),) # always return tuple
def get_obj_from_ident(self, ident_abs):
# print 'get_obj_from_ident',self.ident,ident_abs
if len(ident_abs) == 1:
# arrived at the last element
# check if it corresponds to the present object
if ident_abs[0] == self.ident:
return self
else:
return None # could throw an error
else:
return self.get_childobj(ident_abs[1]).get_obj_from_ident(ident_abs[1:])
# this is an attemt to restore objects from
# root objects without childs
# def search_ident_abs(self, childobj):
# """
# Returns root and absolute ident for the found root.
# """
# #if hasattr(self,'_is_root'):
# # print 'get_root',self.ident,'is_root',self._is_root
# # if self._is_root:
# # return self
#
# if self.parent is not None:
# if self.parent.childs.has_key(childobj.ident)
# return self.parent.get_root()
# else:
# return self
# def search_obj_from_ident(self, ident_abs, obj_init):
#
# #print 'get_obj_from_ident',self.ident,ident_abs
# if len(ident_abs)==1:
# # arrived at the last element
# # check if it corresponds to the present object
# if ident_abs[0] == self.ident:
# return self
# else:
# return None # could throw an error
# else:
# return self.get_childobj(ident_abs[1]).get_obj_from_ident(ident_abs[1:])
def get_childobj(self, attrname):
"""
Return child instance
"""
if self.childs.has_key(attrname):
config = self.childs[attrname]
return config.get_value()
else:
return BaseObjman(self)
def set_child(self, childconfig):
"""
Set child childconfig
"""
self.childs[childconfig.attrname] = childconfig
def del_child(self, attrname):
"""
Return child instance
"""
del self.childs[attrname]
def get_parent(self):
return self.parent
# def reset_parent(self, parent):
# self.parent=parent
# def set_attrsman(self, attrsman):
# # for quicker acces and because it is only on
# # the attribute management is public and also directly accessible
# #setattr(self, attrname,Attrsman(self))# attribute management
# self._attrsman = attrsman
# #return attrsman
def get_attrsman(self):
return self._attrsman
def _getstate_specific(self, state):
"""
Called by __getstate__ to add/change specific states,
before returning states.
To be overridden.
"""
pass
def __getstate__(self):
# print 'BaseObjman.__getstate__',self.ident,self._is_saved
# print ' self.__dict__=\n',self.__dict__.keys()
state = {}
# if not self._is_saved:
# if self._is_saved:
# # this message indicates a loop!!
# print 'WARNING in __getstate__: object already saved',self.format_ident_abs()
# print ' save standart values'
for attr in ATTRS_SAVE:
if hasattr(self, attr):
state[attr] = getattr(self, attr)
# print ' save all scalar stuctured attributes'
# attrsman knows which and how
# self._attrsman.save_values(state)
#
# values of configured attributes are not saved here
# values are now ALWAYS stored in the value attribute of the
# attrconfig and reset in main obj
# print ' save also attrsman'
state['_attrsman'] = self._attrsman
self._getstate_specific(state)
self._is_saved = True
# else:
# print 'WARNING in __getstate__: object %s already saved'%self.ident
return state
def __setstate__(self, state):
# print '__setstate__',self
# this is always required, but will not be saved
# self.plugins={}
for key in state.keys():
# print ' set state',key
self.__dict__[key] = state[key]
self._is_saved = False
# done in init2_config...
# set default values for all states tha have not been saved
# for attr in self._config.keys():
# if (not self._config[attr]['save']) & (not hasattr(self,attr)):
# print ' config attr',attr
# self.config(attr,**self._config[attr])
# set other states
# self._setstate(state)
def init_postload_internal(self, parent):
"""
Called after set state.
Link internal states and call constant settings.
"""
print('BaseObjman.init_postload_internal', self.ident, 'parent:')
# if parent is not None:
# print parent.ident
# else:
# print 'ROOT'
self.parent = parent
self.childs = {}
self._attrsman.init_postload_internal(self)
def init_postload_external(self):
"""
Called after set state.
Link internal states.
"""
#self._is_root = is_root
# print 'init_postload_external',self.ident#,self._is_root
# set default logger
self.set_logger(Logger(self))
# for child in self.childs.values():
# child.reset_parent(self)
self._attrsman.init_postload_external()
self._init_attributes()
self._init_constants()
class TableMixin(BaseObjman):
def format_ident_row(self, _id):
# print 'format_ident_row',_id
return self.format_ident()+'['+str(_id)+']'
def format_ident_row_abs(self, _id):
return self.format_ident_abs()+'['+str(_id)+']'
def get_obj_from_ident(self, ident_abs):
# print 'get_obj_from_ident',self.ident,ident_abs,type(ident_abs)
if len(ident_abs) == 1:
# arrived at the last element
# check if it corresponds to the present object
ident_check = ident_abs[0]
# now 2 things can happen:
# 1.) the ident is a simple string identical to ident of the object
# in this case, return the whole object
# 2.) ident is a tuple with string and id
# in this case return object and ID
# if hasattr(ident_check, '__iter__'):
# #if (ident_check[0] == self.ident)&(ident_check[1] in self._ids):
# if ident_check[1] in self._ids:
# return (self, ident_check[1])
# else:
# return None # could throw an error
# else:
if ident_check == self.ident:
return self
else:
childobj = self.get_childobj(ident_abs[1])
return childobj.get_obj_from_ident(ident_abs[1:])
def get_childobj(self, ident):
"""
Return child instance.
This is any object with ident
"""
if hasattr(ident, '__iter__'):
# access of ObjsConf configured child
# get object from column attrname
attrname, _id = ident
config = self.childs[attrname]
return config[_id] # config.get_valueobj(_id)
else:
# access of ObjConf configured child
# get object from attrname
config = self.childs[ident]
return config.get_value()
def __getstate__(self):
# print '__getstate__',self.ident,self._is_saved
# print ' self.__dict__=\n',self.__dict__.keys()
state = {}
if 1: # not self._is_saved:
# print ' save standart values'
for attr in ATTRS_SAVE+ATTRS_SAVE_TABLE:
if attr == 'plugin':
plugin = self.__dict__[attr]
if plugin is not None:
state[attr] = True
else:
state[attr] = False
elif hasattr(self, attr):
state[attr] = getattr(self, attr)
# save managed attributes !!!
for attrconfig in self.get_configs(is_all=True):
state[attrconfig.attrname] = attrconfig
# print ' save all scalar stuctured attributes'
# attrsman knows which and how
# self.save_values(state)
# print ' save also attrsman'
#state['attrsman'] = self._attrsman
self._is_saved = True
else:
print('WARNING in __getstate__: object %s already saved' % self.ident)
return state
def __setstate__(self, state):
# print '__setstate__',self.ident
# this is always required, but will not be saved
self.plugins = {}
for attr in state.keys():
# print ' set state',key
if attr == 'plugin':
if state[attr] == True:
self.__dict__[attr] = Plugin(self)
else:
self.__dict__[attr] = None
else:
self.__dict__[attr] = state[attr]
self._is_saved = False
# done in init2_config...
# set default values for all states tha have not been saved
# for attr in self._config.keys():
# if (not self._config[attr]['save']) & (not hasattr(self,attr)):
# print ' config attr',attr
# self.config(attr,**self._config[attr])
# set other states
# self._setstate(state)
def init_postload_internal(self, parent):
"""
Called after set state.
Link internal states.
"""
# print 'TableObjman.init_postload_internal',self.ident,'parent:',
# if parent is not None:
# print parent.ident
# else:
# print 'ROOT'
if not hasattr(self, '_attrs_nosave'):
self._attrs_nosave = set(ATTRS_NOSAVE)
self.parent = parent
self.childs = {}
self.set_attrsman(self)
Attrsman.init_postload_internal(self, self)
self._is_saved = False
def init_postload_external(self):
"""
Called after set state.
Link internal states.
"""
Attrsman.init_postload_external(self)
# no: BaseObjman.init_postload_external(self)
self._init_attributes()
self._init_constants()
def export_csv(self, filepath, sep=',',
ids=None, attrconfigs=None, groupnames=None,
is_header=True, is_ident=False, is_timestamp=True,
show_parentesis=True, name_id='ID'):
"""
Export scalars to file feed in csv format.
"""
print('TableMixin.export_csv', filepath, "*"+sep+"*") # ,'attrconfigs',attrconfigs,self.get_attrsman()
fd = open(filepath, 'w')
# header
if is_header:
row = self._clean_csv(self.get_name(), sep)
if is_ident:
row += sep+'(ident=%s)' % self.format_ident_abs()
fd.write(row+'\n')
if is_timestamp:
now = datetime.now()
fd.write(self._clean_csv(now.isoformat(), sep)+'\n')
fd.write('\n\n')
self.get_attrsman().write_csv(fd, sep=sep, ids=ids,
show_parentesis=show_parentesis, groupnames=groupnames,
attrconfigs=attrconfigs, name_id='ID')
fd.close()
def clear_rows(self):
if self.plugin:
self.plugin.exec_events_ids(EVTDELITEM, self.get_ids())
self._ids = []
for colconfig in self.get_attrsman()._colconfigs:
# print 'ArrayObjman.clear_rows',colconfig.attrname,len(colconfig.get_value())
colconfig.clear()
# print ' done',len(colconfig.get_value())
def clear(self):
# print 'ArrayObjman.clear',self.ident
# clear/reset scalars
for attrconfig in self.get_attrsman().get_configs(structs=STRUCTS_SCALAR):
attrconfig.clear()
self.clear_rows()
self.set_modified()
def _write_xml_body(self, fd, indent, objconfigs, idcolconfig_include_tab, colconfigs,
objcolconfigs, xmltag_item, attrconfig_id, xmltag_id, ids, ids_xml):
# print '_write_xml_body ident,ids',self.ident,ids
if ids is None:
ids = self.get_ids()
if ids_xml is None:
ids_xml = ids
for attrconfig in objconfigs:
attrconfig.get_value().write_xml(fd, indent+2)
# check if columns contain objects
#objcolconfigs = []
scalarcolconfigs = colconfigs
# for attrconfig in colconfigs:
# if attrconfig.metatype == 'obj':
# objcolconfigs.append(attrconfig)
# else:
# scalarcolconfigs.append(attrconfig)
for _id, id_xml in zip(ids, ids_xml):
fd.write(xm.start(xmltag_item, indent+2))
# print ' make tag and id',_id
if xmltag_id == '':
# no id tag will be written
pass
elif (attrconfig_id is None) & (xmltag_id is not None):
# use specified id tag and and specified id values
fd.write(xm.num(xmltag_id, id_xml))
elif (attrconfig_id is not None):
# use id tag and values of attrconfig_id
attrconfig_id.write_xml(fd, _id)
# print ' write columns',len(scalarcolconfigs)>0,len(idcolconfig_include_tab)>0,len(objcolconfigs)>0
for attrconfig in scalarcolconfigs:
# print ' scalarcolconfig',attrconfig.attrname
attrconfig.write_xml(fd, _id)
if (len(idcolconfig_include_tab) > 0) | (len(objcolconfigs) > 0):
fd.write(xm.stop())
for attrconfig in idcolconfig_include_tab:
# print ' include_tab',attrconfig.attrname
attrconfig.write_xml(fd, _id, indent+4)
for attrconfig in objcolconfigs:
# print ' objcolconfig',attrconfig.attrname
attrconfig[_id].write_xml(fd, indent+4)
fd.write(xm.end(xmltag_item, indent+2))
else:
fd.write(xm.stopit())
# print ' _write_xml_body: done'
def write_xml(self, fd, indent, xmltag_id='id', ids=None, ids_xml=None,
is_print_begin_end=True, attrconfigs_excluded=[]):
# print 'write_xml',self.ident#,ids
if self.xmltag is not None:
xmltag, xmltag_item, attrname_id = self.xmltag
if xmltag == '': # no begin end statements
is_print_begin_end = False
if ids is not None:
if not hasattr(ids, '__iter__'):
ids = [ids]
if attrname_id == '': # no id info will be written
attrconfig_id = None
xmltag_id = ''
elif attrname_id is not None: # an attrconf for id has been defined
attrconfig_id = getattr(self.get_attrsman(), attrname_id)
xmltag_id = None # this will define the id tag
else:
attrconfig_id = None # native id will be written using xmltag_id from args
# print ' attrname_id,attrconfig_id',attrname_id,attrconfig_id
# if attrconfig_id is not None:
# print ' attrconfig_id',attrconfig_id.attrname
# figure out scalar attributes and child objects
attrconfigs = []
objconfigs = []
colconfigs = []
objcolconfigs = []
idcolconfig_include_tab = []
for attrconfig in self.get_attrsman().get_configs(is_all=True):
# print ' check',attrconfig.attrname,attrconfig.xmltagis not None,attrconfig.is_colattr(),attrconfig.metatype
if attrconfig == attrconfig_id:
pass
elif attrconfig in attrconfigs_excluded:
pass
elif attrconfig.is_colattr() & (attrconfig.metatype == 'obj'):
objcolconfigs.append(attrconfig)
elif (attrconfig.is_colattr()) & (attrconfig.metatype in ('ids', 'id')) & (attrconfig.xmltag is not None):
if hasattr(attrconfig, "is_xml_include_tab"):
if attrconfig.is_xml_include_tab:
idcolconfig_include_tab.append(attrconfig)
else:
colconfigs.append(attrconfig)
else:
colconfigs.append(attrconfig)
elif attrconfig.is_colattr() & (attrconfig.xmltag is not None):
colconfigs.append(attrconfig)
elif (attrconfig.metatype == 'obj'): # better use self.childs
if (attrconfig.get_value().xmltag is not None) & attrconfig.is_child():
objconfigs.append(attrconfig)
elif attrconfig.xmltag is not None:
attrconfigs.append(attrconfig)
# print ' attrconfigs',attrconfigs
# print ' objconfigs',objconfigs
# print ' idcolconfig_include_tab',idcolconfig_include_tab
# print ' colconfigs',colconfigs
# start writing
if len(attrconfigs) > 0:
# print ' there are scalar attributes'
if is_print_begin_end:
fd.write(xm.start(xmltag, indent))
for attrconfig in attrconfigs:
attrconfig.write_xml(fd)
# are there child objects to write
if (len(objconfigs) > 0) | (len(colconfigs) > 0) | (len(idcolconfig_include_tab) > 0):
fd.write(xm.stop())
self._write_xml_body(fd, indent, objconfigs, idcolconfig_include_tab,
colconfigs,
objcolconfigs,
xmltag_item, attrconfig_id,
xmltag_id, ids, ids_xml)
fd.write(xm.end(xmltag, indent))
else:
fd.write(xm.stopit())
else:
# print ' no scalars'
if is_print_begin_end:
fd.write(xm.begin(xmltag, indent))
self._write_xml_body(fd, indent, objconfigs, idcolconfig_include_tab,
colconfigs,
objcolconfigs,
xmltag_item, attrconfig_id,
xmltag_id, ids, ids_xml)
if is_print_begin_end:
fd.write(xm.end(xmltag, indent))
class TableObjman(Tabman, TableMixin):
"""
Table Object management manages objects with list and dict based columns.
For faster operation use ArrayObjman in arrayman package, which requires numpy.
"""
def __init__(self, ident, **kwargs):
self._init_objman(ident, **kwargs)
self._init_attributes()
self._init_constants()
def _init_objman(self, ident, is_plugin=False, **kwargs):
BaseObjman._init_objman(self, ident, managertype='table', **kwargs)
Tabman.__init__(self, is_plugin=is_plugin)
# self.set_attrsman(self)
self.set_attrsman(self)
###############################################################################
if __name__ == '__main__':
"""
Test
"""
pass
|