-
Notifications
You must be signed in to change notification settings - Fork 0
/
wks.py
364 lines (277 loc) · 10.4 KB
/
wks.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
wks - command line tool for webAppWorkshop
"""
from __future__ import with_statement
from contextlib import contextmanager
import os, sys
import __main__
from handy import trim, genshiText, xml, indent
@contextmanager
def outstream(filename, mode="w"):
try:
f = open(filename, mode)
yield f.write
finally:
f.close()
def init(appname):
os.mkdir(appname)
os.chdir(appname)
dirs = ['spec','test','model','theme','theme/default']
for d in dirs:
os.mkdir(d)
appClass = appname.capitalize() + "App"
with outstream("%s.py" % appname) as out:
out(trim(
'''
import model, schema
from dispatch import urlmap
from wsgiapp import WebApp, GenshiTheme, fileHandlerFactory
__version__="$version$"
def app_factory(cfg, dbhost, dbname, dbpass, dbuser, baseURL,**etc):
"""
entry point for paste
"""
clerk = schema.makeClerk(dbhost, dbuser, dbpass, dbname)
return %(appClass)s
class %(appClass)s(WebApp):
def __init__(self, baseURL, urlmap, clerk=None):
super(%(appClass)s, self).__init__(
baseURL,
[urlmap, fileHandlerFactory])
self.theme = GenshiTheme(baseURL)
self.clerk = clerk or schema.mockClerk()
''') % dict(appClass=appClass))
with outstream("%s.app.in" % appname) as out:
out(trim(
"""
#!/bin/env paster
# $version$
[server:main]
paste.server_factory = cherrypaste:server_factory
host = localhost
port = 8080
baseURL = http://localhost:8080/
[app:main]
paste.app_factory = %(app)s:app_factory
dbhost = %(dbhost)s
dbname = %(dbname)s
dbuser = %(dbuser)s
dbpass = %(dbpass)s
""") % dict(app=appname,
dbhost='localhost',
dbname=appname,
dbuser=appname,
dbpass=appname))
with outstream("model/__init__.py", "a") as out:
out('')
with outstream("dispatch.py") as out:
out(trim(
"""
import %s as app
from platonic import REST,URI
urlmap = REST(
URI("/", GET= lambda: handler)
)
""") % (appname))
with outstream("schema.py") as out:
out(trim(
"""
import clerks
import storage
import MySQLdb
from model import *
schema = clerks.Schema({
})
def connect(dbhost, dbuser, dbpass, dbname):
return MySQLdb.connect(
user = dbuser,
passwd = dbpass,
host = dbhost,
db = dbname)
def makeClerk(dbhost, dbuser, dbpass, dbname):
dbc = connect(dbhost, dbuser, dbpass, dbname)
return clerks.Clerk(storage.MySQLStorage(dbc), schema)
def mockClerk():
return clerks.MockClerk(schema)
"""))
with outstream('theme/default/layout.gen') as out:
out(trim(
'''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<title>${title}</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<h1>${title}</h1>
</body>
</html>
'''))
with outstream('theme/default/style.css') as out:
out(trim(
"""
body {
color: black;
background: white;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
}
"""))
def currentAppName():
if not os.path.exists("model"):
raise SystemExit("please run from app directory (model directory not found)")
return os.path.split(os.getcwd())[-1]
def addToSQL(sql):
# @TODO: actuall run the sql!
with outstream("%s.sql" % currentAppName(), "a") as out:
out(sql)
def modelClassExists(className):
return os.path.exists("model/%s.py" % className)
def addToModel(className, tableName=None):
"""
Adds a class to the model.
"""
appname = currentAppName()
if not className[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
raise SystemExit("class must start with uppercase letter")
if modelClassExists(className):
raise SystemExit("class %s.model.%s already exists."
% (appname, className))
tableName = tableName or className.lower()
addToSQL(trim(
'''
CREATE TABLE %s (
ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY
);
''') % tableName)
with outstream("test/%sTest.py" % className) as out:
out(trim(
"""
from %s import model
import unittest
from strongbox import *
class %sTest(unittest.TestCase):
def setUp(self):
pass
def test_constructor(self):
obj = %s()
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()
""") % (appname, className, className))
with outstream("model/%s.py" % className) as out:
out(trim(
"""
from strongbox import *
from pytypes import *
import model
class %s(Strongbox):
ID = attr(long)
""") % className)
with outstream("model/__init__.py", "a") as out:
out(trim(
"""
from %s import %s
""" % (className, className)))
addToSchema(className, repr(tableName))
def addToSchema(key, value):
lines = open("schema.py").readlines()
state = 'start'
for i, line in enumerate(lines):
if line.startswith('schema ='):
state = 'in-schema'
elif state=='in-schema' and line.startswith("})"):
lines.insert(i, ' %s : %s,\n' % (key, value))
break
else: pass
else:
raise SystemExit("couldn't add line to schema.py! [end state: %s]" % state)
with outstream("schema.py") as out:
out(''.join(lines))
def getTableForClass(className):
import schema, model
return schema.schema.dbmap[getattr(model, className)]
def expectModelClass(name):
if not os.path.exists("model/%s.py" % name):
raise SystemExit("class %s not found in model" % name)
def addLinkset(parent, linksetName, child, backlink=None):
expectModelClass(parent)
expectModelClass(child)
#@TODO: auto-generate a test case for linkset
with outstream("model/%s.py" % parent, "a") as out:
out(' %s = linkset(lambda: model.%s, "%s")\n'
% (linksetName, parent, backlink))
if backLink:
addLink(child, backlink, parent)
def addLink(fromClass, linkName, toClass):
#@TODO: auto-generate a test case for link
expectModelClass(fromClass)
expectModelClass(toClass)
with outstream("model/%s.py" % fromClass, "a") as out:
out(' %s = link(lambda: model.%s)\n'
% (linkName, toClass))
foreignKey = "%sID" % linkName
addToSchema("%s.%s" % (fromClass, linkName), repr(foreignKey))
addToSQL('ALTER TABLE %s ADD COLUMN %s INTEGER NOT NULL DEFAULT 0;\n'
% (getTableForClass(fromClass), foreignKey))
code_for_attr = genshiText(
"$name = attr($type${', default=%s' % default if default else ''})\n")
def fromXMI(filename):
"""
generates workshop-flavored python from xmi
"""
NS = {
"xmi": "http://schema.omg.org/spec/XMI/2.1",
"uml": "http://schema.omg.org/spec/UML/2.0",
}
doc = xml(open(filename).read())
for node in doc.xpath("//@xmi:type[string()='uml:Class']/..", NS):
className = node.xpath("string(@name)")
if modelClassExists(className):
print "skipping existing class %s" % className
else:
addToModel(className, className.lower())
with outstream("model/%s.py" % className, 'a') as out:
for attr in node.xpath("ownedAttribute"):
out(indent(code_for_attr(
name = attr.xpath("string(@name)"),
type = attr.xpath('string(@type)')[:-3] or 'str',
default = attr.xpath('string(defaultValue/@value)'))))
print "added class %s" % className
def expectArg(argv, index, msg):
try:
return argv[index]
except:
raise SystemExit("usage: %s" % msg)
def allowArg(argv, index):
return argv.get(index)
def makeArgParser(usage):
args = usage.split()
cmd = args[0]
func = getattr(__main__, cmd)
return cmd, lambda argv: func(*[
allowArg(argv, 2+i) if slot.startswith("[")
else expectArg(argv, 2+i, usage)
for i,slot in enumerate(args[1:])])
def main(argv):
cmds = dict(makeArgParser(line) for line in trim(
"""
init appname
addToModel ClassName [tableName]
addLink FromClass linkName ToClass
addLinkset ParentClass linksetName ChildClass [backLinkName]
fromXMI filename
""").split('\n') if line)
usage = "usage: wks [ %s ]" % (' | '.join(sorted(cmds.keys())))
cmd = expectArg(argv, 1, usage)
if cmd in cmds:
cmds[cmd](argv)
else:
raise SystemExit(usage)
if __name__ == '__main__':
main(sys.argv)