This file is indexed.

/usr/lib/python2.7/dist-packages/paraview/servermanager.py is in paraview-python 4.0.1-1ubuntu1.

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
r"""servermanager is a module for using paraview server manager in Python.
One can always use the server manager API directly. However, this module
provides an interface easier to use from Python by wrapping several VTK
classes around Python classes.

Note that, upon load, this module will create several sub-modules: sources,
filters and rendering. These modules can be used to instantiate specific
proxy types. For a list, try "dir(servermanager.sources)"

Usually users should use the paraview.simple module instead as it provide a
more user friendly API.

A simple example::

  from paraview.servermanager import *

  # Creates a new built-in session and makes it the active session.
  Connect()

  # Creates a new render view on the active session.
  renModule = CreateRenderView()

  # Create a new sphere proxy on the active session and register it
  # in the sources group.
  sphere = sources.SphereSource(registrationGroup="sources", ThetaResolution=16, PhiResolution=32)

  # Create a representation for the sphere proxy and adds it to the render
  # module.
  display = CreateRepresentation(sphere, renModule)

  renModule.StillRender()

"""
#==============================================================================
#
#  Program:   ParaView
#  Module:    servermanager.py
#
#  Copyright (c) Kitware, Inc.
#  All rights reserved.
#  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
#
#     This software is distributed WITHOUT ANY WARRANTY without even
#     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#     PURPOSE.  See the above copyright notice for more information.
#
#==============================================================================
import paraview, re, os, os.path, new, sys, atexit, vtk

if not paraview.compatibility.major:
    paraview.compatibility.major = 3
if not paraview.compatibility.minor:
    paraview.compatibility.minor = 5

from vtkPVServerImplementationCorePython import *
from vtkPVClientServerCoreCorePython import *
from vtkPVServerManagerCorePython import *

try:
  from vtkPVServerManagerDefaultPython import *
except:
  paraview.print_error("Error: Cannot import vtkPVServerManagerDefaultPython")
try:
  from vtkPVServerManagerRenderingPython import *
except:
  paraview.print_error("Error: Cannot import vtkPVServerManagerRenderingPython")
try:
  from vtkPVServerManagerApplicationPython import *
except:
  paraview.print_error("Error: Cannot import vtkPVServerManagerApplicationPython")
from vtkPVCommonPython import *

def _wrap_property(proxy, smproperty):
    """ Internal function.
    Given a server manager property and its domains, returns the
    appropriate python object.
    """
    property = None
    if paraview.compatibility.GetVersion() >= 3.5 and \
      smproperty.IsA("vtkSMStringVectorProperty"):
        al = smproperty.GetDomain("array_list")
        if  al and al.IsA("vtkSMArraySelectionDomain") and \
            smproperty.GetRepeatable():
            property = ArrayListProperty(proxy, smproperty)
        elif al and al.IsA("vtkSMArrayListDomain") and smproperty.GetNumberOfElements() == 5:
            property = ArraySelectionProperty(proxy, smproperty)
        else:
            iter = smproperty.NewDomainIterator()
            isFileName = False
            while not iter.IsAtEnd():
                # Refer to BUG #9710 to see why optional domains need to be
                # ignored.
                if iter.GetDomain().IsA("vtkSMFileListDomain") and \
                  iter.GetDomain().GetIsOptional() == 0 :
                    isFileName = True
                    break
                iter.Next()
            iter.UnRegister(None)
            if isFileName:
                property = FileNameProperty(proxy, smproperty)
            elif _make_name_valid(smproperty.GetXMLLabel()) == 'ColorArrayName':
                property = ColorArrayProperty(proxy, smproperty)
            else:
                property = VectorProperty(proxy, smproperty)
    elif smproperty.IsA("vtkSMVectorProperty"):
        if smproperty.IsA("vtkSMIntVectorProperty") and \
          smproperty.GetDomain("enum"):
            property = EnumerationProperty(proxy, smproperty)
        else:
            property = VectorProperty(proxy, smproperty)
    elif smproperty.IsA("vtkSMInputProperty"):
        property = InputProperty(proxy, smproperty)
    elif smproperty.IsA("vtkSMProxyProperty"):
        property = ProxyProperty(proxy, smproperty)
    elif smproperty.IsA("vtkSMDoubleMapProperty"):
	property = DoubleMapProperty(proxy, smproperty)
    else:
        property = Property(proxy, smproperty)
    return property

class Proxy(object):
    """Proxy for a server side object. A proxy manages the lifetime of
    one or more server manager objects. It also provides an interface
    to set and get the properties of the server side objects. These
    properties are presented as Python properties. For example,
    you can set a property Foo using the following::

       proxy.Foo = (1,2)

    or

       proxy.Foo.SetData((1,2))

    or

       proxy.Foo[0:2] = (1,2)

    For more information, see the documentation of the property which
    you can obtain with
    help(proxy.Foo).

    This class also provides an iterator which can be used to iterate
    over all properties.
    eg::

        proxy = Proxy(proxy=smproxy)
        for property in proxy:
            print property


    For advanced users:
    This is a python class that wraps a vtkSMProxy.. Makes it easier to
    set/get properties.
    Instead of::

        proxy.GetProperty("Foo").SetElement(0, 1)
        proxy.GetProperty("Foo").SetElement(0, 2)

    you can do::

        proxy.Foo = (1,2)

    or

        proxy.Foo.SetData((1,2))

    or

        proxy.Foo[0:2] = (1,2)

    Instead of::

        proxy.GetProperty("Foo").GetElement(0)

    you can do::

        proxy.Foo.GetData()[0]

    or

        proxy.Foo[0]

    For proxy properties, you can use append:
     proxy.GetProperty("Bar").AddProxy(foo)
    you can do:
     proxy.Bar.append(foo)
    Properties support most of the list API. See VectorProperty and
    ProxyProperty documentation for details.

    Please note that some of the methods accessible through the Proxy
    class are not listed by help() because the Proxy objects forward
    unresolved attributes to the underlying object. To get the full list,
    see also dir(proxy.SMProxy). See also the doxygen based documentation
    of the vtkSMProxy C++ class.
    """

    def __init__(self, **args):
        """ Default constructor. It can be used to initialize properties
        by passing keyword arguments where the key is the name of the
        property. In addition registrationGroup and registrationName (optional)
        can be specified (as keyword arguments) to automatically register
        the proxy with the proxy manager. """
        self.add_attribute('Observed', None)
        self.add_attribute('ObserverTag', -1)
        self.add_attribute('_Proxy__Properties', {})
        self.add_attribute('_Proxy__LastAttrName', None)
        self.add_attribute('SMProxy', None)
        self.add_attribute('Port', 0)

        if 'port' in args:
            self.Port = args['port']
            del args['port']

        update = True
        if 'no_update' in args:
            if args['no_update']:
                update = False
            del args['no_update']

        if 'proxy' in args:
            self.InitializeFromProxy(args['proxy'])
            del args['proxy']
        else:
            self.Initialize(None, update)
        if 'registrationGroup' in args:
            registrationGroup = args['registrationGroup']
            del args['registrationGroup']
            registrationName = self.SMProxy.GetGlobalIDAsString()
            if 'registrationName' in args:
                registrationName = args['registrationName']
                del args['registrationName']
            pxm = ProxyManager()
            pxm.RegisterProxy(registrationGroup, registrationName, self.SMProxy)
        if update:
            self.UpdateVTKObjects()
        for key in args.keys():
            setattr(self, key, args[key])
        # Visit all properties so that they are created
        for prop in self:
            pass

    def __setattr__(self, name, value):
        try:
            setter = getattr(self.__class__, name)
            setter = setter.__set__
        except AttributeError:
            if not hasattr(self, name):
                raise AttributeError("Attribute %s does not exist. " % name +
                  " This class does not allow addition of new attributes to avoid " +
                  "mistakes due to typos. Use add_attribute() if you really want " +
                  "to add this attribute.")
            self.__dict__[name] = value
        else:
            setter(self, value)

    def add_attribute(self, name, value):
        self.__dict__[name] = value

    def __del__(self):
        """Destructor. Cleans up all observers as well as remove
        the proxy from the _pyproxies dictionary"""
        # Make sure that we remove observers we added
        if self.Observed:
            observed = self.Observed
            tag = self.ObserverTag
            self.Observed = None
            self.ObserverTag = -1
            observed.RemoveObserver(tag)
        if _pyproxies and self.SMProxy and (self.SMProxy, self.Port) in _pyproxies:
            del _pyproxies[(self.SMProxy, self.Port)]

    def InitializeFromProxy(self, aProxy, update=True):
        """Constructor. Assigns proxy to self.SMProxy, updates the server
        object as well as register the proxy in _pyproxies dictionary."""
        import weakref
        self.SMProxy = aProxy
        if update:
            self.SMProxy.UpdateVTKObjects()
        _pyproxies[(self.SMProxy, self.Port)] = weakref.ref(self)

    def Initialize(self):
        "Overridden by the subclass created automatically"
        pass

    def __eq__(self, other):
        "Returns true if the underlying SMProxies are the same."
        if isinstance(other, Proxy):
            try:
                if self.Port != other.Port:
                    return False
            except:
                pass
            return self.SMProxy == other.SMProxy
        return self.SMProxy == other

    def __ne__(self, other):
        "Returns false if the underlying SMProxies are the same."
        return not self.__eq__(other)

    def __iter__(self):
        "Creates an iterator for the properties."
        return PropertyIterator(self)

    def SetPropertyWithName(self, pname, arg):
        """Generic method for setting the value of a property."""
        prop = self.GetProperty(pname)
        if prop is None:
            raise RuntimeError, "Property %s does not exist. Please check the property name for typos." % pname
        prop.SetData(arg)

    def GetPropertyValue(self, name):
        """Returns a scalar for properties with 1 elements, the property
        itself for vectors."""
        p = self.GetProperty(name)
        if isinstance(p, VectorProperty):
            if p.GetNumberOfElements() == 1 and not p.GetRepeatable():
                if p.SMProperty.IsA("vtkSMStringVectorProperty") or not p.GetArgumentIsArray():
                    return p[0]
        elif isinstance(p, InputProperty):
            if not p.GetMultipleInput():
                if len(p) > 0:
                    return p[0]
                else:
                    return None
        elif isinstance(p, ProxyProperty):
            if not p.GetRepeatable():
                if len(p) > 0:
                    return p[0]
                else:
                    return None
        return p

    def GetProperty(self, name):
        """Given a property name, returns the property object."""
        if name in self.__Properties and self.__Properties[name]():
            return self.__Properties[name]()
        smproperty = self.SMProxy.GetProperty(name)
        # Maybe they are looking by the label. Try to match that.
        if not smproperty:
            iter = PropertyIterator(self)
            for prop in iter:
                if name == _make_name_valid(iter.PropertyLabel):
                    smproperty = prop.SMProperty
                    break
        if smproperty:
            property = _wrap_property(self, smproperty)
            if property is not None:
                import weakref
                self.__Properties[name] = weakref.ref(property)
            return property
        return None

    def ListProperties(self):
        """Returns a list of all property names on this proxy."""
        property_list = []
        iter = self.__iter__()
        for property in iter:
            name = _make_name_valid(iter.PropertyLabel)
            if name:
                property_list.append(name)
        return property_list

    def __ConvertArgumentsAndCall(self, *args):
        """ Internal function.
        Used to call a function on SMProxy. Converts input and
        output values as appropriate.
        """
        newArgs = []
        for arg in args:
            if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
                newArgs.append(arg.SMProxy)
            else:
                newArgs.append(arg)
        func = getattr(self.SMProxy, self.__LastAttrName)
        retVal = func(*newArgs)
        if type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProxy"):
            return _getPyProxy(retVal)
        elif type(retVal) is type(self.SMProxy) and retVal.IsA("vtkSMProperty"):
            return _wrap_property(self, retVal)
        else:
            return retVal

    def __GetActiveCamera(self):
        """ This method handles GetActiveCamera specially. It adds
        an observer to the camera such that everytime it is modified
        the render view updated"""
        import weakref
        c = self.SMProxy.GetActiveCamera()
        if not c.HasObserver("ModifiedEvent"):
            self.ObserverTag =c.AddObserver("ModifiedEvent", \
                              _makeUpdateCameraMethod(weakref.ref(self)))
            self.Observed = c
        return c

    def __getattr__(self, name):
        """With the exception of a few overloaded methods,
        returns the SMProxy method"""
        if not self.SMProxy:
            raise AttributeError("class %s has no attribute %s" % ("None", name))
            return None
        # Handle GetActiveCamera specially.
        if name == "GetActiveCamera" and \
           hasattr(self.SMProxy, "GetActiveCamera"):
            return self.__GetActiveCamera
        if name == "SaveDefinition" and hasattr(self.SMProxy, "SaveDefinition"):
            return self.__SaveDefinition
        # If not a property, see if SMProxy has the method
        try:
            proxyAttr = getattr(self.SMProxy, name)
            self.__LastAttrName = name
            return self.__ConvertArgumentsAndCall
        except:
            pass
        return getattr(self.SMProxy, name)

