File: scripting_sample_grt.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 (49 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download | duplicates (3)
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

from wb import *
import grt

ModuleInfo = DefineModule(name= "MyModule", author= "My Name", version="1.0")

def process_script(script):
	import re
	import random
	tokens = re.split("(%\[.*?\]%)", script)
	output = []
	for token in tokens:
		out_token = token
		if token.startswith("%[") and token.endswith("]%"):
			command, sep, args = token[2:-2].partition(":")
			if command == "oneof":
				out_token = random.choice(args.split(","))
			elif command == "random":
				min_value, sep, max_value = args.partition(",")
				out_token = "%s" % random.randint(int(min_value), int(max_value))
		output.append(out_token)
	return "".join(output)


# because of a bug in the wbinputs.currentSQLEditor() input	specifier from the wb module 
# in Workbench 5.2.26, we include our own version of it here
def currentSQLEditor():
	arg= grt.classes.app_PluginObjectInput()
    	arg.name= "activeSQLEditor"
    	arg.objectStructName= "db.query.Editor"
    	return arg


@ModuleInfo.plugin("my.plugin.fill_random_query", caption= "Fill in Random Values in Query", description="Replaces %[oneof:opt1,opt2,opt3] and %[random:<min>,<max>]% in a query with a randomly picked value.", input=[currentSQLEditor()], pluginMenu="SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def fill_random_query(editor):
	active_buffer = editor.activeQueryBuffer
	script = active_buffer.script

	try:
		new_script = process_script(script)	
	except Exception, exc:
		new_script = "Error: %s" % exc

	new_buffer = editor.addQueryBuffer()
	new_buffer.replaceContents(new_script)

	return 0