Blame view

test/test2.py 4.42 KB
d73bf339   Alexandre Schulz   first commit
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
import tkinter as tk
from tkinter import ttk

from lxml import etree

class XSDFrame(tk.Frame):
    def __init__(self,parent,  xsd_tree=None,  *args, **kwargs):
        tk.Frame.__init__(self,parent, *args, **kwargs)
        l=tk.Label(self, text="Schema frame").pack()
        self.tree=xsd_tree
        print("SCHEMA TREE : ", xsd_tree, xsd_tree.attrib, xsd_tree.tag)

        self.types={}

        # load types
        self.load_types()

        # now add the widgets for each element that needs filling
        for child in self.tree:
            # ignore comments
            if isinstance(child, etree._Comment):
                continue
            # ignore type definitions
            if child.tag.endswith("Type") or child.tag.endswith("}group"):
                continue
            t=child.tag.split("}")[-1]
            c_type = child.attrib["type"]
            if ":" in c_type:
                c_type = c_type.split(":")[-1]
            print("Child : {}, type: {}".format(t, c_type))
            # add widgets for that type
            f=self.get_type_widget_frame(c_type)
            f.pack()
    def get_type_widget_frame(self, t):
        f=tk.Frame(self)
        tk.Label(f, text="widget frame type : {}".format(t)).pack()
        print("TYPE", self.types[t])
        d = self.types[t]
        for e in d:
            # ignore annotation
            if e.tag.endswith("annotation"):
                continue
            if e.tag.endswith("sequence"):
                seq_frame = SequenceFrame(self, e)
                seq_frame.pack()
                for s_i in e:
                    if s_i.tag.endswith("element"):
                        print("SQEGezq",s_i.tag, s_i.attrib)
                        # create the right widgets for this
                        tk.Label(f, text=s_i.attrib["name"]).pack()
                        tk.Entry(f).pack()
                    elif s_i.tag.endswith("choice"):
                        # add combobox
                        tk.Label(f, text="Choice").pack()
                        cb=ttk.Combobox(f, state="readonly", values=[i.attrib["name"] for i in s_i if "name" in i.attrib])
                        cb.pack()
                        cb.bind("<<ComboboxSelected>>", self.add_type_widgets)
                    else:
                        print("I DONT KNOW")
            if e.tag.endswith("choice"):
                print("choice")
            
        return f
    def add_type_widgets(self, *args):
        print(args)
        tk.Label(self, text="add type widget : {}".format(args)).pack()
    def load_types(self):
        for e in self.tree.iter("{*}simpleType", "{*}complexType", "{*}group"):
            type_name = e.attrib["name"]
            if ":" in type_name:
                type_name=type_name.split(":")[-1]
            self.types[type_name]=e

class XSDForm(tk.Frame):
    def __init__(self, xsd=None, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.tree = etree.parse(xsd)

        # initialize the schema frame
        self.schema_frame = None

        # add combobox for selecting the schema we want
        schema_label = tk.Label(self, text = "Choose schema :")
        schema_label.pack()

        self.schema_selection = ttk.Combobox(self, state="readonly", values=self.get_schema_names())
        self.schema_selection.pack()
        self.schema_selection.bind("<<ComboboxSelected>>", self.schema_selection_callback)
    def get_schema_names(self):
        ans=[]
        c=1
        for e in self.tree.iter(tag="{*}schema"):
            ans.append(str(c))
            c=c+1
        return ans
    def get_schema(self, i):
        c=1
        for e in self.tree.iter(tag="{*}schema"):
            if str(c)==i:
                return e
            else:
                c+=1
        return None

    def schema_selection_callback(self, *args):
        sid=self.schema_selection.get()
        print("Schema selected : {}".format(sid))
        if not self.schema_frame is None:
            self.schema_frame.destroy()
            self.schema_frame=None
        # prepare the frame for editing this schema
        print(self.get_schema(sid))
        self.schema_frame = XSDFrame(self, xsd_tree=self.get_schema(sid))
        self.schema_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

if __name__=="__main__":
    mainwindow = tk.Tk()
    mainwindow.title("XSD form")

    xsd_form = XSDForm(xsd="spase-2.3.1.xsd")
    xsd_form.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    mainwindow.mainloop()