class SourceProxy(Proxy):
    """Proxy for a source object. This class adds a few methods to Proxy
    that are specific to sources. It also provides access to the output
    ports. Output ports can be accessed by name or index:
    > op = source[0]
    or
    > op = source['some name'].
    """
    def UpdatePipeline(self, time=None):
        """This method updates the server-side VTK pipeline and the associated
        data information. Make sure to update a source to validate the output
        meta-data."""
        if time != None:
            self.SMProxy.UpdatePipeline(time)
        else:
            self.SMProxy.UpdatePipeline()
        # This is here to cause a receive
        # on the client side so that progress works properly.
        if ActiveConnection and ActiveConnection.IsRemote():
            self.SMProxy.GetDataInformation()

    def FileNameChanged(self):
        "Called when the filename of a source proxy is changed."
        self.UpdatePipelineInformation()

    def UpdatePipelineInformation(self):
        """This method updates the meta-data of the server-side VTK pipeline and
        the associated information properties"""
        self.SMProxy.UpdatePipelineInformation()

    def GetDataInformation(self, idx=None):
        """This method returns a DataInformation wrapper around a
        vtkPVDataInformation"""
        if idx == None:
            idx = self.Port
        if self.SMProxy:
            return DataInformation( \
                self.SMProxy.GetDataInformation(idx), \
                self.SMProxy, idx)

    def __getitem__(self, idx):
        """Given a slice, int or string, returns the corresponding
        output port"""
        if isinstance(idx, slice):
            indices = idx.indices(self.SMProxy.GetNumberOfOutputPorts())
            retVal = []
            for i in range(*indices):
                retVal.append(OutputPort(self, i))
            return retVal
        elif isinstance(idx, int):
            if idx >= self.SMProxy.GetNumberOfOutputPorts() or idx < 0:
                raise IndexError
            return OutputPort(self, idx)
        else:
            return OutputPort(self, self.SMProxy.GetOutputPortIndex(idx))

    def GetPointDataInformation(self):
        """Returns the associated point data information."""
        self.UpdatePipeline()
        return FieldDataInformation(self.SMProxy, self.Port, "PointData")

    def GetCellDataInformation(self):
        """Returns the associated cell data information."""
        self.UpdatePipeline()
        return FieldDataInformation(self.SMProxy, self.Port, "CellData")

    def GetFieldDataInformation(self):
        """Returns the associated cell data information."""
        self.UpdatePipeline()
        return FieldDataInformation(self.SMProxy, self.Port, "FieldData")

    PointData = property(GetPointDataInformation, None, None, "Returns point data information")
    CellData = property(GetCellDataInformation, None, None, "Returns cell data information")
    FieldData = property(GetFieldDataInformation, None, None, "Returns field data information")


class ExodusIIReaderProxy(SourceProxy):
    """Special class to define convenience functions for array
    selection."""

    if paraview.compatibility.GetVersion() >= 3.5:
        def FileNameChanged(self):
            "Called when the filename changes. Selects all variables."
            SourceProxy.FileNameChanged(self)
            self.SelectAllVariables()

        def SelectAllVariables(self):
            "Select all available variables for reading."
            for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
                'ElementVariables', 'GlobalVariables'):
                f = getattr(self, prop)
                f.SelectAll()

        def DeselectAllVariables(self):
            "Deselects all variables."
            for prop in ('PointVariables', 'EdgeVariables', 'FaceVariables',
                'ElementVariables', 'GlobalVariables'):
                f = getattr(self, prop)
                f.DeselectAll()

class ViewLayoutProxy(Proxy):
    """Special class to define convenience methods for View Layout"""

    def SplitViewHorizontal(self, view, fraction=0.5):
        """Split the cell containing the specified view horizontally.
        If no fraction is specified, the frame is split into equal parts.
        On success returns a positve number that identifying the new cell
        location that can be used to assign view to, or split further.
        Return -1 on failure."""
        location = self.GetViewLocation(view)
        if location == -1:
            raise RuntimeError, "View is not present in this layout."
        if fraction < 0.0 or fraction > 1.0:
            raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
        return self.SMProxy.SplitHorizontal(location, fraction)

    def SplitViewVertical(self, view=None, fraction=0.5):
        """Split the cell containing the specified view horizontally.
        If no view is specified, active view is used.
        If no fraction is specified, the frame is split into equal parts.
        On success returns a positve number that identifying the new cell
        location that can be used to assign view to, or split further.
        Return -1 on failure."""
        location = self.GetViewLocation(view)
        if location == -1:
            raise RuntimeError, "View is not present in this layout."
        if fraction < 0.0 or fraction > 1.0:
            raise RuntimeError, "'fraction' must be in the range [0.0, 1.0]"
        return self.SMProxy.SplitVertical(location, fraction)

    def AssignView(self, location, view):
        """Assign a view at a particular location. Note that the view's position may
        be changed by subsequent Split() calls. Returns true on success."""
        viewproxy = None
        if isinstance(view, Proxy):
            view = view.SMProxy
        return self.SMProxy.AssignView(location, view)

    def GetViewLocation(self, view):
        if isinstance(view, Proxy):
            view = view.SMProxy
        return self.SMProxy.GetViewLocation(view)

class Property(object):
    """Generic property object that provides access to one of the properties of
    a server object. This class does not allow setting/getting any values but
    provides an interface to update a property using __call__. This can be used
    for command properties that correspond to function calls without arguments.
    For example,
    > proxy.Foo()
    would push a Foo property which may cause the proxy to call a Foo method
    on the actual VTK object.

    For advanced users:
    Python wrapper around a vtkSMProperty with a simple interface.
    In addition to all method provided by vtkSMProperty (obtained by
    forwarding unknown attributes requests to the underlying SMProxy),
    Property and sub-class provide a list API.

    Please note that some of the methods accessible through the Property
    class are not listed by help() because the Property objects forward
    unresolved attributes to the underlying object. To get the full list,
    see also dir(proxy.SMProperty). See also the doxygen based documentation
    of the vtkSMProperty C++ class.
    """
    def __init__(self, proxy, smproperty):
        """Default constructor. Stores a reference to the proxy."""
        import weakref
        self.SMProperty = smproperty
        self.Proxy = proxy

    def __repr__(self):
        """Returns a string representation containing property name
        and value"""
        if not type(self) is Property:
            if self.GetData() is not None:
                repr = self.GetData().__repr__()
            else:
                repr = "None"
        else:
            repr = "Property name= "
            name = self.Proxy.GetPropertyName(self.SMProperty)
            if name:
                repr += name
            else:
                repr += "Unknown"

        return repr

    def __call__(self):
        """Forces a property update using InvokeCommand."""
        if type(self) is Property:
            self.Proxy.SMProxy.InvokeCommand(self._FindPropertyName())
        else:
            raise RuntimeError, "Cannot invoke this property"

    def _FindPropertyName(self):
        "Returns the name of this property."
        return self.Proxy.GetPropertyName(self.SMProperty)

    def _UpdateProperty(self):
        "Pushes the value of this property to the server."
        # For now, we are updating all properties. This is due to an
        # issue with the representations. Their VTK objects are not
        # created until Input is set therefore, updating a property
        # has no effect. Updating all properties everytime one is
        # updated has the effect of pushing values set before Input
        # when Input is updated.
        # self.Proxy.SMProxy.UpdateProperty(self._FindPropertyName())
        self.Proxy.SMProxy.UpdateVTKObjects()

    def __getattr__(self, name):
        "Unknown attribute requests get forwarded to SMProperty."
        return getattr(self.SMProperty, name)

    Name = property(_FindPropertyName, None, None,
           "Returns the name for the property")

class GenericIterator(object):
    """Iterator for container type objects"""

    def __init__(self, obj):
        self.Object = obj
        self.index = 0

    def __iter__(self):
        return self

    def next(self):
        if self.index >= len(self.Object):
            raise StopIteration

        idx = self.index
        self.index += 1
        return self.Object[idx]

class VectorProperty(Property):
    """A VectorProperty provides access to one or more values. You can use
    a slice to get one or more property values:
    > val = property[2]
    or
    > vals = property[0:5:2]
    You can use a slice to set one or more property values:
    > property[2] = val
    or
    > property[1:3] = (1,2)
    """
    def ConvertValue(self, value):
        return value

    def __len__(self):
        """Returns the number of elements."""
        return self.SMProperty.GetNumberOfElements()

    def __iter__(self):
        """Implementation of the sequence API"""
        return GenericIterator(self)

    def __setitem__(self, idx, value):
        """Given a list or tuple of values, sets a slice of values [min, max)"""
        if isinstance(idx, slice):
            indices = idx.indices(len(self))
            for i, j in zip(range(*indices), value):
                self.SMProperty.SetElement(i, self.ConvertValue(j))
            self._UpdateProperty()
        elif idx >= len(self) or idx < 0:
            raise IndexError
        else:
            self.SMProperty.SetElement(idx, self.ConvertValue(value))
            self._UpdateProperty()

    def GetElement(self, index):
        return self.SMProperty.GetElement(index)

    def __getitem__(self, idx):
        """Returns the range [min, max) of elements. Raises an IndexError
        exception if an argument is out of bounds."""
        ls = len(self)
        if isinstance(idx, slice):
            indices = idx.indices(ls)
            retVal = []
            for i in range(*indices):
               retVal.append(self.GetElement(i))
            return retVal
        elif idx >= ls:
            raise IndexError
        elif idx < 0:
            idx = ls + idx
            if idx < 0:
                raise IndexError

        return self.GetElement(idx)

    def GetData(self):
        "Returns all elements as either a list or a single value."
        property = self.SMProperty
        if property.GetRepeatable() or \
           property.GetNumberOfElements() > 1:
            return self[0:len(self)]
        elif property.GetNumberOfElements() == 1:
            return self.GetElement(0)

    def SetData(self, values):
        """Allows setting of all values at once. Requires a single value or
        a iterable object."""
        if not hasattr(values, "__iter__"):
            values = (values,)
        if not self.GetRepeatable() and len(values) != self.GetNumberOfElements():
            raise RuntimeError("This property requires %d values." % self.GetNumberOfElements())
        if self.GetRepeatable():
            # Clean up first
            self.SMProperty.SetNumberOfElements(0)
        idx = 0
        for val in values:
            self.SMProperty.SetElement(idx, self.ConvertValue(val))
            idx += 1
        self._UpdateProperty()

    def Clear(self):
        "Removes all elements."
        self.SMProperty().SetNumberOfElements(0)
        self._UpdateProperty()

class DoubleMapProperty(Property):
    """A DoubleMapProperty provides access to a map of double vector values."""

    def __len__(self):
        """Returns the number of elements."""
        return self.SMProperty.GetNumberOfElements()

    def __getitem__(self, key):
        """Returns the values for key."""
        return self.GetData()[key]

    def __setitem__(self, key, values):
        """Sets the values for key."""
        for i, value in enumerate(values):
            self.SMProperty.SetElementComponent(key, i, value)
        self._UpdateProperty()

    def __contains__(self, key):
	"""Returns True if the property contains key."""
        return key in self.keys()

    def keys(self):
	"""Returns the keys."""
        return self.GetData().keys()

    def GetData(self):
	"""Returns all the elements as a dictionary"""

	data = {}

	iter = self.SMProperty.NewIterator()
        while not iter.IsAtEnd():
            values = []
            for i in range(self.SMProperty.GetNumberOfComponents()):
                values.append(iter.GetElementComponent(i))
            data[iter.GetKey()] = values
            iter.Next()
        iter.UnRegister(None)

	return data

    def SetData(self, elements):
        """Sets all the elements at once."""

        # first clear existing data
	self.Clear()

        for key, values in elements.items():
            for i, value in enumerate(values):
                self.SMProperty.SetElementComponent(key, i, value)

        self._UpdateProperty()

    def Clear(self):
        """Removes all elements."""
        self.SMProperty.ClearElements()
        self._UpdateProperty()

class ColorArrayProperty(VectorProperty):
    """This subclass of VectorProperty handles setting of the array to
    color by. It handles attribute type as well as well array name."""

    def GetAvailable(self):
        """Returns the list of available arrays as (attribute type, array name
        tuples."""
        arrays = []
        for a in self.Proxy.Input.PointData:
            arrays.append(('POINT_DATA', a.GetName()))
        for a in self.Proxy.Input.CellData:
            arrays.append(('CELL_DATA', a.GetName()))
        return arrays

    def SetData(self, value):
        """Overwritten to enable setting attribute type (the ColorAttributeType
        property and the array name. The argument should be the array name
        (in which case the first appropriate attribute type is picked) or
        a tuple of attribute type and array name."""
        if isinstance(value, tuple) and len(value) == 2:
            att = value[0]
            arr = value[1]
        elif isinstance(value, str):
            att = None
            arr = value
        else:
            raise ValueError("Expected a tuple of 2 values or a string.")

        if not arr:
            self.SMProperty.SetElement(0, '')
            self._UpdateProperty()
            return

        # if the attribute type was not specified, then we need to determine the
        # attribute type. Thus co-processing scripts and scripts where
        # data may not be up-to-date should always specify the attribute type to
        # overcome this check (BUG #13933).
        if att == None:
            for a in self.Available:
                if a[1] == arr:
                    att = a[0]
                    break

        if att == None:
            pvoptions = vtkProcessModule.GetProcessModule().GetOptions()
            if pvoptions.GetSymmetricMPIMode() == False:
                raise ValueError("Could not locate array %s in the input." % arr)
        else:
            catt = self.Proxy.GetProperty("ColorAttributeType")
            catt.SetData(att)
        self.SMProperty.SetElement(0, arr)
        self._UpdateProperty()

    def __str__(self):
        """Returns a user-friendly representation string."""
        catt = self.Proxy.GetProperty("ColorAttributeType")
        if catt:
            if not type(catt) is Property:
                catt = _wrap_property(self.Proxy, catt)
            return str((catt.GetData(), self.GetData()))
        else:
            return str(self.GetData())

    Available = property(GetAvailable, None, None, \
        "This read-only property returns the list of arrays that can be colored by.")


class EnumerationProperty(VectorProperty):
    """Subclass of VectorProperty that is applicable for enumeration type
    properties."""

    def GetElement(self, index):
        """Returns the text for the given element if available. Returns
        the numerical values otherwise."""
        val = self.SMProperty.GetElement(index)
        domain = self.SMProperty.GetDomain("enum")
        for i in range(domain.GetNumberOfEntries()):
            if domain.GetEntryValue(i) == val:
                return domain.GetEntryText(i)
        return val

    def ConvertValue(self, value):
        """Converts value to type suitable for vtSMProperty::SetElement()"""
        if type(value) == str:
            domain = self.SMProperty.GetDomain("enum")
            if domain.HasEntryText(value):
                return domain.GetEntryValueForText(value)
            else:
                raise ValueError("%s is not a valid value." % value)
        return VectorProperty.ConvertValue(self, value)

    def GetAvailable(self):
        "Returns the list of available values for the property."
        retVal = []
        domain = self.SMProperty.GetDomain("enum")
        for i in range(domain.GetNumberOfEntries()):
            retVal.append(domain.GetEntryText(i))
        return retVal

    Available = property(GetAvailable, None, None, \
        "This read-only property contains the list of values that can be applied to this property.")


class FileNameProperty(VectorProperty):
    """Property to set/get one or more file names.
    This property updates the pipeline information everytime its value changes.
    This is used to keep the array lists up to date."""

    def _UpdateProperty(self):
        "Pushes the value of this property to the server."
        VectorProperty._UpdateProperty(self)
        self.Proxy.FileNameChanged()

class ArraySelectionProperty(VectorProperty):
    "Property to select an array to be processed by a filter."

    def GetAssociation(self):
        val = self.GetElement(3)
        if val == "":
            return None
        for key, value in ASSOCIATIONS.iteritems():
            if value == int(val):
                return key

        return None

    def GetArrayName(self):
        return self.GetElement(4)

    def __len__(self):
        """Returns the number of elements."""
        return 2

    def __setitem__(self, idx, value):
        raise RuntimeError, "This property cannot be accessed using __setitem__"

    def __getitem__(self, idx):
        """Returns attribute type for index 0, array name for index 1"""
        if isinstance(idx, slice):
            indices = idx.indices(len(self))
            retVal = []
            for i in range(*indices):
                if i >= 2 or i < 0:
                    raise IndexError
                if i == 0:
                    retVal.append(self.GetAssociation())
                else:
                    retVal.append(self.GetArrayName())
            return retVal
        elif idx >= 2 or idx < 0:
            raise IndexError

        if i == 0:
            return self.GetAssociation()
        else:
            return self.GetArrayName()

    def SetData(self, values):
        """Allows setting of all values at once. Requires a single value,
        a tuple or list."""
        if not isinstance(values, tuple) and \
           not isinstance(values, list):
            values = (values,)
        if len(values) == 1:
            self.SMProperty.SetElement(4, values[0])
        elif len(values) == 2:
            if isinstance(values[0], str):
                val = str(ASSOCIATIONS[values[0]])
            else:
                # In case user didn't specify valid association,
                # just pick POINTS.
                val = str(ASSOCIATIONS['POINTS'])
            self.SMProperty.SetElement(3,  str(val))
            self.SMProperty.SetElement(4, values[1])
        else:
            raise RuntimeError, "Expected 1 or 2 values."
        self._UpdateProperty()

    def UpdateDefault(self):
        "Helper method to set default values."
        if self.SMProperty.GetNumberOfElements() != 5:
            return
        if self.GetElement(4) != '' or \
            self.GetElement(3) != '':
            return

        for i in range(0,3):
            if self.GetElement(i) == '':
                self.SMProperty.SetElement(i, '0')
        al = self.SMProperty.GetDomain("array_list")
        al.Update(self.SMProperty)
        al.SetDefaultValues(self.SMProperty)

class ArrayListProperty(VectorProperty):
    """This property provides a simpler interface for selecting arrays.
    Simply assign a list of arrays that should be loaded by the reader.
    Use the Available property to get a list of available arrays."""

    def __init__(self, proxy, smproperty):
        VectorProperty.__init__(self, proxy, smproperty)
        self.__arrays = []

    def GetAvailable(self):
        "Returns the list of available arrays"
        dm = self.GetDomain("array_list")
        retVal = []
        for i in range(dm.GetNumberOfStrings()):
            retVal.append(dm.GetString(i))
        return retVal

    Available = property(GetAvailable, None, None, \
        "This read-only property contains the list of items that can be read by a reader.")

    def SelectAll(self):
        "Selects all arrays."
        self.SetData(self.Available)

    def DeselectAll(self):
        "Deselects all arrays."
        self.SetData([])

    def __iter__(self):
        """Implementation of the sequence API"""
        return GenericIterator(self)

    def __len__(self):
        """Returns the number of elements."""
        return len(self.GetData())

    def __setitem__(self, idx, value):
      """Given a list or tuple of values, sets a slice of values [min, max)"""
      self.GetData()
      if isinstance(idx, slice):
          indices = idx.indices(len(self))
          for i, j in zip(range(*indices), value):
              self.__arrays[i] = j
          self.SetData(self.__arrays)
      elif idx >= len(self) or idx < 0:
          raise IndexError
      else:
          self.__arrays[idx] = self.ConvertValue(value)
          self.SetData(self.__arrays)

    def __getitem__(self, idx):
      """Returns the range [min, max) of elements. Raises an IndexError
      exception if an argument is out of bounds."""
      self.GetData()
      if isinstance(idx, slice):
          indices = idx.indices(len(self))
          retVal = []
          for i in range(*indices):
              retVal.append(self.__arrays[i])
          return retVal
      elif idx >= len(self) or idx < 0:
          raise IndexError
      return self.__arrays[idx]

    def SetData(self, values):
        """Allows setting of all values at once. Requires a single value,
        a tuple or list."""
        # Clean up first
        iup = self.SMProperty.GetImmediateUpdate()
        self.SMProperty.SetImmediateUpdate(False)
        # Clean up first
        self.SMProperty.SetNumberOfElements(0)
        if not isinstance(values, tuple) and \
           not isinstance(values, list):
            values = (values,)
        fullvalues = []

        # WARNING:
        # The order of the two loops below are delibrately set in this way
        # so that values passed in will take precedence.
        # This is needed for backward compatibility of the
        # property ElementBlocks for vtkExodusIIReader.
        # If you attemp to change this, please verify that
        # python state files for opening old .ex2 file (<=3.14) still works.
        for array in self.Available:
            if not values.__contains__(array):
                fullvalues.append(array)
                fullvalues.append('0')

        for i in range(len(values)):
            val = self.ConvertValue(values[i])
            fullvalues.append(val)
            fullvalues.append('1')


        i = 0
        for value in fullvalues:
            self.SMProperty.SetElement(i, value)
            i += 1

        self._UpdateProperty()
        self.SMProperty.SetImmediateUpdate(iup)

    def GetData(self):
        "Returns all elements as a list."
        property = self.SMProperty
        nElems = property.GetNumberOfElements()
        if nElems%2 != 0:
            raise ValueError, "The SMProperty with XML label '%s' has a size that is not a multiple of 2." % property.GetXMLLabel()
        self.__arrays = []
        for i in range(0, nElems, 2):
            if self.GetElement(i+1) != '0':
                self.__arrays.append(self.GetElement(i))
        return list(self.__arrays)

class ProxyProperty(Property):
    """A ProxyProperty provides access to one or more proxies. You can use
    a slice to get one or more property values:
    > proxy = property[2]
    or
    > proxies = property[0:5:2]
    You can use a slice to set one or more property values:
    > property[2] = proxy
    or
    > property[1:3] = (proxy1, proxy2)
    You can also append and delete:
    > property.append(proxy)
    and
    > del property[1:2]

    You can also remove all elements with Clear().

    Note that some properties expect only 1 proxy and will complain if
    you set the number of values to be something else.
    """
    def __init__(self, proxy, smproperty):
        """Default constructor.  Stores a reference to the proxy.  Also looks
        at domains to find valid values."""
        Property.__init__(self, proxy, smproperty)
        # Check to see if there is a proxy list domain and, if so,
        # initialize ourself. (Should this go in ProxyProperty?)
        listdomain = self.GetDomain('proxy_list')
        if listdomain:
            if listdomain.GetClassName() != 'vtkSMProxyListDomain':
                raise ValueError, "Found a 'proxy_list' domain on an InputProperty that is not a ProxyListDomain."
            pm = ProxyManager()
            group = "pq_helper_proxies." + proxy.GetGlobalIDAsString()
            if listdomain.GetNumberOfProxies() == 0:
                for i in xrange(listdomain.GetNumberOfProxyTypes()):
                    igroup = listdomain.GetProxyGroup(i)
                    name = listdomain.GetProxyName(i)
                    iproxy = CreateProxy(igroup, name)
                    listdomain.AddProxy(iproxy)
                    pm.RegisterProxy(group, proxy.GetPropertyName(smproperty), iproxy)
                listdomain.SetDefaultValues(self.SMProperty)

    def GetAvailable(self):
        """If this proxy has a list domain, then this function returns the
        strings you can use to select from the domain.  If there is no such
        list domain, the returned list is empty."""
        listdomain = self.GetDomain('proxy_list')
        retval = []
        if listdomain:
            for i in xrange(listdomain.GetNumberOfProxies()):
                proxy = listdomain.GetProxy(i)
                retval.append(proxy.GetXMLLabel())
        return retval

    Available = property(GetAvailable, None, None,
                         """This read only property is a list of strings you can
                         use to select from the list domain.  If there is no
                         such list domain, the array is empty.""")

    def __iter__(self):
        """Implementation of the sequence API"""
        return GenericIterator(self)

    def __len__(self):
        """Returns the number of elements."""
        return self.SMProperty.GetNumberOfProxies()

    def remove(self, proxy):
        """Removes the first occurence of the proxy from the property."""
        self.SMProperty.RemoveProxy(proxy.SMProxy)
        self._UpdateProperty()

    def __setitem__(self, idx, value):
      """Given a list or tuple of values, sets a slice of values [min, max)"""
      if isinstance(idx, slice):
        indices = idx.indices(len(self))
        for i, j in zip(range(*indices), value):
          self.SMProperty.SetProxy(i, j.SMProxy)
        self._UpdateProperty()
      elif idx >= len(self) or idx < 0:
        raise IndexError
      else:
        self.SMProperty.SetProxy(idx, value.SMProxy)
        self._UpdateProperty()

    def __delitem__(self,idx):
      """Removes the element idx"""
      if isinstance(idx, slice):
        indices = idx.indices(len(self))
        # Collect the elements to delete to a new list first.
        # Otherwise indices are screwed up during the actual
        # remove loop.
        toremove = []
        for i in range(*indices):
          toremove.append(self[i])
        for i in toremove:
          self.SMProperty.RemoveProxy(i.SMProxy)
        self._UpdateProperty()
      elif idx >= len(self) or idx < 0:
        raise IndexError
      else:
        self.SMProperty.RemoveProxy(self[idx].SMProxy)
        self._UpdateProperty()

    def __getitem__(self, idx):
      """Returns the range [min, max) of elements. Raises an IndexError
      exception if an argument is out of bounds."""
      if isinstance(idx, slice):
        indices = idx.indices(len(self))
        retVal = []
        for i in range(*indices):
          retVal.append(_getPyProxy(self.SMProperty.GetProxy(i)))
        return retVal
      elif idx >= len(self) or idx < 0:
        raise IndexError
      return _getPyProxy(self.SMProperty.GetProxy(idx))

    def __getattr__(self, name):
        "Unknown attribute requests get forwarded to SMProperty."
        return getattr(self.SMProperty, name)

    def index(self, proxy):
        idx = 0
        for px in self:
            if proxy == px:
                return idx
            idx += 1
        raise ValueError("proxy is not in the list.")

    def append(self, proxy):
        "Appends the given proxy to the property values."
        self.SMProperty.AddProxy(proxy.SMProxy)
        self._UpdateProperty()

    def GetData(self):
        "Returns all elements as either a list or a single value."
        property = self.SMProperty
        if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
            return self[0:len(self)]
        else:
            if property.GetNumberOfProxies() > 0:
                return _getPyProxy(property.GetProxy(0))
        return None

    def SetData(self, values):
        """Allows setting of all values at once. Requires a single value,
        a tuple or list."""
        if isinstance(values, str):
            position = -1
            try:
                position = self.Available.index(values)
            except:
                raise ValueError, values + " is not a valid object in the domain."
            values = self.GetDomain('proxy_list').GetProxy(position)
        if not isinstance(values, tuple) and \
           not isinstance(values, list):
            values = (values,)
        self.SMProperty.RemoveAllProxies()
        for value in values:
            if isinstance(value, Proxy):
                value_proxy = value.SMProxy
            else:
                value_proxy = value
            self.SMProperty.AddProxy(value_proxy)
        self._UpdateProperty()

    def Clear(self):
        "Removes all elements."
        self.SMProperty.RemoveAllProxies()
        self._UpdateProperty()

