Blame view

test/xsd_simpletype.py 3.13 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
import tkinter as tk
from tkinter import ttk

from lxml import etree

from tooltip import ToolTip

from tkcalendar import DateEntry

from datetime_selector import DatetimeEntry

class XSDSimpleTypeFrame(tk.Frame):
    def __init__(self, parent=None, element=None, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.type = self.get_element_type(element)
        if element is not None:
            # add label with name
            self.label = tk.Label(self, text="{}: ".format(element.attrib["name"]))
            #self.label.grid(row=0,column=0)
            self.label.pack(side=tk.LEFT, expand=0)
            # iterate over type definition elements and populate the frame
            for el in element:
                # ignore annotation
                if el.tag.endswith("annotation"):
                    # add tool tip
                    if el[0].tag.endswith("documentation"):
                        if el[0].text is not None:
                            tt=ToolTip(self.label, el[0].text)
                    continue
                if el.tag.endswith("list"):
                    self.input_widget = tk.Entry(self)
                    #self.input_widget.grid(row=0, column=1)
                    #self.input_widget.pack(side=tk.RIGHT, fill=tk.X, expand=1)
                elif el.tag.endswith("restriction"):

                    # get the type of Entry
                    if len(el)==0:
                        # check the type
                        input_type = el.attrib["base"]
                        if input_type == "xsd:dateTime":
                            #self.input_widget = DateEntry(self)
                            self.input_widget = DatetimeEntry(self)
                        else:
                            # only a type restriction, add an entry widget
                            self.input_widget = tk.Entry(self)
                    
                    else:
                        # enumerate options
                        vals = []
                        for item in el.iter("{*}enumeration"):
                            vals.append(item.attrib["value"])
                        if len(vals):
                            self.input_widget = ttk.Combobox(self, state="readonly", values=vals)
                            self.input_widget.current(0)
                            #self.input_widget.grid(row=0,column=1)
                        else:
                            # no enumeration found add a simple entry
                            self.input_widget=tk.Entry(self)
                            #self.input_widget.grid(row=0, column=1)
                else:
                    pass
                #self.input_widget.grid(row=0, column=1)
                self.input_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
    def get_element_type(self, element, attrib_tag="name"):
        t=element.attrib[attrib_tag]
        if ":" in t:
            return t.split(":")[-1]
        return t
 
    def get_value(self):
        return self.input_widget.get()
    def get_content(self):
        # returns tree type
        root = etree.Element(self.type)
        root.text = self.get_value()
        return root