Commit 02679882 authored by Benjamin Fischer's avatar Benjamin Fischer

[bin] updated vispa executable

parent bab18513
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# imports
from argparse import ArgumentParser
from time import sleep
import logging.config
import os
import sys
import webbrowser
import cherrypy
import gtk
base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if os.path.isdir(base):
sys.path.insert(0, base)
from vispa import RELEASE_VERSION
logger = logging.getLogger(__name__)
class Browser(object):
def __init__(self):
if not self.create_webkit():
self.create_mozilla()
def create_window(self):
self.window = gtk.Window()
self.window.connect('destroy', self.on_destroy)
self.window.set_size_request(1280, 800)
self.scroller = gtk.ScrolledWindow()
self.window.add(self.scroller)
def on_destroy(self, widget):
logger.error("Destroy window")
gtk.main_quit()
def create_mozilla(self):
try:
import gtkmozembed
except:
return False
logger.error("Using MozEmbed")
self.create_window()
self.widget = gtkmozembed.MozEmbed()
self.scroller.add(self.widget)
return True
def create_webkit(self):
try:
import webkit
except:
return False
logger.error("Using Webkit")
self.create_window()
self.widget = webkit.WebView()
self.scroller.add(self.widget)
return True
def open(self, url):
if hasattr(self, "widget"):
if hasattr(self.widget, "load_url"):
self.widget.load_url(url)
elif hasattr(self.widget, "open"):
self.widget.open(url)
else:
logger.error("Open external Webbrowser...")
webbrowser.open(url)
def show(self):
if hasattr(self, "window"):
self.window.show_all()
gtk.mainloop()
def parse_commandline():
parser = ArgumentParser()
from vispa import RELEASE_VERSION
config_dir = "~/.vispa-%s/conf" % RELEASE_VERSION
parser.add_argument("--config-dir", "-c", dest="configdir",
default=os.path.expanduser(config_dir),
help="Directory containing config files (default: %s)" % config_dir)
parser.add_argument(
"--config-dir",
"-c",
dest="configdir",
default=os.path.expanduser(config_dir),
help="Directory containing config files (default: %s)" % config_dir,
)
data_dir = "~/.vispa-%s/var" % RELEASE_VERSION
parser.add_argument("--data-dir", "-d", dest="vardir",
default=os.path.expanduser(data_dir),
help="Directory containing data and cache files (default: %s)" % data_dir)
parser.add_argument(
"--data-dir",
"-d",
dest="vardir",
default=os.path.expanduser(data_dir),
help="Directory containing data and cache files (default: %s)" % data_dir,
)
parser.add_argument("--loglevel", "-l", dest="loglevel", default=None,
help="Enable debug messages")
parser.add_argument(
"--loglevel", "-l", dest="loglevel", default=None, help="Enable debug messages"
)
parser.add_argument("--port", "-p", default=None,
help="Which port the server shall listen on")
parser.add_argument(
"--port", "-p", default=None, help="Which port the server shall listen on"
)
parser.add_argument("--browser", "-b", action="store_true",
help="Start a browser.")
parser.add_argument("--browser", "-b", action="store_true", help="Start a browser.")
return parser.parse_args()
def setup_logging(args):
logging_conf = os.path.join(args.configdir, "logging.ini")
logformat = "%(asctime)s - %(levelname)-6s - %(name)-s - %(message)s"
datefmt = "%Y-%m-%d %H:%M"
loglevel = args.loglevel
if os.path.isfile(logging_conf):
logging.config.fileConfig(logging_conf,
defaults={"log_dir": args.vardir})
elif loglevel == "info":
logging.basicConfig(level=logging.INFO,
format=logformat,
datefmt=datefmt)
cherrypy.log.screen = False
cherrypy.log.access_file = os.path.join(args.vardir, "access.log")
elif loglevel == "debug":
logging.basicConfig(level=logging.DEBUG,
format=logformat,
datefmt=datefmt)
cherrypy.log.screen = False
cherrypy.log.access_file = os.path.join(args.vardir, "access.log")
logging.config.fileConfig(logging_conf, defaults={"log_dir": args.vardir})
else:
cherrypy.log('Using default logging.', 'SERVER')
cherrypy.log.access_file = os.path.join(args.vardir, "access.log")
loglevel = dict(info=logging.INFO, debug=logging.DEBUG,).get(loglevel, None)
if loglevel:
logformat = "%(asctime)s - %(levelname)-6s - %(name)-s - %(message)s"
datefmt = "%Y-%m-%d %H:%M"
logging.basicConfig(level=loglevel, format=logformat, datefmt=datefmt)
cherrypy.log.screen = False
cherrypy.log.access_file = os.path.join(args.vardir, "access.log")
def run():
......@@ -137,17 +70,22 @@ def run():
del server_args["browser"]
from vispa.server import Server
server = Server(**server_args)
if args.browser:
browser = Browser()
server = Server(**server_args)
server.start()
if args.browser:
port = int(args.port or 4282)
browser.open("http://localhost:%d" % port)
browser.show()
webbrowser.open(server.start_url(), new=2)
try:
while True:
sleep(9999)
except KeyboardInterrupt:
pass
server.stop()
if __name__ == "__main__":
run()
......@@ -416,10 +416,20 @@ class Server(object):
cherrypy.engine.start()
vispa.setup_thread_dump()
def stop(self):
cherrypy.engine.exit()
def run(self):
self.start()
cherrypy.engine.block()
def start_url(self):
return urlparse("%s:%d%s" % (
cherrypy.config.get("server.socket_host"),
cherrypy.config.get("server.socket_port"),
vispa.url.dynamic('/')
), "http", False).geturl()
def _load_extensions(self):
self._extensions = {}
# loop through all extensions and import their files
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment