This file is indexed.

/usr/bin/python3-parse_xsd2 is in python3-pysaml2 3.0.0-3ubuntu1.

This file is owned by root:root, with mode 0o755.

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
#! /usr/bin/python3

import re
import time
import getopt
import imp
import sys
import types
import errno
import six

__version__ = 0.5

from xml.etree import cElementTree as ElementTree

INDENT = 4*" "
DEBUG = False

XMLSCHEMA = "http://www.w3.org/2001/XMLSchema"
XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'

CLASS_PROP = [("c_children", ".copy()"), 
                ("c_attributes", ".copy()"),
                ("c_child_order", "[:]"),
                ("c_cardinality", ".copy()")]
                
BASE_ELEMENT = ["text", "extension_elements", "extension_attributes"]
    
class MissingPrerequisite(Exception):
    pass

def sd_copy(arg):
    try:
        return arg.copy()
    except AttributeError:
        return {}
        
# ------------------------------------------------------------------------

def class_pyify(ref):
    return ref.replace("-","_")

PROTECTED_KEYWORDS = ["import", "def", "if", "else", "return", "for",
                      "while", "not", "try", "except", "in"]


def def_init(imports, attributes):
    indent = INDENT+INDENT
    indent3 = INDENT+INDENT+INDENT
    line = ["%sdef __init__(self," % INDENT]

    for elem in attributes:
        if elem[0] in PROTECTED_KEYWORDS:
            _name = elem[0] +"_"
        else:
            _name = elem[0]

        if elem[2]:
            line.append("%s%s='%s'," % (indent3, _name, elem[2]))
        else:
            line.append("%s%s=%s," % (indent3, _name, elem[2]))

    for _, elems in imports.items():
        for elem in elems:
            if elem in PROTECTED_KEYWORDS:
                _name = elem +"_"
            else:
                _name = elem
            line.append("%s%s=None," % (indent3, _name))

    line.append("%stext=None," % indent3)
    line.append("%sextension_elements=None," % indent3)
    line.append("%sextension_attributes=None," % indent3)
    line.append("%s):" % indent)
    return line
    

def base_init(imports):
    line = []
    indent4 = INDENT+INDENT+INDENT+INDENT
    if not imports:
        line.append("%sSamlBase.__init__(self, " % (INDENT+INDENT))
        for attr in BASE_ELEMENT:
            if attr in PROTECTED_KEYWORDS:
                _name = attr + "_"
            else:
                _name = attr
            line.append("%s%s=%s," % (indent4, _name, _name))
        line.append("%s)" % indent4)
    else:
        # TODO have to keep apart which properties come from which superior
        for sup, elems in imports.items():
            line.append("%s%s.__init__(self, " % (INDENT+INDENT, sup))
            lattr = elems[:]
            lattr.extend(BASE_ELEMENT)
            for attr in lattr:
                if attr in PROTECTED_KEYWORDS:
                    _name = attr + "_"
                else:
                    _name = attr
                line.append("%s%s=%s," % (indent4, _name, _name))
            line.append("%s)" % indent4)
    return line
    

def initialize(attributes):
    indent = INDENT+INDENT
    line = []
    for prop, val, _default in attributes:
        if prop in PROTECTED_KEYWORDS:
            _name = prop +"_"
        else:
            _name = prop

        if val in PROTECTED_KEYWORDS:
            _vname = val +"_"
        else:
            _vname = val
            
        line.append("%sself.%s=%s" % (indent, _name, _vname))
    return line


def _mod_typ(prop):
    try:
        (mod, typ) = prop.type
    except ValueError:
        typ = prop.type
        mod = None
    except TypeError: # No type property
        try:
            (mod, typ) = prop.ref
        except ValueError:
            if prop.class_name:
                typ = prop.class_name
            else:
                typ = prop.ref
            mod = None
    
    return mod, typ


def _mod_cname(prop, cdict):
    if hasattr(prop, "scoped"):
        cname = prop.class_name
        mod = None
    else:
        (mod, typ) = _mod_typ(prop)
        if not mod:
            try:
                cname = cdict[typ].class_name
            except KeyError:
                cname = cdict[class_pyify(typ)].class_name
        else:
            cname = typ
        
    return mod, cname


def leading_uppercase(string):
    try:
        return string[0].upper()+string[1:]
    except IndexError:
        return string
    except TypeError:
        return ""


def leading_lowercase(string):
    try:
        return string[0].lower()+string[1:]
    except IndexError:
        return string
    except TypeError:
        return ""


def rm_duplicates(properties):
    keys = []
    clist = []
    for prop in properties:
        if prop.name in keys:
            continue
        else:
            clist.append(prop)
            keys.append(prop.name)
    return clist

# def rm_duplicates(lista):
#     res = []
#     for item in lista:
#         if item not in res:
#             res.append(item)
#     return res


def klass_namn(obj):
    if obj.class_name:
        return obj.class_name
    else:
        return obj.name
    

class PyObj(object):
    def __init__(self, name=None, pyname=None, root=None):
        self.name = name
        self.done = False
        self.local = True
        self.root = root
        self.superior = []
        self.value_type = ""
        self.properties = ([], [])
        self.abstract = False
        self.class_name = ""
        
        if pyname:
            self.pyname = pyname
        elif name:
            self.pyname = pyify(name)
        else:
            self.pyname = name

        self.type = None
            
    def child_spec(self, target_namespace, prop, mod, typ, lista):
        if mod:
            namesp = external_namespace(self.root.modul[mod])
            pkey = '{%s}%s' % (namesp, prop.name)
            typ = "%s.%s" % (mod, typ)
        else:
            pkey = '{%s}%s' % (target_namespace, prop.name)

        if lista:
            return "c_children['%s'] = ('%s', [%s])" % (
                        pkey, prop.pyname, typ)
        else:
            return "c_children['%s'] = ('%s', %s)" % (
                        pkey, prop.pyname, typ)
    
    def knamn(self, sup, cdict):
        cname = cdict[sup].class_name
        if not cname:
            (namesp, tag) = cdict[sup].name.split('.')
            if namesp:
                ctag = self.root.modul[namesp].factory(tag).__class__.__name__
                cname = '%s.%s' % (namesp, ctag)
            else:
                cname = tag + "_"
        return cname
    
    def _do_properties(self, line, cdict, ignore, target_namespace):
        args = []
        child = []

        try:
            (own, inh) = self.properties
        except AttributeError:
            (own, inh) = ([], [])
            
        for prop in own:
            if isinstance(prop, PyAttribute):
                line.append("%sc_attributes['%s'] = %s" % (INDENT, 
                                                    prop.name, prop.spec()))
                if prop.fixed:
                    args.append((prop.pyname, prop.fixed, None))
                else:
                    if prop.default:
                        args.append((prop.pyname, prop.pyname, prop.default))
                    else:
                        args.append((prop.pyname, prop.pyname, None))
                        
            elif isinstance(prop, PyElement):

                (mod, cname) = _mod_cname(prop, cdict)
                
                if prop.max == "unbounded":
                    lista = True
                    pmax = 0 # just has to be different from 1
                else:
                    pmax = int(prop.max)
                    lista = False
                    
                if prop.name in ignore:
                    pass
                else:
                    line.append("%s%s" % (INDENT, self.child_spec(
                                                        target_namespace, prop,
                                                        mod, cname,
                                                        lista)))

                pmin = int(getattr(prop, 'min', 1))

                if pmax == 1 and pmin == 1:
                    pass
                elif prop.max == "unbounded":
                    line.append( "%sc_cardinality['%s'] = {\"min\":%s}" % (
                                    INDENT, prop.pyname, pmin))
                else:
                    line.append(
                        "%sc_cardinality['%s'] = {\"min\":%s, \"max\":%d}" % (
                                    INDENT, prop.pyname, pmin, pmax))

                child.append(prop.pyname)
                if lista:
                    args.append((prop.pyname, "%s or []" % (prop.pyname,), 
                                    None))
                else:
                    args.append((prop.pyname, prop.pyname, None))
        
        return args, child, inh
        
    def _superiors(self, cdict):
        imps = {}

        try:
            superior = self.superior
            sups = []
            for sup in superior:
                klass = self.knamn(sup, cdict)
                sups.append(klass)
                
                imps[klass] = []
                for cla in cdict[sup].properties[0]:
                    if cla.pyname and cla.pyname not in imps[klass]: 
                        imps[klass].append(cla.pyname)
        except AttributeError:
            superior = []
            sups = []
        
        return superior, sups, imps
        
    def class_definition(self, target_namespace, cdict=None, ignore=None):
        line = []

        if self.root:
            if self.name not in [c.name for c in self.root.elems]:
                self.root.elems.append(self)

        (superior, sups, imps) = self._superiors(cdict)

        c_name = klass_namn(self)
            
        if not superior:
            line.append("class %s(SamlBase):" % (c_name,))
        else:
            line.append("class %s(%s):" % (c_name, ",".join(sups)))

        if hasattr(self, 'scoped'):
            pass
        else:
            line.append("%s\"\"\"The %s:%s element \"\"\"" % (INDENT, 
                                                        target_namespace,
                                                        self.name))
        line.append("")
        line.append("%sc_tag = '%s'" % (INDENT, self.name))
        line.append("%sc_namespace = NAMESPACE" % (INDENT,))
        try:
            if self.value_type:
                if isinstance(self.value_type, six.string_types):
                    line.append("%sc_value_type = '%s'" % (INDENT, 
                                                            self.value_type))
                else:
                    line.append("%sc_value_type = %s" % (INDENT, 
                                                        self.value_type))
        except AttributeError:
            pass

        if not superior:
            for var, cps in CLASS_PROP:
                line.append("%s%s = SamlBase.%s%s" % (INDENT, var, var, cps))
        else:
            for sup in sups:
                for var, cps in CLASS_PROP:
                    line.append("%s%s = %s.%s%s" % (INDENT, var, sup, var, 
                                                    cps))

        (args, child, inh) = self._do_properties(line, cdict, ignore, 
                                                target_namespace)
        
        if child:
            line.append("%sc_child_order.extend([%s])" % (INDENT,
                            "'"+"', '".join(child)+"'"))
            
        if args:
            if inh:
                cname = self.knamn(self.superior[0], cdict)
                imps = {cname: [c.pyname for c in inh if c.pyname]}
            line.append("")
            line.extend(def_init(imps, args))
            line.extend(base_init(imps))
            line.extend(initialize(args))
        
        line.append("")
        if not self.abstract or not self.class_name.endswith("_"):
            line.append("def %s_from_string(xml_string):" % pyify(
                                                            self.class_name))
            line.append(
                "%sreturn saml2.create_class_from_xml_string(%s, xml_string)" % (
                            INDENT, self.class_name))
            line.append("")
        
        self.done = True
        return "\n".join(line)
    
def prepend(add, orig):
    # return a list which is the lists concatenated with the second list first
    res = [add]
    if orig:
        res.extend(orig)
    return res
    
def pyobj_factory(name, value_type, elms=None):
    pyobj = PyObj(name, pyify(name))
    pyobj.value_type = value_type
    if elms:
        if name not in [c.name for c in elms]:
            elms.append(pyobj)
    return pyobj

def pyelement_factory(name, value_type, elms=None):
    obj = PyElement(name, pyify(name))
    obj.value_type = value_type
    if elms:
        if name not in [c.name for c in elms]:
            elms.append(obj)
    return obj
    
def expand_groups(properties, cdict):
    res = []
    for prop in properties:
        if isinstance(prop, PyGroup):
            # only own, what about inherited ? Not on groups ?
            cname = prop.ref[1]
            res.extend(cdict[cname].properties[0])
        else:
            res.append(prop)
            
    return res
    
class PyElement(PyObj):
    def __init__(self, name=None, pyname=None, root=None, parent=""):
        PyObj.__init__(self, name, pyname, root)
        if parent:
            self.class_name = "%s_%s" % (leading_uppercase(parent), self.name)
        else:
            self.class_name = leading_uppercase(self.name)
        self.ref = None
        self.min = 1
        self.max = 1
        self.definition = None
        self.orig = None
    
    # def prereq(self, prop):
    #     prtext = prop.text(target_namespace, cdict)
    #     if prtext == None:
    #         return []
    #     else:
    #         prop.done = True
    #         return prtext
        
    def undefined(self, cdict):
        try:
            (mod, typ) = self.type
            if not mod:
                cname = leading_uppercase(typ)
                if not cdict[cname].done:
                    return [cdict[cname]], []
        except ValueError:
            pass
        except TypeError: # could be a ref then or a PyObj instance
            if isinstance(self.type, PyType):
                return self.type.undefined(cdict)
            elif isinstance(self.ref, tuple):
                pass
            else:
                cname = leading_uppercase(self.ref)
                if not cdict[cname].done:
                    return [cdict[cname]], []
        return [], []
    
    def _local_class(self, typ, cdict, child, target_namespace, ignore):
        if typ in cdict and not cdict[typ].done:
            raise MissingPrerequisite(typ)
        else:
            self.orig = {"type": self.type}
            try:
                self.orig["superior"] = self.superior
            except AttributeError:
                self.orig["superior"] = []
            self.superior = [typ]
            req = self.class_definition(target_namespace, cdict, 
                                        ignore)
            if not child:
                req = [req]
            
            if not hasattr(self, 'scoped'):
                cdict[self.name] = self
                cdict[self.name].done = True
                if child:
                    cdict[self.name].local = True
            self.type = (None, self.name)
        
        return req
        
    def _external_class(self, mod, typ, cdict, child, target_namespace, 
                        ignore):
        # Will raise exception if class can't be found
        cname = self.root.modul[mod].factory(typ).__class__.__name__
        imp_name = "%s.%s" % (mod, cname)
            
        if imp_name not in cdict:
            # create import object so I can get the properties from it 
            # later
            impo = pyelement_factory(imp_name, None, None)
            impo.properties = [_import_attrs(self.root.modul[mod], typ, 
                                            self.root),[]]
            impo.class_name = imp_name
            cdict[imp_name] = impo
            impo.done = True
            if child:
                impo.local = True
        # and now for this object
        self.superior = [imp_name]
        text = self.class_definition(target_namespace, cdict, 
                                        ignore=ignore)
        
        return text
        
    def text(self, target_namespace, cdict, child=True, ignore=None):
        if ignore is None:
            ignore = []

        if child:
            text = []
        else:
            text = None
        req = []
        try:
            (mod, typ) = self.type
            if not mod:
                req = self._local_class(typ, cdict, child, 
                                        target_namespace, ignore)
            else:
                text = self._external_class(mod, typ, cdict, child, 
                                            target_namespace, ignore)
        except ValueError: # Simple type element
            if self.type:
                text = self.class_definition(target_namespace, cdict, 
                                                ignore=ignore)
                if child:
                    self.local = True
                self.done = True
                    
        except TypeError: # could be a ref then or a PyObj instance
            if isinstance(self.type, PyObj):
                pyobj = self.type
                pyobj.name = self.name
                pyobj.pyname = self.pyname
                pyobj.class_name = self.class_name
                cdict[self.name] = pyobj
                return pyobj.text(target_namespace, cdict, ignore=ignore)
            elif isinstance(self.ref, tuple):
                (mod, typ) = self.ref
                if mod:
                    #self.superior = ["%s.%s" % (mod, typ)]
                    if verify_import(self.root.modul[mod], typ):
                        return req, text
                    else:
                        raise Exception(
            "Import attempted on %s from %s module failed - wasn't there" % (
                                typ,mod))
                elif not child:
                    self.superior = [typ]
                    text = self.class_definition(target_namespace, cdict,
                                                    ignore=ignore)
            else:
                if not cdict[class_pyify(self.ref)].done:
                    raise MissingPrerequisite(self.ref)
                
        self.done = True
        return req, text
        
