/
mdview.py
265 lines (222 loc) · 8.49 KB
/
mdview.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
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
'''
Created on 2012. 9. 30.
@author: stania
'''
import cherrypy
import markdown2
import os, time, sys, imp
import win32api
import win32event
import pywintypes
import StringIO
import webbrowser
import threading
import glob
import mimetypes
import codecs
import urllib
from jinja2 import Template
import resource
from settings import load_settings
from cherrypy.lib.static import serve_file, staticdir, serve_fileobj
markdowner = markdown2.Markdown()
settings = load_settings()
RES_ID = {}
def main_is_frozen():
return (hasattr(sys, "frozen") or
hasattr(sys, "importers") or
imp.is_frozen("__main__"))
def get_base_dir():
return settings.base_dir
def get_res_dir():
return settings.res_dir
if main_is_frozen():
RES_ID = resource.res_id_dict()
def get_res_data(path):
if main_is_frozen():
path = path.replace("/", "\\")
if RES_ID.has_key(path):
res = win32api.LoadResource(0, u'RESOURCE', RES_ID[path])
return StringIO.StringIO(res).read()
else:
return None
else:
return open(os.path.join(get_res_dir(), path)).read()
def get_content_type(path):
guess = mimetypes.guess_type(path)[0]
if guess != None:
return guess
elif os.path.splitext(path)[-1] == ".md":
return "text/plain"
else:
return "application/octet-stream"
def endswith_md_ext(p):
return any(p.endswith(_) for _ in settings.md_exts)
def serve_resource(path):
if main_is_frozen():
path = path.replace("/", "\\")
if RES_ID.has_key(path):
return serve_fileobj(
StringIO.StringIO(win32api.LoadResource(0, u'RESOURCE', RES_ID[path])),
content_type=get_content_type(path)
)
else:
raise cherrypy.NotFound
else:
return serve_file(os.path.abspath(os.path.join(get_res_dir(), path)))
class Root(object):
@classmethod
def _get_subpath(cls, args):
if len(args) > 0 and args[0] is "":
args = args[1:]
subpath = urllib.unquote("/".join(args))
if type(subpath) == str:
subpath = subpath.decode("utf-8")
return subpath
def find_indexfile(self, *args):
basedir = self._get_subpath(args)
for entry in ["index.html"] + ["index" + _ for _ in settings.md_exts]:
if os.path.exists(os.path.join(basedir, entry)):
return entry
return None
def dir_handler(self, basedir, subpath):
abspath = os.path.join(basedir, subpath)
abspath = os.path.normpath(abspath)
if not abspath.startswith(basedir):
raise cherrypy.HTTPError(403)
if self.find_indexfile(abspath):
print "index found", self.find_indexfile(abspath), "/".join([abspath, self.find_indexfile(abspath)])
indexpath = self._get_subpath(abspath) + "/" + self.find_indexfile(abspath)
raise cherrypy.HTTPRedirect(indexpath)
result = u""
result += u"<p>This program renders {} with Markdown renderer.</p>\n".format(str(settings.md_exts))
result += u"<ul>\n"
md_exists = False
# if abspath contains non-ascii char, type(abspath) is unicode. unless, str.
# let's make it as same type
if type(abspath) == str:
abspath = abspath.decode("utf-8")
# below here, abspath is now unicode
entries = os.listdir(abspath)
if abspath != basedir:
href = os.path.dirname(subpath)
result += u"""\t<li><a href="/{}">../</a></li>\n""".format(href)
for entry in entries:
entry4user = entry
if entry is u".":
continue
if endswith_md_ext(entry):
md_exists = True
if os.path.isdir(os.path.join(abspath, entry)):
entry4user += u"/"
href = self._get_subpath([subpath, entry])
if len(href) > 0 and href[0] != "/":
href = "/" + href
result += u"""\t<li><a href="{}">{}</a></li>\n""".format(href, entry4user)
result += u"</ul>\n"
if not md_exists:
result += u"<p>You don't have any Markdown document! You can start with creating Markdown document within same directory with executable(ex: mdview.exe)</p>"
template = Template(get_res_data("dirindex.html"))
return template.render(path=abspath, contents=result)
def cmd_handler(self, *args, **kwargs):
if len(args) > 0 and args[0] == settings.RES_PATH:
return serve_resource(self._get_subpath(args[1:]))
else:
raise cherrypy.NotFound
def render_markdown(self, path):
f = None
try:
f = open(os.path.join(get_base_dir(), path), "rb")
data = f.read()
try:
data = data.decode("utf-8")
except:
print "file is mbcs"
data = data.decode("mbcs")
return markdowner.convert(data.encode("utf-8"))
finally:
if f: f.close()
@cherrypy.expose
def default(self, *args, **kwargs): # fallback
template = Template(get_res_data("default.html"))
return self.default_dispatcher(template, args, **kwargs)
def default_dispatcher(self, template, args, **kwargs):
if kwargs.has_key("ar"):
_subpath = self._get_subpath(["/ar"] + list(args))
if settings.debug: print _subpath
raise cherrypy.HTTPRedirect(_subpath)
if len(args) > 0 and args[0][0] is '.':
return self.cmd_handler(*args, **kwargs)
subpath = self._get_subpath(args)
if settings.debug: print "default:", subpath
if endswith_md_ext(subpath):
if template:
return template.render(path=subpath, contents=self.render_markdown(subpath))
else:
return self.render_markdown(subpath)
elif os.path.isdir(os.path.join(get_base_dir(), subpath)):
return self.dir_handler(get_base_dir(), subpath)
else:
return serve_file(os.path.join(get_base_dir(), subpath))
@cherrypy.expose
def ar(self, *args, **kwargs):
subpath = self._get_subpath(args)
if not endswith_md_ext(subpath):
raise cherrypy.HTTPRedirect(subpath)
template = Template(get_res_data("ar.html"))
return self.default_dispatcher(template, args, **kwargs)
@cherrypy.expose
def wfc(self, *args, **kwargs): #wait for change
subpath = self._get_subpath(args)
if settings.debug: print "wfc:", subpath
f = os.path.abspath(os.path.join(get_base_dir(), subpath))
parent = os.path.dirname(f)
timeout = kwargs.has_key("timeout") and int(kwargs["timeout"]) or 60000
try:
print "watching", f
handle = win32api.FindFirstChangeNotification(parent, 0, 0x010)
ret = win32event.WaitForSingleObject(handle, int(timeout))
if ret is win32event.WAIT_TIMEOUT:
raise cherrypy.HTTPError(304)
time.sleep(0.2)
return self.default_dispatcher(None, args, **kwargs)
except pywintypes.error as e: #@UndefinedVariable
print e[2]
raise cherrypy.HTTPError(500)
finally:
if handle:
win32api.FindCloseChangeNotification(handle)
@cherrypy.expose
def index(self, *args, **kwargs):
return self.default_dispatcher(None, args, **kwargs)
class MutexException(BaseException):
pass
mutex_handle = None
class single_instance():
def __init__(self):
global mutex_handle
ERROR_ALREADY_EXISTS = 183
mutex_handle = win32event.CreateMutex(None, 1, "MDVIEW")
if mutex_handle and win32api.GetLastError() is ERROR_ALREADY_EXISTS:
raise MutexException()
else:
print "mutex created", mutex_handle
t = None
try:
me = single_instance()
if not settings.debug:
print "RUNNING IN PRODUCTION MODE"
print "base directory:", settings.base_dir
cherrypy.tree.mount(Root())
cherrypy.server.socket_port = settings.server_port
cherrypy.engine.blocking = False
cherrypy.engine.start()
t = threading.Thread(target=lambda: webbrowser.open("http://localhost:"+str(settings.server_port)))
t.start()
cherrypy.engine.block()
except MutexException:
pass
finally:
if not t:
t = threading.Thread(target=lambda: webbrowser.open("http://localhost:"+str(settings.server_port)))
t.start()