class InputProperty(ProxyProperty):
    """An InputProperty allows making pipeline connections. You can set either
    a source proxy or an OutputProperty to an input property:

    > property[0] = proxy
    or
    > property[0] = OuputPort(proxy, 1)

    > property.append(proxy)
    or
    > property.append(OutputPort(proxy, 0))
    """
    def __setitem__(self, idx, value):
      """Given a list or tuple of values, sets a slice of values [min, max)"""
      if isinstance(idx, slice):
        indices = idx.indices(len(self))
        for i, j in zip(range(*indices), value):
          op = value[i-min]
          self.SMProperty.SetInputConnection(i, op.SMProxy, op.Port)
        self._UpdateProperty()
      elif idx >= len(self) or idx < 0:
        raise IndexError
      else:
        self.SMProperty.SetInputConnection(idx, value.SMProxy, value.Port)
        self._UpdateProperty()

    def __getitem__(self, idx):
      """Returns the range [min, max) of elements. Raises an IndexError
      exception if an argument is out of bounds."""
      if isinstance(idx, slice):
        indices = idx.indices(len(self))
        retVal = []
        for i in range(*indices):
            port = None
            if self.SMProperty.GetProxy(i):
                port = OutputPort(_getPyProxy(self.SMProperty.GetProxy(i)),\
                                  self.SMProperty.GetOutputPortForConnection(i))
            retVal.append(port)
        return retVal
      elif idx >= len(self) or idx < 0:
        raise IndexError
      return OutputPort(_getPyProxy(self.SMProperty.GetProxy(idx)),\
                        self.SMProperty.GetOutputPortForConnection(idx))

    def append(self, value):
        """Appends the given proxy to the property values.
        Accepts Proxy or OutputPort objects."""
        self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
        self._UpdateProperty()

    def GetData(self):
        """Returns all elements as either a list of OutputPort objects or
        a single OutputPort object."""
        property = self.SMProperty
        if property.GetRepeatable() or property.GetNumberOfProxies() > 1:
            return self[0:len(self)]
        else:
            if property.GetNumberOfProxies() > 0:
                return OutputPort(_getPyProxy(property.GetProxy(0)),\
                                  self.SMProperty.GetOutputPortForConnection(0))
        return None

    def SetData(self, values):
        """Allows setting of all values at once. Requires a single value,
        a tuple or list. Accepts Proxy or OutputPort objects."""
        if isinstance(values, str):
            ProxyProperty.SetData(self, values)
            return
        if not isinstance(values, tuple) and \
           not isinstance(values, list):
            values = (values,)
        self.SMProperty.RemoveAllProxies()
        for value in values:
            if value:
                self.SMProperty.AddInputConnection(value.SMProxy, value.Port)
        self._UpdateProperty()

    def _UpdateProperty(self):
        "Pushes the value of this property to the server."
        ProxyProperty._UpdateProperty(self)
        iter = PropertyIterator(self.Proxy)
        for prop in iter:
            if isinstance(prop, ArraySelectionProperty):
                prop.UpdateDefault()


class DataInformation(object):
    """DataInformation is a contained for meta-data associated with an
    output data.

    DataInformation is a python wrapper around a vtkPVDataInformation.
    In addition to proving all methods of a vtkPVDataInformation, it provides
    a few convenience methods.

    Please note that some of the methods accessible through the DataInformation
    class are not listed by help() because the DataInformation objects forward
    unresolved attributes to the underlying object. To get the full list,
    see also dir(proxy.DataInformation).
    See also the doxygen based documentation of the vtkPVDataInformation C++
    class.
    """
    def __init__(self, dataInformation, proxy, idx):
        """Default constructor. Requires a vtkPVDataInformation, a source proxy
        and an output port id."""
        self.DataInformation = dataInformation
        self.Proxy = proxy
        self.Idx = idx

    def Update(self):
        """****Deprecated**** There is no reason anymore to use this method
        explicitly, it is called automatically when one gets any value from the
        data information object.
        Update the data information if necessary. Note that this
        does not cause execution of the underlying object. In certain
        cases, you may have to call UpdatePipeline() on the proxy."""
        if self.Proxy:
            self.Proxy.GetDataInformation(self.Idx)

    def GetDataSetType(self):
        """Returns the dataset type as defined in vtkDataObjectTypes."""
        self.Update()
        if not self.DataInformation:
            raise RuntimeError, "No data information is available"
        if self.DataInformation.GetCompositeDataSetType() > -1:
            return self.DataInformation.GetCompositeDataSetType()
        return self.DataInformation.GetDataSetType()

    def GetDataSetTypeAsString(self):
        """Returns the dataset type as a user-friendly string. This is
        not the same as the enumaration used by VTK"""
        return vtk.vtkDataObjectTypes.GetClassNameFromTypeId(self.GetDataSetType())

    def __getattr__(self, name):
        """Forwards unknown attribute requests to the underlying
        vtkPVInformation."""
        if not self.DataInformation:
            raise AttributeError("class has no attribute %s" % name)
            return None
        self.Update()
        return getattr(self.DataInformation, name)

class ArrayInformation(object):
    """Meta-information associated with an array. Use the Name
    attribute to get the array name.

    Please note that some of the methods accessible through the ArrayInformation
    class are not listed by help() because the ArrayInformation objects forward
    unresolved attributes to the underlying object.
    See the doxygen based documentation of the vtkPVArrayInformation C++
    class for a full list.
    """
    def __init__(self, proxy, field, name):
        self.Proxy = proxy
        self.FieldData = field
        self.Name = name

    def __getattr__(self, name):
        """Forward unknown methods to vtkPVArrayInformation"""
        array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
        if not array: return None
        return getattr(array, name)

    def __repr__(self):
        """Returns a user-friendly representation string."""
        return "Array: " + self.Name

    def GetRange(self, component=0):
        """Given a component, returns its value range as a tuple of 2 values."""
        array = self.FieldData.GetFieldData().GetArrayInformation(self.Name)
        range = array.GetComponentRange(component)
        return (range[0], range[1])

    if paraview.compatibility.GetVersion() <= 3.4:
       def Range(self, component=0):
           return self.GetRange(component)

class FieldDataInformationIterator(object):
    """Iterator for FieldDataInformation"""

    def __init__(self, info, items=False):
        self.FieldDataInformation = info
        self.index = 0
        self.items = items

    def __iter__(self):
        return self

    def next(self):
        if self.index >= self.FieldDataInformation.GetNumberOfArrays():
            raise StopIteration

        self.index += 1
        ai = self.FieldDataInformation[self.index-1]
        if self.items:
            return (ai.GetName(), ai)
        else:
            return ai


class FieldDataInformation(object):
    """Meta-data for a field of an output object (point data, cell data etc...).
    Provides easy access to the arrays using the slice interface:
    > narrays = len(field_info)
    > for i in range(narrays):
    >   array_info = field_info[i]

    Full slice interface is supported:
    > arrays = field_info[0:5:3]
    where arrays is a list.

    Array access by name is also possible:
    > array_info = field_info['Temperature']

    The number of arrays can also be accessed using the NumberOfArrays
    property.
    """
    def __init__(self, proxy, idx, field):
        self.Proxy = proxy
        self.OutputPort = idx
        self.FieldData = field

    def GetFieldData(self):
        """Convenience method to get the underlying
        vtkPVDataSetAttributesInformation"""
        return getattr(self.Proxy.GetDataInformation(self.OutputPort), "Get%sInformation" % self.FieldData)()

    def GetNumberOfArrays(self):
        """Returns the number of arrays."""
        self.Proxy.UpdatePipeline()
        return self.GetFieldData().GetNumberOfArrays()

    def GetArray(self, idx):
        """Given an index or a string, returns an array information.
        Raises IndexError if the index is out of bounds."""
        self.Proxy.UpdatePipeline()
        if not self.GetFieldData().GetArrayInformation(idx):
            return None
        if isinstance(idx, str):
            return ArrayInformation(self.Proxy, self, idx)
        elif idx >= len(self) or idx < 0:
            raise IndexError
        return ArrayInformation(self.Proxy, self, self.GetFieldData().GetArrayInformation(idx).GetName())

    def __len__(self):
        """Returns the number of arrays."""
        return self.GetNumberOfArrays()

    def __getitem__(self, idx):
        """Implements the [] operator. Accepts an array name."""
        if isinstance(idx, slice):
            indices = idx.indices(self.GetNumberOfArrays())
            retVal = []
            for i in range(*indices):
                retVal.append(self.GetArray(i))
            return retVal
        return self.GetArray(idx)

    def keys(self):
        """Implementation of the dictionary API"""
        kys = []
        narrays = self.GetNumberOfArrays()
        for i in range(narrays):
            kys.append(self.GetArray(i).GetName())
        return kys

    def values(self):
        """Implementation of the dictionary API"""
        vals = []
        narrays = self.GetNumberOfArrays()
        for i in range(narrays):
            vals.append(self.GetArray(i))
        return vals

    def iteritems(self):
        """Implementation of the dictionary API"""
        return FieldDataInformationIterator(self, True)

    def items(self):
        """Implementation of the dictionary API"""
        itms = []
        narrays = self.GetNumberOfArrays()
        for i in range(narrays):
            ai = self.GetArray(i)
            itms.append((ai.GetName(), ai))
        return itms

    def has_key(self, key):
        """Implementation of the dictionary API"""
        if self.GetArray(key):
            return True
        return False

    def __iter__(self):
        """Implementation of the dictionary API"""
        return FieldDataInformationIterator(self)

    def __getattr__(self, name):
        """Forwards unknown attributes to the underlying
        vtkPVDataSetAttributesInformation"""
        array = self.GetArray(name)
        if array: return array
        raise AttributeError("class has no attribute %s" % name)
        return None

    NumberOfArrays = property(GetNumberOfArrays, None, None, "Returns the number of arrays.")

def OutputPort(proxy, outputPort=0):
    if not Proxy:
        return None
    if isinstance(outputPort, str):
        outputPort = proxy.GetOutputPortIndex(outputPort)
    if outputPort >= proxy.GetNumberOfOutputPorts():
        return None
    if proxy.Port == outputPort:
        return proxy
    newinstance = _getPyProxy(proxy.SMProxy, outputPort)
    newinstance.Port = outputPort
    newinstance._Proxy__Properties = proxy._Proxy__Properties
    return newinstance