def _do(obj, target_namespace, cdict, prep):
    try:
        (req, text) = obj.text(target_namespace, cdict)
    except MissingPrerequisite:
        return [], None
        
    if text is None:
        if req:
            #prep = prepend(req, prep)
            prep.append(req)
        return prep, None
    else:
        obj.done = True
        if req:
            if isinstance(req, six.string_types):
                prep.append(req)
            else:
                prep.extend(req)
        if text:
            #prep = prepend(text, prep)
            prep.append(text)
    return prep

def reqursive_superior(supc, cdict):
    properties = supc.properties[0]
    for sup in supc.superior:
        rsup = cdict[sup]
        if rsup.properties[1]:
            properties.extend(rsup.properties[1])
        else:
            properties.extend(reqursive_superior(rsup, cdict))
    return properties
    
class PyType(PyObj):
    def __init__(self, name=None, pyname=None, root=None, superior=None, 
                internal=True, namespace=None):
        PyObj.__init__(self, name, pyname, root)
        self.class_name = leading_uppercase(self.name + '_')
        self.properties = ([], [])
        if superior:
            self.superior = [superior]
        else:
            self.superior = []
        self.value_type = None
        self.internal = internal
        self.namespace = namespace

    def text(self, target_namespace, cdict, _child=True, ignore=None,
             _session=None):
        if not self.properties and not self.type \
                and not self.superior:
            self.done = True
            return [], self.class_definition(target_namespace, cdict)

        if ignore is None:
            ignore = []
        req = []
        inherited_properties = []
        for sup in self.superior:
            try:
                supc = cdict[sup]
            except KeyError:
                (mod, typ) = sup.split('.')
                supc = pyobj_factory(sup, None, None)
                if mod:
                    supc.properties = [_import_attrs(self.root.modul[mod], 
                                                        typ, self.root),[]]
                cdict[sup] = supc
                supc.done = True
                
            if not supc.done:
                res = _do(supc, target_namespace, cdict, req)
                if isinstance(res, tuple):
                    return res
            
            if not self.properties[1]:
                inherited_properties = reqursive_superior(supc, cdict)
        
        if inherited_properties:
            self.properties = (self.properties[0], 
                                rm_duplicates(inherited_properties))
            
        (own, inh) = self.properties
        own = rm_duplicates(expand_groups(own, cdict))
        self.properties = (own, inh)
        for prop in own:
            if not prop.name: # Ignore
                continue 
            if not prop.done:
                if prop.name in ignore:
                    continue
                res = _do(prop, target_namespace, cdict, req)
                if res == ([], None):
                    # # Cleaning up
                    # for prp in own:
                    #     if prp == prop:
                    #         break
                    #     try:
                    #         if cdict[prp.name].local:
                    #             del cdict[prp.name]
                    #             if hasattr(prp, "orig") and prp.orig:
                    #                 for key, val in prp.orig.items():
                    #                     setattr(prp, key, val)
                    #             prp.done = False
                    #             prp.local = False
                    #     except KeyError:
                    #         pass
                    res = (req, None)
                if isinstance(res, tuple):
                    return res
        
        return req, self.class_definition(target_namespace, cdict, ignore)
    
    def undefined(self, cdict):
        undef = ([], [])

        for sup in self.superior:
            supc = cdict[sup]
            if not supc.done:
                undef[0].append(supc)

        (own, _) = self.properties
        for prop in own:
            if not prop.name: # Ignore
                continue 
            if isinstance(prop, PyAttribute):
                continue
            if not prop.done:
                undef[1].append(prop)
        return undef

class PyAttribute(PyObj):
    def __init__(self, name=None, pyname=None, root=None, external=False, 
                    namespace="", required=False, typ=""):
        PyObj.__init__(self, name, pyname, root)

        self.required = required
        self.external = external
        self.namespace = namespace
        self.base = None
        self.type = typ
        self.fixed = False
        self.default = None

    def text(self, _target_namespace, cdict, _child=True):
        if isinstance(self.type, PyObj):
            if not cdict[self.type.name].done:
                raise MissingPrerequisite(self.type.name)
                
        return [], [] # Means this elements definition is empty
        
    def spec(self):
        if isinstance(self.type, PyObj):
            return "('%s', %s, %s)" % (self.pyname, self.type.class_name, 
                                        self.required)
        else:
            if self.type:
                return "('%s', '%s', %s)" % (self.pyname, self.type, 
                                                self.required)
            else:
                return "('%s', '%s', %s)" % (self.pyname, self.base, 
                                                self.required)
       
class PyAny(PyObj):
    def __init__(self, name=None, pyname=None, _external=False, _namespace=""):
        PyObj.__init__(self, name, pyname)
        self.done = True

class PyAttributeGroup(object):
    def __init__(self, name, root):
        self.name = name
        self.root = root
        self.properties = []

class PyGroup(object):
    def __init__(self, name, root):
        self.name = name
        self.root = root
        self.properties = []
        self.done = False
        self.ref = []
    
    def text(self, _target_namespace, _dict, _child, _ignore):
        return [], []
        
    def undefined(self, _cdict):
        undef = ([], [])

        (own, _) = self.properties
        for prop in own:
            if not prop.name: # Ignore
                continue 
            if not prop.done:
                undef[1].append(prop)
        return undef
    
# -----------------------------------------------------------------------------
def verify_import(modul, tag):
    try:
        _ = modul.factory(tag)
        return True
    except Exception:
        return False
    
def external_namespace(modul):
    return modul.NAMESPACE

