-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
395 lines (312 loc) · 14.8 KB
/
app.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
""" Basic todo list using webpy 0.3 """
import web
import json
import os
from tenant_repo_manager import CheckRepo, CreateRepo, getUsers, getUserIndex, createUser, returnOwl, returnSparql
from QueryProcesser import myQuery
from cache import loadCache, saveCache, getresult, update
### Url mappings
urls = (
'/', 'Index',
'/pf', 'platform',
'/login', 'Login',
'/logout', 'Logout',
'/del/(\d+)', 'Delete',
)
render = web.template.render('templates', base='base')
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'))
# urls = ('/', 'cpuload')
# render = web.template.render('templates/')
# app = web.application(urls, globals())
allowed = (
['user','pass'],
['saumil','shah'],
)
usernamex = 'SS'
vpath = 'static/Cloud/'
onto_form = web.form.Form(
web.form.Textbox('username', web.form.notnull),
web.form.Password('password', web.form.notnull),
web.form.Button('Login'),
web.form.Dropdown('french', ['mustard', 'fries', 'wine']),
)
class Login():
login_form = web.form.Form(
web.form.Textbox('username', web.form.notnull),
web.form.Password('password', web.form.notnull),
web.form.Button('Login'),
)
def GET(self):
f = self.login_form()
return render.login(f)
def POST(self):
allowed = getUsers()
print '\n All Users', allowed
if not self.login_form.validates():
print '\n\n{{{{{{{{{{{}}}}}}}}}}'
print '\n\nGuess, I was here...!'
# return render.login(self.login_form)
# return render.platform(my_form(), username, ["Car", "Bike", 'Movie'])
# platform.GET(platform);
x='SS'
raise web.redirect('/pf?user=%s' % x)
# raise web.seeother('/pf/%s' % x)
username = self.login_form['username'].value
password = self.login_form['password'].value
# if [username,password] in allowed:
# session.logged_in = True
# usernamex = username
# print '\n##################'
# print "Ux assigned"
# print '##################\n'
#
#
# vxpath = 'Cloud/U' + str(allowed.index([username,password]))
# raise web.redirect('/pf?user=%s:%s' % (username, vxpath))
# # raise web.seeother('/pf', usernamex)
#
# else:
# print 'User Not Found!\n'
# raise web.seeother('/')
if [username,password] in allowed:
uid = str(allowed.index([username,password]) + 1)
files = []
session.logged_in = True
usernamex = username
print '##################'
print "Ux assigned"
print '##################\n'
if CheckRepo(uid):
print '\nUser Repo exists...for UID :'+uid+'\tUsername :',username
# files = os.listdir(vpath+uid)
root, dirs, files = os.walk(vpath+uid).next()
print '\n\nPath ---',root
print '\nDirs ---',dirs
print '\nFiles ---',files
for dirx in dirs:
rootd, dirsd, filesd = os.walk(vpath+uid+'/'+dirx).next()
print '\n\n\tPathd x--',rootd
print '\n\tDirsd x--',dirsd
print '\n\tFilesd x--',filesd
files = files + ['*UPS*']+ filesd
else:
print '\n\nUser Repo does not exist!!! \tCreating User Repository...'
print 'User Repo created...for UID :'+uid+'\tUsername :'+username+'\tLocated at : '+vpath+uid
CreateRepo(uid)
vxpath = 'static/Cloud/U' + uid
filex = '^'.join(files)
print "\nvxpath : "+vxpath
raise web.redirect('/pf?user=%s:%s:%s' % (username, vxpath, filex))
else:
print '\n\nUser NOT Found!'
createUser(allowed, username,password);
raise web.seeother('/')
return render.login(self.login_form)
class platform:
my_form = web.form.Form(
web.form.Dropdown('Select an Ontology : ', args=['a','b','c'], _class='xtables', id='xtables'),
web.form.Textarea(name='Input Query : ',value='Write your text in Textarea', class_='qfield', id='qfield', cols="50", rows="5",style="background-color:#FCF5D8;color:dimgrey;font-family:Courier"),
)
def GET(self):
print 'In platform Class GET'
params = web.input()
userx = params['user'].split(':')[0]
vpath = params['user'].split(':')[1]
print "\n@@@@@@@@ Raincheck : ",params['user'].split(':')[2]
[optionxx1, optionxx2] = params['user'].split(':')[2].split('*UPS*')
optionx1 = optionxx1.split('^')
optionx2 = optionxx2.split('^')
useridx = vpath.split('static/Cloud/U')
print useridx
print "\noptionx1 : ",optionx1
print "\noptionx2 : ",optionx2
options1 = returnOwl(optionx1)
options_srq1 = returnSparql(optionx1)
options2 = returnOwl(optionx2)
options_srq2 = returnSparql(optionx2)
# options = ['file1.owl', 'file2.owl', 'file3.owl']
print '\nPRM : ',params, type(params)
print 'USR : ',userx
print 'UID : ',useridx
print 'VPT : ',vpath
print '\nOpt1 : ',options1
print 'Opt-Srq1 : ',options_srq1
print '\nOpt2 : ',options2
print 'Opt-Srq2 : ',options_srq2
my_form = web.form.Form(
# web.form.Dropdown('Select an Ontology : ', args=options, _class='xtables', id='xtables'),
web.form.GroupedDropdown('Select an Ontology : ',_class='xtables', id='xtables',
args=(('Default Ontologies',(options1)),('Upload Ontologies',(options2)))),
web.form.Textarea(name='Input Query : ',value="""SELECT * WHERE {
?s ?p ?o
}
limit 10""", class_='qfield', id='qfield', cols="50", rows="5",style="background-color:#FCF5D8;color:dimgrey;font-family:Courier"),
# web.form.Dropdown('Select an Ontology : ', args=options1, _class='xtables', id='xtables1'),
web.form.GroupedDropdown('Select an Ontology : ', _class='xtables', id='xtables1',
args=(('Default Ontologies',(options1)),('Upload Ontologies',(options2)))),
# web.form.Dropdown('Select a Sparql File : ', args=options_srq1, _class='ytables', id='ytables'),
web.form.GroupedDropdown('Select a Sparql File : ', _class='ytables', id='ytables',
args=(('Default Queries',(options_srq1)),('Upload Queries',(options_srq2)))),
web.form.Textarea(name='Input Query : ',value="""SELECT * WHERE {
?s ?p ?o
}
limit 5""", class_='qfield', id='qfield1', cols="50", rows="5",style="background-color:#FCF5D8;color:dimgrey;font-family:Courier"),
# web.form.File('Choose an Ontology : ', args=options, _class='upload', id='upload'),
# web.form.File(name='myfile'),
)
print 'My Form : ',my_form
return render.platform(my_form(), userx)
def POST(self):
print '\nSomeone submitted a Query...'
form = self.my_form()
form.validates()
print 'Form : ',form
try:
sz = form.value['data_tx']
x = str(sz)
print 'X : ',x
# s = web.input(xtables = [])
# dval = str(s.xtables)
typeX = x.split('^')[0]
if typeX == '0':
print 'Type : ',typeX,' Saving'
username = x.split('^')[1].split('|')[0][0:-2]
[dval, q] = x.split('^')[1].split('|')[1].split('~')
uid = str(getUserIndex(username))
print "Loading Cache..."
cache = loadCache(vpath+uid+'/cache.pickle')
print "Saving Cache..."
saveCache(cache,vpath+uid+'/cache.pickle')
print "Cache Saved!"
return json.dumps("Data Saved, "+username+"!")
elif typeX == '1':
print 'Type : ',typeX,' Query'
username = x.split('^')[1].split('|')[0][0:-2]
[dval, q] = x.split('^')[1].split('|')[1].split('~')
print 'Username : ', username
print 'Dropdown : ', dval
print 'UserQuery : ', q
onto_file = vpath+str(getUserIndex(username))+'/'+dval
print 'onto_file_path : ',onto_file
uid = str(getUserIndex(username))
cache = loadCache(vpath+uid+'/cache.pickle')
saveCache(cache,vpath+uid+'/cache.pickle')
answer = getresult(cache,onto_file+q)
if answer == None:
print "\n******Answer None!!!"
js = myQuery(q, onto_file)
else:
print "\n******Answer NOTNone!!!"
js = answer
print '\nAnswer : ',js
cacheU = update(cache, onto_file+q, js)
saveCache(cacheU, vpath+uid+'/cache.pickle')
return json.dumps("Selected File : "+dval+"\nAnswer to Query : "+js)
# elif typeX == '2':
# print 'Type : ',typeX,' Upload'
# username = x.split('^')[1].split('|')[0][0:-2]
#
#
# x = web.input(myfile={})
# print "\n\n^^^^^^^^^^^^^X : ",x
# filedir = '/Users/hellosaumil/Desktop/UF' # change this to the directory you want to store the file in.
# if 'myfile' in x: # to check if the file-object is created
#
# web.debug(x['myfile'].filename) # This is the filename
# web.debug(x['myfile'].value) # This is the file contents
#
# filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
# print "\nFilename1 : ",filepath
#
# filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
# print "\nFilename2 : ",filename
# fout = open(filedir +'/'+ filename,'w+') # creates the file where the uploaded file should be stored
# fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
# fout.close() # closes the file, upload complete.
#
# return json.dumps("File(s) Uploaded Successfully, "+username+"!")
else:
return json.dumps("Invalid Request, "+username+"!")
except Exception as e:
print "******** Exception : ", e
typeX = '2'
print 'Type : ',typeX,' Upload'
# username = x.split('^')[1].split('|')[0][0:-2]
x = web.input(myfile={})
print "\n\n^^^^^^^^^^^^^X : ",x
# filedir = '/Users/hellosaumil/Desktop/UF' # change this to the directory you want to store the file in.
if 'myfile' in x: # to check if the file-object is created
print "File Contents : ", x['myfile'].value # This is the file contents
# uid = x['user'].split(':')[1].split('/')[1][1:]
uid = x['user'].split(':')[1].split('/')[2][1:]
username = x['user'].split(':')[0].title()
print "UID : "+uid+" - Username : "+username
filedir = vpath+uid+"/uploads"
print "\nFile-Dir : ",filedir
filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
print "\nFile-Path : ",filepath
filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
print "File-Name : ",filename
fout = open(filedir +'/'+ filename,'w+') # creates the file where the uploaded file should be stored
fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
fout.close() # closes the file, upload complete.
print "File Uploaded Successfully at "+vpath+uid+", "+username+"!"
root, dirs, files = os.walk(vpath+uid).next()
print '\n\nPath $---',root
print '\nDirs $---',dirs
print '\nFiles $---',files
for dirx in dirs:
rootd, dirsd, filesd = os.walk(vpath+uid+'/'+dirx).next()
print '\n\n\tPathd $x--',rootd
print '\n\tDirsd $x--',dirsd
print '\n\tFilesd $x--',filesd
files = files + ['*UPS*']+ filesd
vxpath = 'static/Cloud/U' + uid
filex = '^'.join(files)
raise web.redirect('/pf?user=%s:%s:%s' % (username, vxpath, filex))
# return json.dumps("File(s) Uploaded Successfully, "+username+"!")
class Index:
form = web.form.Form(
web.form.Textbox('title', web.form.notnull, description="I need to:"),
web.form.Button('Add todo'),
)
def GET(self):
if session.get('logged_in', False):
""" Show page """
todos = model.get_todos()
form = self.form()
return render.index(todos, form)
else:
raise web.seeother('/login')
def POST(self):
""" Add new entry """
form = self.form()
form.validates()
if not form.validates():
print '\n\n[[[[[[[[[[[[]]]]]]]]]]]]'
print '\nSomething\'s Up!'
todos = model.get_todos()
return render.index(todos, form)
model.new_todo(form.d.title)
raise web.seeother('/')
class Logout:
def GET(self):
print "\n========================"
print "Logging User out... "
session.logged_in = False
raise web.seeother('/login')
class Delete:
def POST(self, id):
""" Delete based on ID """
id = int(id)
model.del_todo(id)
raise web.seeother('/')
app = web.application(urls, globals())
if web.config.get('_session') is None:
session = web.session.Session(app, web.session.DiskStore('sessions'), {'count': 0})
web.config._session = session
else:
session = web.config._session
if __name__ == '__main__':
app.run()