class ProxyManager(object):
    """When running scripts from the python shell in the ParaView application,
    registering proxies with the proxy manager is the only mechanism to
    notify the graphical user interface (GUI) that a proxy
    exists. Therefore, unless a proxy is registered, it will not show up in
    the user interface. Also, the proxy manager is the only way to get
    access to proxies created using the GUI. Proxies created using the GUI
    are automatically registered under an appropriate group (sources,
    filters, representations and views). To get access to these objects,
    you can use proxyManager.GetProxy(group, name). The name is the same
    as the name shown in the pipeline browser.

    This class is a python wrapper for vtkSMProxyManager. Note that the
    underlying vtkSMProxyManager is a singleton. All instances of this
    class will refer to the same object. In addition to all methods provided by
    vtkSMProxyManager (all unknown attribute requests are forwarded
    to the vtkSMProxyManager), this class provides several convenience
    methods.

    Please note that some of the methods accessible through the ProxyManager
    class are not listed by help() because the ProxyManager objects forwards
    unresolved attributes to the underlying object. To get the full list,
    see also dir(proxy.SMProxyManager). See also the doxygen based documentation
    of the vtkSMProxyManager C++ class.
    """

    def __init__(self, session=None):
        """Constructor. Assigned self.SMProxyManager to
        vtkSMProxyManager.GetProxyManager()."""
        global ActiveConnection
        if not session:
            session = ActiveConnection.Session
        self.SMProxyManager = session.GetSessionProxyManager()

    def RegisterProxy(self, group, name, aProxy):
        """Registers a proxy (either SMProxy or proxy) with the
        server manager"""
        if isinstance(aProxy, Proxy):
            self.SMProxyManager.RegisterProxy(group, name, aProxy.SMProxy)
        else:
            self.SMProxyManager.RegisterProxy(group, name, aProxy)

    def NewProxy(self, group, name):
        """Creates a new proxy of given group and name and returns an SMProxy.
        Note that this is a server manager object. You should normally create
        proxies using the class objects. For example:
        obj = servermanager.sources.SphereSource()"""
        if not self.SMProxyManager:
            return None
        aProxy = self.SMProxyManager.NewProxy(group, name)
        if not aProxy:
            return None
        aProxy.UnRegister(None)
        return aProxy

    def GetProxy(self, group, name):
        """Returns a Proxy registered under a group and name"""
        if not self.SMProxyManager:
            return None
        aProxy = self.SMProxyManager.GetProxy(group, name)
        if not aProxy:
            return None
        return _getPyProxy(aProxy)

    def GetPrototypeProxy(self, group, name):
        """Returns a prototype proxy given a group and name. This is an
        SMProxy. This is a low-level method. You should not normally
        have to call it."""
        if not self.SMProxyManager:
            return None
        aProxy = self.SMProxyManager.GetPrototypeProxy(group, name)
        if not aProxy:
            return None
        return aProxy

    def GetProxiesInGroup(self, groupname):
        """Returns a map of proxies in a particular group."""
        proxies = {}
        iter = self.NewGroupIterator(groupname)
        for aProxy in iter:
            proxies[(iter.GetKey(), aProxy.GetGlobalIDAsString())] = aProxy
        return proxies

    def UnRegisterProxy(self, groupname, proxyname, aProxy):
        """Unregisters a proxy."""
        if not self.SMProxyManager:
            return
        if aProxy != None and isinstance(aProxy,Proxy):
            aProxy = aProxy.SMProxy
        if aProxy:
            self.SMProxyManager.UnRegisterProxy(groupname, proxyname, aProxy)

    def GetProxies(self, groupname, proxyname):
        """Returns all proxies registered under the given group with the
        given name. Note that it is possible to register more than one
        proxy with the same name in the same group. Because the proxies
        are different, there is no conflict. Use this method instead of
        GetProxy() if you know that there are more than one proxy registered
        with this name."""
        if not self.SMProxyManager:
            return []
        collection = vtk.vtkCollection()
        result = []
        self.SMProxyManager.GetProxies(groupname, proxyname, collection)
        for i in range(0, collection.GetNumberOfItems()):
            aProxy = _getPyProxy(collection.GetItemAsObject(i))
            if aProxy:
                result.append(aProxy)

        return result

    def __iter__(self):
        """Returns a new ProxyIterator."""
        iter = ProxyIterator()
        iter.Begin()
        return iter

    def NewGroupIterator(self, group_name):
        """Returns a ProxyIterator for a group. The resulting object
        can be used to traverse the proxies that are in the given
        group."""
        iter = self.__iter__()
        iter.SetModeToOneGroup()
        iter.Begin(group_name)
        return iter

    def NewDefinitionIterator(self, groupname=None):
        """Returns an iterator that can be used to iterate over
           all groups and types of proxies that the proxy manager
           can create."""
        iter = None
        if groupname != None:
            iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewSingleGroupIterator(groupname,0))
        else:
            iter = ProxyDefinitionIterator(self.GetProxyDefinitionManager().NewIterator(0))

        return iter

    def __ConvertArgumentsAndCall(self, *args):
      newArgs = []
      for arg in args:
          if issubclass(type(arg), Proxy) or isinstance(arg, Proxy):
              newArgs.append(arg.SMProxy)
          else:
              newArgs.append(arg)
      func = getattr(self.SMProxyManager, self.__LastAttrName)
      retVal = func(*newArgs)
      if type(retVal) is type(self.SMProxyManager) and retVal.IsA("vtkSMProxy"):
          return _getPyProxy(retVal)
      else:
          return retVal

    def __getattr__(self, name):
        """Returns attribute from the ProxyManager"""
        try:
            pmAttr = getattr(self.SMProxyManager, name)
            self.__LastAttrName = name
            return self.__ConvertArgumentsAndCall
        except:
            pass
        return getattr(self.SMProxyManager, name)

    def LoadState(self, filename, loader = None):
        self.SMProxyManager.LoadXMLState(filename, loader)

    def SaveState(self, filename):
        self.SMProxyManager.SaveXMLState(filename)

class PropertyIterator(object):
    """Wrapper for a vtkSMPropertyIterator class to satisfy
       the python iterator protocol. Note that the list of
       properties can also be obtained from the class object's
       dictionary.
       See the doxygen documentation for vtkSMPropertyIterator C++
       class for details.
       """

    def __init__(self, aProxy):
        self.SMIterator = aProxy.NewPropertyIterator()
        if self.SMIterator:
            self.SMIterator.UnRegister(None)
            self.SMIterator.Begin()
        self.Key = None
        self.PropertyLabel = None
        self.Proxy = aProxy

    def __iter__(self):
        return self

    def next(self):
        if not self.SMIterator:
            raise StopIteration

        if self.SMIterator.IsAtEnd():
            self.Key = None
            raise StopIteration
        self.Key = self.SMIterator.GetKey()
        self.PropertyLabel = self.SMIterator.GetPropertyLabel()
        self.SMIterator.Next()
        return self.Proxy.GetProperty(self.Key)

    def GetProxy(self):
        """Returns the proxy for the property last returned by the call to
        'next()'"""
        return self.Proxy

    def GetKey(self):
        """Returns the key for the property last returned by the call to
        'next()' """
        return self.Key

    def GetProperty(self):
        """Returns the property last returned by the call to 'next()' """
        return self.Proxy.GetProperty(self.Key)

    def __getattr__(self, name):
        """returns attributes from the vtkSMPropertyIterator."""
        return getattr(self.SMIterator, name)

class ProxyDefinitionIterator(object):
    """Wrapper for a vtkPVProxyDefinitionIterator class to satisfy
       the python iterator protocol.
       See the doxygen documentation of the vtkPVProxyDefinitionIterator
       C++ class for more information."""
    def __init__(self, iter):
        self.SMIterator = iter
        if self.SMIterator:
            self.SMIterator.UnRegister(None)
            self.SMIterator.InitTraversal()
        self.Group = None
        self.Key = None

    def __iter__(self):
        return self

    def next(self):
        if self.SMIterator.IsDoneWithTraversal():
            self.Group = None
            self.Key = None
            raise StopIteration
        self.Group = self.SMIterator.GetGroupName()
        self.Key = self.SMIterator.GetProxyName()
        self.SMIterator.GoToNextItem()
        return {"group": self.Group, "key":self.Key }

    def GetProxyName(self):
        """Returns the key for the proxy definition last returned by the call
        to 'next()' """
        return self.Key

    def GetGroup(self):
        """Returns the group for the proxy definition last returned by the
        call to 'next()' """
        return self.Group

    def __getattr__(self, name):
        """returns attributes from the vtkPVProxyDefinitionIterator."""
        return getattr(self.SMIterator, name)


class ProxyIterator(object):
    """Wrapper for a vtkSMProxyIterator class to satisfy the
     python iterator protocol.
     See the doxygen documentation of vtkSMProxyIterator C++ class for
     more information.
     """
    def __init__(self):
        self.SMIterator = vtkSMProxyIterator()
        self.SMIterator.SetSession(ActiveConnection.Session)
        self.SMIterator.Begin()
        self.AProxy = None
        self.Group = None
        self.Key = None

    def __iter__(self):
        return self

    def next(self):
        if self.SMIterator.IsAtEnd():
            self.AProxy = None
            self.Group = None
            self.Key = None
            raise StopIteration
            return None
        self.AProxy = _getPyProxy(self.SMIterator.GetProxy())
        self.Group = self.SMIterator.GetGroup()
        self.Key = self.SMIterator.GetKey()
        self.SMIterator.Next()
        return self.AProxy

    def GetProxy(self):
        """Returns the proxy last returned by the call to 'next()'"""
        return self.AProxy

    def GetKey(self):
        """Returns the key for the proxy last returned by the call to
        'next()' """
        return self.Key

    def GetGroup(self):
        """Returns the group for the proxy last returned by the call to
        'next()' """
        return self.Group

    def __getattr__(self, name):
        """returns attributes from the vtkSMProxyIterator."""
        return getattr(self.SMIterator, name)

# Caution: Observers must be global methods otherwise we run into memory
#          leak when the interpreter get reset from the C++ layer.
def _update_definitions_callback(self):
    def _update_definitions(caller, event):
        global ActiveConnection
        # HACK: updateModules() works on ActiveConnection, hence we trick it
        # for now.
        old_activeconnection = ActiveConnection
        ActiveConnection = self
        updateModules(self.Modules)
        ActiveConnection = old_activeconnection
    return _update_definitions

class Connection(object):
    """
      This is a python representation for a session/connection.
    """
    def __init__(self, connectionId, session):
        """Default constructor. Creates a Connection with the given
        ID, all other data members initialized to None."""
        global ActiveConnection
        self.ID = connectionId
        self.Session = session
        self.Modules = PVModule()
        self.Alive = True
        self.DefinitionObserverTag = 0
        self.CustomDefinitionObserverTag = 0

        # newly created connection always become the active connection.
        ActiveConnection = self

        # Build the list of available proxies for this connection.
        _createModules(self.Modules)

        # additionally, if the proxy definitions change, we monitor them.
        self.AttachDefinitionUpdater()

        ActiveConnection = None
        #SetActiveConnection(self)

    def __eq__(self, other):
        "Returns true if the connection ids are the same."
        return (self.ID == other.ID)

    def __repr__(self):
        """User friendly string representation"""
        return "Connection (%s) [%d]" % (self.Session.GetURI(), self.ID)

    def GetURI(self):
        """Get URI of the connection"""
        return self.Session.GetURI()

    def IsRemote(self):
        """Returns True if the connection to a remote server, False if
        it is local (built-in)"""
        if self.Session.IsA("vtkSMSessionClient"):
            return True
        return False

    def GetNumberOfDataPartitions(self):
        """Returns the number of partitions on the data server for this
           connection"""
        return self.Session.GetServerInformation().GetNumberOfProcesses()

    def AttachDefinitionUpdater(self):
        """Attach observer to automatically update modules when needed."""
        dfnMgr = self.Session.GetProxyDefinitionManager()
        self.DefinitionObserverTag = dfnMgr.AddObserver(
            dfnMgr.ProxyDefinitionsUpdated, _update_definitions_callback(self))
        self.CustomDefinitionObserverTag = dfnMgr.AddObserver(
            dfnMgr.CompoundProxyDefinitionsUpdated, _update_definitions_callback(self))
        pass

    def close(self):
        if self.DefinitionObserverTag:
            self.Session.GetProxyDefinitionManager().RemoveObserver(self.DefinitionObserverTag)
            self.Session.GetProxyDefinitionManager().RemoveObserver(self.CustomDefinitionObserverTag)
        self.Session = None
        self.Modules = None
        self.Alive = False

    def __del__(self):
        if self.Alive:
           self.close()

def SaveState(filename):
    """Given a state filename, saves the state of objects registered
    with the proxy manager."""
    pm = ProxyManager()
    pm.SaveState(filename)

def LoadState(filename, connection=None):
    """Given a state filename and an optional connection, loads the server
    manager state."""
    if not connection:
        connection = ActiveConnection
    if not connection:
        raise RuntimeError, "Cannot load state without a connection"
    pm = ProxyManager()
    pm.LoadState(filename, None)
    views = GetRenderViews()
    for view in views:
        # Make sure that the client window size matches the
        # ViewSize property. In paraview, the GUI takes care
        # of this.
        if view.GetClassName() == "vtkSMIceTDesktopRenderViewProxy":
            view.GetRenderWindow().SetSize(view.ViewSize[0], \
                                           view.ViewSize[1])

def Connect(ds_host=None, ds_port=11111, rs_host=None, rs_port=22221):
    """
    Use this function call to create a new session. On success,
    it returns a vtkSMSession object that abstracts the connection.
    Otherwise, it returns None.
    There are several ways in which this function can be called:
    * When called with no arguments, it creates a new session
      to the built-in server on the client itself.
    * When called with ds_host and ds_port arguments, it
      attempts to connect to a server(data and render server on the same server)
      on the indicated host:port.
    * When called with ds_host, ds_port, rs_host, rs_port, it
      creates a new connection to the data server on ds_host:ds_port and to the
      render server on rs_host: rs_port.
    """
    if ds_host == None:
        session = vtkSMSession()
    elif rs_host == None:
        session = vtkSMSessionClient()
        session.Connect("cs://%s:%d" % (ds_host, ds_port))
    else:
        session = vtkSMSessionClient()
        session.Connect("cdsrs://%s:%d/%s:%d" % (ds_host, ds_port, rs_host, rs_port))
    id = vtkProcessModule.GetProcessModule().RegisterSession(session)
    connection = GetConnectionFromId(id)

    # This shouldn't be needed. However, it's needed for old Python scripts that
    # directly import servermanager.py without simple.py
    SetActiveConnection(connection)
    return connection