def _import_attrs(modul, tag, top):
    obj = modul.factory(tag)
    properties = [PyAttribute(key, val[0], top, True, obj.c_namespace, val[2],
                            val[1]) for key,val in obj.c_attributes.items()]
    for child in obj.c_child_order:
        for key, val in obj.c_children.items():
            (pyn, mul) = val
            maximum = 1
            if isinstance(mul, list):
                mul = mul[0]
                maximum = "unbounded"
            if pyn == child:
                cpy = PyElement(name=mul.c_tag, pyname=pyn, root=top) 
    #                            internal=False, ns=obj.c_namespace)
                cpy.max = maximum
                properties.append(cpy)

    return properties

# ------------------------------------------------------------------------

def _spec(elem):
    try:
        name = elem.name
    except AttributeError:
        name = "anonymous"
    txt = "%s" % name
    try:
        txt += " ref: %s" % elem.ref
    except AttributeError:
        try:
            txt += " type: %s" % elem.type
        except AttributeError:
            pass

    return txt
        
# def _klass(elem, _namespace, sup, top):
#     if elem.name in top.py_elements:
#         return None
#     else:
#         kl = PyType(elem.name, root=top)
#         top.py_elements[elem.name] = kl
#         if sup != "SamlBase":
#             kl.superior.append(sup)
#         return kl
        
def _do_from_string(name):
    print
    print("def %s_from_string(xml_string):" % pyify(name))
    print("%sreturn saml2.create_class_from_xml_string(%s, xml_string)" % (
                INDENT, name))

def _namespace_and_tag(obj, param, top):
    try:
        (namespace, tag) = param.split(":")
    except ValueError:
        namespace = ""
        tag = param
    # except AttributeError:
    #     namespace = ""
    #     tag = obj.name

    return namespace, tag
    
# -----------------------------------------------------------------------------

class Simple(object):
    def __init__(self, elem):
        self.default = None
        self.fixed = None
        self.xmlns_map = []
        self.name = ""
        self.type = None
        self.use = None
        self.ref = None
        self.scoped = False
        self.itemType = None
        
        for attribute, value in iter(elem.attrib.items()):
            self.__setattr__(attribute, value)

    def collect(self, top, sup, argv=None, parent=""):
        argv_copy = sd_copy(argv)
        rval = self.repr(top, sup, argv_copy, True, parent)
        if rval:
            return [rval], []
        else:
            return [], []

    def repr(self, _top=None, _sup=None, _argv=None, _child=True, _parent=""):
        return None
        
    def elements(self, _top):
        return []

        
class Any(Simple):
    
    def repr(self, _top=None, _sup=None, _argv=None, _child=True, _parent=""):
        return PyAny()
        
class AnyAttribute(Simple):

    def repr(self, _top=None, _sup=None, _argv=None, _child=True, _parent=""):
        return PyAny()

class Attribute(Simple):
    def repr(self, top=None, sup=None, _argv=None, _child=True, _parent=""):
        # default, fixed, use, type
                    
        if DEBUG:
            print("#ATTR", self.__dict__)

        external = False
        name = ""
        try:
            (namespace, tag) = _namespace_and_tag(self, self.ref, top)
            ref = True
            pyname = tag
            if namespace in self.xmlns_map:
                if self.xmlns_map[namespace] == top.target_namespace:
                    name = tag
                else :
                    external = True
                    name = "{%s}%s" % (self.xmlns_map[namespace], tag)
            else:
                if namespace == "xml":
                    name = "{%s}%s" % (XML_NAMESPACE, tag)
        except AttributeError:
            name = self.name
            pyname = pyify(name)
            ref = False
        except ValueError: # self.ref exists but does not split into two parts
            ref = True
            if "" == top.target_namespace:
                name = self.ref
                pyname = pyify(name)
            else: # referering to what
                raise Exception("Strange reference: %s" % self.ref)
                    
        objekt = PyAttribute(name, pyname, external=external, root=top)
        
        # Initial declaration
        if not ref:
            try:
                (namespace, klass) = _namespace_and_tag(self, self.type, top)
                if self.xmlns_map[namespace] == top.target_namespace:
                    ctyp = get_type_def(klass, top.parts)
                    if not ctyp.py_class:
                        ctyp.repr(top, sup)
                    objekt.type = ctyp.py_class
                elif self.xmlns_map[namespace] == XMLSCHEMA:
                    objekt.type = klass
                else:
                    objekt.type = self.type
            except ValueError:
                if self.xmlns_map[""] == top.target_namespace:
                    ctyp = get_type_def(self.type.replace("-","_"), top.parts)
                    if not ctyp.py_class:
                        ctyp.repr(top, sup)
                    objekt.type = ctyp.py_class                    
                else:
                    objekt.type = self.type
            except AttributeError:
                objekt.type = None
        try:
            if self.use == "required":
                objekt.required = True
        except AttributeError:
            pass
            
        # in init
        try:
            objekt.default = self.default
        except AttributeError:
            pass
                
        # attr def
        try:
            objekt.fixed = self.fixed
        except AttributeError:
            pass
        
        if DEBUG:
            print("#--ATTR py_attr:%s" % (objekt,))
            
        return objekt
        
class Enumeration(Simple):
    pass
    
class Union(Simple):
    pass
    
class Import(Simple):
    pass
    
class Documentation(Simple):
    pass
    
class MaxLength(Simple):
    pass

class Length(Simple):
    pass
    
class MinInclusive(Simple):
    pass
    
class MaxInclusive(Simple):
    pass

class MinExclusive(Simple):
    pass

class MaxExclusive(Simple):
    pass
    
class List(Simple):
    pass

class Include(Simple):
    pass


# -----------------------------------------------------------------------------

def sequence(elem):
    return [evaluate(child.tag, child) for child in elem]

def name_or_ref(elem, top):
    try:
        (namespace, name) = _namespace_and_tag(elem, elem.ref, top)
        if namespace and elem.xmlns_map[namespace] == top.target_namespace:
            return name
        else:
            return elem.ref
    except AttributeError:
        return elem.name

class Complex(object):
    def __init__(self, elem):
        self.value_of = ""
        self.parts = []
        self._own = []
        self._inherited = []
        self._generated = False
        self.py_class = None
        self.properties = []
        # From Elementtree
        self.ref = None
        self.type = None
        self.xmlns_map = []
        self.maxOccurs = 1
        self.minOccurs = 1
        self.base = None
        self.scoped = False
        self.abstract = False
        
        for attribute, value in iter(elem.attrib.items()):
            self.__setattr__(attribute, value)

        try:
            if elem.text.strip():
                self.value_of = elem.text.strip()
        except AttributeError:
            pass

        self.do_child(elem)
        
        try:
            self.name = self.name.replace("-","_")
        except AttributeError:
            pass

    def _extend(self, top, sup, argv=None, parent="", base=""):
        argv_copy = sd_copy(argv)
        for part in self.parts:
            (own, inh) = part.collect(top, sup, argv_copy, parent)
            if own and base:
                if len(own) == 1 and isinstance(own[0], PyAttribute):
                    own[0].base = base
            self._own.extend(own)
            self._inherited.extend(inh)
        
    def collect(self, top, sup, argv=None, parent=""):
        if self._own or self._inherited:
            return self._own, self._inherited
            
        if DEBUG:
            print(self.__dict__)
            print("#-- %d parts" % len(self.parts))
        
        self._extend(top, sup, argv, parent)
        
        return self._own, self._inherited
        
    def do_child(self, elem):
        for child in elem:
            self.parts.append(evaluate(child.tag, child))

    def elements(self, top):
        res = []
        # try:
        #     string = "== %s (%s)" % (self.name,self.__class__)
        # except AttributeError:
        #     string = "== (%s)" % (self.__class__,)
        # print(string)
        for part in self.parts:
            if isinstance(part, Element):
                res.append(name_or_ref(part, top))
            else:
                if isinstance(part, Extension):
                    res.append(part.base)
                res.extend(part.elements(top))

        return res

    def repr(self, _top=None, _sup=None, _argv=None, _child=True, parent=""):
        return None

    def significant_parts(self):
        res = []
        for p in self.parts:
            if isinstance(p, Annotation):
                continue
            else:
                res.append(p)
                
        return res
        
