-
Notifications
You must be signed in to change notification settings - Fork 3
/
easyeditor.py
190 lines (136 loc) · 4.9 KB
/
easyeditor.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
import ntpath
import os
import subprocess
import sys
from flask import Flask
from flask import render_template, request
from flask_babel import Babel
from flask_login import login_required, current_user
from flask_security import RoleMixin, UserMixin, SQLAlchemyUserDatastore, \
Security
from flask_sqlalchemy import SQLAlchemy
from config import LANGUAGES
easy_editor = Flask(__name__)
easy_editor.config.from_object('config')
user_file = easy_editor.config['USER_FILE_DIR_PATH']
# for i18n
babel = Babel(easy_editor)
# Define the DB
db = SQLAlchemy(easy_editor)
# Define models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(),
db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(),
db.ForeignKey('role.id')))
# Role table
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
# User's table
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
# file path table
class Path(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
file_path = db.Column(db.String(255))
def __init__(self, user_id, path):
self.user_id = user_id
self.file_path = path
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(easy_editor, user_datastore)
@babel.localeselector
def get_locale():
return request.accept_languages.best_match(LANGUAGES.keys())
@easy_editor.route('/')
def index():
return render_template('index.html', files=[])
@login_required
@easy_editor.route('/_file_compile', methods=['POST'])
def file_compile():
filename = request.form['filename']
assert filename is not None
if len(filename.split('.')[0]) == 0:
return "ERROR"
result = subprocess_open('gcc -S ' + user_file + filename)
if not len(result[1]):
return "Good"
else:
return result[1]
def subprocess_open(command):
popen = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
(stdoutdata, stderrdata) = popen.communicate()
return stdoutdata, stderrdata
@login_required
@easy_editor.route('/_code_to_file', methods=['POST'])
def code_to_file():
filename = request.form['filename']
if len(filename.split('.')[0]) == 0:
return "ERROR"
code = request.form['code']
file_path = os.path.join(user_file, filename)
with open(file_path, 'w') as f:
path_record = Path(user_id=User.query.filter_by(email=current_user.
email).one().id,
path=file_path)
f.write(code)
db.session.add(path_record)
db.session.commit()
return filename
# 목록 보여주기
@login_required
@easy_editor.route('/_file_list', methods=['GET'])
def file_list():
paths = Path.query.filter_by(
user_id=User.query.filter_by(email=current_user.email).one().id).all()
files = [ntpath.basename(path.file_path) for path in paths]
return render_template('index.html', files=files)
@login_required
@easy_editor.route('/_delete', methods=['POST'])
def delete():
filename = request.form['filename']
if len(filename.split('.')[0]) == 0:
return "ERROR"
file_path = user_file + filename
path_record = Path.query.filter_by(user_id=User.query
.filter_by(email=current_user
.email).one().id,
file_path=file_path).one()
if not path_record:
os.remove(file_path)
db.session.delete(path_record)
db.session.commit()
else:
return "ERROR"
return filename
# 목록으로부터 파일 불러오기
@login_required
@easy_editor.route('/_load', methods=['POST', 'GET'])
def load():
filename = request.form['filename']
if len(filename.split('.')[0]) == 0:
return "ERROR"
file_path = os.path.join(user_file, filename)
with open(file_path, 'r') as f:
code = f.read()
return code
if __name__ == "__main__":
try:
mode = sys.argv[1]
except IndexError:
mode = "develop"
if mode == "develop":
easy_editor.run(threaded=True, port=int(8080))
elif mode == "deploy":
easy_editor.config.update(DEBUG=False)
easy_editor.run(host="0.0.0.0", port=int(80), threaded=True)