-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtools.py
More file actions
388 lines (335 loc) · 12.7 KB
/
tools.py
File metadata and controls
388 lines (335 loc) · 12.7 KB
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
# All pourpose dirty, ugly tools and stuff
import os, sys, sqlite3
import shutil, subprocess, webbrowser, time
##############################
######### CLASSES ############
##############################
class BreakPoints():
def __init__(self):
self.breaks = {}
def toggle_line(self, filename, lineno):
""" manages adds and finds break points and files that owns them
brk_obj: a dictionary
filename: the name of the file, serves as the dictionary key
lineno: the line number to toggle
"""
if filename not in self.breaks.keys():
self.breaks[filename] = [lineno]
else:
if lineno not in self.breaks[filename]:
self.breaks[filename].append(lineno)
else:
self.breaks[filename].remove(lineno)
##############################
######### FUNCTIONS ##########
##############################
def add_doubledash(fname):
""" turns slashes into double slashes
in order to pass to javascript,and back
"""
return fname.replace("\\","\\\\")
def filesize(num):
for x in ['bytes','KB','MB','GB','TB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
def findexts (f):
""" finds extention of file
"""
return os.path.splitext(f)[1]
def render_main(mfile):
retvalue = ''
f = open(mfile, 'r')
for l in f:
retvalue += l
f.close()
retvalue += get_latest()
retvalue += '''</body>
</head>'''
return retvalue
def file_lister(root, project):
retvalue = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Django IDE: """ + project + """</title>
<link href="/ui/css/base.css" rel="stylesheet" type="text/css" media="screen" />
<link href="/ui/css/print.css" rel="stylesheet" type="text/css" media="print" />
<link rel="stylesheet" href="/ui/css/pygments.css" type="text/css" />
<link rel="shortcut icon" href="/ui/img/favicon.ico" />
</head>
<body id="documentation" class="default">
<div id="container">
<div id="header">
<h1 id="logo"><a href="/django-ide"><img src="/ui/img/hdr_logo.gif" alt="Django-IDE"/></a></h1>
<ul id="nav-global">
<li id="nav-open"><a href="/django-ide/open">Open Project</a></li>
<li id="nav-new"><a href="/django-ide/new">New Project</a></li>
<li id="nav-docs"><a href="/django-ide/docs">Documentation</a></li>
</ul>
</div>
<div id="billboard">
<h2><a></a></h2>
</div>
<div class="section">
"""
# opens this directory
dirArray = os.listdir(root)
# counts elements in array
indexCount = len(dirArray);
# sorts files
dirArray.sort()
# print 'em
retvalue += """<table width='100%' cellspacing='10'>
<tr>
<td class='head'>Filename</td>
<td class='head'>Type</td>
<td class='head'>Size</td></tr>"""
# loops through the array of files and print them all
for f in dirArray:
extension = findexts(f)
if not extension.endswith(".pyc") and not extension.endswith(".pyo"):
retvalue += """<tr><td><a href='""" + """/django-ide/editor/""" + f + """'>""" + f + """</a></td>"""
retvalue += """<td>"""
retvalue += extension
retvalue += """</td>"""
retvalue += """<td>"""
retvalue += filesize(os.path.getsize(os.path.join(root, f)))
retvalue += """</td>"""
retvalue += """</tr>"""
retvalue += """<tr><td><a href='""" + """/django-ide/editornew/'>add a new file</a></td>"""
retvalue += """<td>"""
retvalue += """</td>"""
retvalue += """<td>"""
retvalue += """</td>"""
retvalue += """</tr>"""
retvalue += """</table>"""
retvalue += """
</div>
<form method='post'>
<br>
<input type="hidden" name="pyaction" id="pyaction" value="">
<input type="submit" onclick="document.getElementById('pyaction').value='run'" value="Run Project"/>
</form>
</body>
</html>
"""
return retvalue
def extension_man(filext):
print filext
if filext == '.py':
return 'python'
elif filext == '.js':
return 'javascript'
elif filext == '.php':
return 'php'
else:
return ''
def div_events():
""" Sets div doble click events
"""
retVal = """
<script>
$('div.ace_gutter-cell').live("dblclick",function() {
var line_no = $(this).text();
// Draw the break point
// TODO: Improve breakpoint icon
if ($(this).attr("class")=="ace_gutter-cell"){
$(this).attr("class", "ace_gutter-cell ace_error");
$(this).attr("title", "break point added");
}else{
$(this).attr("class", "ace_gutter-cell");
$(this).attr("title", "");
}
// Pass the lineno to the pre-debugger
$.post("add.breakpoint", {line_no: line_no});
});
</script>
"""
return retVal
def render_editor(project_path, filename, filext, project, filebuf):
if filebuf:
save_cmd = "saveNotice()"
else:
# If the file content is empty, save() must be saveAs()
save_cmd = "saveasNotice()"
retvalue = """ <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>""" + project + """ : """ + filename + """</title>
<link rel="shortcut icon" href="/ui/img/favicon.ico" />
<link href="/ui/css/toolbar.css" rel="stylesheet" type="text/css" />
<script src="/ui/js/jquery.min.js"></script>
<script src="/ui/js/toolbar.js"></script>"""
retvalue += """<div id="message" style="display: none;">
<a href="/django-ide/open/""" + project + """" class="close-notify">Project</a>
<a href="/django-ide/editornew/" class="close-notify">New File</a>
<a href="#" class="close-notify" onclick=" """ + save_cmd + """ ">Save</a>
<a href="#" class="close-notify" onclick="saveasNotice()">Save As</a>
<a href="#" class="close-notify" onclick="deleteNotice()">Delete</a>"""
if filext == '.py': #Debbuger only for .py files!
retvalue += """<a href="#" class="close-notify" onclick="debugNotice()">Debug</a>
</div>
<div id="debugger" style="display: none;">
<a href="#" class="close-notify" onclick="debuggerStep()">Step</a>
<a href="#" class="close-notify" onclick="debuggerRun()">Run</a>
<a href="#" class="close-notify" onclick="debuggerQuit()">Quit</a>
<a href="#" class="close-notify" onclick="debuggerClose()">X</a>"""
retvalue += """</div>"""
retvalue += """<style type="text/css" media="screen">
body {
overflow: hidden;
}
#editor {
margin: 0;
position: absolute;
top: 35px;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<pre id="editor">"""
retvalue += filebuf
retvalue += """</pre>"""
retvalue += """
<script src="/ui/ace/src/ace-uncompressed.js" type="text/javascript" charset="utf-8"></script>
<script src="/ui/ace/src/theme-crimson_editor.js" type="text/javascript" charset="utf-8"></script>
<script src="/ui/ace/src/mode-""" + extension_man(filext) + """.js" type="text/javascript" charset="utf-8"></script>
<script>
window.onload = function() {
window.aceEditor = ace.edit("editor");
window.curFullFile = " """ + add_doubledash(os.path.join(project_path, filename)) + """ ";
window.curFile = " """ + add_doubledash(os.path.join(filename)) + """ ";
window.projectPath = " """ + add_doubledash(project_path) + """ ";
window.projectName = " """ + project.strip() + """ ";
var editor = window.aceEditor;
editor.setTheme("ace/theme/crimson_editor");
var EditorMode = require("ace/mode/""" + extension_man(filext) + """").Mode;
editor.getSession().setMode(new EditorMode());
};
</script>"""
retvalue += div_events()
retvalue += """
</body>
</html>
"""
return retvalue
# SQL Lite related functions
def get_latest():
# Get the latest used projects, and returns the list, html formated
conn = get_conn()
c = conn.cursor()
html = ''
c.execute('''select name from latest order by qtty''')
buf = ''
for row in c:
if row[0]:
buf += '''<a href="/django-ide/open/''' + row[0] + '''">''' + row[0] + '''</a>'''
buf += '''<br>'''
c.close()
if buf:
buf = '''<h2>Latest Projects<a class="headerlink"></a></h2>''' + buf
return html + buf
def set_latest(project, path):
# Set the project as a project used
conn = get_conn()
c = conn.cursor()
c.execute("""select qtty from latest where name='""" + project + """'""")
qtty = 0
for row in c:
qtty = row[0]
if qtty == 0 or qtty is None:
c.execute("""insert into latest values('""" + project + """','""" + path + """',1)""")
else:
c.execute("""update latest set qtty=qtty+1 where name='""" + project + """'""")
conn.commit()
c.close()
def get_conn():
if not os.path.exists(os.getcwd() + '/ui/data/projects.db'):
conn = sqlite3.connect(os.getcwd() + '/ui/data/projects.db')
c = conn.cursor()
c.execute('''create table latest (name text, path text, qtty real)''')
conn.commit()
c.close()
else:
conn = sqlite3.connect(os.getcwd() + '/ui/data/projects.db')
return conn
def get_project_path(project):
conn = get_conn()
c = conn.cursor()
c.execute("""select path from latest where name='""" + project + """'""")
for row in c:
retval = row[0]
c.close()
return retval
def delete_project(project):
conn = get_conn()
c = conn.cursor()
c.execute("""delete from latest where name='""" + project + """'""")
conn.commit()
c.close()
def get_project_path_dip(project):
print project
f = open(project, 'r')
for l in f:
if l.startswith('NAME: '):
return l.replace("NAME: ", "")
def set_project_data(path, project,ide_path):
# Adds the project file and the debugger code
dpname = project + '.dip'
f = open(os.path.join(path, dpname), 'w')
f.write('This is a project marker file for Django-IDE, do not erase it! \n')
f.write('PATH: ' + path + '\n')
f.write('NAME: ' + project + '\n')
f.close()
# Copy debugger file
shutil.copy2(os.path.join(ide_path, "debugger.py"), os.path.join(path, "debugger.py"))
def launch_debugger(filename, main_func):
# Launchs a debugging session
output = "import " + filename[:-3] + "\n"
output += "import debugger \n"
output += "db = debugger.Debugger()"
output += "db.debug(" + filename + ")"
output += filename[:-1] + "." + main_func
def run_django_run(project_name):
retValue = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Django IDE: """ + project_name + """</title>
<link href="/ui/css/base.css" rel="stylesheet" type="text/css" media="screen" />
<link href="/ui/css/print.css" rel="stylesheet" type="text/css" media="print" />
<link rel="stylesheet" href="/ui/css/pygments.css" type="text/css" />
<link rel="shortcut icon" href="/ui/img/favicon.ico" />
</head>
<body id="documentation" class="default">
<div id="container">
<div id="header">
<h1 id="logo"><a href="/django-ide"><img src="/ui/img/hdr_logo.gif" alt="Django-IDE"/></a></h1>
<ul id="nav-global">
<li id="nav-open"><a href="/django-ide/open">Open Project</a></li>
<li id="nav-new"><a href="/django-ide/new">New Project</a></li>
<li id="nav-docs"><a href="/django-ide/docs">Documentation</a></li>
</ul>
</div>
<div id="billboard">
<h2><a></a></h2>
</div>
<div id="columnwrap">
<div id="content-main">
<div class="section">
<p>Running... on <a href="http://localhost:8000">http://localhost:8000</a></p>
"""
return retValue
def django_runserver(project_path, project_name):
curr_path = os.getcwd()
pid = subprocess.Popen([sys.executable, os.path.join(project_path, 'manage.py'), 'runserver', '8000']).pid
os.chdir(curr_path)
return run_django_run(project_name)