def min_max(cls, objekt, argv):
    try:
        objekt.max = argv["maxOccurs"]
        if cls.maxOccurs != 1:
            objekt.max = cls.maxOccurs
    except (KeyError, TypeError):
        objekt.max = cls.maxOccurs

    try:
        objekt.min = argv["minOccurs"]
        if cls.minOccurs != 1:
            objekt.min = cls.minOccurs
    except (KeyError, TypeError):
        objekt.min = cls.minOccurs
            
    
class Element(Complex):
    def __str__(self):
        return "%s" % (self.__dict__,)

    def klass(self, top):
        xns = None
        ctyp = None
        ref = False
        try:
            (namespace, name) = _namespace_and_tag(self, self.ref, top)
            ref = True
        except AttributeError:
            try:
                (namespace, name) = self.type.split(":")
            except ValueError:
                namespace = None
                name = self.type
            except AttributeError:
                namespace = name = None

        if self.xmlns_map[namespace] == top.target_namespace:
            ctyp = get_type_def(name, top.parts)
        else:
            xns = namespace

        return namespace, name, ctyp, xns, ref

    def collect(self, top, sup, argv=None, parent=""):
        """ means this element is part of a larger object, hence a property of 
        that object """
        
        try:
            argv_copy = sd_copy(argv)
            return [self.repr(top, sup, argv_copy, parent=parent)], []
        except AttributeError as exc:
            print("#!!!!", exc)
            return [], []

    def elements(self, top):            
        (_namespace, name, ctyp, xns, _) = self.klass(top)
        if ctyp:
            return ctyp.elements(top)
        elif xns:
            return ["%s.%s" % (xns, name)]
        else:
            return []

    def repr(self, top=None, sup=None, argv=None, child=True, parent=""):
        #<element ref='xenc:ReferenceList' ...
        #<element name='Transforms' type='xenc:TransformsType' ...
        #<element name='CarriedKeyName' type='string' ...
        #<element name="RecipientKeyInfo" type="ds:KeyInfoType" ...
        #<element name='ReferenceList'>

        if self.py_class:
            return self.py_class
            
        try:
            myname = self.name
        except AttributeError:
            myname = ""

        if DEBUG:
            print("#Element.repr '%s' (child=%s) [%s]" %
                  (myname, child, self._generated))

        self.py_class = objekt = PyElement(myname, root=top)
        min_max(self, objekt, argv)
                
        try:
            (namespace, superkl) = _namespace_and_tag(self, self.ref, top)
            # internal or external reference
            if not myname:
                objekt.name = superkl
                objekt.pyname = pyify(superkl)
            if self.xmlns_map[namespace] == top.target_namespace:
                objekt.ref = superkl 
            else:
                objekt.ref = (namespace, superkl)                
        except AttributeError as exc:
            if DEBUG:
                print("#===>", exc)

            typ = self.type

            try:
                (namespace, klass) = _namespace_and_tag(self, typ, top)
                if self.xmlns_map[namespace] == top.target_namespace:
                    objekt.type = (None, klass)
                elif self.xmlns_map[namespace] == XMLSCHEMA:
                    objekt.type = klass
                    objekt.value_type = {"base": klass}
                else:
                    objekt.type = (namespace, klass)

            except ValueError:
                objekt.type = typ
                objekt.value_type = {"base": typ}

            except AttributeError:
                # neither type nor reference, definitely local
                if hasattr(self, "parts"):
                    if len(self.parts) == 1:
                        if isinstance(self.parts[0], ComplexType) or \
                            isinstance(self.parts[0], SimpleType):
                            self.parts[0].name = self.name
                            objekt.type = self.parts[0].repr(top, sup, 
                                                            parent=self.name)
                            objekt.scoped = True
                    elif len(self.parts) == 2:# One child might be Annotation
                        if isinstance(self.parts[0], Annotation):
                            self.parts[1].name = self.name
                            objekt.type = self.parts[1].repr(top, sup, 
                                                            parent=self.name)
                            objekt.scoped = True
                        elif isinstance(self.parts[1], Annotation):
                            self.parts[0].name = self.name
                            objekt.type = self.parts[0].repr(top, sup, 
                                                            parent=self.name)
                            objekt.scoped = True
                else:
                    if DEBUG:
                        print("$", self)
                    raise 

            if parent:
                objekt.class_name = "%s_%s" % (
                                        leading_uppercase(parent),
                                        objekt.name)
                objekt.scoped = True
                
        return objekt


class SimpleType(Complex):
    def repr(self, top=None, _sup=None, _argv=None, _child=True, parent=""):
        if self.py_class:
            return self.py_class
            
        obj = PyType(self.name, root=top)
        try:
            if len(self.parts) == 1:
                part = self.parts[0]
                if isinstance(part, Restriction):
                    if part.parts:
                        if isinstance(part.parts[0], Enumeration):
                            lista = [p.value for p in part.parts]
                            obj.value_type = {"base":part.base,
                                                "enumeration":lista}
                        elif isinstance(part.parts[0], MaxLength):
                            obj.value_type = {"base":part.base,
                                                "maxlen":part.parts[0].value}
                        elif isinstance(part.parts[0], Length):
                            obj.value_type = {"base":part.base,
                                                "len":part.parts[0].value}
                    else:
                        obj.value_type = {"base":part.base}
                elif isinstance(part, List):
                    if part.itemType:
                        obj.value_type = {"base":"list", "member":part.itemType}
        except ValueError:
            pass
        
        self.py_class = obj
        return obj
        

class Sequence(Complex):
    def collect(self, top, sup, argv=None, parent=""):
        argv_copy = sd_copy(argv)
        for key, val in self.__dict__.items():
            if key not in ['xmlns_map'] and not key.startswith("_"):
                argv_copy[key] = val
    
        if DEBUG:
            print("#Sequence: %s" % argv)
        return Complex.collect(self, top, sup, argv_copy, parent)


class SimpleContent(Complex):
    pass


class ComplexContent(Complex):
    pass


class Key(Complex):
    pass


class Redefine(Complex):
    pass


