qOpenFileWidget.py 7.27 KB
Newer Older
1
2
3
4
5
6
from . import QWidget
from . import QComboBox
from . import QVBoxLayout
from . import myIOs
from . import QFileDialog
from . import QLineEdit
7
8
9
from . import QFormLayout
from . import QPushButton
from . import QSignal
10
from . import QLabel
11
from types import FunctionType
12
from . import sys
13
14

AVAIL_FUNCTIONS = []
15
PYVERSION = sys.version_info
16
17
18

for key in myIOs.__dict__.keys():
    if key not in myIOs.__builtins__.keys() and not key.startswith('__'):
19
20
        if isinstance(myIOs.__dict__[key], FunctionType):
            AVAIL_FUNCTIONS.append(myIOs.__dict__[key])
21
22


23
24
25
26
27
28
29
30
31
32
33
34
35
class QFunctionFormular(QWidget):
    
    def __init__(self, function, parent=None):
        
        super(QFunctionFormular, self).__init__(parent)
        
        self.explicitArgs = ()  # Args except *args and **kwargs
        self.requiredArgs = ()  # required arguments
        self.wRequiredArgs = []  # Widgets Of the Required Args
        self.optionalArgs = ()  # optional arguments
        self.wOptionalArgs = []
        self.unexplicitArgs = False
        self.unexplicitKwArgs = False
36
37
38
        
        documentation = None  # Documentation of the function
        
39
40
41
42
43
44
45
46
        if (PYVERSION < (3, 0)):
            argCount = function.func_code.co_argcount  # number of explicit arguments
            argsName = function.func_code.co_varnames  # list of the names of the local variables
            defaultValues = function.func_defaults     # default value of the optional arguments
        else:
            argCount = function.__code__.co_argcount   # number of explicit arguments
            argsName = function.__code__.co_varnames   # list of the names of the local variables
            defaultValues = function.__defaults__      # default value of the optional arguments
47
        
48
49
        documentation = function.__doc__
        
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
        # extract the known arguments
        self.explicitArgs = argsName[:argCount]
        
        # verify is **kargs is present
        if 'kwargs' in argsName:
            self.unexplicitKwArgs = True
            self.requiredArgs = self.explicitArgs
            self.wUnexplicitKwArgs = QLineEdit(self)
        
        # verify if *args present
        if 'args' in argsName:
            self.unexplicitArgs = True
            self.requiredArgs = self.explicitArgs
            self.wUnexplicitArgs = QLineEdit(self)
        
        # Is there some optional args
        elif len(self.explicitArgs) < len(argsName) and defaultValues is not None:
            # get the number of optional arguments
            print('EXARGS: ' + str(self.explicitArgs))
            print('ARGNAME: ' + str(argsName))
            idxOpt = int(-1 * len(defaultValues))
            self.requiredArgs = self.explicitArgs[:idxOpt]
            self.optionalArgs = self.explicitArgs[idxOpt:]
            self.defaultValues = defaultValues
        
        # If neither *arg nor optionals
        else:
            self.requiredArgs = self.explicitArgs
        
        self.layout = QFormLayout(self)
        
81
82
83
84
85
        if documentation is not None:
            self.docLabel = QLabel(documentation)
            self.docLabel.setWordWrap(True)
            self.layout.addRow(self.docLabel)
        
86
87
88
89
90
91
92
93
94
        if self.requiredArgs != ():
            # make a QLineEdit without default variable for each requiredArgs
            for arg in self.requiredArgs:
                self.wRequiredArgs.append(QLineEdit(self))
                self.layout.addRow(str(arg), self.wRequiredArgs[-1])
        
        if self.optionalArgs != ():
            # make a QLineEdit with default variable for each optionalArgs
            for arg, default in zip(self.optionalArgs, self.defaultValues):
