-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdjango-ide.py
183 lines (156 loc) · 6.16 KB
/
django-ide.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
# Django IDE is based on the following products
# Django: https://www.djangoproject.com
# Bottle: http://bottlepy.org
# Ace: http://ace.ajax.org
# author: mRt: martincerdeira@gmail.com
import sys, os, subprocess, tools
from bottle import run, route, error, request, response, get, post, request, redirect, static_file, debug
PROJECT_PATH = "" # It would be nice to implement this PROJECT_ variables, with sessions...
PROJECT_NAME = ""
debugger = None
breaks = None # Break points Class to be passed to debugger.py
try:
import django
except ImportError, e:
print e
else:
###################
#### JS Events ####
###################
@post('/django-ide/editor/debug.file')
def debug_file():
curFullFile = request.forms.get('curFullFile')
global debugger
# do brilliant stuff here =)
@post('/django-ide/editor/save.file')
def save_file():
content = request.forms.get('content')
curFullFile = request.forms.get('curFullFile')
f = open(curFullFile.strip(),'w')
f.write(content)
f.close
@post('/django-ide/editor/delete.file')
def delete_file():
curFullFile = request.forms.get('curFullFile')
os.remove(curFullFile.strip())
@post('/django-ide/editornew/saveas.file')
@post('/django-ide/editor/saveas.file')
def saveas_file():
content = request.forms.get('content')
newFile = request.forms.get('newFile')
projectPath = request.forms.get('projectPath')
curFullFile = os.path.join(projectPath.strip(), newFile.strip())
f = open(curFullFile.strip(),'w')
f.write(content)
f.close
@post('/django-ide/editornew/add.breakpoint')
@post('/django-ide/editor/add.breakpoint')
def add_breakpoint():
curFile = request.forms.get('curFile')
line_no = request.forms.get('line_no')
global breaks
breaks.toggle_line(curFile,line_no)
##############################
#### Statis files routing ####
##############################
@route('/ui/css/:filename')
def css_file(filename):
return static_file(filename, root=os.getcwd() + '/ui/css/')
@route('/ui/img/:filename')
def css_file(filename):
return static_file(filename, root=os.getcwd() + '/ui/img/')
@route('/ui/ace/src/:filename')
def ace_file(filename):
return static_file(filename, root=os.getcwd() + '/ui/ace/src/')
@route('/ui/js/:filename')
def js_file(filename):
return static_file(filename, root=os.getcwd() + '/ui/js/')
###############################
###############################
###############################
@route('/django-ide/open/:project')
def open_project_with_name(project):
if project:
global PROJECT_PATH
global PROJECT_NAME
PROJECT_PATH = tools.get_project_path(project)
PROJECT_NAME = project
if not os.path.exists(PROJECT_PATH): # Somebody deleted it!
tools.delete_project(PROJECT_NAME)
return "The project no longer exists"
else:
tools.set_latest(PROJECT_NAME, PROJECT_PATH) # Increment project usage
return tools.file_lister(PROJECT_PATH, PROJECT_NAME)
@post('/django-ide/open/:project')
def open_project_with_name(project):
if project:
global PROJECT_PATH
global PROJECT_NAME
action = request.forms.get('pyaction')
if action == 'run':
return tools.django_runserver(PROJECT_PATH, PROJECT_NAME)
@route('/django-ide/open')
def open_project():
return static_file('open.html', root=os.getcwd() + '/ui/')
@post('/django-ide/open')
def openproject():
path = request.forms.get('Path')
project = tools.get_project_path_dip(path)
open_project_with_name(project)
@route('/django-ide/new')
def new_project():
return static_file('new.html', root=os.getcwd() + '/ui/')
@route('/django-ide')
def main_app():
return tools.render_main(os.getcwd() + '/ui/main.html')
@route('/django-ide/editor/:filename')
def editor_req(filename):
global PROJECT_PATH
global PROJECT_NAME
ext = os.path.splitext(filename)[1]
buf = ''
f = open(os.path.join(PROJECT_PATH, filename),'r')
for l in f:
buf += l
f.close
return tools.render_editor(PROJECT_PATH, filename, ext, PROJECT_NAME, buf)
@route('/django-ide/editornew/')
def editor_req():
global PROJECT_PATH
global PROJECT_NAME
ext = '.py'
return tools.render_editor(PROJECT_PATH, 'new file', ext, PROJECT_NAME, '')
@post('/django-ide/new')
def create():
path = request.forms.get('Path')
project = request.forms.get('Name')
if path:
curr_path = os.getcwd()
django_path = os.path.join(django.__path__[0], 'bin\\django-admin.py')
os.chdir(path)
global PROJECT_PATH
global PROJECT_NAME
PROJECT_PATH = os.path.join(path, project)
PROJECT_NAME = project
if not os.path.exists(PROJECT_PATH): # We won't create a project, if it exists...
subprocess.call([sys.executable, django_path, 'startproject', project])
tools.set_project_data(PROJECT_PATH, project, curr_path)
os.chdir(curr_path)
tools.set_latest(PROJECT_NAME, PROJECT_PATH) # Increment project usage
return tools.file_lister(PROJECT_PATH, PROJECT_NAME)
else:
return static_file('error.html', root=os.getcwd() + '/ui/')
@error(404)
def error_hdl(error):
return static_file('error.html', root=os.getcwd() + '/ui/')
def start_ide():
#Development server
global breaks
breaks = tools.BreakPoints() # BreakPoins UNIQUE instance
debug(True)
print '======================================================='
print 'Django IDE Launched on http://localhost:8080/django-ide'
print '======================================================='
run(host='localhost', port=8080)
if __name__ == '__main__':
start_ide()