class Extension(Complex):
    def collect(self, top, sup, argv=None, parent=""):
        if self._own or self._inherited:
            return self._own, self._inherited
        
        if DEBUG:
            print("#!!!", self.__dict__)

        try:
            base = self.base
            (namespace, tag) = _namespace_and_tag(self, base, top)
                    
            if self.xmlns_map[namespace] == top.target_namespace:
                cti = get_type_def(tag, top.parts)
                if not cti.py_class:
                    cti.repr(top, sup)
                #print("#EXT..",ct._collection)
                self._inherited = cti.py_class.properties[0][:]
                self._inherited.extend(cti.py_class.properties[1])
            elif self.xmlns_map[namespace] == XMLSCHEMA: 
                base = tag
            else:
                iattr = _import_attrs(top.modul[namespace], tag, top)
                #print("#EXT..-", ia)
                self._inherited = iattr
        except (AttributeError, ValueError):
            base = None

        self._extend(top, sup, argv, parent, base)

        return self._own, self._inherited

class Choice(Complex):
    def collect(self, top, sup, argv=None, parent=""):
        argv_copy = sd_copy(argv)
        for key, val in self.__dict__.items():
            if key not in ['xmlns_map'] and not key.startswith("_"):
                argv_copy[key] = val

        # A choice means each element may not be part of the choice
        argv_copy["minOccurs"] = 0
            
        if DEBUG:
            print("#Choice: %s" % argv)
        return Complex.collect(self, top, sup, argv_copy, parent=parent)

class Restriction(Complex):
    pass
    # if isinstance(self.parts[0], Enumeration):
    #     values = [enum.value for enum in self.parts]

class ComplexType(Complex):
    def repr(self, top=None, sup=None, _argv=None, _child=True, parent=""):
        if self.py_class:
            return self.py_class
                    
        # looking for a pattern here
        significant_parts = self.significant_parts()
        value_type = ""
        if len(significant_parts) == 1:
            if isinstance(significant_parts[0], ComplexContent) or \
                isinstance(significant_parts[0], SimpleContent):
                cci = significant_parts[0]
                if len(cci.parts) == 1:
                    if isinstance(cci.parts[0], Extension):
                        ext = cci.parts[0]

                        (namespace, name) = _namespace_and_tag(ext, ext.base, 
                                                                top)

                        if ext.xmlns_map[namespace] == top.target_namespace:
                            new_sup = name
                            cti = get_type_def(new_sup, top.parts)
                            if cti and not cti.py_class:
                                cti.repr(top, sup)
                        elif ext.xmlns_map[namespace] == XMLSCHEMA:
                            new_sup = None
                            value_type = name
                        else:
                            new_sup = "%s.%s" % (namespace, name)
                            
                        #print("#Superior: %s" % new_sup)
                        if new_sup:
                            sup = new_sup
            else:
                #print("#>>", self.parts[0].__class__)
                pass
                
        try:
            self.py_class = PyType(self.name, superior=sup, 
                                    namespace=top.target_namespace, root=top)
        except AttributeError: # No name 
            self.py_class = PyType("", superior=sup, 
                                    namespace=top.target_namespace, root=top)

        try:
            self.py_class.abstract = self.abstract
        except AttributeError:
            pass
        
        if value_type:
            self.py_class.value_type = {"base": value_type}
            
        try:
            if not parent:
                try:
                    parent = self.name
                except AttributeError:
                    parent = ""
            
            self.py_class.properties = self.collect(top, sup, parent=parent)
        except ValueError:
            pass
            
        return self.py_class 
        
class Annotation(Complex):
    pass

class All(Complex):
    pass

class Group(Complex):
    def collect(self, top, sup, argv=None, parent=""):
        """ means this element is part of a larger object, hence a property of 
        that object """
        
        try:
            #objekt = PyGroup("", root=top)
            (namespace, tag) = _namespace_and_tag(self, self.ref, top)
    
            try:
                if self.xmlns_map[namespace] == top.target_namespace:
                    cti = get_type_def(tag, top.parts)
                    try:
                        return cti.py_class.properties
                    except ValueError:
                        return cti.collect(top, sup) 
                else:
                    raise Exception(
                        "Reference to group in other XSD file, not supported")
            except KeyError:
                raise Exception("Missing namespace definition")            
        except AttributeError as exc:
            print("#!!!!", exc)
            return [], []

    def repr(self, top=None, sup=None, argv=None, _child=True, parent=""):
        if self.py_class:
            return self.py_class

        self.py_class = objekt = PyGroup(self.name, root=top)

        min_max(self, objekt, argv)
        
        try:
            self._extend(top, sup, argv)
            objekt.properties = (self._own, self._inherited)
        except ValueError:
            pass
            
        return objekt

class Unique(Complex):
    pass

class Selector(Complex):
    pass

class Field(Complex):
    pass

class AttributeGroup(Complex):
    def collect(self, top, sup, argv=None, parent=""):
        try:
            (_namespace, typ) = _namespace_and_tag(self, self.ref, top)
            # TODO: use definitions in other XSD
            cti = get_type_def(typ, top.parts)
            return cti.collect(top, sup)
        except AttributeError:
            if self._own or self._inherited:
                return self._own, self._inherited
            
            argv_copy = sd_copy(argv)
            
            for prop in self.parts:
                if isinstance(prop, Attribute):
                    self._own.append(prop.repr(top, sup, argv_copy, parent))

            return self._own, self._inherited

    def repr(self, top=None, sup=None, _argv=None, _child=True, parent=""):
        if self.py_class:
            return self.py_class
            
        self.py_class = PyAttributeGroup(self.name, root=top)

        try:
            self.py_class.properties = self.collect(top, sup)
        except ValueError:
            pass
            
        return self.py_class 

def pyify_0(name):
    res = ""
    match = re.match(
            r"^(([A-Z])[a-z]+)(([A-Z])[a-z]+)?(([A-Z])[a-z]+)?(([A-Z])[a-z]+)?",
            name)
    res += match.group(1).lower()
    for num in range(3, len(match.groups()), 2):
        try:
            res += "_"+match.group(num+1).lower()+match.group(num)[1:]
        except AttributeError:
            break
    
    res = res.replace("-","_")
    if res in ["class"]:
        res += "_"
    return res


def pyify(name):
    # AssertionIDRef
    res = []
    
    upc = []
    pre = ""
    for char in name:
        if "A" <= char <= "Z":
            upc.append(char)
        elif char == "-":
            upc.append("_")
        else:
            if upc:
                if len(upc) == 1:
                    res.append(pre+upc[0].lower())
                else:
                    if pre:
                        res.append(pre)
                    for uch in upc[:-1]:
                        res.append(uch.lower())
                    res.append("_"+upc[-1].lower())
                        
                upc = []
            res.append(char)
            pre = "_"
    if upc:
        if len(upc) == len(name):
            return name.lower()
        else:
            res.append("_"+("".join(upc).lower()))
        
    return "".join(res)


def get_type_def( typ, defs):
    for cdef in defs:
        try:
            if cdef.name == typ:
                return cdef
        except AttributeError:
            pass
    return None
    

def sort_elements(els):
    res = []
    
    diff = False
    for key, val in els.items():
        if not val:
            res.append(key)
            del els[key]
            diff = True
    
    res.sort()
    while diff:
        diff = False
        for key, val in els.items():
            pres = [v for v in val if v not in res and ':' not in v]
            els[key] = pres
            if pres != val:
                diff = True

        #print(els)
        partres = []
        for key, val in els.items():
            if not val:
                partres.append(key)
                del els[key]
                diff = True
        partres.sort()
        res.extend(partres)
        
    return res, els