def ReverseConnect(port=11111):
    """
    Use this function call to create a new session. On success,
    it returns a Session object that abstracts the connection.
    Otherwise, it returns None.
    In reverse connection mode, the client waits for a connection
    from the server (client has to be started first). The server
    then connects to the client (run pvserver with -rc and -ch
    option).
    The optional port specified the port to listen to.
    """
    session = vtkSMSessionClient()
    session.Connect("csrc://hostname:" + port)
    id = vtkProcessModule.GetProcessModule().RegisterSession(session)
    connection = GetConnectionFromId(id)

    # This shouldn't be needed. However, it's needed for old Python scripts that
    # directly import servermanager.py without simple.py
    SetActiveConnection(connection)
    return connection

def Disconnect(connection=None):
    """Disconnects the connection. Make sure to clear the proxy manager
    first."""
    global ActiveConnection
    if not connection:
        connection = ActiveConnection

    if connection:
        vtkSMSession.Disconnect(connection.ID)

def CreateProxy(xml_group, xml_name, session=None):
    """Creates a proxy. If session is set, the proxy's session is
    set accordingly. If session is None, the current Session is used, if
    present. You should not have to use method normally. Instantiate the
    appropriate class from the appropriate module, for example:
    sph = servermanager.sources.SphereSource()"""
    global ActiveConnection
    if not session:
        session = ActiveConnection.Session
    if not session:
        raise RuntimeError, "Cannot create objects without a session."
    pxm = ProxyManager(session)
    return pxm.NewProxy(xml_group, xml_name)

def GetRenderView(connection=None):
    """Return the render view in use.  If more than one render view is in
    use, return the first one."""

    render_module = None
    for aProxy in ProxyManager():
        if aProxy.IsA("vtkSMRenderViewProxy"):
            render_module = aProxy
            break
    return render_module

def GetRenderViews(connection=None):
    """Returns the set of all render views."""
    render_modules = []
    for aProxy in ProxyManager():
        if aProxy.IsA("vtkSMRenderViewProxy"):
            render_modules.append(aProxy)
    return render_modules

def GetContextViews(connection=None):
    """Returns the set of all context views."""
    context_modules = []
    for aProxy in ProxyManager():
        if aProxy.IsA("vtkSMContextViewProxy"):
            context_modules.append(aProxy)
    return context_modules

def CreateRenderView(session=None, **extraArgs):
    """Creates a render window on the particular session. If session
    is not specified, then the active session is used, if available.

    This method can also be used to initialize properties by passing
    keyword arguments where the key is the name of the property. In addition
    registrationGroup and registrationName (optional) can be specified (as
    keyword arguments) to automatically register the proxy with the proxy
    manager."""
    return _create_view("RenderView", session, **extraArgs)

def _create_view(view_xml_name, session=None, **extraArgs):
    """Creates a view on the particular session. If session
    is not specified, then the active session is used, if available.
    This method can also be used to initialize properties by passing
    keyword arguments where the key is the name of the property."""
    if not session:
        session = ActiveConnection.Session
    if not session:
        raise RuntimeError, "Cannot create view without session."
    pxm = ProxyManager()
    view_module = None
    if view_xml_name:
        view_module = CreateProxy("views", view_xml_name, session)
    if not view_module:
        return None
    extraArgs['proxy'] = view_module
    python_proxy_name = _make_name_valid(view_module.GetXMLName())
    proxy = rendering.__dict__[python_proxy_name](**extraArgs)
    return proxy

def GetRepresentation(aProxy, view):
    for rep in view.Representations:
        try: isRep = rep.Input == aProxy
        except: isRep = False
        if isRep: return rep
    return None

def CreateRepresentation(aProxy, view, **extraArgs):
    """Creates a representation for the proxy and adds it to the render
    module.

    This method can also be used to initialize properties by passing
    keyword arguments where the key is the name of the property.In addition
    registrationGroup and registrationName (optional) can be specified (as
    keyword arguments) to automatically register the proxy with the proxy
    manager.

    This method tries to create the best possible representation for the given
    proxy in the given view. Additionally, the user can specify proxyName
    (optional) to create a representation of a particular type."""

    global rendering
    if not aProxy:
        raise RuntimeError, "proxy argument cannot be None."
    if not view:
        raise RuntimeError, "view argument cannot be None."
    if "proxyName" in extraArgs:
      display = CreateProxy("representations", extraArgs['proxyName'], None)
      del extraArgs['proxyName']
    else:
      display = view.SMProxy.CreateDefaultRepresentation(aProxy.SMProxy, 0)
      if display:
        display.UnRegister(None)
    if not display:
        return None
    extraArgs['proxy'] = display
    proxy = rendering.__dict__[display.GetXMLName()](**extraArgs)
    proxy.Input = aProxy
    proxy.UpdateVTKObjects()
    view.Representations.append(proxy)
    return proxy

class _ModuleLoader(object):
    def find_module(self, fullname, path=None):
        if vtkPVPythonModule.HasModule(fullname):
            return self
        else:
            return None
    def load_module(self, fullname):
        import imp
        moduleInfo = vtkPVPythonModule.GetModule(fullname)
        if not moduleInfo:
            raise ImportError
        module = sys.modules.setdefault(fullname, imp.new_module(fullname))
        module.__file__ = "<%s>" % moduleInfo.GetFullName()
        module.__loader__ = self
        if moduleInfo.GetIsPackage:
            module.__path__ = moduleInfo.GetFullName()
        code = compile(moduleInfo.GetSource(), module.__file__, 'exec')
        exec code in module.__dict__
        return module

def LoadXML(xmlstring):
    """DEPRECATED. Given a server manager XML as a string, parse and process it."""
    raise RuntimeError, "Deprecated. Use LoadPlugin(...) instead."


def LoadPlugin(filename,  remote=True, connection=None):
    """ Given a filename and a session (optional, otherwise uses
    ActiveConnection), loads a plugin. It then updates the sources,
    filters and rendering modules."""

    if not connection:
        connection = ActiveConnection
    if not connection:
        raise RuntimeError, "Cannot load a plugin without a connection."
    plm = vtkSMProxyManager.GetProxyManager().GetPluginManager()

    if remote:
        status = plm.LoadRemotePlugin(filename, connection.Session)
    else:
        status = plm.LoadLocalPlugin(filename)

    # shouldn't the extension check happend before attempting to load the plugin?
    if not status:
        raise RuntimeError, "Problem loading plugin %s" % (filename)
    else:
        # we should never have to call this. The modules should update automatically.
        updateModules(connection.Modules)


def Fetch(input, arg1=None, arg2=None, idx=0):
    """
    A convenience method that moves data from the server to the client,
    optionally performing some operation on the data as it moves.
    The input argument is the name of the (proxy for a) source or filter
    whose output is needed on the client.

    You can use Fetch to do three things:

    If arg1 is None (the default) then all of the data is brought to the client.
    In parallel runs an appropriate append Filter merges the
    data on each processor into one data object. The filter chosen will be
    vtkAppendPolyData for vtkPolyData, vtkAppendRectilinearGrid for
    vtkRectilinearGrid, vtkMultiBlockDataGroupFilter for vtkCompositeData,
    and vtkAppendFilter for anything else.

    If arg1 is an integer then one particular processor's output is brought to
    the client. In serial runs the arg is ignored. If you have a filter that
    computes results in parallel and brings them to the root node, then set
    arg to be 0.

    If arg1 and arg2 are a algorithms, for example vtkMinMax, the algorithm
    will be applied to the data to obtain some result. Here arg1 will be
    applied pre-gather and arg2 will be applied post-gather. In parallel
    runs the algorithm will be run on each processor to make intermediate
    results and then again on the root processor over all of the
    intermediate results to create a global result.

    Optional argument idx is used to specify the output port number to fetch the
    data from. Default is port 0.
    """

    import types

    reducer = filters.ReductionFilter(Input=OutputPort(input,idx))

    #create the pipeline that reduces and transmits the data
    if arg1 == None:
        cdinfo = input.GetDataInformation(idx).GetCompositeDataInformation()
        if cdinfo.GetDataIsComposite():
            paraview.print_debug_info("use composite data append")
            reducer.PostGatherHelperName = "vtkMultiBlockDataGroupFilter"

        elif input.GetDataInformation(idx).GetDataClassName() == "vtkPolyData":
            paraview.print_debug_info("use append poly data filter")
            reducer.PostGatherHelperName = "vtkAppendPolyData"

        elif input.GetDataInformation(idx).GetDataClassName() == "vtkRectilinearGrid":
            paraview.print_debug_info("use append rectilinear grid filter")
            reducer.PostGatherHelperName = "vtkAppendRectilinearGrid"

        elif input.GetDataInformation(idx).IsA("vtkDataSet"):
            paraview.print_debug_info("use unstructured append filter")
            reducer.PostGatherHelperName = "vtkAppendFilter"

    elif type(arg1) is types.IntType:
        reducer.PassThrough = arg1

    else:
        reducer.PreGatherHelper = arg1
        reducer.PostGatherHelper = arg2

    # reduce
    reducer.UpdatePipeline()
    dataInfo = reducer.GetDataInformation(0)
    dataType = dataInfo.GetDataSetType()
    if dataInfo.GetCompositeDataSetType() > 0:
      dataType = dataInfo.GetCompositeDataSetType()

    fetcher = filters.ClientServerMoveData(Input=reducer)
    fetcher.OutputDataType = dataType
    fetcher.WholeExtent = dataInfo.GetExtent()[:]
    #fetch
    fetcher.UpdatePipeline()

    op = fetcher.GetClientSideObject().GetOutputDataObject(0)
    opc = op.NewInstance()
    opc.ShallowCopy(op)
    opc.UnRegister(None)
    return opc

def AnimateReader(reader, view, filename=None):
    """This is a utility function that, given a reader and a view
    animates over all time steps of the reader. If the optional
    filename is provided, a movie is created (type depends on the
    extension of the filename."""
    if not reader:
        raise RuntimeError, "No reader was specified, cannot animate."
    if not view:
        raise RuntimeError, "No view was specified, cannot animate."
    # Create an animation scene
    scene = animation.AnimationScene()

    # We need to have the reader and the view registered with
    # the time keeper. This is how the scene gets its time values.
    try:
        tk = ProxyManager().GetProxiesInGroup("timekeeper").values()[0]
        scene.TimeKeeper = tk
    except IndexError:
        tk = misc.TimeKeeper()
        scene.TimeKeeper = tk

    if not reader in tk.TimeSources:
        tk.TimeSources.append(reader)
    if not view in tk.Views:
        tk.Views.append(view)


    # with 1 view
    scene.ViewModules = [view]
    # Update the reader to get the time information
    reader.UpdatePipelineInformation()
    # Animate from 1st time step to last
    scene.StartTime = reader.TimestepValues.GetData()[0]
    scene.EndTime = reader.TimestepValues.GetData()[-1]

    # Each frame will correspond to a time step
    scene.PlayMode = 2 #Snap To Timesteps

    # Create a special animation cue for time.
    cue = animation.TimeAnimationCue()
    cue.AnimatedProxy = view
    cue.AnimatedPropertyName = "ViewTime"
    scene.Cues = [cue]

    if filename:
        writer = vtkSMAnimationSceneImageWriter()
        writer.SetFileName(filename)
        writer.SetFrameRate(1)
        writer.SetAnimationScene(scene.SMProxy)

        # Now save the animation.
        if not writer.Save():
            raise RuntimeError, "Saving of animation failed!"
    else:
        scene.Play()
    return scene

def GetProgressPrintingIsEnabled():
    return progressObserverTag is not None

def SetProgressPrintingEnabled(value):
    """Turn on/off printing of progress (by default, it is on). You can
    always turn progress off and add your own observer to the process
    module to handle progress in a custom way. See _printProgress for
    an example event observer."""
    global progressObserverTag

    # If value is true and progress printing is currently off...
    if value and not GetProgressPrintingIsEnabled():
        if paraview.fromGUI:
            raise RuntimeError("Printing progress in the GUI is not supported.")
        progressObserverTag = vtkProcessModule.GetProcessModule().AddObserver(\
            "ProgressEvent", _printProgress)

    # If value is false and progress printing is currently on...
    elif GetProgressPrintingIsEnabled():
        vtkProcessModule.GetProcessModule().RemoveObserver(progressObserverTag)
        progressObserverTag = None

def ToggleProgressPrinting():
    """Turn on/off printing of progress.  See SetProgressPrintingEnabled."""
    SetProgressPrintingEnabled(not GetProgressPrintingIsEnabled())

