forked from ankostis/rsted
-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.py
executable file
·195 lines (160 loc) · 5.51 KB
/
application.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
#!/usr/bin/env python
# all the imports
import os, sys
import shutil
try:
reload(sys)
sys.setdefaultencoding('utf-8')
except:
pass
import logging
from flask import Flask, request, render_template, make_response, url_for, jsonify
from rsted.html import rst2html as _rst2html
from rsted.pdf import rst2pdf as _rst2pdf
from flaskext.redis import RedisManager
from flaskext.helpers import render_html
# handle relative path references by changing to project directory
run_from = os.path.dirname(os.path.abspath(__file__))
if run_from != os.path.curdir:
os.chdir(run_from)
# create our little application :)
app = Flask(__name__)
app.config.from_pyfile(os.environ.get('RSTED_CONF', 'settings.py'))
redis = RedisManager(app).get_instance()
REDIS_EXPIRE = app.config.setdefault('REDIS_EXPIRE', 60*60*24*30*6)# Default 6 months
REDIS_PREFIX = app.config.setdefault('REDIS_PREFIX', 'rst_')
FILES_DIR = app.config.setdefault('FILES_DIR', 'files')
def view_is_active(view_name):
if request.path == url_for(view_name):
return 'active'
return ''
@app.context_processor
def ctx_pro():
return {
'MEDIA_URL': '/static/',
'is_active': view_is_active
}
@app.route("/")
@render_html('index.html')
def index():
yield 'js_params', {'theme': request.args.get('theme', '')}
saved_doc_id = request.args.get('n')
if saved_doc_id:
rst = redis.get('%s%s' % (REDIS_PREFIX, saved_doc_id))
if rst:
rst = rst.decode('utf-8')
yield 'rst', rst
yield 'document', saved_doc_id
@app.route('/srv/rst2html/', methods=['POST', 'GET'])
def rst2html():
rst = request.form.get('rst', '')
theme = request.form.get('theme')
if theme == 'basic':
theme = None
try:
html = _rst2html(rst, theme=theme)
except Exception as ex:
html = "<h1>Generation FAILED!</h1>\n<p>%s" % ex
logging.exception(ex)
return html
@app.route('/srv/rst2pdf/', methods=['POST'])
def rst2pdf():
rst = request.form.get('rst', '')
theme = request.form.get('theme')
if theme == 'basic':
theme = None
pdf = _rst2pdf(rst, theme=theme)
responce = make_response(pdf)
responce.headers['Content-Type'] = 'application/pdf'
responce.headers['Content-Disposition'] = 'attachment; filename="rst.pdf"'
responce.headers['Content-Transfer-Encoding'] = 'binary'
return responce
@app.route('/srv/save_rst/', methods=['POST'])
def save_rst():
rst = request.form.get('rst')
if not rst:
return ''
rst = rst.encode('utf-8')
from hashlib import md5
md5sum = md5(rst).hexdigest()
redis_key = '%s%s' % (REDIS_PREFIX, md5sum)
if redis.setnx(redis_key, rst) and REDIS_EXPIRE:
redis.expire(redis_key, REDIS_EXPIRE)
response = make_response(md5sum)
response.headers['Content-Type'] = 'text/plain'
return response
@app.route('/srv/save_file/', methods=['POST'])
def save_file():
rst = request.form.get('rst')
project = request.form.get('project')
filename = request.form.get('filename')
if not rst:
return ''
if not project:
return ''
if not filename:
return ''
file_path = os.path.join(FILES_DIR, project, filename + '.rst')
with open(file_path, 'w') as rst_obj:
rst_obj.write(rst)
return jsonify(**{'files': get_project_files(project)})
@app.route('/srv/load_file/', methods=['GET'])
def load_file():
project = request.args.get('project')
filename = request.args.get('filename')
if not project:
return ''
if not filename:
return ''
file_path = os.path.join(FILES_DIR, project, filename + '.rst')
with open(file_path, 'r') as rst_obj:
content = rst_obj.read()
print(file_path, content)
return jsonify(**{'content': content})
def get_project_files(project):
project_dir = os.path.join(FILES_DIR, project)
os.makedirs(project_dir, exist_ok=True)
content = os.listdir(project_dir)
return sorted([item[:-4] for item in content if item.endswith('.rst')])
def get_projects():
os.makedirs(FILES_DIR, exist_ok=True)
content = os.listdir(FILES_DIR)
return sorted([item for item in content if os.path.isdir(os.path.join(FILES_DIR, item))])
@app.route('/srv/files/', methods=['GET'])
def files_list():
project = request.args.get('project')
if not project:
return ''
return jsonify(**{'files': get_project_files(project)})
@app.route('/srv/projects/', methods=['GET'])
def projects():
return jsonify(**{'projects': get_projects()})
@app.route('/srv/del_rst/', methods=['GET'])
def del_rst():
saved_id = request.args.get('n')
if saved_id:
redis_key = '%s%s' % (REDIS_PREFIX, saved_id)
redis.delete(redis_key)
response = make_response()
response.headers['Content-Type'] = 'text/plain'
return response
@app.route('/srv/delete/', methods=['POST'])
def delete():
project = request.form.get('project')
filename = request.form.get('filename')
project_path = os.path.join(FILES_DIR, project)
file_path = os.path.join(FILES_DIR, project, filename + '.rst')
try:
if os.path.isfile(file_path):
os.remove(file_path)
del_rst()
elif os.path.isdir(project_path):
shutil.rmtree(project_path)
except OSError:
print('path not found')
response = make_response()
response.headers['Content-Type'] = 'text/plain'
return response
if __name__ == '__main__':
app.run(host=app.config.get('HOST', '0.0.0.0'),
port=app.config.get('PORT', 5000))