def output(elem, target_namespace, eldict, ignore=None):
    done = 0

    if ignore is None:
        ignore = []
        
    try:
        (preps, text) = elem.text(target_namespace, eldict, False, ignore)
    except TypeError:
        return done
    except MissingPrerequisite:
        return done
    
    for prep in preps:
        if prep:
            done = 1
            if isinstance(prep, six.string_types):
                print(prep)
            else:
                for item in prep:
                    print(item)
                    print()
            print()

    if text:
        done = 1
        elem.done = True
        print(text)
        print()
    
    return done
    

def intro():
    print("""#!/usr/bin/env python

#
# Generated %s by parse_xsd.py version %s.
#

import saml2
from saml2 import SamlBase
""" % (time.ctime(), __version__))

#NAMESPACE = 'http://www.w3.org/2000/09/xmldsig#'
    

def block_items(objekt, block, eldict):
    if objekt not in block:
        if isinstance(objekt.type, PyType):
            if objekt.type not in block:
                block.append(objekt.type)
        block.append(objekt)
        if isinstance(objekt, PyType):
            others = [p for p in eldict.values() if isinstance(p, 
                                    PyElement) and p.type[1] == objekt.name]
            for item in others:
                if item not in block:
                    block.append(item)
    return block

    
def find_parent(elm, eldict):
    if isinstance(elm, PyElement):
        if elm.type:
            sup = eldict[elm.type[1]]
            return find_parent(sup, eldict)
        elif elm.ref:
            sup = eldict[elm.ref]
            if sup.name == elm.name:
                return elm
            return find_parent(sup, eldict)
    else:
        if elm.superior:
            sup = eldict[elm.superior[0]]
            if sup.done:
                return elm
            return find_parent(sup, eldict)
    
    return elm
    

class Schema(Complex):

    def __init__(self, elem, impo, add, modul, defs):
        Complex.__init__(self, elem)
        self.impo = impo
        self.add = add
        self.modul = modul
        self.py_elements = {}
        self.py_attributes = {}
        self.elems = []
        self.attrgrp = []
        self.defs = []
        try:
            self.target_namespace = self.targetNamespace
        except AttributeError:
            self.target_namespace = ""
        for def_file in defs:
            self.defs.append(open(def_file).read())

    def _mk_list(self, objekt, alla, eldict):
        tup = []
        for prop in alla:
            (mod, cname) = _mod_cname(prop, eldict)

            if prop.max == "unbounded":
                lista = True
            else:
                lista = False

            spec = objekt.child_spec(self.target_namespace, 
                                        prop, mod, cname, 
                                        lista)
            lines = ["%s.%s" % (objekt.class_name, spec)]
            tup.append((prop, lines, spec))
        
        return tup
        
    def adjust(self, eldict, block):
        udict = {}
        for elem in self.elems:
            if isinstance(elem, PyAttribute) or isinstance(elem, PyGroup):
                elem.done = True
                continue
            if elem in block:
                continue
            if not elem.done:
                udict[elem] = elem.undefined(eldict)

        keys = [k.name for k in udict.keys()]
        print("#", keys)
        res = (None, [])
        if not udict:
            return res
        level = 1
        rblocked = [p.name for p in block]
        while True:
            non_child = 0
            for objekt, (sup, elems) in udict.items():
                if sup:
                    continue
                else:
                    non_child += 1
                    signif = []
                    other = []
                    for elem in elems:
                        if elem.name in keys:
                            signif.append(elem)
                        elif elem.ref in rblocked:
                            other.append(elem)
                    if len(signif) <= level:
                        alla = signif
                        alla.extend(other)
                        tup = self._mk_list(objekt, alla, eldict)
                        res = (objekt, tup)
                        break
            if res[0]:
                ref = res[0].name
                tups = res[1]
                for objekt, (sups, elems) in udict.items():
                    if sups:
                        for sup in sups:
                            if sup.name == ref:
                                for tup in tups:
                                    tup[1].append("%s.%s" % (objekt.class_name,
                                                            tup[2]))
                                break
                    else:
                        pass
            elif not non_child or level > 10:
                elm = udict.keys()[0]
                parent = find_parent(elm, eldict)
                signif = []
                other = []
                tot = parent.properties[0]
                tot.extend(parent.properties[1])
                alla = []
                for elem in tot:
                    if isinstance(elem, PyAttribute):
                        continue
                    else:
                        alla.append(elem)
                tup = self._mk_list(parent, alla, eldict)
                res = (parent, tup)
                
            if res[0]:
                break
            else:
                level += 1
        return res

    def _do(self, eldict):
        not_done = 1
        undone = 0
        while not_done:
            not_done = 0
            undone = 0
            for elem in self.elems:
                if isinstance(elem, PyGroup) or elem.done:
                    continue
                undone += 1
                not_done += output(elem, self.target_namespace, eldict)
        return undone
        
    def _element_from_string(self):
        print("ELEMENT_FROM_STRING = {")
        for elem in self.elems:
            if isinstance(elem, PyAttribute) or isinstance(elem, PyGroup):
                continue
            if elem.abstract:
                continue
            print("%s%s.c_tag: %s_from_string," % (INDENT, elem.class_name, 
                                                   pyify(elem.class_name)))
        print("}")
        print()
        
    def _element_by_tag(self):
        print("ELEMENT_BY_TAG = {")
        listed = []
        for elem in self.elems:
            if isinstance(elem, PyAttribute) or isinstance(elem, PyGroup):
                continue
            if elem.abstract:
                continue
            lcen = elem.name
            print("%s'%s': %s," % (INDENT, lcen, elem.class_name))
            listed.append(lcen)
        for elem in self.elems:
            if isinstance(elem, PyAttribute) or isinstance(elem, PyGroup):
                continue
            lcen = elem.name
            if elem.abstract and lcen not in listed:
                print("%s'%s': %s," % (INDENT, lcen, elem.class_name))
                listed.append(lcen)
        print("}")
        print
        
    def out(self):
        for part in self.parts:
            if isinstance(part, Import):
                continue
            if part is None:
                continue
                
            elem = part.repr(self, "", {}, False)
            if elem:
                if isinstance(elem, PyAttributeGroup):
                    self.attrgrp.append(elem)
                else:
                    self.elems.append(elem)
        
        eldict = {}
        for elem in self.elems:
            eldict[elem.name] = elem

        #print(eldict.keys())
        
        intro()
        for modul in self.add:
            print("from %s import *" % modul)
        for _namespace, (mod, namn) in self.impo.items():
            if namn:
                print("import %s as %s" % (mod, namn))
        print(       )
        print("NAMESPACE = '%s'" % self.target_namespace)
        print

        for defs in self.defs:
            print(defs)
            print
        
        exceptions = []
        block = []
        while self._do(eldict):
            print("#..................")
            (objekt, tups) = self.adjust(eldict, block)
            if not objekt:
                break
            ignore = [p.name for (p, _l, _s) in tups]
            done = output(objekt, self.target_namespace, eldict, ignore)
            if done:
                for (prop, lines, _) in tups:
                    exceptions.extend(lines)
                block = []
            else:
                block = block_items(objekt, block, eldict)

        if exceptions:
            print("#", 70*'+')
            for line in exceptions:
                print(line)
            print("#", 70*'+')
            print
        
        for attrgrp in self.attrgrp:
            print("AG_%s = [" % attrgrp.name)
            for prop in attrgrp.properties[0]:
                if isinstance(prop.type, PyObj):
                    print("%s('%s', %s_, %s)," % (INDENT, prop.name,
                                                  prop.type.name,
                                                  prop.required))
                else:
                    print("%s('%s', '%s', %s)," % (INDENT, prop.name,
                                                   prop.type, prop.required))
            print("]")
            print()
           
        self._element_from_string() 
        self._element_by_tag()
        print
        print("def factory(tag, **kwargs):")
        print("    return ELEMENT_BY_TAG[tag](**kwargs)")
        print
        
        
