/usr/lib/python2.7/dist-packages/dispcalGUI/worker.py is in dispcalgui 1.7.1.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 | # -*- coding: utf-8 -*-
# stdlib
from __future__ import with_statement
import getpass
import math
import os
import re
import shutil
import subprocess as sp
import sys
import tempfile
import textwrap
import traceback
from encodings.aliases import aliases
from hashlib import md5
from time import sleep, strftime, time
if sys.platform == "darwin":
from platform import mac_ver
from thread import start_new_thread
elif sys.platform == "win32":
from ctypes import windll
from win32com.shell import shell
import pythoncom
import win32con
# 3rd party
if sys.platform == "win32":
import pywintypes
import win32api
elif sys.platform != "darwin":
try:
import dbus
except ImportError:
dbus = None
dbus_session = None
else:
try:
dbus_session = dbus.SessionBus()
except dbus.exceptions.DBusException:
dbus_session = None
# custom
import CGATS
import ICCProfile as ICCP
import colormath
import config
import defaultpaths
import localization as lang
import wexpect
from argyll_cgats import (add_dispcal_options_to_cal, add_options_to_ti3,
extract_fix_copy_cal, ti3_to_ti1, vcgt_to_cal,
verify_cgats)
from argyll_instruments import (get_canonical_instrument_name,
instruments as all_instruments)
from argyll_names import (names as argyll_names, altnames as argyll_altnames,
optional as argyll_optional, viewconds, intents)
from config import (autostart, autostart_home, script_ext, defaults, enc, exe,
exe_ext, fs_enc, getcfg, geticon, get_ccxx_testchart,
get_data_path, get_verified_path, isapp, isexe,
is_ccxx_testchart, profile_ext, pydir, setcfg, writecfg)
from debughelpers import handle_error
if sys.platform not in ("darwin", "win32"):
from defaultpaths import (iccprofiles_home, iccprofiles_display_home,
xdg_config_home, xdg_config_dirs)
from edid import WMIError, get_edid
from log import DummyLogger, LogFile, get_file_logger, log, safe_print
from meta import name as appname, version
from options import ascii, debug, test, test_require_sensor_cal, verbose
from ordereddict import OrderedDict
from trash import trash
from util_io import Files, GzipFileProper, StringIOu as StringIO
from util_list import intlist
if sys.platform == "darwin":
from util_mac import (mac_app_activate, mac_terminal_do_script,
mac_terminal_set_colors, osascript)
elif sys.platform == "win32":
import util_win
import colord
from util_os import getenvu, is_superuser, movefile, putenvu, quote_args, which
from util_str import safe_str, safe_unicode
from wxaddons import wx
from wxwindows import ConfirmDialog, InfoDialog, ProgressDialog, SimpleTerminal
from wxDisplayAdjustmentFrame import DisplayAdjustmentFrame
from wxDisplayUniformityFrame import DisplayUniformityFrame
from wxUntetheredFrame import UntetheredFrame
import wx.lib.delayedresult as delayedresult
INST_CAL_MSGS = ["Do a reflective white calibration",
"Do a transmissive white calibration",
"Do a transmissive dark calibration",
"Place the instrument on its reflective white reference",
"Click the instrument on its reflective white reference",
"Place the instrument in the dark",
"Place cap on the instrument", # i1 Pro
"Place ambient adapter and cap on the instrument",
"Set instrument sensor to calibration position", # ColorMunki
"Place the instrument on its transmissive white source",
"Use the appropriate tramissive blocking",
"Change filter on instrument to"]
USE_WPOPEN = 0
keycodes = {wx.WXK_NUMPAD0: ord("0"),
wx.WXK_NUMPAD1: ord("1"),
wx.WXK_NUMPAD2: ord("2"),
wx.WXK_NUMPAD3: ord("3"),
wx.WXK_NUMPAD4: ord("4"),
wx.WXK_NUMPAD5: ord("5"),
wx.WXK_NUMPAD6: ord("6"),
wx.WXK_NUMPAD7: ord("7"),
wx.WXK_NUMPAD8: ord("8"),
wx.WXK_NUMPAD9: ord("9"),
wx.WXK_NUMPAD_ADD: ord("+"),
wx.WXK_NUMPAD_ENTER: ord("\n"),
wx.WXK_NUMPAD_EQUAL: ord("="),
wx.WXK_NUMPAD_DIVIDE: ord("/"),
wx.WXK_NUMPAD_MULTIPLY: ord("*"),
wx.WXK_NUMPAD_SUBTRACT: ord("-")}
def Property(func):
return property(**func())
def check_argyll_bin(paths=None):
""" Check if the Argyll binaries can be found. """
prev_dir = None
for name in argyll_names:
exe = get_argyll_util(name, paths)
if not exe:
if name in argyll_optional:
continue
return False
cur_dir = os.path.dirname(exe)
if prev_dir:
if cur_dir != prev_dir:
if verbose: safe_print("Warning - the Argyll executables are "
"scattered. They should be in the same "
"directory.")
return False
else:
prev_dir = cur_dir
if verbose >= 3: safe_print("Argyll binary directory:", cur_dir)
if debug: safe_print("[D] check_argyll_bin OK")
if debug >= 2:
if not paths:
paths = getenvu("PATH", os.defpath).split(os.pathsep)
argyll_dir = (getcfg("argyll.dir") or "").rstrip(os.path.sep)
if argyll_dir:
if argyll_dir in paths:
paths.remove(argyll_dir)
paths = [argyll_dir] + paths
safe_print("[D] Searchpath:\n ", "\n ".join(paths))
argyll_dir = os.path.dirname(cur_dir)
if (os.path.isdir(os.path.join(argyll_dir, "ref")) and
not argyll_dir in config.data_dirs):
config.data_dirs.append(argyll_dir)
config.defaults["3dlut.input.profile"] = get_data_path(os.path.join("ref",
"Rec709.icm")) or ""
config.defaults["testchart.reference"] = get_data_path(os.path.join("ref",
"ColorChecker.cie")) or ""
config.defaults["gamap_profile"] = get_data_path(os.path.join("ref", "sRGB.icm")) or ""
return True
def check_create_dir(path):
"""
Try to create a directory and show an error message on failure.
"""
if not os.path.exists(path):
try:
os.makedirs(path)
except Exception, exception:
return Error(lang.getstr("error.dir_creation", path) + "\n\n" +
safe_unicode(exception))
if not os.path.isdir(path):
return Error(lang.getstr("error.dir_notdir", path))
return True
def check_cal_isfile(cal=None, missing_msg=None, notfile_msg=None,
silent=False):
"""
Check if a calibration file exists and show an error message if not.
"""
if not silent:
if not missing_msg:
missing_msg = lang.getstr("error.calibration.file_missing", cal)
if not notfile_msg:
notfile_msg = lang.getstr("error.calibration.file_notfile", cal)
return check_file_isfile(cal, missing_msg, notfile_msg, silent)
def check_profile_isfile(profile_path=None, missing_msg=None,
notfile_msg=None, silent=False):
"""
Check if a profile exists and show an error message if not.
"""
if not silent:
if not missing_msg:
missing_msg = lang.getstr("error.profile.file_missing",
profile_path)
if not notfile_msg:
notfile_msg = lang.getstr("error.profile.file_notfile",
profile_path)
return check_file_isfile(profile_path, missing_msg, notfile_msg, silent)
def check_file_isfile(filename, missing_msg=None, notfile_msg=None,
silent=False):
"""
Check if a file exists and show an error message if not.
"""
if not os.path.exists(filename):
if not silent:
if not missing_msg:
missing_msg = lang.getstr("file.missing", filename)
return Error(missing_msg)
return False
if not os.path.isfile(filename):
if not silent:
if not notfile_msg:
notfile_msg = lang.getstr("file.notfile", filename)
return Error(notfile_msg)
return False
return True
def check_set_argyll_bin():
"""
Check if Argyll binaries can be found, otherwise let the user choose.
"""
if check_argyll_bin():
return True
else:
return set_argyll_bin()
def check_ti3_criteria1(RGB, XYZ, black_XYZ, white_XYZ,
delta_to_sRGB_threshold_E=10,
delta_to_sRGB_threshold_L=10,
delta_to_sRGB_threshold_C=75,
delta_to_sRGB_threshold_H=75,
print_debuginfo=True):
sRGBLab = colormath.RGB2Lab(RGB[0] / 100.0,
RGB[1] / 100.0,
RGB[2] / 100.0,
noadapt=not white_XYZ)
if white_XYZ:
if black_XYZ:
black_Lab = colormath.XYZ2Lab(*colormath.adapt(black_XYZ[0],
black_XYZ[1],
black_XYZ[2],
white_XYZ))
black_C = math.sqrt(math.pow(black_Lab[1], 2) +
math.pow(black_Lab[2], 2))
if black_Lab[0] < 3 and black_C < 3:
# Sanity check: Is this color reasonably dark and achromatic?
# Then do BPC so we can compare better to perfect black sRGB
XYZ = colormath.apply_bpc(XYZ[0], XYZ[1], XYZ[2], black_XYZ,
(0, 0, 0), white_XYZ)
XYZ = colormath.adapt(XYZ[0], XYZ[1], XYZ[2], white_XYZ)
Lab = colormath.XYZ2Lab(*XYZ)
delta_to_sRGB = colormath.delta(*sRGBLab + Lab + (2000, ))
# Depending on how (a)chromatic the sRGB color is, scale the thresholds
# Use math derived from DE2000 formula to get chroma and hue angle
L, a, b = sRGBLab
b_pow = math.pow(b, 2)
C = math.sqrt(math.pow(a, 2) + b_pow)
C_pow = math.pow(C, 7)
G = .5 * (1 - math.sqrt(C_pow / (C_pow + math.pow(25, 7))))
a = (1 + G) * a
C = math.sqrt(math.pow(a, 2) + b_pow)
h = 0 if a == 0 and b == 0 else math.degrees(math.atan2(b, a)) + (0 if b >= 0 else 360.0)
# C and h scaling factors
C_scale = C / 100.0
h_scale = h / 360.0
# RGB hue, saturation and value scaling factors
H, S, V = colormath.RGB2HSV(*[v / 100.0 for v in RGB])
SV_scale = S * V
# Scale the thresholds
delta_to_sRGB_threshold_E += (delta_to_sRGB_threshold_E *
max(C_scale, SV_scale))
delta_to_sRGB_threshold_L += (delta_to_sRGB_threshold_L *
max(C_scale, SV_scale))
# Allow higher chroma errors as luminance of reference decreases
L_scale = max(1 - (1 * C_scale) + (100.0 - L) / 100.0, 1)
delta_to_sRGB_threshold_C = ((delta_to_sRGB_threshold_C *
max(C_scale, SV_scale) + 2) * L_scale)
delta_to_sRGB_threshold_H = ((delta_to_sRGB_threshold_H *
max(C_scale, h_scale, H, SV_scale) + 2) *
L_scale)
criteria1 = (delta_to_sRGB["E"] > delta_to_sRGB_threshold_E and
(abs(delta_to_sRGB["L"]) > delta_to_sRGB_threshold_L or
abs(delta_to_sRGB["C"]) > delta_to_sRGB_threshold_C or
abs(delta_to_sRGB["H"]) > delta_to_sRGB_threshold_H))
# This patch has an unusually high delta 00 to its sRGB equivalent
delta_to_sRGB["E_ok"] = delta_to_sRGB["E"] <= delta_to_sRGB_threshold_E
delta_to_sRGB["L_ok"] = (abs(delta_to_sRGB["L"]) <=
delta_to_sRGB_threshold_L)
delta_to_sRGB["C_ok"] = (abs(delta_to_sRGB["C"]) <=
delta_to_sRGB_threshold_C)
delta_to_sRGB["H_ok"] = (abs(delta_to_sRGB["H"]) <=
delta_to_sRGB_threshold_H)
delta_to_sRGB["ok"] = (delta_to_sRGB["E_ok"] and
delta_to_sRGB["L_ok"] and
delta_to_sRGB["C_ok"] and
delta_to_sRGB["H_ok"])
debuginfo = ("RGB: %6.2f %6.2f %6.2f RGB(sRGB)->Lab(D50): %6.2f %6.2f %6.2f "
"L_scale: %5.3f C: %5.2f C_scale: %5.3f h: %5.2f "
"h_scale: %5.3f H: %5.2f H_scale: %5.3f S: %5.2f "
"V: %5.2f SV_scale: %5.3f Thresholds: E %5.2f L %5.2f "
"C %5.2f H %5.2f XYZ->Lab(D50): %6.2f %6.2f %6.2f delta "
"RGB(sRGB)->Lab(D50) to XYZ->Lab(D50): dE %5.2f dL %5.2f dC "
"%5.2f dH %5.2f" %
(RGB[0], RGB[1], RGB[2], sRGBLab[0], sRGBLab[1], sRGBLab[2],
L_scale, C, C_scale, h, h_scale, H * 360, H, S, V, SV_scale,
delta_to_sRGB_threshold_E, delta_to_sRGB_threshold_L,
delta_to_sRGB_threshold_C, delta_to_sRGB_threshold_H,
Lab[0], Lab[1], Lab[2],
delta_to_sRGB["E"], delta_to_sRGB["L"], delta_to_sRGB["C"],
delta_to_sRGB["H"]))
if print_debuginfo:
safe_print(debuginfo)
return sRGBLab, Lab, delta_to_sRGB, criteria1, debuginfo
def check_ti3_criteria2(prev_Lab, Lab, prev_sRGBLab, sRGBLab,
prev_RGB, RGB, sRGB_delta_E_scale_factor=.5):
delta = colormath.delta(*prev_Lab + Lab + (2000, ))
sRGB_delta = colormath.delta(*prev_sRGBLab + sRGBLab + (2000, ))
sRGB_delta["E"] *= sRGB_delta_E_scale_factor
criteria2 = delta["E"] < sRGB_delta["E"]
# These two patches have different RGB values
# but suspiciously low delta E 76.
if criteria2 and (prev_RGB[0] == prev_RGB[1] == prev_RGB[2] and
RGB[0] == RGB[1] == RGB[2]):
# If RGB gray, check if the Y difference makes sense
criteria2 = ((RGB[0] > prev_RGB[0] and Lab[0] <= prev_Lab[0]) or
(RGB[0] < prev_RGB[0] and Lab[0] >= prev_Lab[0]))
delta["L_ok"] = not criteria2
delta["E_ok"] = True
else:
delta["E_ok"] = not criteria2
delta["L_ok"] = True
return delta, sRGB_delta, criteria2
def check_ti3(ti3, print_debuginfo=True):
""" Check subsequent patches' expected vs real deltaE and collect patches
with different RGB values, but suspiciously low delta E
Used as a means to find misreads.
The expected dE is calculated by converting from a patches RGB values
(assuming sRGB) to Lab and comparing the values.
"""
if not isinstance(ti3, CGATS.CGATS):
ti3 = CGATS.CGATS(ti3)
data = ti3.queryv1("DATA")
datalen = len(data)
black = data.queryi1({"RGB_R": 0, "RGB_G": 0, "RGB_B": 0})
if black:
black = black["XYZ_X"], black["XYZ_Y"], black["XYZ_Z"]
elif print_debuginfo:
safe_print("Warning - no black patch found in CGATS")
white = data.queryi1({"RGB_R": 100, "RGB_G": 100, "RGB_B": 100})
if white:
white = white["XYZ_X"], white["XYZ_Y"], white["XYZ_Z"]
elif print_debuginfo:
safe_print("Warning - no white patch found in CGATS")
suspicious = []
prev = {}
delta = {}
for index, item in data.iteritems():
(sRGBLab,
Lab,
delta_to_sRGB,
criteria1,
debuginfo) = check_ti3_criteria1((item["RGB_R"],
item["RGB_G"],
item["RGB_B"]),
(item["XYZ_X"],
item["XYZ_Y"],
item["XYZ_Z"]),
black, white, print_debuginfo=False)
if (criteria1 or (prev and (max(prev["item"]["RGB_R"], item["RGB_R"]) -
min(prev["item"]["RGB_R"], item["RGB_R"]) >
1.0 / 2.55 or
max(prev["item"]["RGB_G"], item["RGB_G"]) -
min(prev["item"]["RGB_G"], item["RGB_G"]) >
1.0 / 2.55 or
max(prev["item"]["RGB_B"], item["RGB_B"]) -
min(prev["item"]["RGB_B"], item["RGB_B"]) >
1.0 / 2.55))):
if prev:
(delta,
sRGB_delta,
criteria2) = check_ti3_criteria2(prev["Lab"], Lab,
prev["sRGBLab"], sRGBLab,
(prev["item"]["RGB_R"],
prev["item"]["RGB_G"],
prev["item"]["RGB_B"]),
(item["RGB_R"],
item["RGB_G"],
item["RGB_B"]))
else:
criteria2 = False
if criteria1 or criteria2:
if print_debuginfo:
if criteria2:
debuginfo = (("%s dE to previous XYZ->Lab(D50): "
"%5.3f dE_OK: %s L_OK: %s "
"0.5 dE RGB(sRGB)->Lab(D50) to previous "
"RGB(sRGB)->Lab(D50): %5.3f") %
(debuginfo, delta["E"], delta["E_ok"],
delta["L_ok"], sRGB_delta["E"]))
sample_id = "Patch #%%.0%id" % len(str(datalen))
safe_print(sample_id % item.SAMPLE_ID, debuginfo)
suspicious.append((prev["item"] if criteria2 else None,
item, delta if criteria2 else None,
sRGB_delta if criteria2 else None,
prev["delta_to_sRGB"] if criteria2 else None,
delta_to_sRGB))
prev["item"] = item
prev["sRGBLab"] = sRGBLab
prev["Lab"] = Lab
prev["delta_to_sRGB"] = delta_to_sRGB
return suspicious
def get_argyll_util(name, paths=None):
""" Find a single Argyll utility. Return the full path. """
if not paths:
paths = getenvu("PATH", os.defpath).split(os.pathsep)
argyll_dir = (getcfg("argyll.dir") or "").rstrip(os.path.sep)
if argyll_dir:
if argyll_dir in paths:
paths.remove(argyll_dir)
paths = [argyll_dir] + paths
elif verbose >= 4:
safe_print("Info: Searching for", name, "in", os.pathsep.join(paths))
exe = None
for path in paths:
for altname in argyll_altnames.get(name, []):
exe = which(altname + exe_ext, [path])
if exe:
break
if exe:
break
if verbose >= 4:
if exe:
safe_print("Info:", name, "=", exe)
else:
safe_print("Info:", "|".join(argyll_altnames[name]),
"not found in", os.pathsep.join(paths))
return exe
def get_argyll_utilname(name, paths=None):
""" Find a single Argyll utility. Return the basename without extension. """
exe = get_argyll_util(name, paths)
if exe:
exe = os.path.basename(os.path.splitext(exe)[0])
return exe
def get_argyll_version(name, silent=False):
"""
Determine version of a certain Argyll utility.
"""
argyll_version_string = get_argyll_version_string(name, silent)
return parse_argyll_version_string(argyll_version_string)
def get_argyll_version_string(name, silent=False):
argyll_version_string = "0.0.0"
if (silent and check_argyll_bin()) or (not silent and
check_set_argyll_bin()):
cmd = get_argyll_util(name)
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
try:
p = sp.Popen([cmd.encode(fs_enc), "-?"], stdin=sp.PIPE,
stdout=sp.PIPE, stderr=sp.STDOUT,
startupinfo=startupinfo)
except Exception, exception:
safe_print(cmd)
safe_print(exception)
return argyll_version_string
for i, line in enumerate((p.communicate()[0] or "").splitlines()):
if isinstance(line, basestring):
line = line.strip()
if i == 0 and "version" in line.lower():
argyll_version_string = line[line.lower().find("version")+8:]
break
return argyll_version_string
def get_current_profile_path():
profile = None
profile_path = getcfg("calibration.file")
if profile_path:
try:
profile = ICCP.ICCProfile(profile_path)
except Exception, exception:
return
else:
try:
profile = ICCP.get_display_profile(getcfg("display.number") - 1)
except Exception, exception:
return
if profile:
return profile.fileName
def parse_argument_string(args):
""" Parses an argument string and returns a list of arguments. """
return [re.sub('^["\']|["\']$', '', arg) for arg in
re.findall('(?:^|\s+)(-[^\s"\']+|"[^"]+?"|\'[^\']+?\'|[^\s"\']+)', args)]
def parse_argyll_version_string(argyll_version_string):
argyll_version = re.findall("(\d+|[^.\d]+)", argyll_version_string)
for i, v in enumerate(argyll_version):
try:
argyll_version[i] = int(v)
except ValueError:
pass
return argyll_version
def get_options_from_args(dispcal_args=None, colprof_args=None):
"""
Extract options used for dispcal and colprof from argument strings.
"""
re_options_dispcal = [
"[moupHVF]",
"d(?:\d+(?:,\d+)?|madvr|web)",
"[cv]\d+",
"q(?:%s)" % "|".join(config.valid_values["calibration.quality"]),
"y(?:%s)" % "|".join(filter(None, config.valid_values["measurement_mode"])),
"[tT](?:\d+(?:\.\d+)?)?",
"w\d+(?:\.\d+)?,\d+(?:\.\d+)?",
"[bfakAB]\d+(?:\.\d+)?",
"(?:g(?:240|709|l|s)|[gG]\d+(?:\.\d+)?)",
"[pP]\d+(?:\.\d+)?,\d+(?:\.\d+)?,\d+(?:\.\d+)?",
'X(?:\s*\d+|\s+["\'][^"\']+?["\'])', # Argyll >= 1.3.0 colorimeter correction matrix / Argyll >= 1.3.4 calibration spectral sample
"I[bw]{,2}", # Argyll >= 1.3.0 drift compensation
"YA" # Argyll >= 1.5.0 disable adaptive mode
]
re_options_colprof = [
"q[lmh]",
"b[lmh]", # B2A quality
"a(?:%s)" % "|".join(config.valid_values["profile.type"]),
'[sSMA]\s+["\'][^"\']+?["\']',
"[cd](?:%s)" % "|".join(viewconds),
"[tT](?:%s)" % "|".join(intents)
]
options_dispcal = []
options_colprof = []
if dispcal_args:
options_dispcal = re.findall(" -(" + "|".join(re_options_dispcal) +
")", " " + dispcal_args)
if colprof_args:
options_colprof = re.findall(" -(" + "|".join(re_options_colprof) +
")", " " + colprof_args)
return options_dispcal, options_colprof
def get_options_from_cprt(cprt):
"""
Extract options used for dispcal and colprof from profile copyright.
"""
if not isinstance(cprt, unicode):
if isinstance(cprt, (ICCP.TextDescriptionType,
ICCP.MultiLocalizedUnicodeType)):
cprt = unicode(cprt)
else:
cprt = unicode(cprt, fs_enc, "replace")
dispcal_args = cprt.split(" dispcal ")
colprof_args = None
if len(dispcal_args) > 1:
dispcal_args[1] = dispcal_args[1].split(" colprof ")
if len(dispcal_args[1]) > 1:
colprof_args = dispcal_args[1][1]
dispcal_args = dispcal_args[1][0]
else:
dispcal_args = None
colprof_args = cprt.split(" colprof ")
if len(colprof_args) > 1:
colprof_args = colprof_args[1]
else:
colprof_args = None
return dispcal_args, colprof_args
def get_options_from_cal(cal):
if not isinstance(cal, CGATS.CGATS):
cal = CGATS.CGATS(cal)
if not cal or not "ARGYLL_DISPCAL_ARGS" in cal[0] or \
not cal[0].ARGYLL_DISPCAL_ARGS:
return [], []
dispcal_args = cal[0].ARGYLL_DISPCAL_ARGS[0].decode("UTF-7", "replace")
return get_options_from_args(dispcal_args)
def get_options_from_profile(profile):
""" Try and get options from profile. First, try the 'targ' tag and
look for the special dispcalGUI sections 'ARGYLL_DISPCAL_ARGS' and
'ARGYLL_COLPROF_ARGS'. If either does not exist, fall back to the
copyright tag (dispcalGUI < 0.4.0.2) """
if not isinstance(profile, ICCP.ICCProfile):
profile = ICCP.ICCProfile(profile)
dispcal_args = None
colprof_args = None
if "targ" in profile.tags:
ti3 = CGATS.CGATS(profile.tags.targ)
if len(ti3) > 1 and "ARGYLL_DISPCAL_ARGS" in ti3[1] and \
ti3[1].ARGYLL_DISPCAL_ARGS:
dispcal_args = ti3[1].ARGYLL_DISPCAL_ARGS[0].decode("UTF-7",
"replace")
if "ARGYLL_COLPROF_ARGS" in ti3[0] and \
ti3[0].ARGYLL_COLPROF_ARGS:
colprof_args = ti3[0].ARGYLL_COLPROF_ARGS[0].decode("UTF-7",
"replace")
if not dispcal_args and "cprt" in profile.tags:
dispcal_args = get_options_from_cprt(profile.getCopyright())[0]
if not colprof_args and "cprt" in profile.tags:
colprof_args = get_options_from_cprt(profile.getCopyright())[1]
return get_options_from_args(dispcal_args, colprof_args)
def get_options_from_ti3(ti3):
""" Try and get options from TI3 file by looking for the special
dispcalGUI sections 'ARGYLL_DISPCAL_ARGS' and 'ARGYLL_COLPROF_ARGS'. """
if not isinstance(ti3, CGATS.CGATS):
ti3 = CGATS.CGATS(ti3)
dispcal_args = None
colprof_args = None
if len(ti3) > 1 and "ARGYLL_DISPCAL_ARGS" in ti3[1] and \
ti3[1].ARGYLL_DISPCAL_ARGS:
dispcal_args = ti3[1].ARGYLL_DISPCAL_ARGS[0].decode("UTF-7",
"replace")
if "ARGYLL_COLPROF_ARGS" in ti3[0] and \
ti3[0].ARGYLL_COLPROF_ARGS:
colprof_args = ti3[0].ARGYLL_COLPROF_ARGS[0].decode("UTF-7",
"replace")
return get_options_from_args(dispcal_args, colprof_args)
def get_arg(argmatch, args):
""" Return first found entry beginning with the argmatch string or None """
for arg in args:
if arg.startswith(argmatch):
return arg
def make_argyll_compatible_path(path):
"""
Make the path compatible with the Argyll utilities.
This is currently only effective under Windows to make sure that any
unicode 'division' slashes in the profile name are replaced with
underscores.
"""
make_compat_enc = fs_enc
skip = -1
if re.match(r'\\\\\?\\', path, re.I):
# Don't forget about UNC paths:
# \\?\UNC\Server\Volume\File
# \\?\C:\File
skip = 2
parts = path.split(os.path.sep)
for i, part in enumerate(parts):
if i > skip:
parts[i] = unicode(part.encode(make_compat_enc, "safe_asciize"),
make_compat_enc).replace("/", "_").replace("?",
"_")
return os.path.sep.join(parts)
def printcmdline(cmd, args=None, fn=safe_print, cwd=None):
"""
Pretty-print a command line.
"""
if args is None:
args = []
if cwd is None:
cwd = os.getcwdu()
fn(" " + cmd)
i = 0
lines = []
for item in args:
ispath = False
if item.find(os.path.sep) > -1:
if os.path.dirname(item) == cwd:
item = os.path.basename(item)
ispath = True
item = quote_args([item])[0]
lines.append(item)
i += 1
for line in lines:
fn(textwrap.fill(line, 80, expand_tabs = False,
replace_whitespace = False, initial_indent = " ",
subsequent_indent = " "))
def set_argyll_bin(parent=None):
""" Set the directory containing the Argyll CMS binary executables """
if parent and not parent.IsShownOnScreen():
parent = None # do not center on parent if not visible
defaultPath = os.path.join(*get_verified_path("argyll.dir"))
dlg = wx.DirDialog(parent, lang.getstr("dialog.set_argyll_bin"),
defaultPath=defaultPath, style=wx.DD_DIR_MUST_EXIST)
dlg.Center(wx.BOTH)
result = False
while not result:
result = dlg.ShowModal() == wx.ID_OK
if result:
path = dlg.GetPath().rstrip(os.path.sep)
result = check_argyll_bin([path])
if result:
if verbose >= 3:
safe_print("Setting Argyll binary directory:", path)
setcfg("argyll.dir", path)
writecfg()
break
else:
not_found = []
for name in argyll_names:
if (not get_argyll_util(name, [path]) and
not name in argyll_optional):
not_found.append((" " +
lang.getstr("or") +
" ").join(filter(lambda altname: not "argyll" in altname,
[altname + exe_ext
for altname in
argyll_altnames[name]])))
InfoDialog(parent, msg=path + "\n\n" +
lang.getstr("argyll.dir.invalid",
", ".join(not_found)),
ok=lang.getstr("ok"),
bitmap=geticon(32, "dialog-error"))
else:
break
dlg.Destroy()
return result
def show_result_dialog(result, parent=None, pos=None):
""" Show dialog depending on type of result. Result should be an
exception type. An appropriate visual representation will be chosen
whether result is of exception type 'Info', 'Warning' or other error. """
msg = safe_unicode(result)
if not pos:
pos=(-1, -1)
if isinstance(result, Info):
bitmap = geticon(32, "dialog-information")
elif isinstance(result, Warning):
bitmap = geticon(32, "dialog-warning")
else:
bitmap = geticon(32, "dialog-error")
InfoDialog(parent, pos=pos, msg=msg, ok=lang.getstr("ok"), bitmap=bitmap,
log=not isinstance(result, (UnloggedError, UnloggedInfo,
UnloggedWarning)))
class Error(Exception):
pass
class Info(UserWarning):
pass
class UnloggedError(Error):
pass
class UnloggedInfo(Info):
pass
class UnloggedWarning(UserWarning):
pass
class Warn(UserWarning):
pass
class FilteredStream():
""" Wrap a stream and filter all lines written to it. """
# Discard progress information like ... or *** or %
discard = ""
# If one of the triggers is contained in a line, skip the whole line
triggers = ["Place instrument on test window",
"key to continue",
"key to retry",
"key to take a reading",
"] to read",
"' to set",
"' to report",
"' to toggle",
" or Q to ",
"place on the white calibration reference",
"read failed due to the sensor being in the wrong position",
"Ambient filter should be removed"] + INST_CAL_MSGS
substitutions = {" peqDE ": " DE to previous pass ",
r"\^\[": "", # ESC key on Linux/OSX
"patch ": "Patch ",
re.compile("Point (\\d+ Delta E)", re.I): " point \\1"}
def __init__(self, stream, data_encoding=None, file_encoding=None,
errors="replace", discard=None, linesep_in="\r\n",
linesep_out="\n", substitutions=None,
triggers=None):
self.stream = stream
self.data_encoding = data_encoding
self.file_encoding = file_encoding
self.errors = errors
if discard is not None:
self.discard = discard
self.linesep_in = linesep_in
self.linesep_out = linesep_out
if substitutions is not None:
self.substitutions = substitutions
if triggers is not None:
self.triggers = triggers
def __getattr__(self, name):
return getattr(self.stream, name)
def write(self, data):
""" Write data to stream, stripping all unwanted output.
Incoming lines are expected to be delimited by linesep_in.
"""
if not data:
return
lines = []
for line in data.split(self.linesep_in):
if line and not re.sub(self.discard, "", line):
line = ""
write = True
for trigger in self.triggers:
if trigger.lower() in line.lower():
write = False
break
if write:
if self.data_encoding and not isinstance(line, unicode):
line = line.decode(self.data_encoding, self.errors)
for search, sub in self.substitutions.iteritems():
line = re.sub(search, sub, line)
if self.file_encoding:
line = line.encode(self.file_encoding, self.errors)
lines.append(line)
if lines:
self.stream.write(self.linesep_out.join(lines))
class LineBufferedStream():
""" Buffer lines and only write them to stream if line separator is
detected """
def __init__(self, stream, data_encoding=None, file_encoding=None,
errors="replace", linesep_in="\r\n", linesep_out="\n"):
self.buf = ""
self.data_encoding = data_encoding
self.file_encoding = file_encoding
self.errors = errors
self.linesep_in = linesep_in
self.linesep_out = linesep_out
self.stream = stream
def __del__(self):
self.commit()
def __getattr__(self, name):
return getattr(self.stream, name)
def close(self):
self.commit()
self.stream.close()
def commit(self):
if self.buf:
if self.data_encoding and not isinstance(self.buf, unicode):
self.buf = self.buf.decode(self.data_encoding, self.errors)
if self.file_encoding:
self.buf = self.buf.encode(self.file_encoding, self.errors)
self.stream.write(self.buf)
self.buf = ""
def write(self, data):
data = data.replace(self.linesep_in, "\n")
for char in data:
if char == "\r":
while self.buf and not self.buf.endswith(self.linesep_out):
self.buf = self.buf[:-1]
else:
if char == "\n":
self.buf += self.linesep_out
self.commit()
else:
self.buf += char
class LineCache():
""" When written to it, stores only the last n + 1 lines and
returns only the last n non-empty lines when read. """
def __init__(self, maxlines=1):
self.clear()
self.maxlines = maxlines
def clear(self):
self.cache = [""]
def flush(self):
pass
def read(self, triggers=None):
lines = [""]
for line in self.cache:
read = True
if triggers:
for trigger in triggers:
if trigger.lower() in line.lower():
read = False
break
if read and line:
lines.append(line)
return "\n".join(filter(lambda line: line, lines)[-self.maxlines:])
def write(self, data):
for char in data:
if char == "\r":
self.cache[-1] = ""
elif char == "\n":
self.cache.append("")
else:
self.cache[-1] += char
self.cache = (filter(lambda line: line, self.cache[:-1]) +
self.cache[-1:])[-self.maxlines - 1:]
class WPopen(sp.Popen):
def __init__(self, *args, **kwargs):
sp.Popen.__init__(self, *args, **kwargs)
self._seekpos = 0
self._stdout = kwargs["stdout"]
self.after = None
self.before = None
self.exitstatus = None
self.logfile_read = None
self.match = None
self.maxlen = 80
self.timeout = 30
def isalive(self):
self.exitstatus = self.poll()
return self.exitstatus is None
def expect(self, patterns, timeout=-1):
if not isinstance(patterns, list):
patterns = [patterns]
if timeout == -1:
timeout = self.timeout
if timeout is not None:
end = time() + timeout
while timeout is None or time() < end:
self._stdout.seek(self._seekpos)
buf = self._stdout.read()
self._seekpos += len(buf)
if not buf and not self.isalive():
self.match = wexpect.EOF("End Of File (EOF) in expect() - dead child process")
if wexpect.EOF in patterns:
return self.match
raise self.match
if buf and self.logfile_read:
self.logfile_read.write(buf)
for pattern in patterns:
if isinstance(pattern, basestring) and pattern in buf:
offset = buf.find(pattern)
self.after = buf[offset:]
self.before = buf[:offset]
self.match = buf[offset:offset + len(pattern)]
return self.match
sleep(.01)
if timeout is not None:
self.match = wexpect.TIMEOUT("Timeout exceeded in expect()")
if wexpect.TIMEOUT in patterns:
return self.match
raise self.match
def send(self, s):
self.stdin.write(s)
self._stdout.seek(self._seekpos)
buf = self._stdout.read()
self._seekpos += len(buf)
if buf and self.logfile_read:
self.logfile_read.write(buf)
def terminate(self, force=False):
sp.Popen.terminate(self)
class Worker(object):
def __init__(self, owner=None):
"""
Create and return a new worker instance.
"""
self.owner = owner # owner should be a wxFrame or similar
if sys.platform == "win32":
self.data_encoding = aliases.get(str(windll.kernel32.GetACP()),
"ascii")
else:
self.data_encoding = enc
self.cmdrun = False
self.dispcal_create_fast_matrix_shaper = False
self.dispread_after_dispcal = False
self.finished = True
self.interactive = False
self.lastcmdname = None
self.lastmsg_discard = re.compile("[\\*\\.]+")
self.options_colprof = []
self.options_dispcal = []
self.options_dispread = []
self.options_targen = []
self.recent_discard = re.compile("^\\s*(?:Adjusted )?(Current|[Tt]arget) (?:Brightness|50% Level|white|(?:Near )?[Bb]lack|(?:advertised )?gamma) .+|^Gamma curve .+|^Display adjustment menu:|^Press|^\\d\\).+|^(?:1%|Black|Red|Green|Blue|White)\\s+=.+|^\\s*patch \\d+ of \\d+.*|^\\s*point \\d+.*|^\\s*Added \\d+/\\d+|[\\*\\.]+|\\s*\\d*%?", re.I)
self.subprocess_abort = False
self.sudo_availoptions = None
self.auth_timestamp = 0
self.sessionlogfiles = {}
self.tempdir = None
self.thread_abort = False
self.triggers = []
self.clear_argyll_info()
self.clear_cmd_output()
self._pwdstr = ""
def add_measurement_features(self, args, display=True):
""" Add common options and to dispcal, dispread and spotread arguments """
if display and not get_arg("-d", args):
args += ["-d" + self.get_display()]
if not get_arg("-c", args):
args += ["-c%s" % getcfg("comport.number")]
measurement_mode = getcfg("measurement_mode")
instrument_features = self.get_instrument_features()
if (measurement_mode and (measurement_mode != "p" or
self.get_instrument_name() == "ColorHug") and
not get_arg("-y", args)):
# Always specify -y for colorimeters (won't be read from .cal
# when updating)
# Only ColorHug supports -yp parameter
if self.argyll_version >= [1, 5, 0]:
measurement_mode_map = instrument_features.get("measurement_mode_map",
{})
measurement_mode = measurement_mode_map.get(measurement_mode[0],
measurement_mode)
args += ["-y" + measurement_mode[0]]
if getcfg("measurement_mode.projector") and \
instrument_features.get("projector_mode") and \
self.argyll_version >= [1, 1, 0] and not get_arg("-p", args):
# Projector mode, Argyll >= 1.1.0 Beta
args += ["-p"]
if instrument_features.get("adaptive_mode"):
if getcfg("measurement_mode.adaptive"):
if ((self.argyll_version[0:3] > [1, 1, 0] or
(self.argyll_version[0:3] == [1, 1, 0] and
not "Beta" in self.argyll_version_string and
not "RC1" in self.argyll_version_string and
not "RC2" in self.argyll_version_string)) and
self.argyll_version[0:3] < [1, 5, 0] and
not get_arg("-V", args)):
# Adaptive measurement mode, Argyll >= 1.1.0 RC3
args += ["-V"]
else:
if self.argyll_version[0:3] >= [1, 5, 0]:
# Disable adaptive measurement mode
args += ["-YA"]
if display and not (get_arg("-dweb", args) or get_arg("-dmadvr", args)):
if ((self.argyll_version <= [1, 0, 4] and not get_arg("-p", args)) or
(self.argyll_version > [1, 0, 4] and not get_arg("-P", args))):
args += [("-p" if self.argyll_version <= [1, 0, 4] else "-P") +
getcfg("dimensions.measureframe")]
if getcfg("measure.darken_background") and not get_arg("-F", args):
args += ["-F"]
if getcfg("measurement_mode.highres") and \
instrument_features.get("highres_mode") and not get_arg("-H", args):
args += ["-H"]
if (self.instrument_can_use_ccxx() and
not is_ccxx_testchart() and not get_arg("-X", args)):
# Use colorimeter correction?
ccmx = getcfg("colorimeter_correction_matrix_file").split(":", 1)
if len(ccmx) > 1 and ccmx[1]:
ccmx = ccmx[1]
else:
ccmx = None
if ccmx and (not ccmx.lower().endswith(".ccss") or
self.instrument_supports_ccss()):
result = check_file_isfile(ccmx)
if isinstance(result, Exception):
return result
try:
cgats = CGATS.CGATS(ccmx)
except CGATS.CGATSError, exception:
safe_print("%s:" % ccmx, exception)
instrument = None
else:
instrument = get_canonical_instrument_name(str(cgats.queryv1("INSTRUMENT") or "").replace("eye-one display", "i1 Display"))
if ((instrument and
self.get_instrument_name().lower().replace(" ", "") in
instrument.lower().replace(" ", "")) or
ccmx.lower().endswith(".ccss")):
tempdir = self.create_tempdir()
if isinstance(tempdir, Exception):
return tempdir
ccmxcopy = os.path.join(tempdir,
os.path.basename(ccmx))
if not os.path.isfile(ccmxcopy):
try:
# Copy ccmx to profile dir
shutil.copyfile(ccmx, ccmxcopy)
except Exception, exception:
return Error(lang.getstr("error.copy_failed",
(ccmx, ccmxcopy)) +
"\n\n" +
safe_unicode(exception))
result = check_file_isfile(ccmxcopy)
if isinstance(result, Exception):
return result
args += ["-X"]
args += [os.path.basename(ccmxcopy)]
if (display and (getcfg("drift_compensation.blacklevel") or
getcfg("drift_compensation.whitelevel")) and
self.argyll_version >= [1, 3, 0] and not get_arg("-I", args)):
args += ["-I"]
if getcfg("drift_compensation.blacklevel"):
args[-1] += "b"
if getcfg("drift_compensation.whitelevel"):
args[-1] += "w"
# TTBD/FIXME: Skipping of sensor calibration can't be done in
# emissive mode (see Argyll source spectro/ss.c, around line 40)
if (getcfg("allow_skip_sensor_cal") and
instrument_features.get("skip_sensor_cal") and
self.argyll_version >= [1, 1, 0] and not get_arg("-N", args)):
args += ["-N"]
return True
def instrument_can_use_ccxx(self):
"""
Return boolean whether the instrument in its current measurement mode
can use a CCMX or CCSS colorimeter correction
"""
# Special cases:
# Spectrometer (not needed),
# ColorHug (only sensible in factory or raw measurement mode),
# ColorMunki Smile (only generic LCD CCFL measurement mode),
# Colorimétre HCFR (only raw measurement mode),
# DTP94 (only LCD, refresh and generic measurement modes)
# Spyder 4 (only generic LCD and refresh measurement modes)
return (self.argyll_version >= [1, 3, 0] and
not self.get_instrument_features().get("spectral") and
(self.get_instrument_name() != "ColorHug" or
getcfg("measurement_mode") in ("F", "R")) and
(self.get_instrument_name() != "ColorMunki Smile" or
getcfg("measurement_mode") == "f") and
(self.get_instrument_name() != "Colorimtre HCFR" or # Missing é is NOT a typo
getcfg("measurement_mode") == "R") and
(self.get_instrument_name() != "DTP94" or
getcfg("measurement_mode") in ("l", "c", "g")) and
(self.get_instrument_name() != "Spyder4" or
getcfg("measurement_mode") in ("l", "c")))
@Property
def pwd():
def fget(self):
return self._pwdstr[10:].ljust(int(math.ceil(len(self._pwdstr[10:]) / 4.0) * 4),
"=").decode("base64").decode("UTF-8")
def fset(self, pwd):
self._pwdstr = "/tmp/%s%s" % (md5(getpass.getuser()).hexdigest().encode("base64")[:5],
pwd.encode("UTF-8").encode("base64").rstrip("=\n"))
return locals()
def check_display_conf_oy_compat(self, display_no):
""" Check the screen configuration for oyranos-monitor compatibility
oyranos-monitor works off screen coordinates, so it will not handle
overlapping screens (like separate X screens, which will usually
have the same x, y coordinates)!
So, oyranos-monitor can only be used if:
- The wx.Display count is > 1 which means NOT separate X screens
OR if we use the 1st screen
- The screens don't overlap
"""
oyranos = False
if wx.Display.GetCount() > 1 or display_no == 1:
oyranos = True
for display_rect_1 in self.display_rects:
for display_rect_2 in self.display_rects:
if display_rect_1 is not display_rect_2:
if display_rect_1.Intersects(display_rect_2):
oyranos = False
break
if not oyranos:
break
return oyranos
def check_is_ambient_measuring(self, txt):
if (("ambient light measuring" in txt.lower() or
"Will use emissive mode instead" in txt) and
not getattr(self, "is_ambient_measuring", False)):
self.is_ambient_measuring = True
if (getattr(self, "is_ambient_measuring", False) and
"Place instrument on spot to be measured" in txt):
self.is_ambient_measuring = False
self.do_ambient_measurement()
def do_ambient_measurement(self):
if getattr(self, "subprocess_abort", False) or \
getattr(self, "thread_abort", False):
# If we are aborting, ignore request
return
self.progress_wnd.Pulse(" " * 4)
self.progress_wnd.MakeModal(False)
dlg = ConfirmDialog(self.progress_wnd,
msg=lang.getstr("instrument.measure_ambient"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"))
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
self.progress_wnd.MakeModal(True)
if dlg_result != wx.ID_OK:
self.abort_subprocess()
return False
if self.safe_send(" "):
self.progress_wnd.Pulse(lang.getstr("please_wait"))
def check_instrument_calibration(self, txt):
""" Check if current instrument needs sensor calibration by looking
at Argyll CMS command output """
if not self.instrument_calibration_complete:
if "calibration complete" in txt.lower():
self.instrument_calibration_complete = True
else:
for calmsg in INST_CAL_MSGS:
if calmsg in txt or "calibration failed" in txt.lower():
self.do_instrument_calibration()
break
def check_instrument_place_on_screen(self, txt):
""" Check if instrument should be placed on screen by looking
at Argyll CMS command output """
if "place instrument on test window" in txt.lower():
self.instrument_place_on_screen_msg = True
if (self.instrument_place_on_screen_msg and
"key to continue" in txt.lower()):
self.instrument_place_on_screen_msg = False
if (self.cmdname == get_argyll_utilname("dispcal") and
sys.platform == "darwin"):
# On the Mac dispcal's test window
# hides the cursor and steals focus
start_new_thread(mac_app_activate, (1, appname if isapp
else "Python"))
if (self.instrument_calibration_complete or
((config.get_display_name() == "Web" or
getcfg("measure.darken_background")) and
(not self.dispread_after_dispcal or
self.cmdname == "dispcal"))):
# Show a dialog asking user to place the instrument on the
# screen if the instrument calibration was completed,
# or if we measure a remote ("Web") display,
# or if we use a black background during measurements,
# but in case of the latter two only if dispread is not
# run directly after dispcal
self.instrument_calibration_complete = False
self.instrument_place_on_screen()
else:
if self.subprocess.isalive():
if debug or test:
safe_print('Sending SPACE key')
self.safe_send(" ")
def check_instrument_sensor_position(self, txt):
""" Check instrument sensor position by looking
at Argyll CMS command output """
if "read failed due to the sensor being in the wrong position" in txt.lower():
self.instrument_sensor_position_msg = True
if (self.instrument_sensor_position_msg and
" or q to " in txt.lower()):
self.instrument_sensor_position_msg = False
self.instrument_reposition_sensor()
def check_retry_measurement(self, txt):
if ("key to retry:" in txt and
not "read stopped at user request!"
in self.recent.read() and
("Sample read failed due to misread"
in self.recent.read() or
"Sample read failed due to communication problem"
in self.recent.read()) and
not self.subprocess_abort):
self.retrycount += 1
self.recent.write("\r\n%s: Retrying (%s)..." %
(appname, self.retrycount))
self.safe_send(" ")
def check_spotread_result(self, txt):
""" Check if spotread returned a result """
if (self.cmdname == "spotread" and
getattr(self, "terminal", None) is None and
"Result is XYZ:" in txt):
# Single spotread reading, we are done
wx.CallLater(1000, self.quit_terminate_cmd)
def do_instrument_calibration(self):
""" Ask user to initiate sensor calibration and execute.
Give an option to cancel. """
if getattr(self, "subprocess_abort", False) or \
getattr(self, "thread_abort", False):
# If we are aborting, ignore request
return
self.progress_wnd.Pulse(" " * 4)
self.progress_wnd.MakeModal(False)
if self.get_instrument_name() == "ColorMunki":
lstr ="instrument.calibrate.colormunki"
else:
lstr = "instrument.calibrate"
dlg = ConfirmDialog(self.progress_wnd, msg=lang.getstr(lstr),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"))
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
self.progress_wnd.MakeModal(True)
if dlg_result != wx.ID_OK:
self.abort_subprocess()
return False
self.progress_wnd.Pulse(lang.getstr("please_wait"))
if debug or test:
safe_print('Sending SPACE key')
if self.safe_send(" "):
self.progress_wnd.Pulse(lang.getstr("instrument.calibrating"))
def abort_subprocess(self, confirm=False):
""" Abort the current subprocess """
if confirm and getattr(self, "progress_wnd", None):
if getattr(self.progress_wnd, "dlg", None):
return
pause = (not getattr(self.progress_wnd, "paused", False) and
hasattr(self.progress_wnd, "pause_continue_handler"))
if pause:
self.progress_wnd.pause_continue_handler(True)
self.progress_wnd.MakeModal(False)
dlg = ConfirmDialog(self.progress_wnd,
msg=lang.getstr("dialog.confirm_cancel"),
ok=lang.getstr("yes"),
cancel=lang.getstr("no"),
bitmap=geticon(32, "dialog-warning"))
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
self.progress_wnd.MakeModal(True)
if dlg_result != wx.ID_OK:
self.progress_wnd.Resume()
if pause:
self.progress_wnd.pause_continue_handler()
return
self.subprocess_abort = True
self.thread_abort = True
delayedresult.startWorker(lambda result: None,
self.quit_terminate_cmd)
def instrument_place_on_screen(self):
""" Show a dialog asking user to place the instrument on the screen
and give an option to cancel """
if getattr(self, "subprocess_abort", False) or \
getattr(self, "thread_abort", False):
# If we are aborting, ignore request
return
self.progress_wnd.Pulse(" " * 4)
self.progress_wnd.MakeModal(False)
dlg = ConfirmDialog(self.progress_wnd,
msg=lang.getstr("instrument.place_on_screen"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-information"))
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
self.progress_wnd.MakeModal(True)
if dlg_result != wx.ID_OK:
self.abort_subprocess()
return False
if debug or test:
safe_print('Sending SPACE key')
self.safe_send(" ")
def instrument_reposition_sensor(self):
if getattr(self, "subprocess_abort", False) or \
getattr(self, "thread_abort", False):
# If we are aborting, ignore request
return
self.progress_wnd.Pulse(" " * 4)
self.progress_wnd.MakeModal(False)
dlg = ConfirmDialog(self.progress_wnd,
msg=lang.getstr("instrument.reposition_sensor"),
ok=lang.getstr("ok"),
cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-warning"))
self.progress_wnd.dlg = dlg
dlg_result = dlg.ShowModal()
dlg.Destroy()
if self.finished:
return
self.progress_wnd.MakeModal(True)
if dlg_result != wx.ID_OK:
self.abort_subprocess()
return False
if debug or test:
safe_print('Sending SPACE key')
self.safe_send(" ")
def clear_argyll_info(self):
"""
Clear Argyll CMS version, detected displays and instruments.
"""
self.argyll_bin_dir = None
self.argyll_version = [0, 0, 0]
self.argyll_version_string = "0.0.0"
self._displays = []
self.cmdname = None
self.display_edid = []
self.display_manufacturers = []
self.display_names = []
self.display_rects = []
self.displays = []
self.instruments = []
self.lut_access = []
def clear_cmd_output(self):
"""
Clear any output from the last run command.
"""
self.retcode = -1
self.output = []
self.errors = []
self.recent = FilteredStream(LineCache(maxlines=3), self.data_encoding,
discard=self.recent_discard,
triggers=self.triggers)
self.retrycount = 0
self.lastmsg = FilteredStream(LineCache(), self.data_encoding,
discard=self.lastmsg_discard,
triggers=self.triggers)
self.send_buffer = None
if not hasattr(self, "logger") or (self.interactive and self.owner and
isinstance(self.logger,
DummyLogger)):
# Log interaction with Argyll tools that run interactively
if self.interactive and self.owner:
if self.owner.Name != "mainframe":
name = "interact.%s" % self.owner.Name
else:
name = "interact"
self.logger = get_file_logger(name)
else:
self.logger = DummyLogger()
if self.interactive:
self.logger.info("-" * 80)
self.sessionlogfile = None
def create_3dlut(self, profile_in, path, profile_abst=None, profile_out=None,
apply_cal=True, intent="r", format="3dl",
size=17, input_bits=10, output_bits=12, maxval=1.0,
input_encoding="n", output_encoding="n",
bt1886_gamma=None, bt1886_gamma_type="b",
save_link_icc=True):
""" Create a 3D LUT from one (device link) or two (device) profiles,
optionally incorporating an abstract profile. """
# .cube: http://doc.iridas.com/index.php?title=LUT_Formats
# .3dl: http://www.kodak.com/US/plugins/acrobat/en/motion/products/look/UserGuide.pdf
# http://download.autodesk.com/us/systemdocs/pdf/lustre_color_management_user_guide.pdf
# .spi3d: https://github.com/imageworks/OpenColorIO/blob/master/src/core/FileFormatSpi3D.cpp
# .mga: http://pogle.pandora-int.com/download/manual/lut3d_format.html
for profile in (profile_in, profile_out):
if (profile.profileClass not in ("mntr", "link", "scnr", "spac") or
profile.colorSpace != "RGB"):
raise NotImplementedError(lang.getstr("profile.unsupported",
(profile.profileClass,
profile.colorSpace)))
if profile_in.profileClass == "link":
break
# Setup temp dir
cwd = self.create_tempdir()
if isinstance(cwd, Exception):
raise cwd
result = None
path = os.path.split(path)
path = os.path.join(path[0], make_argyll_compatible_path(path[1]))
filename, ext = os.path.splitext(path)
name = os.path.basename(filename)
if profile_in.profileClass == "link":
link_basename = os.path.basename(profile_in.fileName)
link_filename = os.path.join(cwd, link_basename)
profile_in.write(link_filename)
else:
# Check if files are the same
if profile_in.isSame(profile_out, force_calculation=True):
raise Error(lang.getstr("error.source_dest_same"))
# Prepare building a device link
link_basename = name + profile_ext
link_filename = os.path.join(cwd, link_basename)
profile_in_basename = make_argyll_compatible_path(os.path.basename(profile_in.fileName))
profile_out_basename = make_argyll_compatible_path(os.path.basename(profile_out.fileName))
if profile_in_basename == profile_out_basename:
(profile_out_filename,
profile_out_ext) = os.path.splitext(profile_out_basename)
profile_out_basename = "%s (2)%s" % (profile_out_filename,
profile_out_ext)
profile_in.fileName = os.path.join(cwd, profile_in_basename)
profile_in.write()
profile_out.fileName = os.path.join(cwd, profile_out_basename)
profile_out.write()
profile_out_cal_path = os.path.splitext(profile_out.fileName)[0] + ".cal"
# Apply calibration?
if apply_cal:
# Get the calibration from profile vcgt
if not profile_out.tags.get("vcgt", None):
raise Error(lang.getstr("profile.no_vcgt"))
try:
cgats = vcgt_to_cal(profile_out)
except (CGATS.CGATSInvalidError,
CGATS.CGATSInvalidOperationError, CGATS.CGATSKeyError,
CGATS.CGATSTypeError, CGATS.CGATSValueError), exception:
raise Error(lang.getstr("cal_extraction_failed"))
cgats.write(profile_out_cal_path)
if self.argyll_version < [1, 6]:
# Can't apply the calibration with old collink versions -
# apply the calibration to the 'out' profile prior to
# device linking instead
applycal = get_argyll_util("applycal")
if not applycal:
raise NotImplementedError(lang.getstr("argyll.util.not_found",
"applycal"))
safe_print(lang.getstr("apply_cal"))
result = self.exec_cmd(applycal, ["-v",
profile_out_cal_path,
profile_out_basename,
profile_out.fileName],
capture_output=True,
skip_scripts=True)
if isinstance(result, Exception) and not getcfg("dry_run"):
raise result
elif not result:
raise Error("\n\n".join([lang.getstr("apply_cal.error"),
"\n".join(self.errors)]))
# Now build the device link
collink = get_argyll_util("collink")
if not collink:
raise NotImplementedError(lang.getstr("argyll.util.not_found",
"collink"))
args = ["-v", "-qh", "-G", "-i%s" % intent, "-r65", "-n"]
if profile_abst:
profile_abst.write(os.path.join(cwd, "abstract.icc"))
args += ["-p", "abstract.icc"]
if self.argyll_version >= [1, 6]:
if format == "madVR":
args += ["-3m"]
elif format == "eeColor" and not test:
args += ["-3e"]
args += ["-e%s" % input_encoding]
args += ["-E%s" % output_encoding]
if bt1886_gamma:
args += ["-I%s:%s" % (bt1886_gamma_type, bt1886_gamma)]
if apply_cal:
# Apply the calibration when building our device link
# i.e. use collink -a parameter (apply calibration curves
# to link output and append linear)
args += ["-a", profile_out_cal_path]
if getcfg("extra_args.collink").strip():
args += parse_argument_string(getcfg("extra_args.collink"))
result = self.exec_cmd(collink, args + [profile_in_basename,
profile_out_basename,
link_filename],
capture_output=True, skip_scripts=True)
if (result and not isinstance(result, Exception) and
save_link_icc and
os.path.isfile(link_filename)):
profile_link = ICCP.ICCProfile(link_filename)
profile_link.setDescription(name)
profile_link.setCopyright(getcfg("copyright"))
manufacturer = profile_out.getDeviceManufacturerDescription()
if manufacturer:
profile_link.setDeviceManufacturerDescription(manufacturer)
model = profile_out.getDeviceModelDescription()
if model:
profile_link.setDeviceModelDescription(model)
profile_link.device["manufacturer"] = profile_out.device["manufacturer"]
profile_link.device["model"] = profile_out.device["model"]
if "mmod" in profile_out.tags:
profile_link.tags.mmod = profile_out.tags.mmod
profile_link.calculateID()
profile_link.write(filename + profile_ext)
if self.argyll_version >= [1, 6] and ((format == "eeColor" and
not test) or
format == "madVR"):
# Collink has already written the 3DLUT for us
result2 = self.wrapup(not isinstance(result, UnloggedInfo) and
result, dst_path=path,
ext_filter=[".3dlut", ".cal", ".log",
".txt"])
if not result:
result = UnloggedError(lang.getstr("aborted"))
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(safe_unicode(result) + "\n\n" +
safe_unicode(result2))
else:
result = result2
if not isinstance(result, Exception):
return
if isinstance(result, Exception):
raise result
elif not result:
raise UnloggedError(lang.getstr("aborted"))
# We have to create the 3DLUT ourselves
# Create input RGB values
RGB_in = []
RGB_indexes = []
seen = {}
if format == "eeColor":
# Fixed size
size = 65
step = 1.0 / (size - 1)
RGB_triplet = [0.0, 0.0, 0.0]
RGB_index = [0, 0, 0]
# Set the fastest and slowest changing columns, from right to left
if format in ("3dl", "mga", "spi3d"):
columns = (0, 1, 2)
elif format == "eeColor":
columns = (2, 0, 1)
else:
columns = (2, 1, 0)
for i in xrange(0, size):
# Red
RGB_triplet[columns[0]] = step * i
RGB_index[columns[0]] = i
for j in xrange(0, size):
# Green
RGB_triplet[columns[1]] = step * j
RGB_index[columns[1]] = j
for k in xrange(0, size):
# Blue
RGB_triplet[columns[2]] = step * k
RGB_copy = list(RGB_triplet)
if format == "eeColor":
# eeColor cLUT is fake 65^3 - only 64^3 is usable.
# This affects full range and xvYCC RGB, so un-map
# inputs to cLUT to only use 64^3
if input_encoding == "n":
for l in xrange(3):
RGB_copy[l] = min(RGB_copy[l] * (size - 1.0) /
(size - 2.0), 100.0)
elif input_encoding in ("x", "X"):
for l in xrange(2):
RGB_copy[1 + l] = min((RGB_copy[1 + l] *
(size - 1.0) -
1.0) / (size - 3.0), 100.0)
RGB_index[columns[2]] = k
RGB_in.append(RGB_copy)
RGB_indexes.append(list(RGB_index))
# Convert RGB triplets to list of strings
for i, RGB_triplet in enumerate(RGB_in):
RGB_in[i] = " ".join(str(n) for n in RGB_triplet)
if debug:
safe_print(len(RGB_in), "RGB triplets")
safe_print("\n".join(RGB_in))
# Setup icclu
# (Using icclu instead of xicclu because xicclu in versions
# prior to Argyll CMS 1.6.0 could not deal with devicelink profiles)
icclu = get_argyll_util("icclu").encode(fs_enc)
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
# Lookup RGB -> XYZ values through devicelink profile using icclu
stderr = tempfile.SpooledTemporaryFile()
p = sp.Popen([icclu, "-v0", link_basename],
stdin=sp.PIPE, stdout=sp.PIPE, stderr=stderr,
cwd=cwd.encode(fs_enc), startupinfo=startupinfo)
self.subprocess = p
if p.poll() not in (0, None):
stderr.seek(0)
result = Error(stderr.read().strip())
try:
odata = p.communicate("\n".join(RGB_in))[0].splitlines()
except IOError:
stderr.seek(0)
result = Error(stderr.read().strip())
if p.wait() != 0:
result = IOError(''.join(odata))
stderr.close()
# Remove temporary files, move .cal and .log files
result2 = self.wrapup(dst_path=path, ext_filter=[".cal", ".log"])
if isinstance(result, Exception):
raise result
# Convert icclu output to RGB triplets
RGB_out = []
for line in odata:
RGB_out.append([float(n) for n in line.strip().split()])
if debug:
safe_print(len(odata), "RGB triplets")
safe_print("\n".join(odata))
lut = [["# Created with %s %s" % (appname, version)]]
valsep = " "
linesep = "\n"
if format == "3dl":
if maxval is None:
maxval = 1023
if output_bits is None:
output_bits = math.log(maxval + 1) / math.log(2)
if input_bits is None:
input_bits = output_bits
maxval = math.pow(2, output_bits) - 1
pad = len(str(maxval))
lut.append(["# INPUT RANGE: %i" % input_bits])
lut.append(["# OUTPUT RANGE: %i" % output_bits])
lut.append([])
for i in xrange(0, size):
lut[-1] += ["%i" % int(round(i * step * (math.pow(2, input_bits) - 1)))]
for RGB_triplet in RGB_out:
lut.append([])
for component in (0, 1, 2):
lut[-1] += [("%i" % int(round(RGB_triplet[component] * maxval))).rjust(pad, " ")]
elif format == "cube":
if maxval is None:
maxval = 1.0
lut.append(["LUT_3D_SIZE %i" % size])
lut.append(["DOMAIN_MIN 0.0 0.0 0.0"])
fp_offset = str(maxval).find(".")
domain_max = "DOMAIN_MAX %s %s %s" % (("%%.%if" % len(str(maxval)[fp_offset + 1:]), ) * 3)
lut.append([domain_max % ((maxval ,) * 3)])
lut.append([])
for RGB_triplet in RGB_out:
lut.append([])
for component in (0, 1, 2):
lut[-1] += ["%.6f" % (RGB_triplet[component] * maxval)]
elif format == "spi3d":
if maxval is None:
maxval = 1.0
lut = [["SPILUT 1.0"]]
lut.append(["3 3"])
lut.append(["%i %i %i" % ((size, ) * 3)])
for i, RGB_triplet in enumerate(RGB_out):
lut.append([str(index) for index in RGB_indexes[i]])
for component in (0, 1, 2):
lut[-1] += ["%.6f" % (RGB_triplet[component] * maxval)]
elif format == "eeColor":
if maxval is None:
maxval = 1.0
lut = []
for i, RGB_triplet in enumerate(RGB_out):
lut.append(["%.6f" % (float(component) * maxval) for component in RGB_in[i].split()])
for component in (0, 1, 2):
lut[-1] += ["%.6f" % (RGB_triplet[component] * maxval)]
linesep = "\r\n"
elif format == "mga":
lut = [["#HEADER"],
["#filename: %s" % os.path.basename(path)],
["#type: 3D cube file"],
["#format: 1.00"],
["#created: %s" % strftime("%d %B %Y")],
["#owner: %s" % getpass.getuser()],
["#title: %s" % os.path.splitext(os.path.basename(path))[0]],
["#END"]]
lut.append([])
lut.append(["channel 3d"])
lut.append(["in %i" % (size ** 3)])
maxval = 2 ** output_bits - 1
lut.append(["out %i" % (maxval + 1)])
lut.append([""])
lut.append(["format lut"])
lut.append([""])
lut.append(["values\tred\tgreen\tblue"])
for i, RGB_triplet in enumerate(RGB_out):
lut.append(["%i" % i])
for component in (0, 1, 2):
lut[-1] += [("%i" % int(round(RGB_triplet[component] * maxval)))]
valsep = "\t"
lut.append([])
for i, line in enumerate(lut):
lut[i] = valsep.join(line)
result = linesep.join(lut)
# Write 3DLUT
lut_file = open(path, "wb")
lut_file.write(result)
lut_file.close()
if isinstance(result2, Exception):
raise result2
def create_tempdir(self):
""" Create a temporary working directory and return its path. """
if not self.tempdir or not os.path.isdir(self.tempdir):
# we create the tempdir once each calibrating/profiling run
# (deleted by 'wrapup' after each run)
if verbose >= 2:
if not self.tempdir:
msg = "there is none"
else:
msg = "the previous (%s) no longer exists" % self.tempdir
safe_print(appname + ": Creating a new temporary directory "
"because", msg)
try:
self.tempdir = tempfile.mkdtemp(prefix=appname + u"-")
except Exception, exception:
self.tempdir = None
return Error("Error - couldn't create temporary directory: " +
safe_str(exception))
return self.tempdir
def enumerate_displays_and_ports(self, silent=False, check_lut_access=True,
enumerate_ports=True):
"""
Enumerate the available displays and ports.
Also sets Argyll version number, availability of certain options
like black point rate, and checks LUT access for each display.
"""
if (silent and check_argyll_bin()) or (not silent and
check_set_argyll_bin()):
displays = []
lut_access = []
if verbose >= 1 and not silent:
safe_print(lang.getstr("enumerating_displays_and_comports"))
if getattr(wx.GetApp(), "progress_dlg", None) and \
wx.GetApp().progress_dlg.IsShownOnScreen():
wx.GetApp().progress_dlg.Pulse(
lang.getstr("enumerating_displays_and_comports"))
instruments = []
if enumerate_ports:
cmd = get_argyll_util("dispcal")
else:
cmd = get_argyll_util("dispwin")
for instrument in getcfg("instruments").split(os.pathsep):
# Names are canonical from 1.1.4.7 onwards, but we may have
# verbose names from an old configuration
instrument = get_canonical_instrument_name(instrument)
if instrument.strip():
instruments.append(instrument)
argyll_bin_dir = os.path.dirname(cmd)
if (argyll_bin_dir != self.argyll_bin_dir):
self.argyll_bin_dir = argyll_bin_dir
safe_print(self.argyll_bin_dir)
result = self.exec_cmd(cmd, ["-?"], capture_output=True,
skip_scripts=True, silent=True,
log_output=False)
if isinstance(result, Exception):
safe_print(result)
arg = None
defaults["calibration.black_point_rate.enabled"] = 0
n = -1
self.display_rects = []
non_standard_display_args = ("-dweb[:port]", "-dmadvr")
for line in self.output:
if isinstance(line, unicode):
n += 1
line = line.strip()
if n == 0 and "version" in line.lower():
argyll_version_string = line[line.lower().find("version")
+ 8:]
if (argyll_version_string != self.argyll_version_string):
self.set_argyll_version_from_string(argyll_version_string)
safe_print("Argyll CMS " + self.argyll_version_string)
config.defaults["copyright"] = ("No copyright. Created "
"with %s %s and Argyll "
"CMS %s" %
(appname, version,
argyll_version_string))
if self.argyll_version > [1, 0, 4]:
# Rate of blending from neutral to black point.
defaults["calibration.black_point_rate.enabled"] = 1
continue
line = line.split(None, 1)
if len(line) and line[0][0] == "-":
arg = line[0]
if arg == "-A":
# Rate of blending from neutral to black point.
defaults["calibration.black_point_rate.enabled"] = 1
elif arg in non_standard_display_args:
displays.append(arg)
elif len(line) > 1 and line[1][0] == "=":
value = line[1].strip(" ='")
if arg == "-d":
match = re.findall("(.+?),? at (-?\d+), (-?\d+), "
"width (\d+), height (\d+)",
value)
if len(match):
display = "%s @ %s, %s, %sx%s" % match[0]
if " ".join(value.split()[-2:]) == \
"(Primary Display)":
display += u" [PRIMARY]"
displays.append(display)
self.display_rects.append(
wx.Rect(*[int(item) for item in match[0][1:]]))
elif arg == "-c":
if ((re.match("/dev/tty\w?\d+$", value) or
re.match("COM\d+$", value)) and
getcfg("skip_legacy_serial_ports")):
# Skip all legacy serial ports (this means we
# deliberately don't support DTP92 and
# Spectrolino, although they may work when
# used with a serial to USB adaptor)
continue
value = value.split(None, 1)
if len(value) > 1:
value = value[1].strip("()")
else:
value = value[0]
value = get_canonical_instrument_name(value)
instruments.append(value)
if test:
inames = all_instruments.keys()
inames.sort()
for iname in inames:
iname = get_canonical_instrument_name(iname)
if not iname in instruments:
instruments.append(iname)
if verbose >= 1 and not silent: safe_print(lang.getstr("success"))
if getattr(wx.GetApp(), "progress_dlg", None) and \
wx.GetApp().progress_dlg.IsShownOnScreen():
wx.GetApp().progress_dlg.Pulse(
lang.getstr("success"))
if instruments != self.instruments:
self.instruments = instruments
setcfg("instruments", os.pathsep.join(instruments))
if displays != self._displays:
self._displays = list(displays)
displays = filter(lambda display:
not display in non_standard_display_args,
displays)
self.display_edid = []
self.display_manufacturers = []
self.display_names = []
if sys.platform == "win32":
# The ordering will work as long
# as Argyll continues using
# EnumDisplayMonitors
monitors = util_win.get_real_display_devices_info()
for i, display in enumerate(displays):
display_name = displays[i].split("@")[0].strip()
# Make sure we have nice descriptions
desc = []
if sys.platform == "win32" and i < len(monitors):
# Get monitor description using win32api
device = util_win.get_active_display_device(
monitors[i]["Device"])
if device:
desc.append(device.DeviceString.decode(fs_enc,
"replace"))
# Get monitor descriptions from EDID
try:
# Important: display_name must be given for get_edid
# under Mac OS X, but it doesn't hurt to always
# include it
edid = get_edid(i, display_name)
except (TypeError, ValueError, WMIError):
edid = {}
self.display_edid.append(edid)
if edid:
manufacturer = edid.get("manufacturer", "").split()
monitor = edid.get("monitor_name",
edid.get("ascii",
str(edid["product_id"] or
"")))
if monitor and not monitor in "".join(desc):
desc = [monitor]
else:
manufacturer = []
if desc and desc[-1] not in display:
# Only replace the description if it not already
# contains the monitor model
displays[i] = " @".join([" ".join(desc),
display.split("@")[1]])
self.display_manufacturers.append(" ".join(manufacturer))
self.display_names.append(displays[i].split("@")[0].strip())
if self.argyll_version >= [1, 4, 0]:
displays.append("Web @ localhost")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("Web")
if self.argyll_version >= [1, 6, 0]:
displays.append("madVR")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("madVR")
displays.append("Untethered")
self.display_edid.append({})
self.display_manufacturers.append("")
self.display_names.append("Untethered")
self.displays = displays
setcfg("displays", os.pathsep.join(displays))
# Filter out Untethered
displays = displays[:-1]
if self.argyll_version >= [1, 6, 0]:
# Filter out madVR
displays = displays[:-1]
if self.argyll_version >= [1, 4, 0]:
# Filter out Web @ localhost
displays = displays[:-1]
if check_lut_access:
dispwin = get_argyll_util("dispwin")
for i, disp in enumerate(displays):
if verbose >= 1 and not silent:
safe_print(lang.getstr("checking_lut_access", (i + 1)))
if getattr(wx.GetApp(), "progress_dlg", None) and \
wx.GetApp().progress_dlg.IsShownOnScreen():
wx.GetApp().progress_dlg.Pulse(
lang.getstr("checking_lut_access", (i + 1)))
test_cal = get_data_path("test.cal")
if not test_cal:
safe_print(lang.getstr("file.missing", "test.cal"))
return
# Load test.cal
result = self.exec_cmd(dispwin, ["-d%s" % (i +1), "-c",
test_cal],
capture_output=True,
skip_scripts=True,
silent=True)
if isinstance(result, Exception):
safe_print(result)
# Check if LUT == test.cal
result = self.exec_cmd(dispwin, ["-d%s" % (i +1), "-V",
test_cal],
capture_output=True,
skip_scripts=True,
silent=True)
if isinstance(result, Exception):
safe_print(result)
retcode = -1
for line in self.output:
if line.find("IS loaded") >= 0:
retcode = 0
break
# Reset LUT & load profile cal (if any)
result = self.exec_cmd(dispwin, ["-d%s" % (i + 1), "-c",
self.get_dispwin_display_profile_argument(i)],
capture_output=True,
skip_scripts=True,
silent=True)
if isinstance(result, Exception):
safe_print(result)
lut_access += [retcode == 0]
if verbose >= 1 and not silent:
if retcode == 0:
safe_print(lang.getstr("success"))
else:
safe_print(lang.getstr("failure"))
if getattr(wx.GetApp(), "progress_dlg", None) and \
wx.GetApp().progress_dlg.IsShownOnScreen():
wx.GetApp().progress_dlg.Pulse(
lang.getstr("success" if retcode == 0 else
"failure"))
else:
lut_access += [None] * len(displays)
if self.argyll_version >= [1, 4, 0]:
# Web @ localhost
lut_access.append(False)
if self.argyll_version >= [1, 6, 0]:
# madVR
lut_access.append(True)
# Untethered
lut_access.append(False)
self.lut_access = lut_access
elif silent or not check_argyll_bin():
self.clear_argyll_info()
def exec_cmd(self, cmd, args=[], capture_output=False,
display_output=False, low_contrast=True, skip_scripts=False,
silent=False, parent=None, asroot=False, log_output=True,
title=appname, shell=False, working_dir=None, dry_run=False):
"""
Execute a command.
cmd is the full path of the command.
args are the arguments, if any.
capture_output (if True) swallows any output from the command and
sets the 'output' and 'errors' properties of the Worker instance.
display_output shows any captured output if the Worker instance's
'owner' window has a 'LogWindow' child called 'infoframe'.
low_contrast (if True) sets low contrast shell colors while the
command is run.
skip_scripts (if True) skips the creation of shell scripts that allow
re-running the command which are created by default.
silent (if True) skips most output and also most error dialogs
(except unexpected failures)
parent sets the parent window for any message dialogs.
asroot (if True) on Linux runs the command using sudo.
log_output (if True) logs any output if capture_output is also set.
title = Title for sudo dialog
working_dir = Working directory. If None, will be determined from
absulte path of last argument and last argument will be set to only
the basename. If False, no working dir will be used and file arguments
not changed.
"""
progress_dlg = getattr(self, "progress_wnd",
getattr(wx.GetApp(), "progress_dlg", None))
if parent is None:
if progress_dlg and progress_dlg.IsShownOnScreen():
parent = progress_dlg
else:
parent = self.owner
if not capture_output:
capture_output = not sys.stdout.isatty()
self.clear_cmd_output()
if None in [cmd, args]:
if verbose >= 1 and not silent:
safe_print(lang.getstr("aborted"))
return False
cmdname = os.path.splitext(os.path.basename(cmd))[0]
self.cmdname = cmdname
if cmdname == get_argyll_utilname("dispwin"):
if "-Sl" in args or "-Sn" in args or (sys.platform == "darwin" and
not "-I" in args and
mac_ver()[0] >= '10.6'):
# Mac OS X 10.6 and up needs root privileges if loading/clearing
# calibration
# In all other cases, root is only required if installing a
# profile to a system location
asroot = True
working_basename = None
if args and args[-1].find(os.path.sep) > -1:
working_basename = os.path.basename(args[-1])
if cmdname not in (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("colprof"),
get_argyll_utilname("targen"),
get_argyll_utilname("txt2ti3")):
# Last arg is with extension
working_basename = os.path.splitext(working_basename)[0]
if working_dir is None:
working_dir = os.path.dirname(args[-1])
if working_dir is None:
working_dir = self.tempdir
if working_dir and not os.path.isdir(working_dir):
working_dir = None
if (working_basename and working_dir == self.tempdir and not silent
and log_output and not getcfg("dry_run")):
self.sessionlogfile = LogFile(working_basename, working_dir)
self.sessionlogfiles[working_basename] = self.sessionlogfile
if verbose >= 1 or not silent:
if not silent or verbose >= 3:
self.log("-" * 80)
if self.sessionlogfile:
safe_print("Session log: %s" % working_basename + ".log")
safe_print("")
if (not silent and (dry_run or getcfg("dry_run")) and
not self.cmdrun):
safe_print(lang.getstr("dry_run"))
safe_print("")
self.cmdrun = True
if working_dir:
self.log(lang.getstr("working_dir"))
indent = " "
for name in working_dir.split(os.path.sep):
self.log(textwrap.fill(name + os.path.sep, 80,
expand_tabs=False,
replace_whitespace=False,
initial_indent=indent,
subsequent_indent=indent))
indent += " "
self.log("")
self.log(lang.getstr("commandline"))
printcmdline(cmd if verbose >= 2 else os.path.basename(cmd),
args, fn=self.log, cwd=working_dir)
self.log("")
if not silent and (dry_run or getcfg("dry_run")):
if not self.lastcmdname or self.lastcmdname == cmdname:
safe_print(lang.getstr("dry_run.end"))
if self.owner and hasattr(self.owner, "infoframe"):
wx.CallAfter(self.owner.infoframe.Show)
return UnloggedInfo(lang.getstr("dry_run.info"))
cmdline = [cmd] + args
if working_dir:
for i, item in enumerate(cmdline):
if i > 0 and (item.find(os.path.sep) > -1 and
os.path.dirname(item) == working_dir):
# Strip the path from all items in the working dir
if sys.platform == "win32" and \
re.search("[^\x20-\x7e]",
os.path.basename(item)) and os.path.exists(item):
# Avoid problems with encoding
item = win32api.GetShortPathName(item)
cmdline[i] = os.path.basename(item)
if (sys.platform == "win32" and
re.search("[^\x20-\x7e]", working_dir) and
os.path.exists(working_dir)):
# Avoid problems with encoding
working_dir = win32api.GetShortPathName(working_dir)
sudo = None
# Run commands through wexpect.spawn instead of subprocess.Popen if
# all of these conditions apply:
# - command is dispcal, dispread or spotread
# - arguments are not empty
# - actual user interaction in a terminal is not needed OR
# we are on Windows and running without a console
measure_cmds = (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread"),
get_argyll_utilname("spotread"))
process_cmds = (get_argyll_utilname("collink"),
get_argyll_utilname("colprof"),
get_argyll_utilname("targen"))
interact = args and not "-?" in args and cmdname in measure_cmds + process_cmds
self.measure_cmd = not "-?" in args and cmdname in measure_cmds
if asroot and ((sys.platform != "win32" and os.geteuid() != 0) or
(sys.platform == "win32" and
sys.getwindowsversion() >= (6, ))):
if sys.platform == "win32":
# Vista and later
pass
else:
sudo = which("sudo")
if sudo:
if not self.pwd:
# Determine available sudo options
if not self.sudo_availoptions:
man = which("man")
if man:
manproc = sp.Popen([man, "sudo"], stdout=sp.PIPE,
stderr=sp.PIPE)
# Strip formatting
stdout = re.sub(".\x08", "", manproc.communicate()[0])
self.sudo_availoptions = {"E": bool(re.search("-E", stdout)),
"l [command]": bool(re.search("-l(?:\[l\])?\s+\[command\]", stdout)),
"n": bool(re.search("-n", stdout))}
else:
self.sudo_availoptions = {"E": False,
"l [command]": False,
"n": False}
if debug:
safe_print("[D] Available sudo options:",
", ".join(filter(lambda option: self.sudo_availoptions[option],
self.sudo_availoptions.keys())))
# Set sudo args based on available options
if self.sudo_availoptions["l [command]"]:
sudo_args = ["-l",
"-n" if self.sudo_availoptions["n"] else "-S",
cmd, "-?"]
else:
sudo_args = ["-l", "-S"]
# Set stdin based on -n option availability
if "-S" in sudo_args:
stdin = tempfile.SpooledTemporaryFile()
stdin.write((self.pwd or "").encode(enc, "replace") + os.linesep)
stdin.seek(0)
else:
stdin = None
sudoproc = sp.Popen([sudo] + sudo_args, stdin=stdin, stdout=sp.PIPE,
stderr=sp.PIPE)
stdout, stderr = sudoproc.communicate()
if stdin and not stdin.closed:
stdin.close()
if not stdout.strip():
# ask for password
dlg = ConfirmDialog(
parent, title=title,
msg=lang.getstr("dialog.enter_password"),
ok=lang.getstr("ok"), cancel=lang.getstr("cancel"),
bitmap=geticon(32, "dialog-question"))
dlg.pwd_txt_ctrl = wx.TextCtrl(dlg, -1, "",
size=(320, -1),
style=wx.TE_PASSWORD |
wx.TE_PROCESS_ENTER)
dlg.pwd_txt_ctrl.Bind(wx.EVT_TEXT_ENTER,
lambda event: dlg.EndModal(wx.ID_OK))
dlg.sizer3.Add(dlg.pwd_txt_ctrl, 1,
flag=wx.TOP | wx.ALIGN_LEFT, border=12)
dlg.ok.SetDefault()
dlg.pwd_txt_ctrl.SetFocus()
dlg.sizer0.SetSizeHints(dlg)
dlg.sizer0.Layout()
sudo_args = ["-l", "-S"]
if self.sudo_availoptions["l [command]"]:
sudo_args += [cmd, "-?"]
while True:
if parent and parent is progress_dlg:
progress_dlg.MakeModal(False)
result = dlg.ShowModal()
if parent and parent is progress_dlg:
progress_dlg.MakeModal(True)
pwd = dlg.pwd_txt_ctrl.GetValue()
if result != wx.ID_OK:
safe_print(lang.getstr("aborted"))
return None
stdin = tempfile.SpooledTemporaryFile()
stdin.write(pwd.encode(enc, "replace") + os.linesep)
stdin.seek(0)
sudoproc = sp.Popen([sudo] + sudo_args, stdin=stdin,
stdout=sp.PIPE, stderr=sp.PIPE)
stdout, stderr = sudoproc.communicate()
if not stdin.closed:
stdin.close()
if stdout.strip():
# password was accepted
self.auth_timestamp = time()
self.pwd = pwd
break
else:
errstr = unicode(stderr, enc, "replace")
if not silent:
safe_print(errstr)
else:
log(errstr)
dlg.message.SetLabel(
lang.getstr("auth.failed") + "\n" +
errstr)
dlg.sizer0.SetSizeHints(dlg)
dlg.sizer0.Layout()
dlg.Destroy()
cmdline.insert(0, sudo)
if (cmdname == get_argyll_utilname("dispwin")
and sys.platform != "darwin"
and self.sudo_availoptions["E"]
and getcfg("sudo.preserve_environment")):
# Preserve environment so $DISPLAY is set
cmdline.insert(1, "-E")
if not interact:
cmdline.insert(1, "-S")
if working_dir and working_basename and not skip_scripts:
try:
cmdfilename = os.path.join(working_dir, working_basename +
"." + cmdname + script_ext)
allfilename = os.path.join(working_dir, working_basename +
".all" + script_ext)
first = not os.path.exists(allfilename)
last = cmdname == get_argyll_utilname("dispwin")
cmdfile = open(cmdfilename, "w")
allfile = open(allfilename, "a")
cmdfiles = Files((cmdfile, allfile))
if first:
context = cmdfiles
else:
context = cmdfile
if sys.platform == "win32":
context.write("@echo off\n")
context.write(('PATH %s;%%PATH%%\n' %
os.path.dirname(cmd)).encode(enc,
"safe_asciize"))
cmdfiles.write('pushd "%~dp0"\n'.encode(enc, "safe_asciize"))
if cmdname in (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread")):
cmdfiles.write("color 07\n")
else:
context.write(('PATH=%s:$PATH\n' %
os.path.dirname(cmd)).encode(enc,
"safe_asciize"))
if sys.platform == "darwin" and config.mac_create_app:
cmdfiles.write('pushd "`dirname '
'\\"$0\\"`/../../.."\n')
else:
cmdfiles.write('pushd "`dirname \\"$0\\"`"\n')
if cmdname in (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread")) and \
sys.platform != "darwin":
cmdfiles.write('echo -e "\\033[40;2;37m" && clear\n')
os.chmod(cmdfilename, 0755)
os.chmod(allfilename, 0755)
cmdfiles.write(u" ".join(quote_args(cmdline)).replace(cmd,
cmdname).encode(enc, "safe_asciize") + "\n")
if sys.platform == "win32":
cmdfiles.write("set exitcode=%errorlevel%\n")
if cmdname in (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread")):
# Reset to default commandline shell colors
cmdfiles.write("color\n")
cmdfiles.write("popd\n")
cmdfiles.write("if not %exitcode%==0 exit /B %exitcode%\n")
else:
cmdfiles.write("exitcode=$?\n")
if cmdname in (get_argyll_utilname("dispcal"),
get_argyll_utilname("dispread")) and \
sys.platform != "darwin":
# reset to default commandline shell colors
cmdfiles.write('echo -e "\\033[0m" && clear\n')
cmdfiles.write("popd\n")
cmdfiles.write("if [ $exitcode -ne 0 ]; "
"then exit $exitcode; fi\n")
cmdfiles.close()
if sys.platform == "darwin":
if config.mac_create_app:
# Could also use .command file directly, but using
# applescript allows giving focus to the terminal
# window automatically after a delay
script = mac_terminal_do_script() + \
mac_terminal_set_colors(do=False) + \
['-e', 'set shellscript to quoted form of '
'(POSIX path of (path to resource '
'"main.command"))', '-e', 'tell app '
'"Terminal"', '-e', 'do script shellscript '
'in first window', '-e', 'delay 3', '-e',
'activate', '-e', 'end tell', '-o']
# Part 1: "cmdfile"
appfilename = os.path.join(working_dir,
working_basename + "." +
cmdname +
".app").encode(fs_enc)
cmdargs = ['osacompile'] + script + [appfilename]
p = sp.Popen(cmdargs, stdin=sp.PIPE, stdout=sp.PIPE,
stderr=sp.PIPE)
p.communicate()
shutil.move(cmdfilename, appfilename +
"/Contents/Resources/main.command")
os.chmod(appfilename +
"/Contents/Resources/main.command", 0755)
# Part 2: "allfile"
appfilename = os.path.join(
working_dir, working_basename + ".all.app")
cmdargs = ['osacompile'] + script + [appfilename]
p = sp.Popen(cmdargs, stdin=sp.PIPE, stdout=sp.PIPE,
stderr=sp.PIPE)
p.communicate()
shutil.copyfile(allfilename, appfilename +
"/Contents/Resources/main.command")
os.chmod(appfilename +
"/Contents/Resources/main.command", 0755)
if last:
os.remove(allfilename)
except Exception, exception:
safe_print("Warning - error during shell script creation:",
safe_unicode(exception))
cmdline = [arg.encode(fs_enc) for arg in cmdline]
working_dir = None if not working_dir else working_dir.encode(fs_enc)
try:
if not self.measure_cmd and self.argyll_version >= [1, 2]:
# Argyll tools will no longer respond to keys
if debug:
safe_print("[D] Setting ARGYLL_NOT_INTERACTIVE 1")
putenvu("ARGYLL_NOT_INTERACTIVE", "1")
elif "ARGYLL_NOT_INTERACTIVE" in os.environ:
del os.environ["ARGYLL_NOT_INTERACTIVE"]
if debug:
safe_print("[D] argyll_version", self.argyll_version)
safe_print("[D] ARGYLL_NOT_INTERACTIVE",
os.environ.get("ARGYLL_NOT_INTERACTIVE"))
if sys.platform not in ("darwin", "win32"):
putenvu("ENABLE_COLORHUG", "1")
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
if not interact:
if silent:
stderr = sp.STDOUT
else:
stderr = tempfile.SpooledTemporaryFile()
if capture_output:
stdout = tempfile.SpooledTemporaryFile()
elif sys.stdout.isatty():
stdout = sys.stdout
else:
stdout = sp.PIPE
if sudo:
stdin = tempfile.SpooledTemporaryFile()
stdin.write((self.pwd or "").encode(enc, "replace") + os.linesep)
stdin.seek(0)
elif sys.stdin.isatty():
stdin = None
else:
stdin = sp.PIPE
else:
kwargs = dict(timeout=5, cwd=working_dir,
env=os.environ)
if sys.platform == "win32":
kwargs["codepage"] = windll.kernel32.GetACP()
stderr = StringIO()
stdout = StringIO()
logfiles = []
if (self.interactive and getattr(self, "terminal", None)):
logfiles.append(FilteredStream(self.terminal,
discard="",
triggers=self.triggers))
if log_output:
linebuffered_logfiles = []
if sys.stdout.isatty():
linebuffered_logfiles.append(safe_print)
else:
linebuffered_logfiles.append(log)
if self.sessionlogfile:
linebuffered_logfiles.append(self.sessionlogfile)
logfiles.append(LineBufferedStream(
FilteredStream(Files(linebuffered_logfiles),
self.data_encoding,
discard="",
linesep_in="\n",
triggers=[])))
logfiles += [stdout, self.recent, self.lastmsg, self]
tries = 1
while tries > 0:
if interact:
if self.argyll_version >= [1, 2] and USE_WPOPEN and \
os.environ.get("ARGYLL_NOT_INTERACTIVE"):
self.subprocess = WPopen(" ".join(cmdline) if shell else
cmdline, stdin=sp.PIPE,
stdout=tempfile.SpooledTemporaryFile(),
stderr=sp.STDOUT,
shell=shell, cwd=working_dir,
startupinfo=startupinfo)
else:
# Minimum Windows version: XP or Server 2003
if (sys.platform == "win32" and
sys.getwindowsversion() < (5, 1)):
return Error(lang.getstr("windows.version.unsupported"))
try:
self.subprocess = wexpect.spawn(cmdline[0],
cmdline[1:],
**kwargs)
except wexpect.ExceptionPexpect, exception:
self.retcode = -1
return Error(safe_unicode(exception))
if debug >= 9 or (test and not "-?" in args):
self.subprocess.interact()
self.subprocess.logfile_read = Files(logfiles)
if self.subprocess.isalive():
if self.measure_cmd:
keyhit_strs = [" or Q to ",
"8\) Exit"]
while self.subprocess.isalive():
self.subprocess.expect(keyhit_strs + ["Current",
r" \d+ of \d+",
wexpect.EOF],
timeout=None)
if self.subprocess.after in (wexpect.EOF, None):
break
if filter(lambda keyhit_str:
re.search(keyhit_str,
self.subprocess.after),
keyhit_strs):
# Wait for the keypress
while not self.send_buffer:
if not self.subprocess.isalive():
break
sleep(.05)
if (self.send_buffer and
self.subprocess.isalive()):
self._safe_send(self.send_buffer)
self.send_buffer = None
else:
self.subprocess.expect(wexpect.EOF,
timeout=None)
if self.subprocess.after not in (wexpect.EOF,
wexpect.TIMEOUT):
self.subprocess.expect(wexpect.EOF, timeout=None)
# We need to call isalive() to set the exitstatus.
# We can't use wait() because it might block in the
# case of a timeout
while self.subprocess.isalive():
sleep(.1)
self.retcode = self.subprocess.exitstatus
else:
try:
self.subprocess = sp.Popen(" ".join(cmdline) if shell else
cmdline, stdin=stdin,
stdout=stdout, stderr=stderr,
shell=shell, cwd=working_dir,
startupinfo=startupinfo)
except Exception, exception:
self.retcode = -1
return Error("\n".join([safe_unicode(v) for v in
(cmd, exception)]))
self.retcode = self.subprocess.wait()
if stdin and not getattr(stdin, "closed", True):
stdin.close()
if self.is_working() and self.subprocess_abort and \
self.retcode == 0:
self.retcode = -1
self.subprocess = None
tries -= 1
if not silent:
stderr.seek(0)
errors = stderr.readlines()
if not capture_output or stderr is not stdout:
stderr.close()
if len(errors):
for line in errors:
if "Instrument Access Failed" in line and \
"-N" in cmdline[:-1]:
cmdline.remove("-N")
tries = 1
break
if line.strip() and \
line.find("User Aborted") < 0 and \
line.find("XRandR 1.2 is faulty - falling back "
"to older extensions") < 0:
self.errors += [line.decode(enc, "replace")]
if tries > 0 and not interact:
stderr = tempfile.SpooledTemporaryFile()
if capture_output or interact:
stdout.seek(0)
self.output = [re.sub("^\.{4,}\s*$", "",
line.decode(enc, "replace"))
for line in stdout.readlines()]
stdout.close()
if len(self.output) and log_output:
if not interact:
logfn = log if silent else safe_print
self.log("".join(self.output).strip(), logfn)
if display_output and self.owner and \
hasattr(self.owner, "infoframe"):
wx.CallAfter(self.owner.infoframe.Show)
if tries > 0 and not interact:
stdout = tempfile.SpooledTemporaryFile()
if not silent and len(self.errors):
errstr = "".join(self.errors).strip()
self.log(errstr)
except Exception, exception:
if debug:
safe_print('[D] working_dir:', working_dir)
errmsg = (" ".join(cmdline).decode(fs_enc) + "\n" +
safe_unicode(traceback.format_exc()))
self.retcode = -1
return Error(errmsg)
if debug and not silent:
safe_print("*** Returncode:", self.retcode)
if self.retcode != 0:
if interact and verbose >= 1 and not silent:
safe_print(lang.getstr("aborted"))
if interact and len(self.output):
for i, line in enumerate(self.output):
if "Calibrate failed with 'User hit Abort Key' (No device error)" in line:
break
if ((": Error" in line and
not "failed with 'User Aborted'" in line and
not "test_crt returned error code 1" in line) or
line.startswith("Failed to") or
("Requested ambient light capability" in line and
len(self.output) == i + 2) or
("Diagnostic:" in line and
(len(self.output) == i + 1 or
self.output[i + 1].startswith("usage:")))):
# "test_crt returned error code 1" == user aborted
if (sys.platform == "win32" and
("config 1 failed (Operation not supported or "
"unimplemented on this platform) (Permissions ?)")
in line):
self.output.insert(i, lang.getstr("argyll.instrument.driver.missing") +
"\n\n" +
lang.getstr("argyll.error.detail") +
" ")
if "Diagnostic:" in line:
errmsg = line
else:
errmsg = "".join(self.output[i:])
startpos = errmsg.find(": Error")
if startpos > -1:
errmsg = errmsg[startpos + 2:]
return UnloggedError(errmsg.strip())
return False
return True
def flush(self):
pass
def _generic_consumer(self, delayedResult, consumer, continue_next, *args,
**kwargs):
# consumer must accept result as first arg
result = None
exception = None
try:
result = delayedResult.get()
except Exception, exception:
result = Error(u"Error - delayedResult.get() failed: " +
safe_unicode(traceback.format_exc()))
if self.progress_start_timer.IsRunning():
self.progress_start_timer.Stop()
self.finished = True
if not continue_next or isinstance(result, Exception) or not result:
self.stop_progress()
self.subprocess_abort = False
self.thread_abort = False
wx.CallAfter(consumer, result, *args, **kwargs)
def get_device_id(self, quirk=True):
""" Get org.freedesktop.ColorManager device key """
if config.get_display_name() in ("Web", "Untethered", "madVR"):
return None
edid = self.display_edid[max(0, min(len(self.displays) - 1,
getcfg("display.number") - 1))]
return colord.device_id_from_edid(edid, quirk=quirk)
def get_display(self):
""" Get the currently configured display number.
Returned is the Argyll CMS dispcal/dispread -d argument
"""
if config.get_display_name() == "Web":
return "web:%i" % getcfg("webserver.portnumber")
if config.get_display_name() == "madVR":
return "madvr"
if config.get_display_name() == "Untethered":
return "0"
display_no = min(len(self.displays), getcfg("display.number")) - 1
display = str(display_no + 1)
if (self.has_separate_lut_access() or
getcfg("use_separate_lut_access")) and (
not getcfg("display_lut.link") or
(display_no > -1 and not self.lut_access[display_no])):
display_lut_no = min(len(self.displays),
getcfg("display_lut.number")) - 1
if display_lut_no > -1 and not self.lut_access[display_lut_no]:
for display_lut_no, disp in enumerate(self.lut_access):
if disp:
break
display += "," + str(display_lut_no + 1)
return display
def get_display_edid(self):
""" Return EDID of currently configured display """
n = getcfg("display.number") - 1
if n >= 0 and n < len(self.display_edid):
return self.display_edid[n]
return {}
def get_display_name(self, prepend_manufacturer=False, prefer_edid=False):
""" Return name of currently configured display """
n = getcfg("display.number") - 1
if n >= 0 and n < len(self.display_names):
display = []
manufacturer = None
display_name = None
if prefer_edid:
edid = self.get_display_edid()
manufacturer = edid.get("manufacturer")
display_name = edid.get("monitor_name",
edid.get("ascii",
str(edid.get("product_id") or
"")))
if not manufacturer:
manufacturer = self.display_manufacturers[n]
if not display_name:
display_name = self.display_names[n]
if manufacturer:
manufacturer = colord.quirk_manufacturer(manufacturer)
if prepend_manufacturer:
if manufacturer.lower() not in display_name.lower():
display.append(manufacturer)
else:
start = display_name.lower().find(manufacturer.lower())
if start > -1:
display_name = (display_name[:start] +
display_name[start + len(manufacturer):]).lstrip()
display.append(display_name)
return " ".join(display)
return ""
def get_display_name_short(self, prepend_manufacturer=False, prefer_edid=False):
""" Return shortened name of configured display (if possible)
If name can't be shortened (e.g. because it's already 10 characters
or less), return full string
"""
display_name = self.get_display_name(prepend_manufacturer, prefer_edid)
if len(display_name) > 10:
maxweight = 0
for part in re.findall('[^\s_]+(?:\s*\d+)?', re.sub("\([^)]+\)", "",
display_name)):
digits = re.search("\d+", part)
if digits:
# Weigh parts with digits higher than those without
chars = re.sub("\d+", "", part)
weight = len(chars) + len(digits.group()) * 5
else:
# Weigh parts with uppercase letters higher than those without
chars = ""
for char in part:
if char.lower() != char:
chars += char
weight = len(chars)
if chars and weight >= maxweight:
# Weigh parts further to the right higher
display_name = part
maxweight = weight
return display_name
def get_dispwin_display_profile_argument(self, display_no=0):
""" Return argument corresponding to the display profile for use
with dispwin.
Will either return '-L' (use current profile) or a filename
"""
arg = "-L"
try:
profile = ICCP.get_display_profile(display_no)
except Exception, exception:
safe_print("Error - couldn't get profile for display %s" %
display_no)
else:
if profile and profile.fileName:
arg = profile.fileName
return arg
def update_display_name_manufacturer(self, ti3, display_name=None,
display_manufacturer=None,
write=True):
""" Update display name and manufacturer in colprof arguments
embedded in 'ARGYLL_COLPROF_ARGS' section in a TI3 file. """
options_colprof = []
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
for option in get_options_from_ti3(ti3)[1]:
if option[0] == "M":
display_name = option.split(None, 1)[-1][1:-1]
elif option[0] == "A":
display_manufacturer = option.split(None, 1)[-1][1:-1]
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
edid = self.display_edid[max(0, min(len(self.displays),
getcfg("display.number") - 1))]
display_name = edid.get("monitor_name",
edid.get("ascii",
str(edid.get("product_id") or "")))
display_manufacturer = edid.get("manufacturer")
if not display_name and not display_manufacturer:
# Note: Do not mix'n'match display name and manufacturer from
# different sources
display_name = self.get_display_name()
if display_name:
options_colprof.append("-M")
options_colprof.append(display_name)
if display_manufacturer:
options_colprof.append("-A")
options_colprof.append(display_manufacturer)
if write:
# Add dispcal and colprof arguments to ti3
ti3 = add_options_to_ti3(ti3, self.options_dispcal, options_colprof)
if ti3:
ti3.write()
return options_colprof
def get_instrument_features(self):
""" Return features of currently configured instrument """
features = all_instruments.get(self.get_instrument_name(), {})
if test_require_sensor_cal:
features["sensor_cal"] = True
features["skip_sensor_cal"] = False
return features
def get_instrument_name(self):
""" Return name of currently configured instrument """
n = getcfg("comport.number") - 1
if n >= 0 and n < len(self.instruments):
return self.instruments[n]
return ""
def has_lut_access(self):
display_no = min(len(self.lut_access), getcfg("display.number")) - 1
return display_no > -1 and self.lut_access[display_no]
def has_separate_lut_access(self):
""" Return True if separate LUT access is possible and needed. """
if self.argyll_version >= [1, 6, 0]:
# filter out Web @ localhost, madVR and Untethered
lut_access = self.lut_access[:-3]
elif self.argyll_version >= [1, 4, 0]:
# filter out Web @ localhost and Untethered
lut_access = self.lut_access[:-2]
else:
# filter out Untethered
lut_access = self.lut_access[:-1]
return (len(self.displays) > 1 and False in lut_access and True in
lut_access)
def import_colorimeter_corrections(self, cmd, args=None):
""" Import colorimeter corrections. cmd can be 'i1d3ccss', 'spyd4en'
or 'oeminst' """
if not args:
args = []
needroot = sys.platform != "win32"
if (is_superuser() or needroot) and not "-Sl" in args:
# If we are root or need root privs anyway, install to local
# system scope
args.insert(0, "-Sl")
return self.exec_cmd(cmd, ["-v"] + args, capture_output=True,
skip_scripts=True, silent=False,
asroot=needroot)
def import_edr(self, args=None):
""" Import X-Rite .edr files """
return self.import_colorimeter_corrections(get_argyll_util("i1d3ccss"),
args)
def import_spyd4cal(self, args=None):
""" Import Spyder4 calibrations to spy4cal.bin """
return self.import_colorimeter_corrections(get_argyll_util("spyd4en"),
args)
def install_profile(self, profile_path, capture_output=True,
skip_scripts=False, silent=False):
""" Install a profile by copying it to an appropriate location and
registering it with the system """
result = True
colord_install = False
gcm_import = False
oy_install = False
argyll_install = self._install_profile_argyll(profile_path,
capture_output,
skip_scripts, silent)
if sys.platform not in ("darwin", "win32") and not getcfg("dry_run"):
device_id = self.get_device_id(quirk=True)
if device_id and colord.Colord:
try:
client = colord.client_connect()
device = colord.device_connect(client, device_id)
except colord.CDError:
device_id = self.get_device_id(quirk=False)
# FIXME: This can block, so should really be run in separate
# thread with progress dialog in 'indeterminate' mode
result = self._install_profile_colord(profile_path, device_id)
colord_install = result
if (not device_id or not colord.Colord or
isinstance(result, Exception) or not result):
gcm_import = bool(which("gcm-import"))
if (isinstance(result, Exception) or not result) and gcm_import:
# Fall back to gcm-import if colord profile install failed
result = gcm_import
if (not isinstance(result, Exception) and result and
which("oyranos-monitor") and
self.check_display_conf_oy_compat(getcfg("display.number"))):
if device_id:
profile_name = re.sub("[- ]", "_", device_id.lower()) + ".icc"
else:
profile_name = None
result = self._install_profile_oy(profile_path, profile_name,
capture_output, skip_scripts,
silent)
oy_install = result
if not isinstance(result, Exception) and result:
if isinstance(argyll_install, Exception) or not argyll_install:
# Fedora's Argyll cannot install profiles using dispwin
# Check if profile installation via colord or oyranos-monitor
# was successful and continue
if not isinstance(colord_install, Exception) and colord_install:
result = colord_install
elif not isinstance(oy_install, Exception) and oy_install:
result = oy_install
else:
result = argyll_install
if not isinstance(result, Exception) and result:
if getcfg("profile.install_scope") == "l":
# We need a system-wide config file to store the path to
# the Argyll binaries
result = config.makecfgdir("system", self)
if result:
result = config.writecfg("system", self)
if not result:
return Error(lang.getstr("error.autostart_system"))
if sys.platform == "win32":
if getcfg("profile.load_on_login"):
result = self._install_profile_loader_win32(silent)
elif sys.platform != "darwin":
if getcfg("profile.load_on_login"):
result = self._install_profile_loader_xdg(silent)
if gcm_import:
self._install_profile_gcm(profile_path)
if not isinstance(result, Exception) and result and not gcm_import:
if verbose >= 1: safe_print(lang.getstr("success"))
if sys.platform == "darwin" and False: # NEVER
# If installing the profile by just copying it to the
# right location, tell user to select it manually
msg = lang.getstr("profile.install.osx_manual_select")
else:
msg = lang.getstr("profile.install.success")
result = Info(msg)
else:
if result is not None and not getcfg("dry_run"):
if verbose >= 1: safe_print(lang.getstr("failure"))
result = Error(lang.getstr("profile.install.error"))
return result
def _install_profile_argyll(self, profile_path, capture_output=False,
skip_scripts=False, silent=False):
""" Install profile using dispwin """
if (sys.platform == "darwin" and False): # NEVER
# Alternate way of 'installing' the profile under OS X by just
# copying it
profiles = os.path.join("Library", "ColorSync", "Profiles")
profile_install_path = os.path.join(profiles,
os.path.basename(profile_path))
network = os.path.join(os.path.sep, "Network", profiles)
if getcfg("profile.install_scope") == "l":
profile_install_path = os.path.join(os.path.sep,
profile_install_path)
elif (getcfg("profile.install_scope") == "n" and
os.path.isdir(network)):
profile_install_path = os.path.join(network,
profile_install_path)
else:
profile_install_path = os.path.join(os.path.expanduser("~"),
profile_install_path)
cmd, args = "cp", ["-f", profile_path, profile_install_path]
result = self.exec_cmd(cmd, args, capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
asroot=getcfg("profile.install_scope") in ("l", "n"),
title=lang.getstr("profile.install"))
if not isinstance(result, Exception) and result:
self.output = ["Installed"]
else:
if (sys.platform == "win32" and
sys.getwindowsversion() >= (6, ) and
not util_win.per_user_profiles_isenabled()):
# Enable per-user profiles under Vista / Windows 7
try:
util_win.enable_per_user_profiles(True,
getcfg("display.number") - 1)
except Exception, exception:
safe_print("util_win.enable_per_user_profiles(True, %s): %s" %
(getcfg("display.number") - 1,
safe_unicode(exception)))
cmd, args = self.prepare_dispwin(None, profile_path, True)
if not isinstance(cmd, Exception):
if "-Sl" in args and (sys.platform != "darwin" or
intlist(mac_ver()[0].split(".")) >= [10, 6]):
# If a 'system' install is requested under Linux,
# Mac OS X >= 10.6 or Windows,
# install in 'user' scope first because a system-wide install
# doesn't also set it as current user profile on those systems
# (on Mac OS X < 10.6, we can use ColorSyncScripting to set it).
# It has the small drawback under Linux and OS X 10.6 that
# it will copy the profile to both the user and system-wide
# locations, though, which is not a problem under Windows as
# they are the same.
args.remove("-Sl")
result = self.exec_cmd(cmd, args, capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
title=lang.getstr("profile.install"))
args.insert(0, "-Sl")
else:
result = True
if not isinstance(result, Exception) and result:
result = self.exec_cmd(cmd, args, capture_output,
low_contrast=False,
skip_scripts=skip_scripts,
silent=silent,
title=lang.getstr("profile.install"))
else:
result = cmd
if not isinstance(result, Exception) and result is not None:
result = False
for line in self.output:
if "Installed" in line:
if (sys.platform == "darwin" and "-Sl" in args and
intlist(mac_ver()[0].split(".")) < [10, 6]):
# The profile has been installed, but we need a little
# help from AppleScript to actually make it the default
# for the current user. Only works under Mac OS < 10.6
n = getcfg("display.number")
path = os.path.join(os.path.sep, "Library",
"ColorSync", "Profiles",
os.path.basename(args[-1]))
applescript = ['tell app "ColorSyncScripting"',
'set displayProfile to POSIX file "%s" as alias' % path,
'set display profile of display %i to displayProfile' % n,
'end tell']
try:
retcode, output, errors = osascript(applescript)
except Exception, exception:
safe_print(exception)
else:
if errors.strip():
safe_print("osascript error: %s" % errors)
else:
result = True
break
elif (sys.platform == "darwin" and False): # NEVER
# After 'installing' a profile under Mac OS X by just
# copying it, show system preferences
applescript = ['tell application "System Preferences"',
'activate',
'set current pane to pane id "com.apple.preference.displays"',
'reveal (first anchor of current pane whose name is "displaysColorTab")',
# This needs access for assistive devices enabled
#'tell application "System Events"',
#'tell process "System Preferences"',
#'select row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "<Display name from EDID here>"',
#'end tell',
#'end tell',
'end tell']
try:
retcode, output, errors = osascript(applescript)
except Exception, exception:
safe_print(exception)
else:
if errors.strip():
safe_print("osascript error: %s" % errors)
else:
result = True
else:
result = True
break
self.wrapup(False)
return result
def _install_profile_colord(self, profile_path, device_id):
""" Install profile using colord """
try:
colord.install_profile(device_id, profile_path)
except Exception, exception:
safe_print(exception)
return exception
return True
def _install_profile_gcm(self, profile_path):
""" Install profile using gcm-import """
# Remove old profile so gcm-import can work
profilename = os.path.basename(profile_path)
for dirname in iccprofiles_home:
profile_install_path = os.path.join(dirname, profilename)
if os.path.isfile(profile_install_path) and \
profile_install_path != profile_path:
try:
trash([profile_install_path])
except Exception, exception:
safe_print(exception)
# Run gcm-import
cmd, args = which("gcm-import"), [profile_path]
args = " ".join('"%s"' % arg for arg in args)
safe_print('%s %s &' % (cmd, args))
sp.call(('%s %s &' % (cmd, args)).encode(fs_enc), shell=True)
def _install_profile_oy(self, profile_path, profile_name=None,
capture_output=False, skip_scripts=False,
silent=False):
""" Install profile using oyranos-monitor """
display = self.displays[max(0, min(len(self.displays) - 1,
getcfg("display.number") - 1))]
x, y = [pos.strip() for pos in display.split(" @")[1].split(",")[0:2]]
if getcfg("profile.install_scope") == "l":
# If system-wide install, copy profile to
# /var/lib/color/icc/devices/display
var_icc = "/var/lib/color/icc/devices/display"
if not profile_name:
profile_name = os.path.basename(profile_path)
profile_install_path = os.path.join(var_icc, profile_name)
result = self.exec_cmd("mkdir",
["-p", os.path.dirname(profile_install_path)],
capture_output=True, low_contrast=False,
skip_scripts=True, silent=True, asroot=True)
if not isinstance(result, Exception) and result:
result = self.exec_cmd("cp", ["-f", profile_path,
profile_install_path],
capture_output=True, low_contrast=False,
skip_scripts=True, silent=True,
asroot=True)
else:
result = True
dirname = None
for dirname in iccprofiles_display_home:
if os.path.isdir(dirname):
# Use the first one that exists
break
else:
dirname = None
if not dirname:
# Create the first one in the list
dirname = iccprofiles_display_home[0]
try:
os.makedirs(dirname)
except Exception, exception:
safe_print(exception)
result = False
if result is not False:
profile_install_path = os.path.join(dirname,
os.path.basename(profile_path))
try:
shutil.copyfile(profile_path,
profile_install_path)
except Exception, exception:
safe_print(exception)
result = False
if not isinstance(result, Exception) and result is not False:
cmd = which("oyranos-monitor")
args = ["-x", x, "-y", y, profile_install_path]
result = self.exec_cmd(cmd, args, capture_output,
low_contrast=False, skip_scripts=skip_scripts,
silent=silent, working_dir=False)
##if getcfg("profile.install_scope") == "l":
##result = self.exec_cmd(cmd, args,
##capture_output,
##low_contrast=False,
##skip_scripts=skip_scripts,
##silent=silent,
##asroot=True,
##working_dir=False)
return result
def _install_profile_loader_win32(self, silent=False):
""" Install profile loader """
if (sys.platform == "win32" and sys.getwindowsversion() >= (6, 1) and
util_win.calibration_management_isenabled()):
self._uninstall_profile_loader_win32()
return True
# Must return either True on success or an Exception object on error
result = True
# Remove outdated (pre-0.5.5.9) profile loaders
display_no = self.get_display()
name = "%s Calibration Loader (Display %s)" % (appname, display_no)
if autostart_home:
loader_v01b = os.path.join(autostart_home,
("dispwin-d%s-c-L" % display_no) +
".lnk")
if os.path.exists(loader_v01b):
try:
# delete v0.1b loader
os.remove(loader_v01b)
except Exception, exception:
safe_print(u"Warning - could not remove old "
u"v0.1b calibration loader '%s': %s"
% tuple(safe_unicode(s) for s in
(loader_v01b, exception)))
loader_v02b = os.path.join(autostart_home,
name + ".lnk")
if os.path.exists(loader_v02b):
try:
# delete v02.b/v0.2.1b loader
os.remove(loader_v02b)
except Exception, exception:
safe_print(u"Warning - could not remove old "
u"v0.2b calibration loader '%s': %s"
% tuple(safe_unicode(s) for s in
(loader_v02b, exception)))
loader_v0558 = os.path.join(autostart_home,
name + ".lnk")
if os.path.exists(loader_v0558):
try:
# delete v0.5.5.8 user loader
os.remove(loader_v0558)
except Exception, exception:
safe_print(u"Warning - could not remove old "
u"v0.2b calibration loader '%s': %s"
% tuple(safe_unicode(s) for s in
(loader_v02b, exception)))
if autostart:
loader_v0558 = os.path.join(autostart,
name + ".lnk")
if os.path.exists(loader_v0558):
try:
# delete v0.5.5.8 system loader
os.remove(loader_v0558)
except Exception, exception:
safe_print(u"Warning - could not remove old "
u"v0.2b calibration loader '%s': %s"
% tuple(safe_unicode(s) for s in
(loader_v02b, exception)))
# Create unified loader
name = appname + " Profile Loader"
if autostart:
autostart_lnkname = os.path.join(autostart,
name + ".lnk")
if autostart_home:
autostart_home_lnkname = os.path.join(autostart_home,
name + ".lnk")
loader_args = []
if os.path.basename(sys.executable) in ("python.exe",
"pythonw.exe"):
cmd = sys.executable
loader_args += [u'"%s"' % get_data_path(os.path.join("scripts",
"dispcalGUI-apply-profiles"))]
else:
cmd = os.path.join(pydir, "dispcalGUI-apply-profiles.exe")
try:
scut = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IShellLink)
scut.SetPath(cmd)
scut.SetWorkingDirectory(pydir)
if isexe:
scut.SetIconLocation(exe, 0)
else:
scut.SetIconLocation(get_data_path(os.path.join("theme",
"icons",
appname +
".ico")), 0)
scut.SetArguments(" ".join(loader_args))
scut.SetShowCmd(win32con.SW_SHOWMINNOACTIVE)
if is_superuser():
if autostart:
try:
scut.QueryInterface(pythoncom.IID_IPersistFile).Save(autostart_lnkname, 0)
except Exception, exception:
if not silent:
result = Warning(lang.getstr("error.autostart_creation",
autostart) + "\n\n" +
safe_unicode(exception))
# Now try user scope
else:
if not silent:
result = Warning(lang.getstr("error.autostart_system"))
if autostart_home:
if (autostart and
os.path.isfile(autostart_lnkname)):
# Remove existing user loader
if os.path.isfile(autostart_home_lnkname):
os.remove(autostart_home_lnkname)
else:
# Only create user loader if no system loader
try:
scut.QueryInterface(
pythoncom.IID_IPersistFile).Save(
autostart_home_lnkname, 0)
except Exception, exception:
if not silent:
result = Warning(lang.getstr("error.autostart_creation",
autostart_home) + "\n\n" +
safe_unicode(exception))
else:
if not silent:
result = Warning(lang.getstr("error.autostart_user"))
except Exception, exception:
if not silent:
result = Warning(lang.getstr("error.autostart_creation",
autostart_home) + "\n\n" +
safe_unicode(exception))
return result
def _uninstall_profile_loader_win32(self):
""" Uninstall profile loader """
name = appname + " Profile Loader"
if autostart and is_superuser():
autostart_lnkname = os.path.join(autostart,
name + ".lnk")
if os.path.exists(autostart_lnkname):
try:
os.remove(autostart_lnkname)
except Exception, exception:
safe_print(autostart_lnkname, exception)
if autostart_home:
autostart_home_lnkname = os.path.join(autostart_home,
name + ".lnk")
if os.path.exists(autostart_home_lnkname):
try:
os.remove(autostart_home_lnkname)
except Exception, exception:
safe_print(autostart_home_lnkname, exception)
return True
def _install_profile_loader_xdg(self, silent=False):
""" Install profile loader """
# See http://standards.freedesktop.org/autostart-spec
# Must return either True on success or an Exception object on error
result = True
# Remove outdated (pre-0.5.5.9) profile loaders
name = "%s-Calibration-Loader-Display-%s" % (appname,
self.get_display())
desktopfile_path = os.path.join(autostart_home,
name + ".desktop")
oy_desktopfile_path = os.path.join(autostart_home,
"oyranos-monitor.desktop")
system_desktopfile_path = os.path.join(
autostart, name + ".desktop")
# Remove old (pre-0.5.5.9) dispwin user loader
if os.path.exists(desktopfile_path):
try:
os.remove(desktopfile_path)
except Exception, exception:
result = Warning(lang.getstr("error.autostart_remove_old",
desktopfile_path))
# Remove old (pre-0.5.5.9) oyranos user loader
if os.path.exists(oy_desktopfile_path):
try:
os.remove(oy_desktopfile_path)
except Exception, exception:
result = Warning(lang.getstr("error.autostart_remove_old",
oy_desktopfile_path))
# Remove old (pre-0.5.5.9) dispwin system loader
if (os.path.exists(system_desktopfile_path) and
(self.exec_cmd("rm", ["-f", system_desktopfile_path],
capture_output=True, low_contrast=False,
skip_scripts=True, silent=False, asroot=True,
title=lang.getstr("autostart_remove_old"))
is not True) and not silent):
result = Warning(lang.getstr("error.autostart_remove_old",
system_desktopfile_path))
# Create unified loader
# Prepend 'z' so our loader hopefully loads after
# possible nvidia-settings entry (which resets gamma table)
name = "z-%s-apply-profiles" % appname
desktopfile_path = os.path.join(autostart_home,
name + ".desktop")
system_desktopfile_path = os.path.join(autostart, name + ".desktop")
if not os.path.exists(system_desktopfile_path) and \
not os.path.exists(desktopfile_path):
try:
# Create user loader, even if we later try to
# move it to the system-wide location so that atleast
# the user loader is present if the move to the system
# dir fails
if not os.path.exists(autostart_home):
os.makedirs(autostart_home)
desktopfile = open(desktopfile_path, "w")
desktopfile.write('[Desktop Entry]\n')
desktopfile.write('Version=1.0\n')
desktopfile.write('Encoding=UTF-8\n')
desktopfile.write('Type=Application\n')
desktopfile.write('Name=%s\n' % (appname +
' Profile Loader').encode("UTF-8"))
desktopfile.write('Comment=%s\n' %
lang.getstr("calibrationloader.description",
lcode="en").encode("UTF-8"))
if lang.getcode() != "en":
desktopfile.write(('Comment[%s]=%s\n' %
(lang.getcode(),
lang.getstr("calibrationloader.description"))).encode("UTF-8"))
desktopfile.write('Icon=%s\n' % appname.encode("UTF-8"))
desktopfile.write('Exec=%s-apply-profiles\n' % appname.encode("UTF-8"))
desktopfile.write('Terminal=false\n')
desktopfile.close()
except Exception, exception:
if not silent:
result = Warning(lang.getstr("error.autostart_creation",
desktopfile_path) + "\n\n" +
safe_unicode(exception))
else:
if getcfg("profile.install_scope") == "l" and autostart:
# Move system-wide loader
if (self.exec_cmd("mkdir",
["-p", autostart],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True) is not True or
self.exec_cmd("mv",
["-f",
desktopfile_path,
system_desktopfile_path],
capture_output=True,
low_contrast=False,
skip_scripts=True,
silent=True,
asroot=True) is not True) and \
not silent:
result = Warning(lang.getstr("error.autostart_creation",
system_desktopfile_path))
return result
def instrument_supports_ccss(self):
""" Return whether instrument supports CCSS files or not """
instrument_name = self.get_instrument_name()
return ("i1 DisplayPro, ColorMunki Display" in instrument_name or
"Spyder4" in instrument_name)
def create_ccxx(self, args=None, working_dir=None):
""" Create CCMX or CCSS """
if not args:
args = []
cmd = get_argyll_util("ccxxmake")
if not "-I" in args:
# Display manufacturer & name
name = self.get_display_name(True)
if name:
args.insert(0, "-I")
args.insert(1, name)
elif not "-T" in args:
# Display technology
args.insert(0, "-T")
displaytech = ["LCD" if getcfg("measurement_mode") == "l" else "CRT"]
if (self.get_instrument_features().get("projector_mode") and
getcfg("measurement_mode.projector")):
displaytech.append("Projector")
args.insert(1, " ".join(displaytech))
return self.exec_cmd(cmd, ["-v"] + args, capture_output=True,
skip_scripts=True, silent=False,
working_dir=working_dir)
def create_gamut_views(self, profile_path):
""" Generate gamut views (VRML files) and show progress in current
progress dialog """
if getcfg("profile.create_gamut_views"):
safe_print("-" * 80)
safe_print(lang.getstr("gamut.view.create"))
self.recent.clear()
self.recent.write(lang.getstr("gamut.view.create"))
sleep(.75) # Allow time for progress window to update
return self.calculate_gamut(profile_path)
else:
return None, None
def create_profile(self, dst_path=None,
skip_scripts=False, display_name=None,
display_manufacturer=None, tags=None):
""" Create an ICC profile and process the generated file """
safe_print(lang.getstr("create_profile"))
if dst_path is None:
dst_path = os.path.join(getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded") +
profile_ext)
cmd, args = self.prepare_colprof(
os.path.basename(os.path.splitext(dst_path)[0]), display_name,
display_manufacturer)
if not isinstance(cmd, Exception):
result = self.exec_cmd(cmd, args, low_contrast=False,
skip_scripts=skip_scripts)
else:
result = cmd
# Get profile max and avg err to be later added to metadata
# Argyll outputs the following:
# Profile check complete, peak err = x.xxxxxx, avg err = x.xxxxxx, RMS = x.xxxxxx
peak = None
avg = None
rms = None
for line in self.output:
if line.startswith("Profile check complete"):
peak = re.search("peak err = (\d+(?:\.\d+))", line)
avg = re.search("avg err = (\d+(?:\.\d+))", line)
rms = re.search("RMS = (\d+(?:\.\d+))", line)
if peak:
peak = peak.groups()[0]
if avg:
avg = avg.groups()[0]
if rms:
rms = rms.groups()[0]
break
if (os.path.isfile(args[-1] + ".ti3.backup") and
os.path.isfile(args[-1] + ".ti3")):
# Restore backed up TI3
os.rename(args[-1] + ".ti3", args[-1] + ".bpc.ti3")
os.rename(args[-1] + ".ti3.backup", args[-1] + ".ti3")
ti3_file = open(args[-1] + ".ti3", "rb")
ti3 = ti3_file.read()
ti3_file.close()
else:
ti3 = None
if os.path.isfile(args[-1] + ".chrm"):
# Get ChromaticityType tag
with open(args[-1] + ".chrm", "rb") as blob:
chrm = ICCP.ChromaticityType(blob.read())
else:
chrm = None
if not isinstance(result, Exception) and result:
(gamut_volume,
gamut_coverage) = self.create_gamut_views(args[-1] + profile_ext)
safe_print("-" * 80)
if not isinstance(result, Exception) and result:
result = self.update_profile(args[-1] + profile_ext, ti3, chrm,
tags, avg, peak, rms, gamut_volume,
gamut_coverage,
quality=getcfg("profile.quality"))
result2 = self.wrapup(not isinstance(result, UnloggedInfo) and result,
dst_path=dst_path)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(safe_unicode(result) + "\n\n" +
safe_unicode(result2))
else:
result = result2
elif not isinstance(result, Exception) and result:
setcfg("last_cal_or_icc_path", dst_path)
setcfg("last_icc_path", dst_path)
return result
def update_profile(self, profile, ti3=None, chrm=None, tags=None,
avg=None, peak=None, rms=None, gamut_volume=None,
gamut_coverage=None, quality=None):
""" Update profile tags and metadata """
if isinstance(profile, basestring):
profile_path = profile
try:
profile = ICCP.ICCProfile(profile_path)
except (IOError, ICCP.ICCProfileInvalidError), exception:
return Error(lang.getstr("profile.invalid") + "\n" + profile_path)
else:
profile_path = profile.fileName
if (profile.profileClass == "mntr" and profile.colorSpace == "RGB" and
not (self.tempdir and profile_path.startswith(self.tempdir))):
setcfg("last_cal_or_icc_path", profile_path)
setcfg("last_icc_path", profile_path)
if ti3:
# Embed original TI3
profile.tags.targ = profile.tags.DevD = profile.tags.CIED = ICCP.TextType(
"text\0\0\0\0" + ti3 + "\0",
"targ")
if chrm:
# Add ChromaticityType tag
profile.tags.chrm = chrm
# Fixup desc tags - ASCII needs to be 7-bit
# also add Unicode strings if different from ASCII
if "desc" in profile.tags and isinstance(profile.tags.desc,
ICCP.TextDescriptionType):
profile.setDescription(profile.getDescription())
if "dmdd" in profile.tags and isinstance(profile.tags.dmdd,
ICCP.TextDescriptionType):
profile.setDeviceModelDescription(
profile.getDeviceModelDescription())
if "dmnd" in profile.tags and isinstance(profile.tags.dmnd,
ICCP.TextDescriptionType):
profile.setDeviceManufacturerDescription(
profile.getDeviceManufacturerDescription())
if tags and tags is not True:
# Add custom tags
for tagname, tag in tags.iteritems():
if tagname == "mmod":
profile.device["manufacturer"] = "\0\0" + tag["manufacturer"][1] + tag["manufacturer"][0]
profile.device["model"] = "\0\0" + tag["model"][0] + tag["model"][1]
profile.tags[tagname] = tag
elif tags is True:
edid = self.get_display_edid()
if edid:
profile.device["manufacturer"] = "\0\0" + edid["edid"][9] + edid["edid"][8]
profile.device["model"] = "\0\0" + edid["edid"][11] + edid["edid"][10]
# Add Apple-specific 'mmod' tag (TODO: need full spec)
mmod = ("mmod" + ("\x00" * 6) + edid["edid"][8:10] +
("\x00" * 2) + edid["edid"][11] + edid["edid"][10] +
("\x00" * 4) + ("\x00" * 20))
profile.tags.mmod = ICCP.ICCProfileTag(mmod, "mmod")
# Add new meta information based on EDID
profile.set_edid_metadata(edid)
elif not "meta" in profile.tags:
# Make sure meta tag exists
profile.tags.meta = ICCP.DictType()
profile.tags.meta.update({"CMF_product": appname,
"CMF_binary": appname,
"CMF_version": version})
# Set license
profile.tags.meta["License"] = getcfg("profile.license")
# Set profile quality
quality = {"v": "very low",
"l": "low",
"m": "medium",
"h": "high"}.get(quality)
if quality:
profile.tags.meta["Quality"] = quality
# Set OPENICC_automatic_generated to "0"
profile.tags.meta["OPENICC_automatic_generated"] = "0"
# Set GCM DATA_source to "calib"
profile.tags.meta["DATA_source"] = "calib"
# Add instrument
profile.tags.meta["MEASUREMENT_device"] = self.get_instrument_name().lower()
spec_prefixes = "CMF_,DATA_,MEASUREMENT_,OPENICC_"
# Add screen brightness if applicable
if sys.platform not in ("darwin", "win32") and dbus_session:
try:
proxy = dbus_session.get_object("org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/Power")
iface = dbus.Interface(proxy,
dbus_interface="org.gnome.SettingsDaemon.Power.Screen")
brightness = iface.GetPercentage()
except dbus.exceptions.DBusException:
pass
else:
profile.tags.meta["SCREEN_brightness"] = str(brightness)
spec_prefixes += ",SCREEN_"
# Set device ID
device_id = self.get_device_id(quirk=True)
if device_id:
profile.tags.meta["MAPPING_device_id"] = device_id
spec_prefixes += ",MAPPING_"
prefixes = (profile.tags.meta.getvalue("prefix", "", None) or spec_prefixes).split(",")
for prefix in spec_prefixes.split(","):
if not prefix in prefixes:
prefixes.append(prefix)
profile.tags.meta["prefix"] = ",".join(prefixes)
if (avg, peak, rms) != (None, ) * 3:
# Make sure meta tag exists
if not "meta" in profile.tags:
profile.tags.meta = ICCP.DictType()
# Update meta prefix
prefixes = (profile.tags.meta.getvalue("prefix", "", None) or "ACCURACY_").split(",")
if not "ACCURACY_" in prefixes:
prefixes.append("ACCURACY_")
profile.tags.meta["prefix"] = ",".join(prefixes)
# Add error info
if avg is not None:
profile.tags.meta["ACCURACY_dE76_avg"] = avg
if peak is not None:
profile.tags.meta["ACCURACY_dE76_max"] = peak
if rms is not None:
profile.tags.meta["ACCURACY_dE76_rms"] = rms
profile.set_gamut_metadata(gamut_volume, gamut_coverage)
# Set default rendering intent
if ((getcfg("gamap_perceptual") and "B2A0" in profile.tags) or
(getcfg("gamap_saturation") and "B2A2" in profile.tags)):
profile.intent = {"p": 0,
"r": 1,
"s": 2,
"a": 3}[getcfg("gamap_default_intent")]
# Calculate profile ID
profile.calculateID()
try:
profile.write()
except Exception, exception:
return exception
return True
def is_working(self):
""" Check if the Worker instance is busy. Return True or False. """
return not getattr(self, "finished", True)
def log(self, msg, fn=safe_print):
fn(msg)
if self.sessionlogfile:
self.sessionlogfile.write(msg + "\n")
def start_measurement(self, consumer, apply_calibration=True,
progress_msg="", resume=False, continue_next=False):
""" Start a measurement and use a progress dialog for progress
information """
self.start(consumer, self.measure,
wkwargs={"apply_calibration": apply_calibration},
progress_msg=progress_msg, resume=resume,
continue_next=continue_next, pauseable=True)
def start_calibration(self, consumer, remove=False, progress_msg="",
continue_next=False):
""" Start a calibration and use a progress dialog for progress
information """
self.start(consumer, self.calibrate, wkwargs={"remove": remove},
progress_msg=progress_msg, continue_next=continue_next,
interactive_frame="adjust", pauseable=True)
def measure(self, apply_calibration=True):
""" Measure the configured testchart """
cmd, args = self.prepare_dispread(apply_calibration)
if not isinstance(cmd, Exception):
if config.get_display_name() == "Untethered":
cmd, args2 = get_argyll_util("spotread"), ["-v", "-e"]
if getcfg("extra_args.spotread").strip():
args2 += parse_argument_string(getcfg("extra_args.spotread"))
result = self.add_measurement_features(args2, False)
if isinstance(result, Exception):
return result
else:
args2 = args
result = self.exec_cmd(cmd, args2)
if not isinstance(result, Exception) and result:
self.update_display_name_manufacturer(args[-1] + ".ti3")
else:
result = cmd
result2 = self.wrapup(not isinstance(result, UnloggedInfo) and result,
isinstance(result, Exception) or not result)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(safe_unicode(result) + "\n\n" +
safe_unicode(result2))
else:
result = result2
return result
def parse(self, txt):
if not txt:
return
self.logger.info("%r" % txt)
self.check_instrument_calibration(txt)
self.check_instrument_place_on_screen(txt)
self.check_instrument_sensor_position(txt)
self.check_retry_measurement(txt)
self.check_is_ambient_measuring(txt)
self.check_spotread_result(txt)
def prepare_colprof(self, profile_name=None, display_name=None,
display_manufacturer=None):
"""
Prepare a colprof commandline.
All options are read from the user configuration.
Profile name and display name can be ovverridden by passing the
corresponding arguments.
"""
profile_save_path = self.create_tempdir()
if not profile_save_path or isinstance(profile_save_path, Exception):
return profile_save_path, None
# Check directory and in/output file(s)
result = check_create_dir(profile_save_path)
if isinstance(result, Exception):
return result, None
if profile_name is None:
profile_name = getcfg("profile.name.expanded")
inoutfile = os.path.join(profile_save_path,
make_argyll_compatible_path(profile_name))
if not os.path.exists(inoutfile + ".ti3"):
return Error(lang.getstr("error.measurement.file_missing",
inoutfile + ".ti3")), None
if not os.path.isfile(inoutfile + ".ti3"):
return Error(lang.getstr("error.measurement.file_notfile",
inoutfile + ".ti3")), None
#
cmd = get_argyll_util("colprof")
args = []
args += ["-v"] # verbose
args += ["-q" + getcfg("profile.quality")]
args += ["-a" + getcfg("profile.type")]
if getcfg("profile.type") in ["l", "x", "X"]:
b2a_q = getcfg("profile.quality.b2a")
if b2a_q:
args += ["-b" + b2a_q]
if getcfg("gamap_saturation"):
gamap = "S"
elif getcfg("gamap_perceptual"):
gamap = "s"
else:
gamap = None
if gamap and getcfg("gamap_profile"):
args += ["-" + gamap]
args += [getcfg("gamap_profile")]
args += ["-t" + getcfg("gamap_perceptual_intent")]
if gamap == "S":
args += ["-T" + getcfg("gamap_saturation_intent")]
if getcfg("gamap_src_viewcond"):
args += ["-c" + getcfg("gamap_src_viewcond")]
if getcfg("gamap_out_viewcond"):
args += ["-d" + getcfg("gamap_out_viewcond")]
args += ["-C"]
args += [getcfg("copyright").encode("ASCII", "asciize")]
if getcfg("extra_args.colprof").strip():
args += parse_argument_string(getcfg("extra_args.colprof"))
options_dispcal = None
if "-d3" in self.options_targen:
# only add display desc and dispcal options if creating RGB profile
options_dispcal = self.options_dispcal
if len(self.displays):
args.extend(
self.update_display_name_manufacturer(inoutfile + ".ti3",
display_name,
display_manufacturer,
write=False))
self.options_colprof = list(args)
args += ["-D"]
args += [profile_name]
args += [inoutfile]
# Add dispcal and colprof arguments to ti3
ti3 = add_options_to_ti3(inoutfile + ".ti3", options_dispcal,
self.options_colprof)
if ti3:
# Prepare ChromaticityType tag
colorants = ti3.get_colorants()
if colorants and not None in colorants:
color_rep = ti3.queryv1("COLOR_REP").split("_")
chrm = ICCP.ChromaticityType()
chrm.type = 0
for colorant in colorants:
if color_rep[1] == "LAB":
XYZ = colormath.Lab2XYZ(colorant["LAB_L"],
colorant["LAB_A"],
colorant["LAB_B"])
else:
XYZ = (colorant["XYZ_X"], colorant["XYZ_Y"],
colorant["XYZ_Z"])
chrm.channels.append(colormath.XYZ2xyY(*XYZ)[:-1])
with open(inoutfile + ".chrm", "wb") as blob:
blob.write(chrm.tagData)
# Black point compensation
ti3[0].add_keyword("USE_BLACK_POINT_COMPENSATION",
"YES" if getcfg("profile.black_point_compensation")
else "NO")
if getcfg("profile.black_point_compensation"):
# Backup TI3
ti3.write(inoutfile + ".ti3.backup")
# Apply black point compensation
ti3[0].apply_bpc()
ti3.write()
return cmd, args
def prepare_dispcal(self, calibrate=True, verify=False, dry_run=False):
"""
Prepare a dispcal commandline.
All options are read from the user configuration.
You can choose if you want to calibrate and/or verify by passing
the corresponding arguments.
"""
cmd = get_argyll_util("dispcal")
args = []
args += ["-v2"] # verbose
if getcfg("argyll.debug"):
args += ["-D6"]
result = self.add_measurement_features(args)
if isinstance(result, Exception):
return result, None
if calibrate:
args += ["-q" + getcfg("calibration.quality")]
profile_save_path = self.create_tempdir()
if not profile_save_path or isinstance(profile_save_path, Exception):
return profile_save_path, None
# Check directory and in/output file(s)
result = check_create_dir(profile_save_path)
if isinstance(result, Exception):
return result, None
inoutfile = os.path.join(profile_save_path,
make_argyll_compatible_path(getcfg("profile.name.expanded")))
if getcfg("profile.update") or \
self.dispcal_create_fast_matrix_shaper:
args += ["-o"]
if getcfg("calibration.update") and not dry_run:
cal = getcfg("calibration.file")
calcopy = os.path.join(inoutfile + ".cal")
filename, ext = os.path.splitext(cal)
ext = ".cal"
cal = filename + ext
if ext.lower() == ".cal":
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
if not os.path.exists(calcopy):
try:
# Copy cal to profile dir
shutil.copyfile(cal, calcopy)
except Exception, exception:
return Error(lang.getstr("error.copy_failed",
(cal, calcopy)) +
"\n\n" +
safe_unicode(exception)), None
result = check_cal_isfile(calcopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
cal = calcopy
else:
rslt = extract_fix_copy_cal(cal, calcopy)
if isinstance(rslt, ICCP.ICCProfileInvalidError):
return Error(lang.getstr("profile.invalid") +
"\n" + cal), None
elif isinstance(rslt, Exception):
return Error(lang.getstr("cal_extraction_failed") +
"\n" + cal + "\n\n" +
unicode(str(rslt), enc, "replace")), None
if not isinstance(rslt, list):
return None, None
if getcfg("profile.update"):
profile_path = os.path.splitext(
getcfg("calibration.file"))[0] + profile_ext
result = check_profile_isfile(profile_path)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
profilecopy = os.path.join(inoutfile + profile_ext)
if not os.path.exists(profilecopy):
try:
# Copy profile to profile dir
shutil.copyfile(profile_path, profilecopy)
except Exception, exception:
return Error(lang.getstr("error.copy_failed",
(profile_path,
profilecopy)) +
"\n\n" + safe_unicode(exception)), None
result = check_profile_isfile(profilecopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
args += ["-u"]
if calibrate or verify:
if calibrate and not \
getcfg("calibration.interactive_display_adjustment"):
# Skip interactive display adjustment
args += ["-m"]
whitepoint_colortemp = getcfg("whitepoint.colortemp", False)
whitepoint_x = getcfg("whitepoint.x", False)
whitepoint_y = getcfg("whitepoint.y", False)
if whitepoint_colortemp or None in (whitepoint_x, whitepoint_y):
whitepoint = getcfg("whitepoint.colortemp.locus")
if whitepoint_colortemp:
whitepoint += str(whitepoint_colortemp)
args += ["-" + whitepoint]
else:
args += ["-w%s,%s" % (whitepoint_x, whitepoint_y)]
luminance = getcfg("calibration.luminance", False)
if luminance:
args += ["-b%s" % luminance]
args += ["-" + getcfg("trc.type") + str(getcfg("trc"))]
args += ["-f%s" % getcfg("calibration.black_output_offset")]
if bool(int(getcfg("calibration.ambient_viewcond_adjust"))):
args += ["-a%s" %
getcfg("calibration.ambient_viewcond_adjust.lux")]
if not getcfg("calibration.black_point_correction.auto"):
args += ["-k%s" % getcfg("calibration.black_point_correction")]
if defaults["calibration.black_point_rate.enabled"] and \
float(getcfg("calibration.black_point_correction")) < 1:
black_point_rate = getcfg("calibration.black_point_rate")
if black_point_rate:
args += ["-A%s" % black_point_rate]
black_luminance = getcfg("calibration.black_luminance", False)
if black_luminance:
args += ["-B%f" % black_luminance]
if verify:
if calibrate and type(verify) == int:
args += ["-e%s" % verify] # Verify final computed curves
elif self.argyll_version >= [1, 6]:
args += ["-z"] # Verify current curves
else:
args += ["-E"] # Verify current curves
if getcfg("extra_args.dispcal").strip():
args += parse_argument_string(getcfg("extra_args.dispcal"))
self.options_dispcal = list(args)
if calibrate:
args += [inoutfile]
return cmd, args
def prepare_dispread(self, apply_calibration=True):
"""
Prepare a dispread commandline.
All options are read from the user configuration.
You can choose if you want to apply the current calibration,
either the previously by dispcal created one by passing in True, by
passing in a valid path to a .cal file, or by passing in None
(current video card gamma table).
"""
self.lastcmdname = get_argyll_utilname("dispread")
profile_save_path = self.create_tempdir()
if not profile_save_path or isinstance(profile_save_path, Exception):
return profile_save_path, None
# Check directory and in/output file(s)
result = check_create_dir(profile_save_path)
if isinstance(result, Exception):
return result, None
inoutfile = os.path.join(profile_save_path,
make_argyll_compatible_path(getcfg("profile.name.expanded")))
if not os.path.exists(inoutfile + ".ti1"):
filename, ext = os.path.splitext(getcfg("testchart.file"))
result = check_file_isfile(filename + ext)
if isinstance(result, Exception):
return result, None
try:
if ext.lower() in (".icc", ".icm"):
try:
profile = ICCP.ICCProfile(filename + ext)
except (IOError, ICCP.ICCProfileInvalidError), exception:
return Error(lang.getstr("error.testchart.read",
getcfg("testchart.file"))), None
ti3 = StringIO(profile.tags.get("CIED", "") or
profile.tags.get("targ", ""))
elif ext.lower() == ".ti1":
shutil.copyfile(filename + ext, inoutfile + ".ti1")
else: # ti3
try:
ti3 = open(filename + ext, "rU")
except Exception, exception:
return Error(lang.getstr("error.testchart.read",
getcfg("testchart.file"))), None
if ext.lower() != ".ti1":
ti3_lines = [line.strip() for line in ti3]
ti3.close()
if not "CTI3" in ti3_lines:
return Error(lang.getstr("error.testchart.invalid",
getcfg("testchart.file"))), None
ti1 = open(inoutfile + ".ti1", "w")
ti1.write(ti3_to_ti1(ti3_lines))
ti1.close()
except Exception, exception:
return Error(lang.getstr("error.testchart.creation_failed",
inoutfile + ".ti1") + "\n\n" +
safe_unicode(exception)), None
if apply_calibration is not False:
if apply_calibration is True:
# Always a .cal file in that case
cal = os.path.join(getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded")) + ".cal"
elif apply_calibration is None:
result = None
if self.argyll_version >= [1, 1, 0]:
cal = inoutfile + ".cal"
cmd, args = (get_argyll_util("dispwin"),
["-d" + self.get_display(), "-s", cal])
result = self.exec_cmd(cmd, args, capture_output=True,
skip_scripts=True, silent=False)
if (isinstance(result, Exception) and
not isinstance(result, UnloggedInfo)):
return result, None
if not result:
return Error(lang.getstr("calibration.load_error")), None
else:
cal = apply_calibration # can be .cal or .icc / .icm
calcopy = inoutfile + ".cal"
filename, ext = os.path.splitext(cal)
if getcfg("dry_run"):
options_dispcal = []
elif ext.lower() == ".cal":
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
# Get dispcal options if present
options_dispcal = get_options_from_cal(cal)[0]
if not os.path.exists(calcopy):
try:
# Copy cal to temp dir
shutil.copyfile(cal, calcopy)
except Exception, exception:
return Error(lang.getstr("error.copy_failed",
(cal, calcopy)) + "\n\n" +
safe_unicode(exception)), None
result = check_cal_isfile(calcopy)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
else:
# .icc / .icm
result = check_profile_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
try:
profile = ICCP.ICCProfile(filename + ext)
except (IOError, ICCP.ICCProfileInvalidError), exception:
profile = None
if profile:
ti3 = StringIO(profile.tags.get("CIED", "") or
profile.tags.get("targ", ""))
# Get dispcal options if present
options_dispcal = get_options_from_profile(profile)[0]
else:
ti3 = StringIO("")
ti3_lines = [line.strip() for line in ti3]
ti3.close()
if not "CTI3" in ti3_lines:
return Error(lang.getstr("error.cal_extraction",
(cal))), None
try:
tmpcal = open(calcopy, "w")
tmpcal.write(extract_cal_from_ti3(ti3_lines))
tmpcal.close()
except Exception, exception:
return Error(lang.getstr("error.cal_extraction", (cal)) +
"\n\n" + safe_unicode(exception)), None
cal = calcopy
if options_dispcal:
self.options_dispcal = ["-" + arg for arg in options_dispcal]
#
# Make sure any measurement options are present
if not self.options_dispcal:
self.prepare_dispcal(dry_run=True)
# Special case -X because it can have a separate filename argument
if "-X" in self.options_dispcal:
index = self.options_dispcal.index("-X")
if (len(self.options_dispcal) > index + 1 and
self.options_dispcal[index + 1][0] != "-"):
self.options_dispcal = (self.options_dispcal[:index] +
self.options_dispcal[index + 2:])
# Strip options we may override (basically all the stuff which can be
# added by add_measurement_features. -X is repeated because it can
# have a number instead of explicit filename argument, e.g. -X1)
dispcal_override_args = ("-F", "-H", "-I", "-P", "-V", "-X", "-d", "-c",
"-p", "-y")
self.options_dispcal = filter(lambda arg: not arg[:2] in dispcal_override_args,
self.options_dispcal)
# Only add the dispcal extra args which may override measurement features
dispcal_extra_args = parse_argument_string(getcfg("extra_args.dispcal"))
for i, arg in enumerate(dispcal_extra_args):
if not arg.startswith("-") and i > 0:
# Assume option to previous arg
arg = dispcal_extra_args[i - 1]
if arg[:2] in dispcal_override_args:
self.options_dispcal += [dispcal_extra_args[i]]
result = self.add_measurement_features(self.options_dispcal)
if isinstance(result, Exception):
return result, None
cmd = get_argyll_util("dispread")
args = []
args += ["-v"] # verbose
if getcfg("argyll.debug"):
args += ["-D6"]
result = self.add_measurement_features(args)
if isinstance(result, Exception):
return result, None
if apply_calibration is not False:
if (self.argyll_version >= [1, 3, 3] and
(not self.has_lut_access() or
not getcfg("calibration.use_video_lut"))):
if config.get_display_name() == "madVR":
# Normally -K will automatically reset the video LUT,
# but when using madVR, we have to do it explicitly
result = self.reset_cal()
if (isinstance(result, Exception) and
not isinstance(result, UnloggedInfo)):
return result, None
args += ["-K"]
else:
args += ["-k"]
args += [cal]
if self.get_instrument_features().get("spectral"):
args += ["-s"]
if getcfg("extra_args.dispread").strip():
args += parse_argument_string(getcfg("extra_args.dispread"))
self.options_dispread = list(args)
if getattr(self, "terminal", None) and isinstance(self.terminal,
UntetheredFrame):
self.set_terminal_cgats(inoutfile + ".ti1")
return cmd, self.options_dispread + [inoutfile]
def prepare_dispwin(self, cal=None, profile_path=None, install=True):
"""
Prepare a dispwin commandline.
All options are read from the user configuration.
If you pass in cal as True, it will try to load the current
display profile's calibration. If cal is a path, it'll use
that instead. If cal is False, it'll clear the current calibration.
If cal is None, it'll try to load the calibration from a profile
specified by profile_path.
"""
cmd = get_argyll_util("dispwin")
args = []
args += ["-v"]
if getcfg("argyll.debug"):
if self.argyll_version >= [1, 3, 1]:
args += ["-D6"]
else:
args += ["-E6"]
args += ["-d" + self.get_display()]
if sys.platform != "darwin" or cal is False:
# Mac OS X 10.7 Lion needs root privileges when clearing
# calibration
args += ["-c"]
if cal is True:
args += [self.get_dispwin_display_profile_argument(
max(0, min(len(self.displays),
getcfg("display.number")) - 1))]
elif cal:
result = check_cal_isfile(cal)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
args += [cal]
else:
if cal is None:
if not profile_path:
profile_save_path = os.path.join(
getcfg("profile.save_path"),
getcfg("profile.name.expanded"))
profile_path = os.path.join(profile_save_path,
getcfg("profile.name.expanded") + profile_ext)
result = check_profile_isfile(profile_path)
if isinstance(result, Exception):
return result, None
if not result:
return None, None
try:
profile = ICCP.ICCProfile(profile_path)
except (IOError, ICCP.ICCProfileInvalidError), exception:
return Error(lang.getstr("profile.invalid") +
"\n" + profile_path), None
if profile.profileClass != "mntr" or \
profile.colorSpace != "RGB":
return Error(lang.getstr("profile.unsupported",
(profile.profileClass,
profile.colorSpace)) +
"\n" + profile_path), None
if install:
if getcfg("profile.install_scope") != "u" and \
(((sys.platform == "darwin" or
(sys.platform != "win32" and
self.argyll_version >= [1, 1, 0])) and
(os.geteuid() == 0 or which("sudo"))) or
(sys.platform == "win32" and
sys.getwindowsversion() >= (6, ) and
self.argyll_version > [1, 1, 1])):
# -S option is broken on Linux with current Argyll
# releases
args += ["-S" + getcfg("profile.install_scope")]
args += ["-I"]
if (sys.platform in ("win32", "darwin") or
fs_enc.upper() not in ("UTF8", "UTF-8")) and \
re.search("[^\x20-\x7e]",
os.path.basename(profile_path)):
# Copy to temp dir and give unique ASCII-only name to
# avoid profile install issues
tmp_dir = self.create_tempdir()
if not tmp_dir or isinstance(tmp_dir, Exception):
return tmp_dir, None
# Check directory and in/output file(s)
result = check_create_dir(tmp_dir)
if isinstance(result, Exception):
return result, None
# profile name: 'display<n>-<hexmd5sum>.icc'
profile_tmp_path = os.path.join(tmp_dir, "display" +
self.get_display() +
"-" +
md5(profile.data).hexdigest() +
profile_ext)
shutil.copyfile(profile_path, profile_tmp_path)
profile_path = profile_tmp_path
args += [profile_path]
return cmd, args
def prepare_targen(self):
"""
Prepare a targen commandline.
All options are read from the user configuration.
"""
path = self.create_tempdir()
if not path or isinstance(path, Exception):
return path, None
# Check directory and in/output file(s)
result = check_create_dir(path)
if isinstance(result, Exception):
return result, None
inoutfile = os.path.join(path, "temp")
cmd = get_argyll_util("targen")
args = []
args += ['-v']
args += ['-d3']
args += ['-e%s' % getcfg("tc_white_patches")]
if self.argyll_version >= [1, 6]:
args += ['-B%s' % getcfg("tc_black_patches")]
args += ['-s%s' % getcfg("tc_single_channel_patches")]
args += ['-g%s' % getcfg("tc_gray_patches")]
args += ['-m%s' % getcfg("tc_multi_steps")]
if self.argyll_version >= [1, 6, 0]:
args += ['-b%s' % getcfg("tc_multi_bcc_steps")]
tc_algo = getcfg("tc_algo")
if getcfg("tc_fullspread_patches") > 0:
args += ['-f%s' % config.get_total_patches()]
if tc_algo:
args += ['-' + tc_algo]
if tc_algo in ("i", "I"):
args += ['-a%s' % getcfg("tc_angle")]
if tc_algo == "":
args += ['-A%s' % getcfg("tc_adaption")]
if self.argyll_version >= [1, 3, 3]:
args += ['-N%s' % getcfg("tc_neutral_axis_emphasis")]
if (self.argyll_version == [1, 1, "RC1"] or
self.argyll_version >= [1, 1]):
args += ['-G']
else:
args += ['-f0']
if getcfg("tc_precond") and getcfg("tc_precond_profile"):
args += ['-c']
args += [getcfg("tc_precond_profile")]
if getcfg("tc_filter"):
args += ['-F%s,%s,%s,%s' % (getcfg("tc_filter_L"),
getcfg("tc_filter_a"),
getcfg("tc_filter_b"),
getcfg("tc_filter_rad"))]
if (self.argyll_version >= [1, 6, 2] and
("-c" in args or self.argyll_version >= [1, 6, 3])):
args += ['-V%s' % (1 + getcfg("tc_dark_emphasis") * 3)]
if self.argyll_version == [1, 1, "RC2"] or self.argyll_version >= [1, 1]:
args += ['-p%s' % getcfg("tc_gamma")]
if getcfg("extra_args.targen").strip():
# Disallow -d and -D as the testchart editor only supports
# video RGB (-d3)
args += filter(lambda arg: not arg.lower().startswith("-d"),
parse_argument_string(getcfg("extra_args.targen")))
self.options_targen = list(args)
args += [inoutfile]
return cmd, args
def progress_handler(self, event):
""" Handle progress dialog updates and react to Argyll CMS command output """
if getattr(self, "subprocess_abort", False) or \
getattr(self, "thread_abort", False):
self.progress_wnd.Pulse(lang.getstr("aborting"))
return
percentage = None
msg = self.recent.read(FilteredStream.triggers)
lastmsg = self.lastmsg.read(FilteredStream.triggers).strip()
if re.match("\\s*\\d+%", lastmsg):
# colprof
try:
percentage = int(self.lastmsg.read().strip("%"))
except ValueError:
pass
elif re.match("Patch \\d+ of \\d+", lastmsg, re.I):
# dispcal/dispread
components = lastmsg.split()
try:
start = float(components[1])
end = float(components[3])
except ValueError:
pass
else:
percentage = start / end * 100
elif re.match("Added \\d+/\\d+", lastmsg, re.I):
# targen
components = lastmsg.lower().replace("added ", "").split("/")
try:
start = float(components[0])
end = float(components[1])
except ValueError:
pass
else:
percentage = start / end * 100
if (percentage and time() > self.starttime + 3 and
self.progress_wnd is getattr(self, "terminal", None)):
# We no longer need keyboard interaction, switch over to
# progress dialog
wx.CallAfter(self.swap_progress_wnds)
if getattr(self.progress_wnd, "original_msg", None) and \
msg != self.progress_wnd.original_msg:
# UGLY HACK: This 'safe_print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title
safe_print("")
self.progress_wnd.SetTitle(self.progress_wnd.original_msg)
self.progress_wnd.original_msg = None
if percentage:
if "Setting up the instrument" in msg or \
"Commencing device calibration" in msg or \
"Calibration complete" in msg:
self.recent.clear()
msg = ""
keepGoing, skip = self.progress_wnd.Update(math.ceil(percentage),
msg + "\n" +
lastmsg)
else:
if getattr(self.progress_wnd, "lastmsg", "") == msg or not msg:
keepGoing, skip = self.progress_wnd.Pulse()
else:
if "Setting up the instrument" in lastmsg:
msg = lang.getstr("instrument.initializing")
elif "Created web server at" in msg:
webserver = re.search("(http\:\/\/[^']+)", msg)
if webserver:
msg = (lang.getstr("webserver.waiting") +
" " + webserver.groups()[0])
keepGoing, skip = self.progress_wnd.Pulse(msg)
if (getattr(self.progress_wnd, "paused", False) and
not getattr(self, "paused", False)):
self.paused = True
self.safe_send("\x1b")
elif (not getattr(self.progress_wnd, "paused", False) and
getattr(self, "paused", False)):
self.paused = False
self.recent.clear()
self.safe_send(" ")
if (hasattr(self.progress_wnd, "pause_continue") and
"read stopped at user request!" in lastmsg):
self.progress_wnd.pause_continue.Enable()
if not keepGoing:
if getattr(self, "subprocess", None) and \
not getattr(self, "subprocess_abort", False):
if debug:
safe_print('[D] calling quit_terminate_cmd')
self.abort_subprocess(True)
elif not getattr(self, "thread_abort", False):
if debug:
safe_print('[D] thread_abort')
self.thread_abort = True
if self.finished is True:
return
if (not self.activated and self.progress_wnd.IsShownOnScreen() and
(not wx.GetApp().IsActive() or not self.progress_wnd.IsActive()) and
not getattr(self.progress_wnd, "dlg", None)):
self.activated = True
self.progress_wnd.Raise()
def progress_dlg_start(self, progress_title="", progress_msg="",
parent=None, resume=False):
""" Start a progress dialog, replacing existing one if present """
if getattr(self, "progress_dlg", None) and not resume:
self.progress_dlg.Destroy()
self.progress_dlg = None
if getattr(self, "progress_wnd", None) and \
self.progress_wnd is getattr(self, "terminal", None):
self.terminal.stop_timer()
self.terminal.Hide()
if self.finished is True:
return
pauseable = getattr(self, "pauseable", False)
if getattr(self, "progress_dlg", None):
self.progress_wnd = self.progress_dlg
self.progress_wnd.MakeModal(True)
# UGLY HACK: This 'safe_print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title
safe_print("")
self.progress_wnd.SetTitle(progress_title)
self.progress_wnd.Update(0, progress_msg)
if hasattr(self.progress_wnd, "pause_continue"):
self.progress_wnd.pause_continue.Show(pauseable)
self.progress_wnd.Layout()
self.progress_wnd.Resume()
if not self.progress_wnd.IsShownOnScreen():
self.progress_wnd.Show()
self.progress_wnd.start_timer()
else:
# Set maximum to 101 to prevent the 'cancel' changing to 'close'
# when 100 is reached
self.progress_dlg = ProgressDialog(progress_title, progress_msg,
maximum=101,
parent=parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler,
pauseable=pauseable)
self.progress_wnd = self.progress_dlg
self.progress_wnd.original_msg = progress_msg
def quit_terminate_cmd(self):
""" Forcefully abort the current subprocess.
Try to gracefully exit first by sending common Argyll CMS abort
keystrokes (ESC), forcefully terminate the subprocess if not
reacting
"""
if debug:
safe_print('[D] safe_quit')
if getattr(self, "subprocess", None) and \
(hasattr(self.subprocess, "poll") and
self.subprocess.poll() is None) or \
(hasattr(self.subprocess, "isalive") and
self.subprocess.isalive()):
if debug or test:
safe_print('User requested abort')
try:
if self.measure_cmd and hasattr(self.subprocess, "send"):
try:
if self.subprocess.after == "Current":
# Stop measurement
self.safe_send(" ")
sleep(1)
ts = time()
while getattr(self, "subprocess", None) and \
self.subprocess.isalive():
self.safe_send("\x1b")
if time() > ts + 9:
break
sleep(.5)
except Exception, exception:
self.logger.exception("Exception")
if getattr(self, "subprocess", None) and \
(hasattr(self.subprocess, "poll") and
self.subprocess.poll() is None) or \
(hasattr(self.subprocess, "isalive") and
self.subprocess.isalive()):
self.logger.info("Trying to terminate subprocess...")
self.subprocess.terminate()
ts = time()
while getattr(self, "subprocess", None) and \
hasattr(self.subprocess, "isalive") and \
self.subprocess.isalive():
if time() > ts + 3:
break
sleep(.25)
if getattr(self, "subprocess", None) and \
hasattr(self.subprocess, "isalive") and \
self.subprocess.isalive():
self.logger.info("Trying to terminate subprocess forcefully...")
self.subprocess.terminate(force=True)
except Exception, exception:
self.logger.exception("Exception")
if debug:
safe_print('[D] end try')
elif debug:
safe_print('[D] subprocess: %r' % getattr(self, "subprocess", None))
safe_print('[D] subprocess_abort: %r' % getattr(self, "subprocess_abort",
False))
if getattr(self, "subprocess", None):
safe_print('[D] subprocess has poll: %r' % hasattr(self.subprocess,
"poll"))
if hasattr(self.subprocess, "poll"):
safe_print('[D] subprocess.poll(): %r' % self.subprocess.poll())
safe_print('[D] subprocess has isalive: %r' % hasattr(self.subprocess,
"isalive"))
if hasattr(self.subprocess, "isalive"):
safe_print('[D] subprocess.isalive(): %r' % self.subprocess.isalive())
def report(self, report_calibrated=True):
""" Report on calibrated or uncalibrated display device response """
cmd, args = self.prepare_dispcal(calibrate=False)
if isinstance(cmd, Exception):
return cmd
if args:
if report_calibrated:
args += ["-r"]
else:
args += ["-R"]
return self.exec_cmd(cmd, args, capture_output=True, skip_scripts=True)
def reset_cal(self):
cmd, args = self.prepare_dispwin(False)
result = self.exec_cmd(cmd, args, capture_output=True,
skip_scripts=True, silent=False)
return result
def safe_send(self, bytes):
self.send_buffer = bytes
return True
def _safe_send(self, bytes, retry=3):
""" Safely send a keystroke to the current subprocess """
for i in xrange(0, retry):
self.logger.info("Sending key(s) %r (%i)" % (bytes, i + 1))
try:
wrote = self.subprocess.send(bytes)
except Exception, exception:
self.logger.exception("Exception: %s" % safe_unicode(exception))
else:
if wrote == len(bytes):
return True
sleep(.25)
return False
def set_argyll_version(self, name, silent=False, cfg=False):
self.set_argyll_version_from_string(get_argyll_version_string(name,
silent),
cfg)
def set_argyll_version_from_string(self, argyll_version_string, cfg=True):
self.argyll_version_string = argyll_version_string
if cfg:
setcfg("argyll.version", argyll_version_string)
self.argyll_version = parse_argyll_version_string(argyll_version_string)
def set_terminal_cgats(self, cgats_filename):
self.terminal.cgats = CGATS.CGATS(cgats_filename)
def argyll_support_file_exists(self, name):
""" Check if named file exists in any of the known Argyll support
locations valid for the chosen Argyll CMS version. """
if sys.platform != "darwin":
paths = [defaultpaths.appdata] + defaultpaths.commonappdata
else:
paths = [defaultpaths.library_home, defaultpaths.library]
searchpaths = []
if self.argyll_version >= [1, 5, 0]:
if sys.platform != "darwin":
searchpaths += [os.path.join(dir_, "ArgyllCMS", name)
for dir_ in paths]
else:
searchpaths += [os.path.join(dir_, "ArgyllCMS", name)
for dir_ in [defaultpaths.appdata,
defaultpaths.library]]
searchpaths += [os.path.join(dir_, "color", name) for dir_ in paths]
for searchpath in searchpaths:
if os.path.isfile(searchpath):
return True
return False
def spyder2_firmware_exists(self):
""" Check if the Spyder 2 firmware file exists in any of the known
locations valid for the chosen Argyll CMS version. """
if self.argyll_version < [1, 2, 0]:
spyd2en = get_argyll_util("spyd2en")
if not spyd2en:
return False
return os.path.isfile(os.path.join(os.path.dirname(spyd2en),
"spyd2PLD.bin"))
else:
return self.argyll_support_file_exists("spyd2PLD.bin")
def spyder4_cal_exists(self):
""" Check if the Spyder 4 calibration file exists in any of the known
locations valid for the chosen Argyll CMS version. """
if self.argyll_version < [1, 3, 6]:
# We couldn't use it even if it exists
return False
return self.argyll_support_file_exists("spyd4cal.bin")
def start(self, consumer, producer, cargs=(), ckwargs=None, wargs=(),
wkwargs=None, progress_title=appname, progress_msg="",
parent=None, progress_start=100, resume=False,
continue_next=False, stop_timers=True, interactive_frame="",
pauseable=False):
"""
Start a worker process.
Also show a progress dialog while the process is running.
consumer consumer function.
producer producer function.
cargs consumer arguments.
ckwargs consumer keyword arguments.
wargs producer arguments.
wkwargs producer keyword arguments.
progress_title progress dialog title. Defaults to '%s'.
progress_msg progress dialog message. Defaults to ''.
progress_start show progress dialog after delay (ms).
resume resume previous progress dialog (elapsed time etc).
continue_next do not hide progress dialog after producer finishes.
stop_timers stop the timers on the owner window if True
interactive_frame "" or "uniformity" (selects the type of
interactive window)
pauseable Is the operation pauseable? (show pause button on
progress dialog)
""" % appname
if ckwargs is None:
ckwargs = {}
if wkwargs is None:
wkwargs = {}
while self.is_working():
sleep(.25) # wait until previous worker thread finishes
if hasattr(self.owner, "stop_timers") and stop_timers:
self.owner.stop_timers()
if not parent:
parent = self.owner
if progress_start < 100:
progress_start = 100
self.activated = False
self.cmdrun = False
self.finished = False
self.instrument_calibration_complete = False
self.instrument_place_on_screen_msg = False
self.instrument_sensor_position_msg = False
self.is_ambient_measuring = False
self.lastcmdname = None
self.pauseable = pauseable
self.paused = False
self.resume = resume
self.subprocess_abort = False
self.starttime = time()
self.thread_abort = False
if self.interactive:
self.progress_start_timer = wx.Timer()
if getattr(self, "progress_wnd", None) and \
self.progress_wnd is getattr(self, "progress_dlg", None):
self.progress_dlg.Destroy()
self.progress_dlg = None
if progress_msg and progress_title == appname:
progress_title = progress_msg
if (config.get_display_name() == "Untethered" and
interactive_frame != "uniformity"):
interactive_frame = "untethered"
if interactive_frame == "adjust":
windowclass = DisplayAdjustmentFrame
elif interactive_frame == "uniformity":
windowclass = DisplayUniformityFrame
elif interactive_frame == "untethered":
windowclass = UntetheredFrame
else:
windowclass = SimpleTerminal
if getattr(self, "terminal", None) and isinstance(self.terminal,
windowclass):
self.progress_wnd = self.terminal
if not resume:
if isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.console.SetValue("")
elif (isinstance(self.progress_wnd, DisplayAdjustmentFrame) or
isinstance(self.progress_wnd, DisplayUniformityFrame) or
isinstance(self.progress_wnd, UntetheredFrame)):
self.progress_wnd.reset()
self.progress_wnd.stop_timer()
self.progress_wnd.Resume()
self.progress_wnd.start_timer()
# UGLY HACK: This 'safe_print' call fixes a GTK assertion and
# segfault under Arch Linux when setting the window title
safe_print("")
if isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.SetTitle(progress_title)
self.progress_wnd.Show()
if resume and isinstance(self.progress_wnd, SimpleTerminal):
self.progress_wnd.console.ScrollLines(
self.progress_wnd.console.GetNumberOfLines())
else:
if interactive_frame == "adjust":
self.terminal = DisplayAdjustmentFrame(parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler)
elif interactive_frame == "uniformity":
self.terminal = DisplayUniformityFrame(parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler)
elif interactive_frame == "untethered":
self.terminal = UntetheredFrame(parent,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler)
else:
self.terminal = SimpleTerminal(parent, title=progress_title,
handler=self.progress_handler,
keyhandler=self.terminal_key_handler)
self.terminal.worker = self
self.progress_wnd = self.terminal
else:
if not progress_msg:
progress_msg = lang.getstr("please_wait")
# Show the progress dialog after a delay
self.progress_start_timer = wx.CallLater(progress_start,
self.progress_dlg_start,
progress_title,
progress_msg, parent,
resume)
self.thread = delayedresult.startWorker(self._generic_consumer,
producer, [consumer,
continue_next] +
list(cargs), ckwargs, wargs,
wkwargs)
return True
def stop_progress(self):
if getattr(self, "progress_wnd", False):
if getattr(self.progress_wnd, "dlg", None):
self.progress_wnd.dlg.Close()
del self.progress_wnd.dlg
self.progress_wnd.stop_timer()
self.progress_wnd.MakeModal(False)
# under Linux, destroying it here causes segfault
if sys.platform == "win32" and wx.VERSION >= (2, 9):
self.progress_wnd.Destroy()
else:
self.progress_wnd.Hide()
def swap_progress_wnds(self):
""" Swap the current interactive window with a progress dialog """
parent = self.terminal.GetParent()
if isinstance(self.terminal, DisplayAdjustmentFrame):
title = lang.getstr("calibration")
else:
title = self.terminal.GetTitle()
self.progress_dlg_start(title, "", parent, self.resume)
def terminal_key_handler(self, event):
""" Key handler for the interactive window or progress dialog. """
keycode = None
if event.GetEventType() in (wx.EVT_CHAR_HOOK.typeId,
wx.EVT_KEY_DOWN.typeId):
keycode = event.GetKeyCode()
elif event.GetEventType() == wx.EVT_MENU.typeId:
keycode = self.progress_wnd.id_to_keycode.get(event.GetId())
if keycode is not None and getattr(self, "subprocess", None) and \
hasattr(self.subprocess, "send"):
keycode = keycodes.get(keycode, keycode)
if keycode in (ord("\x1b"), ord("8"), ord("Q"), ord("q")):
# exit
self.abort_subprocess(True)
return
try:
self.safe_send(chr(keycode))
except:
pass
def calculate_gamut(self, profile_path):
"""
Calculate gamut, volume, and coverage % against sRGB and Adobe RGB.
Return gamut volume (int, scaled to sRGB = 1.0) and
coverage (dict) as tuple.
"""
outname = os.path.splitext(profile_path)[0]
gamut_volume = None
gamut_coverage = {}
# Create profile gamut and vrml
if (sys.platform == "win32" and
re.search("[^\x20-\x7e]", os.path.basename(profile_path))):
# Avoid problems with encoding
profile_path = win32api.GetShortPathName(profile_path)
result = self.exec_cmd(get_argyll_util("iccgamut"),
["-v", "-w", "-ir", profile_path],
capture_output=True,
skip_scripts=True)
if not isinstance(result, Exception) and result:
# iccgamut output looks like this:
# Header:
# <...>
#
# Total volume of gamut is xxxxxx.xxxxxx cubic colorspace units
for line in self.output:
match = re.search("(\d+(?:\.\d+)?)\s+cubic\s+colorspace\s+"
"units", line)
if match:
gamut_volume = float(match.groups()[0]) / ICCP.GAMUT_VOLUME_SRGB
break
name = os.path.splitext(profile_path)[0]
gamfilename = name + ".gam"
wrlfilename = name + ".wrl"
tmpfilenames = [gamfilename, wrlfilename]
for key, src in (("srgb", "sRGB"), ("adobe-rgb", "ClayRGB1998")):
if not isinstance(result, Exception) and result:
# Create gamut view and intersection
src_path = get_data_path("ref/%s.gam" % src)
if not src_path:
continue
outfilename = outname + (" vs %s.wrl" % src)
tmpfilenames.append(outfilename)
result = self.exec_cmd(get_argyll_util("viewgam"),
["-cw", "-t.75", "-s", src_path, "-cn",
"-t.25", "-s", gamfilename, "-i",
outfilename],
capture_output=True,
skip_scripts=True)
if not isinstance(result, Exception) and result:
# viewgam output looks like this:
# Intersecting volume = xxx.x cubic units
# 'path/to/1.gam' volume = xxx.x cubic units, intersect = xx.xx%
# 'path/to/2.gam' volume = xxx.x cubic units, intersect = xx.xx%
for line in self.output:
match = re.search("[\\\/]%s.gam'\s+volume\s*=\s*"
"\d+(?:\.\d+)?\s+cubic\s+units,?"
"\s+intersect\s*=\s*"
"(\d+(?:\.\d+)?)" %
re.escape(src), line)
if match:
gamut_coverage[key] = float(match.groups()[0]) / 100.0
break
if not isinstance(result, Exception) and result:
for tmpfilename in tmpfilenames:
if (tmpfilename == gamfilename and
tmpfilename != outname + ".gam"):
# Use the original file name
filename = outname + ".gam"
elif (tmpfilename == wrlfilename and
tmpfilename != outname + ".wrl"):
# Use the original file name
filename = outname + ".wrl"
else:
filename = tmpfilename
try:
if getcfg("vrml.compress"):
# Compress gam and wrl files using gzip
if filename.endswith(".wrl"):
filename = filename[:-4] + ".wrz"
else:
filename = filename + ".gz"
with GzipFileProper(tmpfilename + ".gz", "wb") as gz:
# Always use original filename with '.gz' extension,
# that way the filename in the header will be correct
with open(tmpfilename, "rb") as infile:
gz.write(infile.read())
# Remove uncompressed file
os.remove(tmpfilename)
tmpfilename = tmpfilename + ".gz"
if tmpfilename != filename:
# Rename the file if filename is different
if os.path.exists(filename):
os.remove(filename)
os.rename(tmpfilename, filename)
except Exception, exception:
safe_print(safe_unicode(exception))
elif result:
# Exception
safe_print(safe_unicode(result))
return gamut_volume, gamut_coverage
def calibrate(self, remove=False):
""" Calibrate the screen and process the generated file(s). """
capture_output = not sys.stdout.isatty()
cmd, args = self.prepare_dispcal()
if not isinstance(cmd, Exception):
result = self.exec_cmd(cmd, args, capture_output=capture_output)
else:
result = cmd
if not isinstance(result, Exception) and result:
dst_pathname = os.path.join(getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded"))
cal = args[-1] + ".cal"
result = check_cal_isfile(
cal, lang.getstr("error.calibration.file_not_created"))
if not isinstance(result, Exception) and result:
cal_cgats = add_dispcal_options_to_cal(cal,
self.options_dispcal)
if cal_cgats:
cal_cgats.write()
if getcfg("profile.update") or \
self.dispcal_create_fast_matrix_shaper:
profile_path = args[-1] + profile_ext
result = check_profile_isfile(
profile_path,
lang.getstr("error.profile.file_not_created"))
if not isinstance(result, Exception) and result:
try:
profile = ICCP.ICCProfile(profile_path)
except (IOError, ICCP.ICCProfileInvalidError), exception:
result = Error(lang.getstr("profile.invalid") + "\n"
+ profile_path)
if not isinstance(result, Exception) and result:
if not getcfg("profile.update"):
# Created fast matrix shaper profile
# we need to set cprt, targ and a few other things
profile.setCopyright(getcfg("copyright"))
# Fast matrix shaper profiles currently don't
# contain TI3 data, but look for it anyways
# to be future-proof
ti3 = add_options_to_ti3(
profile.tags.get("targ",
profile.tags.get("CIED",
"")),
self.options_dispcal)
if not ti3:
ti3 = CGATS.CGATS("TI3\n")
ti3[1] = cal_cgats
edid = self.get_display_edid()
display_name = edid.get("monitor_name",
edid.get("ascii",
str(edid.get("product_id") or "")))
display_manufacturer = edid.get("manufacturer")
profile.setDeviceModelDescription(display_name)
profile.setDeviceManufacturerDescription(
display_manufacturer)
(gamut_volume,
gamut_coverage) = self.create_gamut_views(profile_path)
self.update_profile(profile, ti3=str(ti3),
chrm=None, tags=True, avg=None,
peak=None, rms=None,
gamut_volume=gamut_volume,
gamut_coverage=gamut_coverage,
quality=getcfg("calibration.quality"))
else:
# Update desc tag - ASCII needs to be 7-bit
# also add Unicode string if different from ASCII
if "desc" in profile.tags and isinstance(profile.tags.desc,
ICCP.TextDescriptionType):
profile.setDescription(
getcfg("profile.name.expanded"))
# Calculate profile ID
profile.calculateID()
try:
profile.write()
except Exception, exception:
safe_print(exception)
result2 = self.wrapup(not isinstance(result, UnloggedInfo) and result,
remove or isinstance(result, Exception) or
not result)
if isinstance(result2, Exception):
if isinstance(result, Exception):
result = Error(safe_unicode(result) + "\n\n" +
safe_unicode(result2))
else:
result = result2
elif not isinstance(result, Exception) and result:
setcfg("last_cal_path", dst_pathname + ".cal")
setcfg("calibration.file.previous", getcfg("calibration.file"))
if (getcfg("profile.update") or
self.dispcal_create_fast_matrix_shaper):
setcfg("last_cal_or_icc_path", dst_pathname + profile_ext)
setcfg("last_icc_path", dst_pathname + profile_ext)
setcfg("calibration.file", dst_pathname + profile_ext)
else:
setcfg("calibration.file", dst_pathname + ".cal")
setcfg("last_cal_or_icc_path", dst_pathname + ".cal")
return result
def chart_lookup(self, cgats, profile, as_ti3=False, fields=None,
check_missing_fields=False, function="f", pcs="l",
intent="r", bt1886=None, add_white_patches=True):
""" Lookup CIE or device values through profile """
if profile.colorSpace == "RGB":
labels = ('RGB_R', 'RGB_G', 'RGB_B')
else:
labels = ('CMYK_C', 'CMYK_M', 'CMYK_Y', 'CMYK_K')
ti1 = None
ti3_ref = None
gray = None
try:
if not isinstance(cgats, CGATS.CGATS):
cgats = CGATS.CGATS(cgats, True)
else:
# Always make a copy and do not alter a passed in CGATS instance!
cgats = CGATS.CGATS(str(cgats))
if 0 in cgats:
# only look at the first section
cgats[0].filename = cgats.filename
cgats = cgats[0]
primaries = cgats.queryi(labels)
if primaries and not as_ti3:
primaries.fix_device_values_scaling(profile.colorSpace)
cgats.type = 'CTI1'
cgats.COLOR_REP = profile.colorSpace
ti1, ti3_ref, gray = self.ti1_lookup_to_ti3(cgats, profile,
function, pcs,
"r",
add_white_patches)
if bt1886 or intent == "a":
cat = profile.guess_cat() or "Bradford"
for item in ti3_ref.DATA.itervalues():
if pcs == "l":
X, Y, Z = colormath.Lab2XYZ(item["LAB_L"],
item["LAB_A"],
item["LAB_B"])
else:
X, Y, Z = item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"]
if bt1886:
X, Y, Z = bt1886.apply(X, Y, Z)
if intent == "a":
X, Y, Z = colormath.adapt(X, Y, Z,
"D50",
profile.tags.wtpt.values(),
cat=cat)
X, Y, Z = [v * 100 for v in (X, Y, Z)]
if pcs == "l":
(item["LAB_L"],
item["LAB_A"],
item["LAB_B"]) = colormath.XYZ2Lab(X, Y, Z)
else:
item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"] = X, Y, Z
else:
if not primaries and check_missing_fields:
raise ValueError(lang.getstr("error.testchart.missing_fields",
(cgats.filename, ", ".join(labels))))
ti1, ti3_ref = self.ti3_lookup_to_ti1(cgats, profile, fields,
intent, add_white_patches)
except Exception, exception:
InfoDialog(self.owner, msg=safe_unicode(exception),
ok=lang.getstr("ok"), bitmap=geticon(32, "dialog-error"))
return ti1, ti3_ref, gray
def ti1_lookup_to_ti3(self, ti1, profile, function="f", pcs=None,
intent="r", add_white_patches=True):
"""
Read TI1 (filename or CGATS instance), lookup device->pcs values
colorimetrically through profile using Argyll's xicclu
utility and return TI3 (CGATS instance)
"""
# ti1
if isinstance(ti1, basestring):
ti1 = CGATS.CGATS(ti1)
if not isinstance(ti1, CGATS.CGATS):
raise TypeError('Wrong type for ti1, needs to be CGATS.CGATS '
'instance')
# profile
if isinstance(profile, basestring):
profile = ICCP.ICCProfile(profile)
if not isinstance(profile, ICCP.ICCProfile):
raise TypeError('Wrong type for profile, needs to be '
'ICCP.ICCProfile instance')
# determine pcs for lookup
color_rep = profile.connectionColorSpace.upper()
if color_rep == "RGB":
pcs = None
elif not pcs:
if color_rep == 'LAB':
pcs = 'l'
elif color_rep == 'XYZ':
pcs = 'x'
else:
raise ValueError('Unknown CIE color representation ' + color_rep)
# get profile color space
colorspace = profile.colorSpace
# required fields for ti1
if colorspace == "CMYK":
required = ("CMYK_C", "CMYK_M", "CMYK_Y", "CMYK_K")
else:
required = ("RGB_R", "RGB_G", "RGB_B")
ti1_filename = ti1.filename
try:
ti1 = verify_cgats(ti1, required, True)
except CGATS.CGATSInvalidError:
raise ValueError(lang.getstr("error.testchart.invalid",
ti1_filename))
except CGATS.CGATSKeyError:
raise ValueError(lang.getstr("error.testchart.missing_fields",
(ti1_filename, ", ".join(required))))
# read device values from ti1
data = ti1.queryv1("DATA")
if not data:
raise ValueError(lang.getstr("error.testchart.invalid",
ti1_filename))
device_data = data.queryv(required)
if not device_data:
raise ValueError(lang.getstr("error.testchart.missing_fields",
(ti1_filename, ", ".join(required))))
if colorspace == "RGB" and add_white_patches:
# make sure the first four patches are white so the whitepoint can be
# averaged
white_rgb = {'RGB_R': 100, 'RGB_G': 100, 'RGB_B': 100}
white = dict(white_rgb)
wp = ti1.queryv1("APPROX_WHITE_POINT")
if wp:
wp = [float(v) for v in wp.split()]
wp = [CGATS.rpad((v / wp[1]) * 100.0, data.vmaxlen) for v in wp]
else:
wp = colormath.get_standard_illuminant("D65", scale=100)
for label in data.parent.DATA_FORMAT.values():
if not label in white:
if label.upper() == 'LAB_L':
value = 100
elif label.upper() in ('LAB_A', 'LAB_B'):
value = 0
elif label.upper() == 'XYZ_X':
value = wp[0]
elif label.upper() == 'XYZ_Y':
value = 100
elif label.upper() == 'XYZ_Z':
value = wp[2]
else:
value = '0'
white.update({label: value})
white_added_count = 0
while len(data.queryi(white_rgb)) < 4: # add white patches
data.insert(0, white)
white_added_count += 1
safe_print("Added %i white patch(es)" % white_added_count)
idata = []
for primaries in device_data.values():
idata.append(' '.join(str(n) for n in primaries.values()))
# lookup device->cie values through profile using (x)icclu
if pcs or self.argyll_version >= [1, 6]:
xicclu = get_argyll_util("xicclu").encode(fs_enc)
else:
# DeviceLink profile, we have to use icclu under older Argyll CMS
# versions because older xicclu cannot handle devicelink
xicclu = get_argyll_util("icclu").encode(fs_enc)
cwd = self.create_tempdir()
if isinstance(cwd, Exception):
raise cwd
profile.write(os.path.join(cwd, "temp.icc"))
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
args = [xicclu, '-f' + function]
if pcs:
args += ['-i' + intent, '-p' + pcs]
p = sp.Popen(args + ['-s100', "temp.icc"],
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT,
cwd=cwd.encode(fs_enc), startupinfo=startupinfo)
odata = p.communicate('\n'.join(idata))[0].splitlines()
if p.wait() != 0:
# error
raise IOError(''.join(odata))
gray = []
igray = []
igray_idx = []
if colorspace == "RGB":
# treat r=g=b specially: set expected a=b=0
for i, line in enumerate(odata):
line = line.strip().split('->')
line = ''.join(line).split()
if line[-1] == '(clip)':
line.pop()
r, g, b = [float(n) for n in line[:3]]
if r == g == b < 100:
# if grayscale and not white
cie = [float(n) for n in line[5:-1]]
if pcs == 'x':
# Need to scale XYZ coming from xicclu
# Lab is already scaled
cie = colormath.XYZ2Lab(*[n * 100.0 for n in cie])
cie = (cie[0], 0, 0) # set a=b=0
igray.append("%s %s %s" % cie)
igray_idx.append(i)
if pcs == 'x':
cie = colormath.Lab2XYZ(*cie)
luminance = cie[1]
else:
luminance = colormath.Lab2XYZ(*cie)[1]
if luminance * 100.0 >= 1:
# only add if luminance is greater or equal 1% because
# dark tones fluctuate too much
gray.append((r, g, b))
if False: # NEVER?
# set cie in odata to a=b=0
line[5:-1] = [str(n) for n in cie]
odata[i] = ' -> '.join([' '.join(line[:4]), line[4],
' '.join(line[5:])])
if igray and False: # NEVER?
# lookup cie->device values for grays through profile using xicclu
gray = []
xicclu = get_argyll_util("xicclu").encode(fs_enc)
cwd = self.create_tempdir()
if isinstance(cwd, Exception):
raise cwd
profile.write(os.path.join(cwd, "temp.icc"))
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
p = sp.Popen([xicclu, '-fb', '-ir', '-pl', '-s100', "temp.icc"],
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT,
cwd=cwd.encode(fs_enc), startupinfo=startupinfo)
ogray = p.communicate('\n'.join(igray))[0].splitlines()
if p.wait() != 0:
# error
raise IOError(''.join(odata))
for i, line in enumerate(ogray):
line = line.strip().split('->')
line = ''.join(line).split()
if line[-1] == '(clip)':
line.pop()
cie = [float(n) for n in line[:3]]
rgb = [float(n) for n in line[5:-1]]
if colormath.Lab2XYZ(cie[0], 0, 0)[1] * 100.0 >= 1:
# only add if luminance is greater or equal 1% because
# dark tones fluctuate too much
gray.append(rgb)
# update values in ti1 and data for ti3
for n, channel in enumerate(("R", "G", "B")):
data[igray_idx[i] +
white_added_count]["RGB_" + channel] = rgb[n]
oline = odata[igray_idx[i]].strip().split('->', 1)
odata[igray_idx[i]] = ' [RGB] ->'.join([' '.join(line[5:-1])] +
oline[1:])
self.wrapup(False)
# write output ti3
ofile = StringIO()
if pcs:
ofile.write('CTI3 \n')
ofile.write('\nDESCRIPTOR "Argyll Calibration Target chart information 3"\n')
else:
ofile.write('CTI1 \n')
ofile.write('\nDESCRIPTOR "Argyll Calibration Target chart information 1"\n')
ofile.write('KEYWORD "DEVICE_CLASS"\n')
ofile.write('DEVICE_CLASS "' + ('DISPLAY' if colorspace == 'RGB' else
'OUTPUT') + '"\n')
include_sample_name = False
i = 0
offset = 0 if colorspace == "RGB" else 1
for line in odata:
line = line.strip().split('->')
line = ''.join(line).split()
if line[-1] == '(clip)':
line.pop()
if i == 0:
icolor = line[3 + offset].strip('[]')
if icolor == 'RGB':
olabel = 'RGB_R RGB_G RGB_B'
elif icolor == 'CMYK':
olabel = 'CMYK_C CMYK_M CMYK_Y CMYK_K'
else:
raise ValueError('Unknown color representation ' + icolor)
ocolor = line[-1].strip('[]').upper()
if ocolor == 'LAB':
ilabel = 'LAB_L LAB_A LAB_B'
elif ocolor in ('XYZ', 'RGB'):
ilabel = 'XYZ_X XYZ_Y XYZ_Z'
else:
raise ValueError('Unknown CIE color representation ' + ocolor)
ofile.write('KEYWORD "COLOR_REP"\n')
if icolor == ocolor:
ofile.write('COLOR_REP "' + icolor + '"\n')
else:
ofile.write('COLOR_REP "' + icolor + '_' + ocolor + '"\n')
ofile.write('\n')
ofile.write('NUMBER_OF_FIELDS ')
if include_sample_name:
ofile.write(str(2 + len(icolor) + len(ocolor)) + '\n')
else:
ofile.write(str(1 + len(icolor) + len(ocolor)) + '\n')
ofile.write('BEGIN_DATA_FORMAT\n')
ofile.write('SAMPLE_ID ')
if include_sample_name:
ofile.write('SAMPLE_NAME ' + olabel + ' ' + ilabel + '\n')
else:
ofile.write(olabel + ' ' + ilabel + '\n')
ofile.write('END_DATA_FORMAT\n')
ofile.write('\n')
ofile.write('NUMBER_OF_SETS ' + str(len(odata)) + '\n')
ofile.write('BEGIN_DATA\n')
i += 1
cie = [float(n) for n in line[5 + offset:-1]]
if pcs == 'x':
# Need to scale XYZ coming from xicclu, Lab is already scaled
cie = [round(n * 100.0, 5 - len(str(int(abs(n * 100.0)))))
for n in cie]
elif not pcs:
line[:3] = line[5 + offset:-1]
cie = [round(n * 100.0, 5 - len(str(int(abs(n * 100.0)))))
for n in colormath.RGB2XYZ(*[n / 100.0 for n in cie])]
cie = [str(n) for n in cie]
if include_sample_name:
ofile.write(str(i) + ' ' + data[i - 1][1].strip('"') + ' ' +
' '.join(line[:3 + offset]) + ' ' + ' '.join(cie) + '\n')
else:
ofile.write(str(i) + ' ' + ' '.join(line[:3 + offset]) + ' ' +
' '.join(cie) + '\n')
ofile.write('END_DATA\n')
ofile.seek(0)
return ti1, CGATS.CGATS(ofile)[0], map(list, gray)
def ti3_lookup_to_ti1(self, ti3, profile, fields=None, intent="r",
add_white_patches=True):
"""
Read TI3 (filename or CGATS instance), lookup cie->device values
colorimetrically through profile using Argyll's xicclu
utility and return TI1 and compatible TI3 (CGATS instances)
"""
# ti3
copy = True
if isinstance(ti3, basestring):
copy = False
ti3 = CGATS.CGATS(ti3)
if not isinstance(ti3, CGATS.CGATS):
raise TypeError('Wrong type for ti3, needs to be CGATS.CGATS '
'instance')
ti3_filename = ti3.filename
if copy:
# Make a copy and do not alter a passed in CGATS instance!
ti3 = CGATS.CGATS(str(ti3))
if fields == "XYZ":
labels = ("XYZ_X", "XYZ_Y", "XYZ_Z")
else:
labels = ("LAB_L", "LAB_A", "LAB_B")
try:
ti3v = verify_cgats(ti3, labels, True)
except CGATS.CGATSInvalidError, exception:
raise ValueError(lang.getstr("error.testchart.invalid",
ti3_filename) + "\n" +
lang.getstr(safe_str(exception)))
except CGATS.CGATSKeyError:
try:
if fields:
raise
else:
labels = ("XYZ_X", "XYZ_Y", "XYZ_Z")
ti3v = verify_cgats(ti3, labels, True)
except CGATS.CGATSKeyError:
missing = ", ".join(labels)
if not fields:
missing += " " + lang.getstr("or") + " LAB_L, LAB_A, LAB_B"
raise ValueError(lang.getstr("error.testchart.missing_fields",
(ti3_filename, missing)))
else:
color_rep = 'XYZ'
else:
color_rep = fields or 'LAB'
# profile
if isinstance(profile, basestring):
profile = ICCP.ICCProfile(profile)
if not isinstance(profile, ICCP.ICCProfile):
raise TypeError('Wrong type for profile, needs to be '
'ICCP.ICCProfile instance')
# determine pcs for lookup
if color_rep == 'LAB':
pcs = 'l'
required = ("LAB_L", "LAB_A", "LAB_B")
elif color_rep == 'XYZ':
pcs = 'x'
required = ("XYZ_X", "XYZ_Y", "XYZ_Z")
else:
raise ValueError('Unknown CIE color representation ' + color_rep)
# get profile color space
colorspace = profile.colorSpace
# read cie values from ti3
data = ti3v.queryv1("DATA")
if not data:
raise ValueError(lang.getstr("error.testchart.invalid",
ti3_filename))
cie_data = data.queryv(required)
if not cie_data:
raise ValueError(lang.getstr("error.testchart.missing_fields",
(ti3_filename, ", ".join(required))))
idata = []
if colorspace == "RGB" and add_white_patches:
# make sure the first four patches are white so the whitepoint can be
# averaged
wp = [n * 100.0 for n in profile.tags.wtpt.values()]
if color_rep == 'LAB':
wp = colormath.XYZ2Lab(*wp)
wp = OrderedDict((('L', wp[0]), ('a', wp[1]), ('b', wp[2])))
else:
wp = OrderedDict((('X', wp[0]), ('Y', wp[1]), ('Z', wp[2])))
wp = [wp] * 4
safe_print("Added 4 white patches")
else:
wp = []
for cie in wp + cie_data.values():
cie = cie.values()
if color_rep == 'XYZ':
# assume scale 0...100 in ti3, we need to convert to 0...1
cie = [n / 100.0 for n in cie]
idata.append(' '.join(str(n) for n in cie))
# lookup cie->device values through profile.icc using xicclu
xicclu = get_argyll_util("xicclu").encode(fs_enc)
cwd = self.create_tempdir()
if isinstance(cwd, Exception):
raise cwd
profile.write(os.path.join(cwd, "temp.icc"))
if sys.platform == "win32":
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = sp.SW_HIDE
else:
startupinfo = None
p = sp.Popen([xicclu, '-fb', '-i' + intent, '-p' + pcs, '-s100', "temp.icc"],
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT,
cwd=cwd.encode(fs_enc), startupinfo=startupinfo)
odata = p.communicate('\n'.join(idata))[0].splitlines()
if p.wait() != 0:
# error
raise IOError(''.join(odata))
self.wrapup(False)
# write output ti1/ti3
ti1out = StringIO()
ti1out.write('CTI1\n')
ti1out.write('\n')
ti1out.write('DESCRIPTOR "Argyll Calibration Target chart information 1"\n')
include_sample_name = False
i = 0
for line in odata:
line = line.strip().split('->')
line = ''.join(line).split()
if line[-1] == '(clip)':
line.pop()
if i == 0:
icolor = line[3].strip('[]').upper()
if icolor == 'LAB':
ilabel = 'LAB_L LAB_A LAB_B'
elif icolor == 'XYZ':
ilabel = 'XYZ_X XYZ_Y XYZ_Z'
else:
raise ValueError('Unknown CIE color representation ' + icolor)
ocolor = line[-1].strip('[]')
if ocolor == 'RGB':
olabel = 'RGB_R RGB_G RGB_B'
elif ocolor == 'CMYK':
olabel = 'CMYK_C CMYK_M CMYK_Y CMYK_K'
else:
raise ValueError('Unknown color representation ' + ocolor)
olabels = olabel.split()
# add device fields to DATA_FORMAT if not yet present
if not olabels[0] in ti3v.DATA_FORMAT.values() and \
not olabels[1] in ti3v.DATA_FORMAT.values() and \
not olabels[2] in ti3v.DATA_FORMAT.values() and \
(ocolor == 'RGB' or (ocolor == 'CMYK' and
not olabels[3] in ti3v.DATA_FORMAT.values())):
ti3v.DATA_FORMAT.add_data(olabels)
# add required fields to DATA_FORMAT if not yet present
if not required[0] in ti3v.DATA_FORMAT.values() and \
not required[1] in ti3v.DATA_FORMAT.values() and \
not required[2] in ti3v.DATA_FORMAT.values():
ti3v.DATA_FORMAT.add_data(required)
ti1out.write('KEYWORD "COLOR_REP"\n')
ti1out.write('COLOR_REP "' + ocolor + '"\n')
ti1out.write('\n')
ti1out.write('NUMBER_OF_FIELDS ')
if include_sample_name:
ti1out.write(str(2 + len(icolor) + len(ocolor)) + '\n')
else:
ti1out.write(str(1 + len(icolor) + len(ocolor)) + '\n')
ti1out.write('BEGIN_DATA_FORMAT\n')
ti1out.write('SAMPLE_ID ')
if include_sample_name:
ti1out.write('SAMPLE_NAME ' + olabel + ' ' + ilabel + '\n')
else:
ti1out.write(olabel + ' ' + ilabel + '\n')
ti1out.write('END_DATA_FORMAT\n')
ti1out.write('\n')
ti1out.write('NUMBER_OF_SETS ' + str(len(odata)) + '\n')
ti1out.write('BEGIN_DATA\n')
if i < len(wp):
if ocolor == 'RGB':
device = '100.00 100.00 100.00'.split()
else:
device = '0 0 0 0'.split()
else:
device = line[5:-1]
# Make sure device values do not exceed valid range of 0..100
device = [str(max(0, min(float(v), 100))) for v in device]
cie = (wp + cie_data.values())[i].values()
cie = [str(n) for n in cie]
if include_sample_name:
ti1out.write(str(i + 1) + ' ' + data[i][1].strip('"') + ' ' +
' '.join(device) + ' ' + ' '.join(cie) + '\n')
else:
ti1out.write(str(i + 1) + ' ' + ' '.join(device) + ' ' +
' '.join(cie) + '\n')
if i > len(wp) - 1: # don't include whitepoint patches in ti3
# set device values in ti3
for n, v in enumerate(olabels):
ti3v.DATA[i - len(wp)][v] = float(line[5 + n])
# set PCS values in ti3
for n, v in enumerate(cie):
ti3v.DATA[i - len(wp)][required[n]] = float(v)
i += 1
ti1out.write('END_DATA\n')
ti1out.seek(0)
return CGATS.CGATS(ti1out), ti3v
def verify_calibration(self):
""" Verify the current calibration """
cmd, args = self.prepare_dispcal(calibrate=False, verify=True)
if not isinstance(cmd, Exception):
result = self.exec_cmd(cmd, args, capture_output=True,
skip_scripts=True)
else:
result = cmd
return result
def measure_ti1(self, ti1_path, cal_path=None, colormanaged=False):
""" Measure a TI1 testchart file """
if config.get_display_name() == "Untethered":
cmd, args = get_argyll_util("spotread"), ["-v", "-e"]
if getcfg("extra_args.spotread").strip():
args += parse_argument_string(getcfg("extra_args.spotread"))
self.set_terminal_cgats(ti1_path)
else:
cmd = get_argyll_util("dispread")
args = ["-v"]
if config.get_display_name() == "madVR" and colormanaged:
args += ["-V"]
if cal_path:
if (self.argyll_version >= [1, 3, 3] and
(not self.has_lut_access() or
not getcfg("calibration.use_video_lut"))):
if config.get_display_name() == "madVR":
# Normally -K will automatically reset the video LUT,
# but when using madVR, we have to do it explicitly
result = self.reset_cal()
if (isinstance(result, Exception) and
not isinstance(result, UnloggedInfo)):
return result, None
args += ["-K"]
else:
args += ["-k"]
args += [cal_path]
if getcfg("extra_args.dispread").strip():
args += parse_argument_string(getcfg("extra_args.dispread"))
result = self.add_measurement_features(args,
cmd == get_argyll_util("dispread"))
if isinstance(result, Exception):
return result
if config.get_display_name() != "Untethered":
args += [os.path.splitext(ti1_path)[0]]
return self.exec_cmd(cmd, args, skip_scripts=True)
def wrapup(self, copy=True, remove=True, dst_path=None, ext_filter=None):
"""
Wrap up - copy and/or clean temporary file(s).
"""
if debug: safe_print("[D] wrapup(copy=%s, remove=%s)" % (copy, remove))
if not self.tempdir or not os.path.isdir(self.tempdir):
return # nothing to do
while self.sessionlogfiles:
self.sessionlogfiles.popitem()[1].close()
result = True
if copy:
try:
src_listdir = os.listdir(self.tempdir)
except Exception, exception:
result = exception
remove = False
if not isinstance(result, Exception) and src_listdir:
if not ext_filter:
ext_filter = [".app", ".cal", ".ccmx", ".ccss", ".cmd",
".command", ".gam", ".gz", ".icc", ".icm", ".log",
".sh", ".ti1", ".ti3", ".wrl", ".wrz"]
if dst_path is None:
dst_path = os.path.join(getcfg("profile.save_path"),
getcfg("profile.name.expanded"),
getcfg("profile.name.expanded") +
".ext")
result = check_create_dir(os.path.dirname(dst_path))
if isinstance(result, Exception):
remove = False
else:
for basename in src_listdir:
name, ext = os.path.splitext(basename)
if ext_filter is None or ext.lower() in ext_filter:
src = os.path.join(self.tempdir, basename)
dst = os.path.join(os.path.dirname(dst_path), basename)
if os.path.exists(dst):
if os.path.isdir(dst):
if verbose >= 2:
safe_print(appname +
": Removing existing "
"destination directory tree",
dst)
try:
shutil.rmtree(dst, True)
except Exception, exception:
safe_print(u"Warning - directory '%s' "
u"could not be removed: %s" %
tuple(safe_unicode(s)
for s in (dst,
exception)))
else:
if verbose >= 2:
safe_print(appname +
": Removing existing "
"destination file", dst)
try:
os.remove(dst)
except Exception, exception:
safe_print(u"Warning - file '%s' could "
u"not be removed: %s" %
tuple(safe_unicode(s)
for s in (dst,
exception)))
if remove:
if verbose >= 2:
safe_print(appname + ": Moving temporary "
"object %s to %s" % (src, dst))
try:
shutil.move(src, dst)
except Exception, exception:
safe_print(u"Warning - temporary object "
u"'%s' could not be moved to "
u"'%s': %s" %
tuple(safe_unicode(s) for s in
(src, dst, exception)))
try:
shutil.copyfile(src, dst)
except Exception, exception:
result = Error(lang.getstr("error.copy_failed",
(src, dst)))
else:
if os.path.isdir(src):
if verbose >= 2:
safe_print(appname +
": Copying temporary "
"directory tree %s to %s" %
(src, dst))
try:
shutil.copytree(src, dst)
except Exception, exception:
safe_print(u"Warning - temporary "
u"directory '%s' could not "
u"be copied to '%s': %s" %
tuple(safe_unicode(s)
for s in
(src, dst, exception)))
else:
if verbose >= 2:
safe_print(appname +
": Copying temporary "
"file %s to %s" % (src, dst))
try:
shutil.copyfile(src, dst)
except Exception, exception:
safe_print(u"Warning - temporary file "
u"'%s' could not be copied "
u"to '%s': %s" %
tuple(safe_unicode(s)
for s in
(src, dst, exception)))
if remove:
try:
src_listdir = os.listdir(self.tempdir)
except Exception, exception:
safe_print(u"Error - directory '%s' listing failed: %s" %
tuple(safe_unicode(s) for s in (self.tempdir,
exception)))
else:
for basename in src_listdir:
name, ext = os.path.splitext(basename)
if ext_filter is None or ext.lower() not in ext_filter:
src = os.path.join(self.tempdir, basename)
isdir = os.path.isdir(src)
if isdir:
if verbose >= 2:
safe_print(appname + ": Removing temporary "
"directory tree", src)
try:
shutil.rmtree(src, True)
except Exception, exception:
safe_print(u"Warning - temporary directory "
u"'%s' could not be removed: %s" %
tuple(safe_unicode(s) for s in
(src, exception)))
else:
if verbose >= 2:
safe_print(appname +
": Removing temporary file",
src)
try:
os.remove(src)
except Exception, exception:
safe_print(u"Warning - temporary file "
u"'%s' could not be removed: %s" %
tuple(safe_unicode(s) for s in
(src, exception)))
try:
src_listdir = os.listdir(self.tempdir)
except Exception, exception:
safe_print(u"Error - directory '%s' listing failed: %s" %
tuple(safe_unicode(s) for s in (self.tempdir,
exception)))
else:
if not src_listdir:
if verbose >= 2:
safe_print(appname +
": Removing empty temporary directory",
self.tempdir)
try:
shutil.rmtree(self.tempdir, True)
except Exception, exception:
safe_print(u"Warning - temporary directory '%s' could "
u"not be removed: %s" %
tuple(safe_unicode(s) for s in
(self.tempdir, exception)))
if isinstance(result, Exception):
result = Error(safe_unicode(result) + "\n\n" +
lang.getstr("tempdir_should_still_contain_files",
self.tempdir))
return result
def write(self, txt):
wx.CallAfter(self.parse, txt)
|