File: wb.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (321 lines) | stat: -rw-r--r-- 10,749 bytes parent folder | download | duplicates (2)
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

import grt

# NOTE: If you're using this in a module, it should be imported as 
# from wb import *


class DefineModule(dict):
    def __init__(self, name, implements=None, author="", version= "", description= ""):
        """Define a GRT module. Must be called before any function declaration as
        wbmodule = DefineModule('modname')
        """

        self.name= name
        self.author= author
        self.version= version
        self.description= description
    
        # List of functions exported by the module (automatically updated by @declare)
        self.functions= []
        # List of interfaces implemented by the module
        self.implements= implements or []
    
        self._pluginList= grt.List(grt.OBJECT, "app.Plugin")

        
        
    def __getitem__(self, name):
        return getattr(self, name)


    @property
    def moduleDataDirectory(self):
      return grt.root.wb.registry.appDataDirectory+"/modules/data"


    ##
    ## Decorators for Writing Modules and Plugins
    ##
    def plugin(self, name, caption= "", description="", type="standalone", input= [], groups= [], pluginMenu= None):
        """Decorator to declare a Plugin, used in addition to @wbexport
        Usage:
        @wbmodule.plugin("db.utils.mangleNames", caption="Mangle Names", description="Mangles all object names in current catalog beyond recognition.", input= [wbinputs.currentCatalog()], groups=["Menu/Catalog"])
        @wbmodule.export(grt.INT, grt.classes.db_Catalog)
        def mangleNames(catalog):
           return 1
        """
        
        def setup_plugin(fn):    
            # make sure getPluginInfo() is in the function list
            if "getPluginInfo" not in [x[0] for x in self.functions]:
              self.functions.append(("getPluginInfo", 
                                              ((grt.LIST, (grt.OBJECT, "app.Plugin")),
                                              []),
                                              lambda: self._pluginList))
              if "PluginInterface" not in self.implements:
                  self.implements.append("PluginInterface")
            
            plug= grt.classes.app_Plugin()
            plug.name= name
            plug.caption= caption
            plug.description= description
            plug.pluginType= type
            plug.moduleName= self.name
            plug.moduleFunctionName= fn.func_code.co_name
            for i in input:
              i.owner= plug
              plug.inputValues.append(i)
            for g in groups:
              plug.groups.append(g)
            if pluginMenu:
              plug.groups.append("Menu/"+pluginMenu)
            plug.rating= 100
            plug.showProgress= 0
            self._pluginList.append(plug)
            
            return fn
        
        return setup_plugin

    def exportFilter(self, name, caption="", input="selectedText"):
        def setup_plugin(fn):    
            # make sure getPluginInfo() is in the function list
            if "getPluginInfo" not in [x[0] for x in self.functions]:
              self.functions.append(("getPluginInfo", 
                                              ((grt.LIST, (grt.OBJECT, "app.Plugin")),
                                              []),
                                              lambda: self._pluginList))
              if "PluginInterface" not in self.implements:
                  self.implements.append("PluginInterface")
            
            plug= grt.classes.app_Plugin()
            plug.name= name
            plug.caption= caption
            plug.pluginType= "standalone"
            plug.moduleName= self.name
            plug.moduleFunctionName= fn.func_code.co_name
            if input:
                arg = grt.classes.app_PluginInputDefinition()
                arg.name= input
                plug.inputValues.append(arg)
            plug.groups.append("Filter")
            plug.rating= 100
            plug.showProgress= 0
            self._pluginList.append(plug)

            signature= (grt.STRING, [("text", grt.STRING)])
            self.functions.append((fn.func_code.co_name, signature, fn))

            return fn

        return setup_plugin

       
    
    
    def export(self, returntype, *argtypes):
        """Decorator to declare an exported Module function.
        Usage:
        @wbmodule.export(grt.INT, grt.classes.db_Table, (grt.LIST, grt.STRING))
        def dostuff(arg1, arg2):
          return 1
        
        Declares the function dostuff as returning an integer and having the 1st arg a db.Table and
        the 2nd a list of strings.
        """
        typenames= [grt.INT,grt.DOUBLE,grt.STRING,grt.LIST,grt.DICT, grt.OBJECT]
        def set_types(fn):
            if len(argtypes) != fn.func_code.co_argcount:
              raise TypeError("module function '%s' has %i arguments, but @export declares %i" % (fn.func_code.co_name, fn.func_code.co_argcount, len(argtypes)))
            arglist= []
            for i in range(len(argtypes)+1):
              if i == 0:
                arg = returntype
              else:
                arg= argtypes[i-1]
              
              if arg == grt.List:
                  arg = grt.LIST
              elif arg == grt.Dict:
                  arg = grt.DICT
              
              if type(arg) == tuple:
                containertype, contenttype= arg
                if containertype not in [grt.LIST, grt.DICT]:
                  raise TypeError("argument %i has invalid specification (type %s is not a container type and takes no extra argument)"%(i, containertype))
                
                if contenttype in dir(grt.classes) or (getattr(contenttype, "__name__", None) in dir(grt.classes)):
                  contenttype= (grt.OBJECT, contenttype if type(contenttype) is str else contenttype.__name__.replace("_", "."))
                elif contenttype not in typenames:
                  raise TypeError("argument %i has invalid specification (%s it not a valid content type or class)"%(i, contenttype))

                arg= (containertype, contenttype)
              elif arg in (grt.INT, grt.DOUBLE, grt.STRING, grt.LIST, grt.DICT, grt.OBJECT):
                pass                
              elif (type(arg) == str and arg not in typenames) and arg not in grt.classes:
                raise TypeError("%s not a valid GRT type specification"%str(arg))
              else:
                arg= (grt.OBJECT, arg.__name__.replace("_", "."))
            
              if i == 0:
                arglist.append(arg)
              else:
                arglist.append((fn.func_code.co_varnames[i-1], arg))
            signature= (arglist[0], arglist[1:])
        
            self.functions.append((fn.func_code.co_name, signature, fn))
            return fn
        return set_types
 


#def SimplePlugin(name, author="", version="", caption="", input=[], returns=grt.INT):
#    def auto_wrap(fn):
#        global ModuleInfo
#        ModuleInfo = DefineModule(name, author=author, version=version)
#        exp = ModuleInfo.export(returns, *input)
#        pl = ModuleInfo.plugin(name+"."+fn.name, caption=caption if caption else fn.name)
#        return pl(exp(fn))
#    return auto_wrap
  

#
# Plugin input type helpers and predefined types.
#


class _wbinputs:
  def objectOfClass(self, className):
    assert type(className) == str
    arg= grt.classes.app_PluginObjectInput()
    arg.objectStructName= className
    return arg


  def string(self):
    arg= grt.classes.app_PluginInputDefinition()
    arg.name= "string"
    return arg

  # Home
  def selectedConnection(self):
    arg= grt.classes.app_PluginObjectInput() 
    arg.name= "selectedConnection"
    arg.objectStructName= "db.mgmt.Connection"
    return arg

  def selectedInstance(self):
    arg= grt.classes.app_PluginObjectInput() 
    arg.name= "selectedInstance"
    arg.objectStructName= "db.mgmt.ServerInstance"
    return arg



  # Modeling
  def currentModel(self):
    arg= grt.classes.app_PluginObjectInput() 
    arg.name= "activeModel"
    arg.objectStructName= "workbench.physical.Model"
    return arg

  def currentCatalog(self):
    arg= grt.classes.app_PluginObjectInput() 
    arg.name= "activeCatalog"
    arg.objectStructName= "db.Catalog"
    return arg
    
  def currentDiagram(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "activeDiagram"
    arg.objectStructName= "workbench.physical.Diagram"
    return arg

  def selectedDiagram(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= ""
    arg.objectStructName= "workbench.physical.Diagram"
    return arg

  # SQL Editor
  def currentSQLEditor(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "activeSQLEditor"
    arg.objectStructName= "db.query.Editor"
    return arg
    

  def currentQueryEditor(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "activeQueryEditor"
    arg.objectStructName= "db.query.QueryEditor"
    return arg
  currentQueryBuffer = currentQueryEditor
    
  def currentResultset(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "activeResultset"
    arg.objectStructName= "db.query.Resultset"
    return arg

  def currentEditableResultset(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "activeResultset"
    arg.objectStructName= "db.query.EditableResultset"
    return arg

  def selectedLiveObject(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "" # any
    arg.objectStructName= "db.query.LiveDBObject"
    return arg

  def selectedLiveSchema(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "schema"
    arg.objectStructName= "db.query.LiveDBObject"
    return arg

  def selectedLiveTable(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name= "table"
    arg.objectStructName= "db.query.LiveDBObject"
    return arg

  def selectedLiveView(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name="view"
    arg.objectStructName= "db.query.LiveDBObject"
    return arg

  def selectedLiveRoutine(self):
    arg= grt.classes.app_PluginObjectInput()
    arg.name="routine"
    arg.objectStructName= "db.query.LiveDBObject"
    return arg

  def selectedRowList(self):
    arg= grt.classes.app_PluginInputDefinition()
    arg.name= "selectedRowList"
    return arg

  def clickedRow(self):
    arg= grt.classes.app_PluginInputDefinition()
    arg.name= "clickedRow"
    return arg

  def clickedColumn(self):
    arg= grt.classes.app_PluginInputDefinition()
    arg.name= "clickedColumn"
    return arg

  def simpleValue(self, name):
      arg= grt.classes.app_PluginInputDefinition()
      arg.name= name
      return arg



wbinputs= _wbinputs()