Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 333 lines (288 sloc) 11.451 kb
e451e6d @mcerdeira 'initial'
mcerdeira authored
1 # All pourpose dirty, ugly tools and stuff
2
3 import os, sys, sqlite3
4 import shutil, subprocess, webbrowser, time
5
6 def add_doubledash(fname):
7 """ turns slashes into double slashes
8 in order to pass to javascript,and back
9 """
10 return fname.replace("\\","\\\\")
11
12 def filesize(num):
13 for x in ['bytes','KB','MB','GB','TB']:
14 if num < 1024.0:
15 return "%3.1f%s" % (num, x)
16 num /= 1024.0
17
18
19 def findexts (f):
20 """ finds extention of file
21 """
22 return os.path.splitext(f)[1]
23
24
25 def render_main(mfile):
26 retvalue = ''
27 f = open(mfile, 'r')
28 for l in f:
29 retvalue += l
30 f.close()
31
32 retvalue += get_latest()
33 retvalue += '''</body>
34 </head>'''
35
36 return retvalue
37
38 def file_lister(root, project):
39 retvalue = """
40 <!DOCTYPE html>
41 <html>
42 <head>
43 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
44 <title>Django IDE: """ + project + """</title>
45 <link href="/ui/css/base.css" rel="stylesheet" type="text/css" media="screen" />
46 <link href="/ui/css/print.css" rel="stylesheet" type="text/css" media="print" />
47 <link rel="stylesheet" href="/ui/css/pygments.css" type="text/css" />
48 <link rel="shortcut icon" href="/ui/img/favicon.ico" />
49 </head>
50
51 <body id="documentation" class="default">
52 <div id="container">
53 <div id="header">
54 <h1 id="logo"><a href="/django-ide"><img src="/ui/img/hdr_logo.gif" alt="Django-IDE"/></a></h1>
55 <ul id="nav-global">
56 <li id="nav-open"><a href="/django-ide/open">Open Project</a></li>
57 <li id="nav-new"><a href="/django-ide/new">New Project</a></li>
58 <li id="nav-docs"><a href="/django-ide/docs">Documentation</a></li>
59 </ul>
60 </div>
61 <div id="billboard">
62 <h2><a></a></h2>
63 </div>
64
65
66 <div class="section">
67 """
68
69 # opens this directory
70 dirArray = os.listdir(root)
71
72 # counts elements in array
73 indexCount = len(dirArray);
74
75 # sorts files
76 dirArray.sort()
77
78 # print 'em
79 retvalue += """<table width='100%' cellspacing='10'>
80 <tr>
81 <td class='head'>Filename</td>
82 <td class='head'>Type</td>
83 <td class='head'>Size</td></tr>"""
84
85 # loops through the array of files and print them all
86 for f in dirArray:
87 extension = findexts(f)
88 if not extension.endswith(".pyc") and not extension.endswith(".pyo"):
89 retvalue += """<tr><td><a href='""" + """/django-ide/editor/""" + f + """'>""" + f + """</a></td>"""
90 retvalue += """<td>"""
91 retvalue += extension
92 retvalue += """</td>"""
93 retvalue += """<td>"""
94 retvalue += filesize(os.path.getsize(os.path.join(root, f)))
95 retvalue += """</td>"""
96 retvalue += """</tr>"""
97
98 retvalue += """<tr><td><a href='""" + """/django-ide/editornew/'>add a new file</a></td>"""
99 retvalue += """<td>"""
100 retvalue += """</td>"""
101 retvalue += """<td>"""
102 retvalue += """</td>"""
103 retvalue += """</tr>"""
104
105 retvalue += """</table>"""
106 retvalue += """
107 </div>
108 <form method='post'>
109 <br>
110 <input type="hidden" name="pyaction" id="pyaction" value="">
111 <input type="submit" onclick="document.getElementById('pyaction').value='run'" value="Run Project"/>
112 </form>
113 </body>
114 </html>
115 """
116 return retvalue
117
118 def extension_man(filext):
119 print filext
120 if filext == '.py':
121 return 'python'
122 elif filext == '.js':
123 return 'javascript'
124 elif filext == '.php':
125 return 'php'
126 else:
127 return ''
128
129 def render_editor(project_path, filename, filext, project, filebuf):
130 if filebuf:
131 save_cmd = "saveNotice()"
132 else:
133 # If the file content is empty, save() must be saveAs()
134 save_cmd = "saveasNotice()"
135
136 retvalue = """ <!DOCTYPE html>
137 <html lang="en">
138 <head>
139 <meta charset="UTF-8">
140 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
141 <title>""" + project + """ : """ + filename + """</title>
142 <link rel="shortcut icon" href="/ui/img/favicon.ico" />
143 <link href="/ui/css/toolbar.css" rel="stylesheet" type="text/css" />
144 <script src="/ui/js/jquery.min.js"></script>
145 <script src="/ui/js/toolbar.js"></script>"""
146 retvalue += """<div id="message" style="display: none;">
147 <a href="/django-ide/open/""" + project + """" class="close-notify">Project</a>
148 <a href="/django-ide/editornew/" class="close-notify">New File</a>
149 <a href="#" class="close-notify" onclick=" """ + save_cmd + """ ">Save</a>
150 <a href="#" class="close-notify" onclick="saveasNotice()">Save As</a>
151 <a href="#" class="close-notify" onclick="deleteNotice()">Delete</a>"""
152
153 if filext == '.py': #Debbuger only for .py files!
154 retvalue += """<a href="#" class="close-notify" onclick="debugNotice()">Debug</a>
155 </div>
156 <div id="debugger" style="display: none;">
157 <a href="#" class="close-notify" onclick="debuggerStep()">Step</a>
158 <a href="#" class="close-notify" onclick="debuggerRun()">Run</a>
159 <a href="#" class="close-notify" onclick="debuggerQuit()">Quit</a>
160 <a href="#" class="close-notify" onclick="debuggerClose()">X</a>"""
161
162 retvalue += """</div>"""
163 retvalue += """<style type="text/css" media="screen">
164 body {
165 overflow: hidden;
166 }
167 #editor {
168 margin: 0;
169 position: absolute;
170 top: 35px;
171 bottom: 0;
172 left: 0;
173 right: 0;
174 }
175 </style>
176 </head>
177 <body>
178
179 <pre id="editor">"""
180 retvalue += filebuf
181 retvalue += """</pre>
182
183 <script src="/ui/ace/src/ace-uncompressed.js" type="text/javascript" charset="utf-8"></script>
184 <script src="/ui/ace/src/theme-crimson_editor.js" type="text/javascript" charset="utf-8"></script>
185 <script src="/ui/ace/src/mode-""" + extension_man(filext) + """.js" type="text/javascript" charset="utf-8"></script>
186
187 <script>
188 window.onload = function() {
189 window.aceEditor = ace.edit("editor");
190 window.curFullFile = " """ + add_doubledash(os.path.join(project_path, filename)) + """ ";
191 window.curFile = " """ + add_doubledash(os.path.join(filename)) + """ ";
192 window.projectPath = " """ + add_doubledash(project_path) + """ ";
193 window.projectName = " """ + project.strip() + """ ";
194
195 var editor = window.aceEditor;
196 editor.setTheme("ace/theme/crimson_editor");
197 var EditorMode = require("ace/mode/""" + extension_man(filext) + """").Mode;
198 editor.getSession().setMode(new EditorMode());
199 };
200 </script>
201
202 </body>
203 </html>
204 """
205 return retvalue
206
207
208 # SQL Lite related functions
209
210 def get_latest():
211 # Get the latest used projects, and returns the list, html formated
212 conn = get_conn()
213 c = conn.cursor()
214 html = ''
215 c.execute('''select name from latest order by qtty''')
216 buf = ''
217 for row in c:
218 if row[0]:
219 buf += '''<a href="/django-ide/open/''' + row[0] + '''">''' + row[0] + '''</a>'''
220 buf += '''<br>'''
221 c.close()
222 if buf:
223 buf = '''<h2>Latest Projects<a class="headerlink"></a></h2>''' + buf
224
225 return html + buf
226
227 def set_latest(project, path):
228 # Set the project as a project used
229 conn = get_conn()
230 c = conn.cursor()
231 c.execute("""select qtty from latest where name='""" + project + """'""")
232 qtty = 0
233 for row in c:
234 qtty = row[0]
235
236 if qtty == 0 or qtty is None:
237 c.execute("""insert into latest values('""" + project + """','""" + path + """',1)""")
238 else:
239 c.execute("""update latest set qtty=qtty+1 where name='""" + project + """'""")
240
241 conn.commit()
242 c.close()
243
244 def get_conn():
245 if not os.path.exists(os.getcwd() + '/ui/data/projects.db'):
246 conn = sqlite3.connect(os.getcwd() + '/ui/data/projects.db')
247 c = conn.cursor()
248 c.execute('''create table latest (name text, path text, qtty real)''')
249 conn.commit()
250 c.close()
251 else:
252 conn = sqlite3.connect(os.getcwd() + '/ui/data/projects.db')
253 return conn
254
255 def get_project_path(project):
256 conn = get_conn()
257 c = conn.cursor()
258 c.execute("""select path from latest where name='""" + project + """'""")
259 for row in c:
260 retval = row[0]
261 c.close()
262 return retval
263
264 def delete_project(project):
265 conn = get_conn()
266 c = conn.cursor()
267 c.execute("""delete from latest where name='""" + project + """'""")
268 conn.commit()
269 c.close()
270
271 def get_project_path_dip(project):
272 print project
273 f = open(project, 'r')
274 for l in f:
275 if l.startswith('NAME: '):
276 return l.replace("NAME: ", "")
277
278 def set_project_data(path, project,ide_path):
279 # Adds the project file and the debugger code
280 dpname = project + '.dip'
281 f = open(os.path.join(path, dpname), 'w')
282 f.write('This is a project marker file for Django-IDE, do not erase it! \n')
283 f.write('PATH: ' + path + '\n')
284 f.write('NAME: ' + project + '\n')
285 f.close()
286 # Copy debugger file
287 shutil.copy2(os.path.join(ide_path, "debugger.py"), os.path.join(path, "debugger.py"))
288
289 def launch_debugger(filename, main_func):
290 # Launchs a debugging session
291 output = "import " + filename[:-3] + "\n"
292 output += "import debugger \n"
293 output += "db = debugger.Debugger()"
294 output += "db.debug(" + filename + ")"
295 output += filename[:-1] + "." + main_func
296
297 def run_django_run(project_name):
298 retValue = """
299 <!DOCTYPE html>
300 <html>
301 <head>
302 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
303 <title>Django IDE: """ + project_name + """</title>
304 <link href="/ui/css/base.css" rel="stylesheet" type="text/css" media="screen" />
305 <link href="/ui/css/print.css" rel="stylesheet" type="text/css" media="print" />
306 <link rel="stylesheet" href="/ui/css/pygments.css" type="text/css" />
307 <link rel="shortcut icon" href="/ui/img/favicon.ico" />
308 </head>
309 <body id="documentation" class="default">
310 <div id="container">
311 <div id="header">
312 <h1 id="logo"><a href="/django-ide"><img src="/ui/img/hdr_logo.gif" alt="Django-IDE"/></a></h1>
313 <ul id="nav-global">
314 <li id="nav-open"><a href="/django-ide/open">Open Project</a></li>
315 <li id="nav-new"><a href="/django-ide/new">New Project</a></li>
316 <li id="nav-docs"><a href="/django-ide/docs">Documentation</a></li>
317 </ul>
318 </div>
319 <div id="billboard">
320 <h2><a></a></h2>
321 </div>
322 <div id="columnwrap">
323 <div id="content-main">
324 <div class="section">
325 <p>Running... on <a href="http://localhost:8000">http://localhost:8000</a></p>
326 """
327 return retValue
328
329 def django_runserver(project_path, project_name):
330 curr_path = os.getcwd()
331 pid = subprocess.Popen([sys.executable, os.path.join(project_path, 'manage.py'), 'runserver', '8000']).pid
332 os.chdir(curr_path)
333 return run_django_run(project_name)
Something went wrong with that request. Please try again.