def Finalize():
    """Although not required, this can be called at exit to cleanup."""
    global progressObserverTag
    # Make sure to remove the observer
    if progressObserverTag:
        ToggleProgressPrinting()
    vtkInitializationHelper.Finalize()

# Internal methods

def _getPyProxy(smproxy, outputPort=0):
    """Returns a python wrapper for a server manager proxy. This method
    first checks if there is already such an object by looking in the
    _pyproxies group and returns it if found. Otherwise, it creates a
    new one. Proxies register themselves in _pyproxies upon creation."""
    if not smproxy:
        return None
    if (smproxy, outputPort) in _pyproxies:
        return _pyproxies[(smproxy, outputPort)]()

    xmlName = smproxy.GetXMLName()
    if paraview.compatibility.GetVersion() >= 3.5:
        if smproxy.GetXMLLabel():
            xmlName = smproxy.GetXMLLabel()
    classForProxy = _findClassForProxy(_make_name_valid(xmlName), smproxy.GetXMLGroup())
    if classForProxy:
        retVal = classForProxy(proxy=smproxy, port=outputPort)
    else:
        retVal = Proxy(proxy=smproxy, port=outputPort)
    return retVal

def _makeUpdateCameraMethod(rv):
    """ This internal method is used to create observer methods """
    if not hasattr(rv(), "BlockUpdateCamera"):
        rv().add_attribute("BlockUpdateCamera", False)
    def UpdateCamera(obj, string):
        if not rv().BlockUpdateCamera:
          # used to avoid some nasty recursion that occurs when interacting in
          # the GUI.
          rv().BlockUpdateCamera = True
          rv().SynchronizeCameraProperties()
          rv().BlockUpdateCamera = False
    return UpdateCamera

def _createInitialize(group, name):
    """Internal method to create an Initialize() method for the sub-classes
    of Proxy"""
    pgroup = group
    pname = name
    def aInitialize(self, connection=None, update=True):
        if not connection:
            connection = ActiveConnection
        if not connection:
            raise RuntimeError,\
                  'Cannot create a proxy without a session.'
        if not connection.Session.GetProxyDefinitionManager().HasDefinition(pgroup, pname):
            error_msg = "The connection does not provide any definition for %s." % pname
            raise RuntimeError, error_msg
        self.InitializeFromProxy(\
            CreateProxy(pgroup, pname, connection.Session), update)
    return aInitialize

def _createGetProperty(pName):
    """Internal method to create a GetXXX() method where XXX == pName."""
    propName = pName
    def getProperty(self):
        if paraview.compatibility.GetVersion() >= 3.5:
            return self.GetPropertyValue(propName)
        else:
            return self.GetProperty(propName)
    return getProperty

def _createSetProperty(pName):
    """Internal method to create a SetXXX() method where XXX == pName."""
    propName = pName
    def setProperty(self, value):
        return self.SetPropertyWithName(propName, value)
    return setProperty

def _findClassForProxy(xmlName, xmlGroup):
    """Given the xmlName for a proxy, returns a Proxy class. Note
    that if there are duplicates, the first one is returned."""
    global sources, filters, writers, rendering, animation, implicit_functions,\
           piecewise_functions, extended_sources, misc
    if not xmlName:
        return None
    if xmlGroup == "sources":
        return sources.__dict__[xmlName]
    elif xmlGroup == "filters":
        return filters.__dict__[xmlName]
    elif xmlGroup == "implicit_functions":
        return implicit_functions.__dict__[xmlName]
    elif xmlGroup == "piecewise_functions":
        return piecewise_functions.__dict__[xmlName]
    elif xmlGroup == "writers":
        return writers.__dict__[xmlName]
    elif xmlGroup == "extended_sources":
        return extended_sources.__dict__[xmlName]
    elif xmlName in rendering.__dict__:
        return rendering.__dict__[xmlName]
    elif xmlName in animation.__dict__:
        return animation.__dict__[xmlName]
    elif xmlName in misc.__dict__:
        return misc.__dict__[xmlName]
    else:
        return None

def _printProgress(caller, event):
    """The default event handler for progress. Prints algorithm
    name and 1 '.' per 10% progress."""
    global currentAlgorithm, currentProgress

    pm = vtkProcessModule.GetProcessModule()
    progress = pm.GetLastProgress() / 10
    # If we got a 100% as the first thing, ignore
    # This is to get around the fact that some vtk
    # algorithms report 100% more than once (which is
    # a bug)
    if not currentAlgorithm and progress == 10:
        return
    alg = pm.GetLastProgressName()
    if alg != currentAlgorithm and alg:
        if currentAlgorithm:
            while currentProgress <= 10:
                import sys
                sys.stdout.write(".")
                currentProgress += 1
            print "]"
            currentProgress = 0
        print alg, ": [ ",
        currentAlgorithm = alg
    while currentProgress <= progress:
        import sys
        sys.stdout.write(".")
        #sys.stdout.write("%d " % pm.GetLastProgress())
        currentProgress += 1
    if progress == 10:
        print "]"
        currentAlgorithm = None
        currentProgress = 0

def updateModules(m):
    """Called when a plugin is loaded, this method updates
    the proxy class object in all known modules."""

    createModule("sources", m.sources)
    createModule("filters", m.filters)
    createModule("writers", m.writers)
    createModule("representations", m.rendering)
    createModule("views", m.rendering)
    createModule("lookup_tables", m.rendering)
    createModule("textures", m.rendering)
    createModule('cameramanipulators', m.rendering)
    createModule("animation", m.animation)
    createModule("misc", m.misc)
    createModule('animation_keyframes', m.animation)
    createModule('implicit_functions', m.implicit_functions)
    createModule('piecewise_functions', m.piecewise_functions)
    createModule("extended_sources", m.extended_sources)
    createModule("incremental_point_locators", m.misc)

def _createModules(m):
    """Called when the module is loaded, this creates sub-
    modules for all know proxy groups."""

    m.sources = createModule('sources')
    m.filters = createModule('filters')
    m.writers = createModule('writers')
    m.rendering = createModule('representations')
    createModule('views', m.rendering)
    createModule("lookup_tables", m.rendering)
    createModule("textures", m.rendering)
    createModule('cameramanipulators', m.rendering)
    m.animation = createModule('animation')
    createModule('animation_keyframes', m.animation)
    m.implicit_functions = createModule('implicit_functions')
    m.piecewise_functions = createModule('piecewise_functions')
    m.extended_sources = createModule("extended_sources")
    m.misc = createModule("misc")
    createModule("incremental_point_locators", m.misc)

class PVModule(object):
    pass

def _make_name_valid(name):
    return paraview.make_name_valid(name)

def createModule(groupName, mdl=None):
    """Populates a module with proxy classes defined in the given group.
    If mdl is not specified, it also creates the module"""
    global ActiveConnection

    if not ActiveConnection:
      raise RuntimeError, "Please connect to a server using \"Connect\""

    pxm = ProxyManager()
    # Use prototypes to find all proxy types.
    pxm.InstantiateGroupPrototypes(groupName)

    debug = False
    if not mdl:
        debug = True
        mdl = PVModule()
    definitionIter = pxm.NewDefinitionIterator(groupName)
    for i in definitionIter:
        proxyName = i['key']
        proto = pxm.GetPrototypeProxy(groupName, proxyName)
        if not proto:
           paraview.print_error("Error while loading %s/%s %s"%(groupName, i['group'], proxyName))
           continue
        pname = proxyName
        if paraview.compatibility.GetVersion() >= 3.5 and\
           proto.GetXMLLabel():
            pname = proto.GetXMLLabel()
        pname = _make_name_valid(pname)
        if not pname:
            continue
        if pname in mdl.__dict__:
            if debug:
                paraview.print_warning("Warning: %s is being overwritten. This may point to an issue in the ParaView configuration files" % pname)
        cdict = {}
        # Create an Initialize() method for this sub-class.
        cdict['Initialize'] = _createInitialize(groupName, proxyName)
        iter = PropertyIterator(proto)
        # Add all properties as python properties.
        for prop in iter:
            propName = iter.GetKey()
            if paraview.compatibility.GetVersion() >= 3.5:
                if (prop.GetInformationOnly() and propName != "TimestepValues" ) \
                  or prop.GetIsInternal():
                    continue
            names = [propName]
            if paraview.compatibility.GetVersion() >= 3.5:
                names = [iter.PropertyLabel]

            propDoc = None
            if prop.GetDocumentation():
                propDoc = prop.GetDocumentation().GetDescription()
            for name in names:
                name = _make_name_valid(name)
                if name:
                    cdict[name] = property(_createGetProperty(propName),
                                           _createSetProperty(propName),
                                           None,
                                           propDoc)
        # Add the documentation as the class __doc__
        if proto.GetDocumentation() and \
           proto.GetDocumentation().GetDescription():
            doc = proto.GetDocumentation().GetDescription()
        else:
            doc = Proxy.__doc__
        cdict['__doc__'] = doc
        # Create the new type
        if proto.GetXMLName() == "ExodusIIReader":
            superclasses = (ExodusIIReaderProxy,)
        elif proto.IsA("vtkSMSourceProxy"):
            superclasses = (SourceProxy,)
        elif proto.IsA("vtkSMViewLayoutProxy"):
            superclasses = (ViewLayoutProxy,)
        else:
            superclasses = (Proxy,)

        cobj = type(pname, superclasses, cdict)
        # Add it to the modules dictionary
        mdl.__dict__[pname] = cobj
    return mdl


def __determineGroup(proxy):
    """Internal method"""
    if not proxy:
        return None
    xmlgroup = proxy.GetXMLGroup()
    xmlname = proxy.GetXMLName()
    if xmlgroup == "sources":
        if xmlname in ["BlockSelectionSource",
                       "FrustumSelectionSource",
                       "GlobalIDSelectionSource",
                       "PedigreeIDSelectionSource",
                       "IDSelectionSource",
                       "CompositeDataIDSelectionSource",
                       "HierarchicalDataIDSelectionSource",
                       "ThresholdSelectionSource",
                       "LocationSelectionSource"]:
            return "selection_sources"
        return "sources"
    elif xmlgroup == "filters":
        return "sources"
    elif xmlgroup == "representations":
        if xmlname == "ScalarBarWidgetRepresentation":
            return "scalar_bars"
        return "representations"
    elif xmlgroup == "animation_keyframes":
        return "animation"
    return xmlgroup

__nameCounter = {}
def __determineName(proxy, group):
    global __nameCounter
    name = _make_name_valid(proxy.GetXMLLabel())
    if not name:
        return None
    if not __nameCounter.has_key(name):
        __nameCounter[name] = 1
        val = 1
    else:
        __nameCounter[name] += 1
        val = __nameCounter[name]
    return "%s%d" % (name, val)

def __getName(proxy, group):
    pxm = ProxyManager(proxy.GetSession())
    if isinstance(proxy, Proxy):
        proxy = proxy.SMProxy
    return pxm.GetProxyName(group, proxy)

class MissingRegistrationInformation(Exception):
    """Exception for missing registration information. Raised when a name or group
    is not specified or when a group cannot be deduced."""
    pass

class MissingProxy(Exception):
    """Exception fired when the requested proxy is missing."""
    pass

def Register(proxy, **extraArgs):
    """Registers a proxy with the proxy manager. If no 'registrationGroup' is
    specified, then the group is inferred from the type of the proxy.
    'registrationName' may be specified to register with a particular name
    otherwise a default name will be created."""
    # TODO: handle duplicate registration
    if "registrationGroup" in extraArgs:
        registrationGroup = extraArgs["registrationGroup"]
    else:
        registrationGroup = __determineGroup(proxy)

    if "registrationName" in extraArgs:
        registrationName = extraArgs["registrationName"]
    else:
        registrationName = __determineName(proxy, registrationGroup)
    if registrationGroup and registrationName:
        pxm = ProxyManager()
        pxm.RegisterProxy(registrationGroup, registrationName, proxy)
    else:
        raise MissingRegistrationInformation, "Registration error %s %s." % (registrationGroup, registrationName)
    return (registrationGroup, registrationName)

def UnRegister(proxy, **extraArgs):
    """UnRegisters proxies registered using Register()."""
    if "registrationGroup" in extraArgs:
        registrationGroup = extraArgs["registrationGroup"]
    else:
        registrationGroup = __determineGroup(proxy)

    if "registrationName" in extraArgs:
        registrationName = extraArgs["registrationName"]
    else:
        registrationName = __getName(proxy, registrationGroup)

    if registrationGroup and registrationName:
        pxm = ProxyManager()
        pxm.UnRegisterProxy(registrationGroup, registrationName, proxy)
    else:
        raise RuntimeError, "UnRegistration error."
    return (registrationGroup, registrationName)