95
96
97
98
99
                if isinstance(default, str):
                    strDefault = "'" + default + "'"
                else:
                    strDefault = default
                self.wOptionalArgs.append(QLineEdit(str(strDefault), self))
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
                self.layout.addRow(str(arg), self.wOptionalArgs[-1])
        
        # make a QLineEdit for the args
        if self.unexplicitArgs:
            self.layout.addRow('*args', self.wUnexplicitArgs)
        
        # make a QLineEdit for the kwargs
        if self.unexplicitKwArgs:
            self.layout.addRow('**kwargs', self.wUnexplicitKwArgs)
    
    def getArguments(self):

        arguments = []
        
        for widget in self.wRequiredArgs:
            arguments.append(str(widget.text()))
        
        if self.wOptionalArgs != []:
            for arg, widget in zip(self.optionalArgs, self.wOptionalArgs):
                arguments.append(str(arg)+'='+str(widget.text()))
        
        elif self.unexplicitArgs:
            arguments.append(str(self.wUnexplicitArgs.text()))
        
        if self.unexplicitKwArgs:
            arguments.append(str(self.wUnexplicitKwArgs.text()))
            
        farguments = '({args})'.format(args=', '.join(tuple(arguments)))
        
        return(farguments)

131

132
133
class QOpenFileWidget(QWidget):
    
134
135
    triggered = QSignal([str])
    
136
137
138
139
    def __init__(self, parent=None):
        
        super(QOpenFileWidget, self).__init__(parent)
        
140
141
        self.layout = QVBoxLayout(self)
        
142
        self.functionsCombo = QComboBox(self)
143
144
145
        self.layout.addWidget(self.functionsCombo)
        
        self.functionWidgets = []
146
        for function in AVAIL_FUNCTIONS:
147
148
            self.functionsCombo.addItem(function.__name__)
            index = AVAIL_FUNCTIONS.index(function)
149
            print('> FUNCTION:' + str(AVAIL_FUNCTIONS[index].__name__) + " " + str(type(AVAIL_FUNCTIONS[index])))
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            self.functionWidgets.append(QFunctionFormular(AVAIL_FUNCTIONS[index], self))
            self.layout.addWidget(self.functionWidgets[-1])
        
        self.openButton = QPushButton("Open Data", self)
        self.layout.addWidget(self.openButton)
        self.openButton.clicked.connect(self.emitOpenData)
        
        self.functionsCombo.currentIndexChanged.connect(self.switchFormular)
        
        for widget in self.functionWidgets:
            widget.hide()
            
        self.functionWidgets[0].show()
        self.currentFunctionWidget = self.functionWidgets[0]
    
    def emitOpenData(self):
166
        
167
168
169
170
171
172
173
174
175
176
177
178
        functionName = self.functionsCombo.currentText()
        arguments = self.currentFunctionWidget.getArguments()
        self.triggered.emit(str(functionName)+str(arguments))
        self.hide()
    
    def switchFormular(self, i):
        
        for widget in self.functionWidgets:
            widget.hide()
        
        self.functionWidgets[i].show()
        self.currentFunctionWidget = self.functionWidgets[i]
179
        self.resize(self.layout.sizeHint())
180

181

182
183
184
185
186
187
188
189
190
191
192
#        self.filename = QFileDialog.getOpenFileName(self)
#        self.fileNameLabel = QLineEdit(self)
#        self.fileNameLabel.setText(self.filename)

#        self.argLabels = []
#        for i in range(0, myIOs.__dict__[AVAIL_FUNCTIONS[0]].func_code.co_argcount):
#            self.argLabels.append(myIOs.__dict__[AVAIL_FUNCTIONS[0]].func_code.co_varnames[i])

#        a = int(-1 * len(myIOs.__dict__[AVAIL_FUNCTIONS[0]].func_defaults))
#        for label in self.argLabels[a:]:
#            self.argLabels.append(myIOs.__dict__[AVAIL_FUNCTIONS[0]].func_code.co_varnames[i] + '=')
193
            
194
195
196
197
198
199
#        self.fileNameLabel.setText(str(self.argLabels))
        


#        self.layout.addWidget(self.fileNameLabel)
        
200