-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-workbench-plugin-doc-generating.py
148 lines (102 loc) · 3.91 KB
/
mysql-workbench-plugin-doc-generating.py
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
# MySQL Workbench Plugin
# <description>
# Written in MySQL Workbench 6.3.4
from wb import *
import grt
import mforms
ModuleInfo = DefineModule("ModelDocumentation", author="Hieu Le", version="1.0.0", description="Generate Markdown documentation from a model")
# This plugin takes no arguments
@ModuleInfo.plugin("info.hieule.wb.documentation", caption="Generate documentation (Markdown)", description="description", input=[wbinputs.currentDiagram()], pluginMenu="Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_Catalog)
def documentation(diagram):
text = "#Schema documentation\n\n"
text += "Generated by MySQL Workbench Model Documentation v1.0.0 - Copyright (c) 2015 Hieu Le\n\n";
for figure in diagram.figures:
if hasattr(figure, "table") and figure.table:
text += writeTableDoc(figure.table)
mforms.Utilities.set_clipboard_text(text)
mforms.App.get().set_status_text("Documentation generated into the clipboard. Paste it to your editor.")
print "Documentation is copied to the clipboard."
return 0
def writeTableDoc(table):
text = "##Table: `" + table.name + "`\n\n"
text += "###Description: \n\n"
text += table.comment + "\n\n"
text += "###Columns: \n\n"
text += "| Column | Data type | Attributes | Default | Description |\n| --- | --- | --- | --- | --- |\n"
for column in table.columns:
text += writeColumnDoc(column, table)
text += "\n\n"
if (len(table.indices)):
text += "### Indices: \n\n"
text += "| Name | Columns | Type | Description |\n| --- | --- | --- | --- |\n"
for index in table.indices:
text += writeIndexDoc(index)
text += "\n\n"
return text
def writeColumnDoc(column, table):
# column name
text = "| `" + column.name + "`"
# column type name
if column.simpleType:
text += " | " + column.simpleType.name
# column max lenght if any
if column.length != -1:
text += "(" + str(column.length) + ")"
else:
text += " | "
text += " | "
# column attributes
attribs = [];
isPrimary = False;
isUnique = False;
for index in table.indices:
if index.indexType == "PRIMARY":
for c in index.columns:
if c.referencedColumn.name == column.name:
isPrimary = True
break
if index.indexType == "UNIQUE":
for c in index.columns:
if c.referencedColumn.name == column.name:
isUnique = True
break
# primary?
if isPrimary:
attribs.append("PRIMARY")
# auto increment?
if column.autoIncrement == 1:
attribs.append("Auto increments")
# not null?
if column.isNotNull == 1:
attribs.append("Not null")
# unique?
if isUnique:
attribs.append("Unique")
text += ", ".join(attribs)
# column default value
text += " | " + (("`" + column.defaultValue + "`") if column.defaultValue else " ")
# column description
text += " | " + (nl2br(column.comment) if column.comment else " ")
# foreign key
for fk in table.foreignKeys:
if fk.columns[0].name == column.name:
text += ("<br /><br />" if column.comment else "") + "**foreign key** to column `" + fk.referencedColumns[0].name + "` on table `" + fk.referencedColumns[0].owner.name + "`."
break
# finish
text += " |" + "\n"
return text
def writeIndexDoc(index):
# index name
text = "| " + index.name
# index columns
text += " | " + ", ".join(map(lambda x: "`" + x.referencedColumn.name + "`", index.columns))
# index type
text += " | " + index.indexType
# index description
text += " | " + (nl2br(index.comment) if index.comment else " ")
# finish
text += " |\n"
return text
def nl2br(text):
return "<br />".join(map(lambda x: x.strip(), text.split("\n")))