开发者

Writing to filesystem in App Engine development server

开发者 https://www.devze.com 2023-01-31 02:31 出处:网络
I\'m just trying out using scala and the scalate templating system on an appengine application. By default, scalate tries to write the compiled template to the filesystem. Now, obviously this won\'t w

I'm just trying out using scala and the scalate templating system on an appengine application. By default, scalate tries to write the compiled template to the filesystem. Now, obviously this won't work on appengine, and there is a way to precompile the templates. But I was wondering if it is possible 开发者_如何学JAVAto switch off this restriction, just during development. It slows down the compile/test cycle quite a bit.


In the Python dev server you can, I use it to log to a file when using the dev server:

if os.environ.get('SERVER_SOFTWARE','').startswith('Dev'):
    from google.appengine.tools.dev_appserver import FakeFile
    FakeFile.ALLOWED_MODES = frozenset(['a','r', 'w', 'rb', 'U', 'rU'])

If you want to write binary files or unicode you might need to add 'wb' or 'wU' to that list. Maybe there is something equivalent in the Java dev server.


I'm currently using webpy that has the same limitation, its templating system can't access parser module (blocked) and can't write to filesystem on Google App Engine, so you need to precompile the templates upfront.

I have resolved this annoying issue with a Python script that, everytime a file of a given directory is changed, triggers the precompilation of that file.

I'm on OSX and I'm using FSEvents but I believe you can find other solutions/libraries on any other platform (incron in Linux, FileSystemWatcher on Windows):

from fsevents import Observer
from fsevents import Stream
from datetime import datetime
import subprocess
import os
import time

PROJECT_PATH = '/Users/.../Project/GoogleAppEngine/stackprinter/'
TEMPLATE_COMPILE_PATH = os.path.join(PROJECT_PATH,'web','template.py')
VIEWS_PATH = os.path.join(PROJECT_PATH,'app','views')

def callback(event):
    if event.name.endswith('.html'):
        subprocess.Popen('python2.5 %s %s %s' % ( TEMPLATE_COMPILE_PATH ,'--compile', VIEWS_PATH) , shell=True)
        print '%s - %s compiled!' % (datetime.now(), event.name.split('/')[-1])

observer = Observer()
observer.start()
stream = Stream(callback, VIEWS_PATH, file_events=True)
observer.schedule(stream)

while not observer.isAlive():
    time.sleep(0.1) 


I'd strongly advise against using AppEngine...

If you're just looking for free JVM/webapp hosting, then Stax.net offers a better alternative . Amongst other features, it allows you to write to the filesystem and to spawn threads.

They also use Scala internally, so they're very accommodating towards other Scala developers :)

Stax.net: http://www.stax.net/

(Note: I'm in no way affilliated to Stax)

0

精彩评论

暂无评论...
验证码 换一张
取 消