def __init__(self, handlers=None, default_host="", transforms=None, wsgi=False, **settings): if not handlers: handlers = [ (r"/_/api/changes", ChangeRequestHandler), (r"/_/api/?(.*)", APIRequestHandler), (r"/_/?(.*)", AssetsHandler, {"path": os.path.join(module_path(), '_')}), (r"/", RedirectHandler, {'url': '/_/index.html'}), ] if not settings: settings = { 'debug': True, 'template_path': os.path.join(module_path(), '_'), } if not default_host: default_host = '.*$' Application.__init__(self, handlers, default_host, transforms, wsgi, **settings) self.internal_handler_count = len(handlers) self.change_request_handlers = set() self.observer = ChangesObserver(changes_handler=self.change_happened) self.config_path = config_path() self.config = self.load_config() self.project = None for project in self.config.get('projects', []): if project.get('isCurrent'): self.project = project self.set_project(project) break
def load_project(self, target_project): found = False for old_project in self.config.projects: old_project.active = False if old_project.path == target_project.path: old_project.active = True found = True if not found: self.config.projects.append(target_project) target_project.active = True if len(self.handlers) > 1: self.handlers.pop(-1) if target_project.targetHost: self.add_handlers(".*$", [ (r"/(.*)", ForwardRequestHandler), ]) ForwardRequestHandler.forward_host = target_project.targetHost else: self.add_handlers(".*$", [ (r"/", RedirectHandler, {'url': '/_/index.html'}), (r"/(.*)\.md", MarkDownHandler), (r"/(.*)", StaticSiteHandler, {"path": target_project.path}), ]) handle = self.handlers.pop(0) self.handlers.insert(self._handlers_count, handle) self.watcher.observe(target_project.path, target_project.muteList) if MODE == DEVELOP_MODE: self.watcher.observer.schedule(self.watcher, module_path(), recursive=True)
def __init__(self): handlers = [ (r"/_/api/changes", ChangeRequestHandler), (r"/_/api/(.*)", APIRequestHandler), (r"/_/(.+)", AssetsHandler, {"path": os.path.join(module_path(), '_')}), (r"/_/?", RedirectHandler, {'url': '/_/index.html'}), # (r"/", RedirectHandler, {'url': '/_/index.html'}), ] self._handlers_count = len(handlers) settings = { 'debug': True, 'template_path': os.path.join(module_path(), '_'), } Application.__init__(self, handlers, ".*$", None, False, **settings) if self.project: self.load_project(self.project)
def set_project(self, project): self.project = project path = project['path'] black_list = project.get('blockPaths', []) if len(self.handlers) > 1: self.handlers.pop(-1) self.add_handlers(".*$", [ (r"/(.*)\.md", MarkDownHandler), (r"/(.*)", StaticSiteHandler, {"path": path}), ]) handle = self.handlers.pop(0) self.handlers.insert(self.internal_handler_count, handle) self.observer.observe(path, black_list) if MODE == 'develop': self.observer.observer.schedule(self.observer, module_path(), recursive=True)
from collections import namedtuple import cPickle from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler, \ EVENT_TYPE_CREATED, EVENT_TYPE_DELETED, EVENT_TYPE_MOVED from tornado import ioloop from tornado.web import Application, RedirectHandler, StaticFileHandler from pyf5.utils import module_path, get_rel_path, we_are_frozen, config_path from pyf5.handlers import ChangeRequestHandler, AssetsHandler, StaticSiteHandler, APIRequestHandler, MarkDownHandler MODE = None if we_are_frozen(): MODE = 'freezed' elif 'site-package' in module_path(): MODE = 'source' else: MODE = 'develop' # freeze 模式下面,使用打包的Assets文件 if MODE == 'source': # 源代码发布出去以后,使用最普通的StaticFileHandler AssetsHandler = StaticFileHandler elif MODE == 'develop': # 开发中,使用会自动更新的StaticSiteHandler AssetsHandler = StaticSiteHandler Change = namedtuple('Change', 'time, path, type')
#!/usr/bin/python #coding:utf-8 import socket import webbrowser from tornado import ioloop from pyf5.server import F5Server from pyf5.utils import module_path if __name__ == '__main__': path = module_path() server = F5Server() port = 0 for port in range(80, 90) + range(8000, 8100): try: server.listen(port) break except socket.error: continue print 'F5 server started, please visit:', '127.0.0.1' if port == 80 else '127.0.0.1:%s' % port if 'script' in __file__.lower(): webbrowser.open_new_tab('http://127.0.0.1:%s' % port) try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: print 'exit'