# -----------------------------------------------------------------------------


NAMESPACE_BASE = ["http://www.w3.org/2001/XMLSchema",
    "http://www.w3.org/2000/10/XMLSchema"]

_MAP = {    
    "element": Element,
    "complexType": ComplexType,
    "sequence": Sequence,
    "any": Any,
    "all": All,
    "anyAttribute": AnyAttribute,
    "simpleContent": SimpleContent,
    "extension": Extension,
    "union": Union,
    "restriction": Restriction,
    "enumeration": Enumeration,
    "import": Import,
    "annotation": Annotation,
    "attributeGroup":AttributeGroup,
    "attribute":Attribute,
    "choice": Choice,
    "complexContent": ComplexContent,
    "documentation": Documentation,
    "simpleType": SimpleType,
    "maxLength": MaxLength,
    "list": List,
    "unique": Unique,
    "group": Group,
    "selector": Selector,
    "field": Field,
    "key": Key,
    "include": Include,
    "redefine": Redefine
    }
    
ELEMENTFUNCTION = {}

for nsp in NAMESPACE_BASE:
    for nskey, func in _MAP.items():
        ELEMENTFUNCTION["{%s}%s" % (nsp, nskey)] = func

    
def evaluate(typ, elem):
    try:
        return ELEMENTFUNCTION[typ](elem)
    except KeyError:
        print("Unknown type", typ)
        
    
NS_MAP = "xmlns_map"

def parse_nsmap(fil):
    events = "start", "start-ns", "end-ns"

    root = None
    ns_map = []

    for event, elem in ElementTree.iterparse(fil, events):
        if event == "start-ns":
            ns_map.append(elem)
        elif event == "end-ns":
            ns_map.pop()
        elif event == "start":
            if root is None:
                root = elem
            elem.set(NS_MAP, dict(ns_map))

    return ElementTree.ElementTree(root)

def usage():
    print("Usage: parse_xsd [-i <module:as>] xsd.file > module.py")
    
def recursive_find_module(name, path=None):
    parts = name.split(".")

    mod_a = None
    for part in parts:
        #print("$$", part, path)
        try:
            (fil, pathname, desc) = imp.find_module(part, path)
        except ImportError:
            raise 

        mod_a = imp.load_module(name, fil, pathname, desc)
        sys.modules[name] = mod_a
        path = mod_a.__path__

    return mod_a

def get_mod(name, path=None):
    try:
        mod_a = sys.modules[name]
        if not isinstance(mod_a, types.ModuleType):
            raise KeyError
    except KeyError:
        try:
            (fil, pathname, desc) = imp.find_module(name, path)
            mod_a = imp.load_module(name, fil, pathname, desc)
        except ImportError:
            if "." in name:
                mod_a = recursive_find_module(name, path)
            else:
                raise
        sys.modules[name] = mod_a
    return mod_a


def recursive_add_xmlns_map(_sch, base):
    for _part in _sch.parts:
        _part.xmlns_map.update(base.xmlns_map)
        if isinstance(_part, Complex):
            recursive_add_xmlns_map(_part, base)

def find_and_replace(base, mods):
    base.xmlns_map = mods.xmlns_map
    recursive_add_xmlns_map(base, mods)
    rm = []
    for part in mods.parts:
        try:
            _name = part.name
        except AttributeError:
            continue
        for _part in base.parts:
            try:
                if _name == _part.name:
                    rm.append(_part)
            except AttributeError:
                continue
    for part in rm:
        base.parts.remove(part)
    base.parts.extend(mods.parts)
    return base

def read_schema(doc, add, defs, impo, modul, ignore, sdir):
    for path in sdir:
        fil = "%s%s" % (path, doc)
        try:
            fp = open(fil)
            fp.close()
            break
        except IOError as e:
            if e.errno == errno.EACCES:
                continue
    else:
        raise Exception("Could not find schema file")

    tree = parse_nsmap(fil)

    known = NAMESPACE_BASE[:]
    known.append(XML_NAMESPACE)
    for key, namespace in tree._root.attrib["xmlns_map"].items():
        if namespace in known:
            continue
        else:
            try:
                modul[key] = modul[namespace]
                impo[namespace][1] = key
            except KeyError:
                if namespace == tree._root.attrib["targetNamespace"]:
                    continue
                elif namespace in ignore:
                    continue
                else:
                    raise Exception("Undefined namespace: %s" % namespace)

    _schema =  Schema(tree._root, impo, add, modul, defs)
    _included_parts = []
    _remove_parts = []
    _replace = []
    for part in _schema.parts:
        if isinstance(part, Include):
            _sch = read_schema(part.schemaLocation, add, defs, impo, modul,
                               ignore, sdir)
            # Add namespace information
            recursive_add_xmlns_map(_sch, _schema)
            _included_parts.extend(_sch.parts)
            _remove_parts.append(part)
        elif isinstance(part, Redefine):
            # This is the schema that is going to be redefined
            _redef = read_schema(part.schemaLocation, add, defs, impo, modul,
                                 ignore, sdir)
            # so find and replace
            # Use the schema to be redefined as starting point
            _replacement = find_and_replace(_redef, part)
            _replace.append((part, _replacement.parts))

    for part in _remove_parts:
        _schema.parts.remove(part)
    _schema.parts.extend(_included_parts)
    if _replace:
        for vad, med in _replace:
            _schema.parts.remove(vad)
            _schema.parts.extend(med)
    return _schema

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "a:d:hi:I:s:",
                                    ["add=", "help", "import=", "defs="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(str(err)) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    add = []
    defs = []
    impo = {}
    modul = {}
    ignore = []
    sdir = ["./"]

    for opt, arg in opts:
        if opt in ("-a", "--add"):
            add.append(arg)
        elif opt in ("-d", "--defs"):
            defs.append(arg)
        elif opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-s", "--schemadir"):
            sdir.append(arg)
        elif opt in ("-i", "--import"):
            mod = get_mod(arg, ['.'])
            modul[mod.NAMESPACE] = mod
            impo[mod.NAMESPACE] = [arg, None]
        elif opt in ("-I", "--ignore"):
            ignore.append(arg)
        else:
            assert False, "unhandled option"

    if not args:
        print("No XSD-file specified")
        usage()
        sys.exit(2)

    schema = read_schema(args[0], add, defs, impo, modul, ignore, sdir)
    #print(schema.__dict__)
    schema.out()

if __name__ == "__main__":    
    main(sys.argv[1:])