def demo1():
    """This simple demonstration creates a sphere, renders it and delivers
    it to the client using Fetch. It returns a tuple of (data, render
    view)"""
    if not ActiveConnection:
        Connect()
    if paraview.compatibility.GetVersion() <= 3.4:
        ss = sources.SphereSource(Radius=2, ThetaResolution=32)
        shr = filters.ShrinkFilter(Input=OutputPort(ss,0))
        cs = sources.ConeSource()
        app = filters.Append()
    else:
        ss = sources.Sphere(Radius=2, ThetaResolution=32)
        shr = filters.Shrink(Input=OutputPort(ss,0))
        cs = sources.Cone()
        app = filters.AppendDatasets()
    app.Input = [shr, cs]
    rv = CreateRenderView()
    rep = CreateRepresentation(app, rv)
    rv.ResetCamera()
    rv.StillRender()
    data = Fetch(ss)

    return (data, rv)

def demo2(fname="/Users/berk/Work/ParaViewData/Data/disk_out_ref.ex2"):
    """This method demonstrates the user of a reader, representation and
    view. It also demonstrates how meta-data can be obtained using proxies.
    Make sure to pass the full path to an exodus file. Also note that certain
    parameters are hard-coded for disk_out_ref.ex2 which can be found
    in ParaViewData. This method returns the render view."""
    if not ActiveConnection:
        Connect()
    # Create the exodus reader and specify a file name
    reader = sources.ExodusIIReader(FileName=fname)
    # Get the list of point arrays.
    if paraview.compatibility.GetVersion() <= 3.4:
        arraySelection = reader.PointResultArrayStatus
    else:
        arraySelection = reader.PointVariables
    print arraySelection.Available
    # Select all arrays
    arraySelection.SetData(arraySelection.Available)

    # Next create a default render view appropriate for the session type.
    rv = CreateRenderView()
    # Create the matching representation
    rep = CreateRepresentation(reader, rv)
    rep.Representation = 1 # Wireframe
    # Black background is not pretty
    rv.Background = [0.4, 0.4, 0.6]
    rv.StillRender()
    # Reset the camera to include the whole thing
    rv.ResetCamera()
    rv.StillRender()
    # Change the elevation of the camera. See VTK documentation of vtkCamera
    # for camera parameters.
    c = rv.GetActiveCamera()
    c.Elevation(45)
    rv.StillRender()
    # Now that the reader execute, let's get some information about it's
    # output.
    pdi = reader[0].PointData
    # This prints a list of all read point data arrays as well as their
    # value ranges.
    print 'Number of point arrays:', len(pdi)
    for i in range(len(pdi)):
        ai = pdi[i]
        print "----------------"
        print "Array:", i, ai.Name, ":"
        numComps = ai.GetNumberOfComponents()
        print "Number of components:", numComps
        for j in range(numComps):
            if paraview.compatibility.GetVersion() <= 3.4:
                print "Range:", ai.Range(j)
            else:
                print "Range:", ai.GetRange(j)
    # White is boring. Let's color the geometry using a variable.
    # First create a lookup table. This object controls how scalar
    # values are mapped to colors. See VTK documentation for
    # details.
    lt = rendering.PVLookupTable()
    # Assign it to the representation
    rep.LookupTable = lt
    # Color by point array called Pres
    rep.ColorAttributeType = 0 # point data
    rep.ColorArrayName = "Pres"
    # Add to RGB points. These are tuples of 4 values. First one is
    # the scalar values, the other 3 the RGB values. This list has
    # 2 points: Pres: 0.00678, color: blue, Pres: 0.0288, color: red
    lt.RGBPoints = [0.00678, 0, 0, 1, 0.0288, 1, 0, 0]
    lt.ColorSpace = 1 # HSV
    rv.StillRender()
    return rv

def demo3():
    """This method demonstrates the use of servermanager with numpy as
    well as pylab for plotting. It creates an artificial data sources,
    probes it with a line, delivers the result to the client using Fetch
    and plots it using pylab. This demo requires numpy and pylab installed.
    It returns a tuple of (data, render view)."""
    import paraview.numpy_support
    import pylab

    if not ActiveConnection:
        Connect()
    # Create a synthetic data source
    if paraview.compatibility.GetVersion() <= 3.4:
        source = sources.RTAnalyticSource()
    else:
        source = sources.Wavelet()
    # Let's get some information about the data. First, for the
    # source to execute
    source.UpdatePipeline()

    di = source.GetDataInformation()
    print "Data type:", di.GetPrettyDataTypeString()
    print "Extent:", di.GetExtent()
    print "Array name:", \
          source[0].PointData[0].Name

    rv = CreateRenderView()

    rep1 = CreateRepresentation(source, rv)
    rep1.Representation = 3 # outline

    # Let's apply a contour filter
    cf = filters.Contour(Input=source, ContourValues=[200])

    # Select the array to contour by
    #cf.SelectInputScalars = 'RTData'

    rep2 = CreateRepresentation(cf, rv)

    rv.Background = (0.4, 0.4, 0.6)
    # Reset the camera to include the whole thing
    rv.StillRender()
    rv.ResetCamera()
    rv.StillRender()

    # Now, let's probe the data
    if paraview.compatibility.GetVersion() <= 3.4:
        probe = filters.Probe(Input=source)
        # with a line
        line = sources.LineSource(Resolution=60)
    else:
        probe = filters.ResampleWithDataset(Input=source)
        # with a line
        line = sources.Line(Resolution=60)
    # that spans the dataset
    bounds = di.GetBounds()
    print "Bounds: ", bounds
    line.Point1 = bounds[0:6:2]
    line.Point2 = bounds[1:6:2]

    probe.Source = line

    # Render with the line
    rep3 = CreateRepresentation(line, rv)
    rv.StillRender()

    # Now deliver it to the client. Remember, this is for small data.
    data = Fetch(probe)
    # Convert it to a numpy array
    data = paraview.numpy_support.vtk_to_numpy(
      data.GetPointData().GetArray("RTData"))
    # Plot it using matplotlib
    pylab.plot(data)
    pylab.show()

    return (data, rv, probe)

def demo4(fname="/Users/berk/Work/ParaViewData/Data/can.ex2"):
    """This method demonstrates the user of AnimateReader for
    creating animations."""
    if not ActiveConnection:
        Connect()
    reader = sources.ExodusIIReader(FileName=fname)
    view = CreateRenderView()
    repr = CreateRepresentation(reader, view)
    view.StillRender()
    view.ResetCamera()
    view.StillRender()
    c = view.GetActiveCamera()
    c.Elevation(95)
    return AnimateReader(reader, view)


def demo5():
    """ Simple sphere animation"""
    if not ActiveConnection:
        Connect()
    if paraview.compatibility.GetVersion() <= 3.4:
        sphere = sources.SphereSource()
    else:
        sphere = sources.Sphere()
    view = CreateRenderView()
    repr = CreateRepresentation(sphere, view)

    view.StillRender()
    view.ResetCamera()
    view.StillRender()

    # Create an animation scene
    scene = animation.AnimationScene()
    # Add 1 view
    scene.ViewModules = [view]

    # Create a cue to animate the StartTheta property
    cue = animation.KeyFrameAnimationCue()
    cue.AnimatedProxy = sphere
    cue.AnimatedPropertyName = "StartTheta"
    # Add it to the scene's cues
    scene.Cues = [cue]

    # Create 2 keyframes for the StartTheta track
    keyf0 = animation.CompositeKeyFrame()
    keyf0.Type = 2 # Set keyframe interpolation type to Ramp.
    # At time = 0, value = 0
    keyf0.KeyTime = 0
    keyf0.KeyValues= [0]

    keyf1 = animation.CompositeKeyFrame()
    # At time = 1.0, value = 200
    keyf1.KeyTime = 1.0
    keyf1.KeyValues= [200]

    # Add keyframes.
    cue.KeyFrames = [keyf0, keyf1]

    scene.Play()
    return scene

ASSOCIATIONS = { 'POINTS' : 0, 'CELLS' : 1, 'VERTICES' : 4, 'EDGES' : 5, 'ROWS' : 6}

# Users can set the active connection which will be used by API
# to create proxies etc when no connection argument is passed.
# Connect() automatically sets this if it is not already set.
ActiveConnection = None

"""Keeps track of all connection objects. Unless the process was run with
--multi-servers flag set to True, this will generally be just 1 item long at the
most."""
Connections = []

def SetActiveConnection(connection=None):
    """Set the active connection. If the process was run without multi-server
       enabled and this method is called with a non-None argument while an
       ActiveConnection is present, it will raise a RuntimeError."""
    global ActiveConnection

    #supports_simutaneous_connections =\
    #    vtkProcessModule.GetProcessModule().GetMultipleSessionsSupport()

    #print "updating active connection", connection
    ActiveConnection = connection

    #  This will ensure that servemanager.sources.* will point to the right
    #  constructors.
    __exposeActiveModules__()

    # If this method was initiated from Python, we need to pass that info to the
    # ServerManager.
    session = None
    if ActiveConnection:
        session = ActiveConnection.Session

    # This is a no-op if the session is unchanged.
    pxm = vtkSMProxyManager.GetProxyManager()
    pxm.SetActiveSession(session)
    return ActiveConnection

# Needs to be called when paraview module is loaded from python instead
# of pvpython, pvbatch or GUI.
if not vtkProcessModule.GetProcessModule():
    pvoptions = None
    if paraview.options.batch:
      pvoptions = vtkPVOptions();
      pvoptions.SetProcessType(vtkPVOptions.PVBATCH)
      if paraview.options.symmetric:
        pvoptions.SetSymmetricMPIMode(True)
    vtkInitializationHelper.Initialize(sys.executable,
        vtkProcessModule.PROCESS_CLIENT, pvoptions)

# Initialize progress printing. Can be turned off by calling
# ToggleProgressPrinting() again.
progressObserverTag = None
currentAlgorithm = False
currentProgress = 0
if not paraview.fromGUI:
    ToggleProgressPrinting()

_pyproxies = {}

# Create needed sub-modules
# We can no longer create modules, unless we have connected to a server.
# _createModules()

# Set up our custom importer (if possible)
loader = _ModuleLoader()
sys.meta_path.append(loader)


def __exposeActiveModules__():
    """Update servermanager submodules to point to the current
    ActiveConnection.Modules.*"""
    global ActiveConnection

    # Expose all active module to the current servermanager module
    if ActiveConnection:
       for m in [mName for mName in dir(ActiveConnection.Modules) if mName[0] != '_' ]:
          exec "global %s;%s = ActiveConnection.Modules.%s" % (m,m,m)

def GetConnectionFromId(id):
    """Returns the Connection object corresponding a connection identified by
       the id."""
    global Connections
    for connection in Connections:
        if connection.ID == id:
            return connection
    return None

def GetConnectionFromSession(session):
    """Retuns the Connection object corresponding to a vtkSMSession instance."""
    global Connections
    for connection in Connections:
        if connection.Session == session:
            return connection

    pm = vtkProcessModule.GetProcessModule()
    sid = pm.GetSessionID(session)
    if session and sid:
        # it implies that the we simply may not have received the
        # ConnectionCreatedEvent event yet. Create a connection object.
        c = Connection(sid, session)
        Connections.append(c)
        return c
    return None


def __connectionCreatedCallback(obj, string):
    """Callback called when a new session is created."""
    global Connections
    pm = vtkProcessModule.GetProcessModule()
    sid = pm.GetEventCallDataSessionId()
    # this creates the Connection object if needed.
    GetConnectionFromSession(pm.GetSession(sid))

def __connectionClosedCallback(obg, string):
    """Callback called when a new session is closed."""
    global Connections, ActiveConnection
    pm = vtkProcessModule.GetProcessModule()
    sid = pm.GetEventCallDataSessionId()
    if sid:
        c = GetConnectionFromId(sid)
        if c:
            for cc in range(len(Connections)):
                if Connections[cc] == c:
                    del Connections[cc]
                    break
            c.close()
            del c
    import gc
    gc.collect()

def __initialize():
    """Does initialization of the module, ensuring that the module's state
        correctly reflects that of the ProcessModule/ServerManager."""
    global ActiveConnection, Connections

    # Monitor connection creations/deletions on the ProcessModule.
    pm = vtkProcessModule.GetProcessModule()
    pm.AddObserver("ConnectionCreatedEvent", __connectionCreatedCallback)
    pm.AddObserver("ConnectionClosedEvent", __connectionClosedCallback)

    # Iterate over existing connections, if any, and set the datastructures up.
    iter = vtkProcessModule.GetProcessModule().NewSessionIterator();
    iter.UnRegister(None)
    iter.InitTraversal()
    firstSession = None
    while not iter.IsDoneWithTraversal():
        c = Connection(iter.GetCurrentSessionId(), iter.GetCurrentSession())
        Connections.append(c)
        iter.GoToNextItem()

    # Update active session.
    activeConnection = GetConnectionFromSession(\
        vtkSMProxyManager.GetProxyManager().GetActiveSession())
    SetActiveConnection(activeConnection)

__initialize()

if hasattr(sys, "ps1"):
    # session is interactive.
    paraview.print_debug_info(vtkSMProxyManager.GetParaViewSourceVersion());