Remove no longer needed cruft from serve_coffee

This commit is contained in:
Kovid Goyal 2012-01-17 09:20:39 +05:30
parent 41a31b4c48
commit 7a6963dac3

View File

@ -12,7 +12,7 @@ Utilities to help with developing coffeescript based apps.
A coffeescript compiler and a simple web server that automatically serves A coffeescript compiler and a simple web server that automatically serves
coffeescript files as javascript. coffeescript files as javascript.
''' '''
import sys, traceback, importlib, io import sys, traceback, io
if sys.version_info.major > 2: if sys.version_info.major > 2:
print('This script is not Python 3 compatible. Run it with Python 2', print('This script is not Python 3 compatible. Run it with Python 2',
file=sys.stderr) file=sys.stderr)
@ -24,43 +24,15 @@ from SimpleHTTPServer import SimpleHTTPRequestHandler
from PyQt4.Qt import QCoreApplication, QScriptEngine, QScriptValue from PyQt4.Qt import QCoreApplication, QScriptEngine, QScriptValue
# Infrastructure {{{
def import_from_calibre(mod):
try:
return importlib.import_module(mod)
except ImportError:
import init_calibre
init_calibre
return importlib.import_module(mod)
_store_app = gui_thread = None
def fork_job(*args, **kwargs):
try:
return import_from_calibre('calibre.utils.ipc.simple_worker').fork_job(*args,
**kwargs)
except ImportError:
# We aren't running in calibre
import subprocess
raw, filename = kwargs['args']
cs = ''
try:
p = subprocess.Popen([sys.executable, __file__, 'compile', '-'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if isinstance(raw, unicode):
raw = raw.encode('utf-8')
stdout, stderr = p.communicate(raw)
cs = stdout.decode('utf-8')
errors = [stderr]
except:
errors = [traceback.format_exc()]
return {'result':(cs, errors)}
# }}}
class Compiler(QScriptEngine): # {{{ class Compiler(QScriptEngine): # {{{
'''
You can use this class in any thread, but make sure you instantiate it in
the main thread. Alternatively, construct a QCoreApplication in the main
thread, after which you can instantiate this class and use it in any
thread.
'''
def __init__(self): def __init__(self):
if QCoreApplication.instance() is None: if QCoreApplication.instance() is None:
self.__app_ = QCoreApplication([]) self.__app_ = QCoreApplication([])
@ -87,27 +59,11 @@ class Compiler(QScriptEngine): # {{{
return '', [unicode(res.toString())] return '', [unicode(res.toString())]
return unicode(res.toString()), [] return unicode(res.toString()), []
def forked_compile(raw, fname):
# Entry point for the compile worker
try:
ans, errors = Compiler()(raw, fname)
except:
ans, errors = '', [traceback.format_exc()]
return ans, errors
# }}} # }}}
def compile_coffeescript(raw, filename=None): def compile_coffeescript(raw, filename=None):
try: return Compiler()(raw, filename)
cs, errors = fork_job('calibre.utils.serve_coffee',
'forked_compile', args=(raw, filename), timeout=5,
no_output=True)['result']
except Exception as e:
cs = None
errors = [getattr(e, 'orig_tb', traceback.format_exc())]
return cs, errors
class HTTPRequestHandler(SimpleHTTPRequestHandler): # {{{ class HTTPRequestHandler(SimpleHTTPRequestHandler): # {{{
''' '''
@ -284,7 +240,7 @@ class Handler(HTTPRequestHandler): # {{{
mtime = time.time() mtime = time.time()
with open(src, 'rb') as f: with open(src, 'rb') as f:
raw = f.read() raw = f.read()
cs, errors = compile_coffeescript(raw, src) cs, errors = self.compiler(raw, src)
for line in errors: for line in errors:
print(line, file=sys.stderr) print(line, file=sys.stderr)
if not cs: if not cs:
@ -318,6 +274,7 @@ class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): # {{{
def serve(resources={}, port=8000, host='0.0.0.0'): def serve(resources={}, port=8000, host='0.0.0.0'):
Handler.special_resources = resources Handler.special_resources = resources
Handler.compiler = Compiler()
httpd = Server((host, port), Handler) httpd = Server((host, port), Handler)
print('serving %s at %s:%d with PID=%d'%(os.getcwdu(), host, port, os.getpid())) print('serving %s at %s:%d with PID=%d'%(os.getcwdu(), host, port, os.getpid()))
try: try: