From 3383318feacca92af673d64544f07c534557f0c1 Mon Sep 17 00:00:00 2001 From: Tasio Mendez Date: Mon, 5 Feb 2018 12:47:56 +0100 Subject: [PATCH] Script options --- run.py | 18 +++++++++++++----- server.py | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/run.py b/run.py index dd3abdd..b096c01 100644 --- a/run.py +++ b/run.py @@ -1,15 +1,23 @@ - +import argparse from server import ModularServer from visualization import GraphVisualization, Model -def run(model, params=None): +def run(model, name="SOIL Model", verbose=False, params=None): graphVisualization = GraphVisualization(params) - server = ModularServer(model, graphVisualization, name="SOIL Model") + server = ModularServer(model, graphVisualization, name=(name[0] if isinstance(name, list) else name), verbose=verbose) server.port = 8001 server.launch() if __name__ == "__main__": - soil = Model(dump=False) - run(soil) + + parser = argparse.ArgumentParser(description='Visualization of a Graph Model') + + parser.add_argument('--name', '-n', nargs=1, default='SOIL Model', help='name of the simulation') + parser.add_argument('--dump', '-d', help='dumping results in folder output', action='store_true') + parser.add_argument('--verbose', '-v', help='verbose mode', action='store_true') + args = parser.parse_args() + + soil = Model(dump=args.dump) + run(soil, name=args.name, verbose=args.verbose) diff --git a/server.py b/server.py index 8961548..2ac20cc 100644 --- a/server.py +++ b/server.py @@ -1,19 +1,17 @@ +import io +import logging +import networkx as nx import os +import threading import tornado.ioloop import tornado.web import tornado.websocket import tornado.escape import tornado.gen -import webbrowser - import yaml -import logging - -import threading -import io -import networkx as nx -from time import sleep +import webbrowser from contextlib import contextmanager +from time import sleep logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -56,6 +54,7 @@ class PageHandler(tornado.web.RequestHandler): class SocketHandler(tornado.websocket.WebSocketHandler): + """ Handler for websocket. """ def open(self): if self.application.verbose: @@ -116,6 +115,9 @@ class SocketHandler(tornado.websocket.WebSocketHandler): 'data': self.get_trial( int(msg['data'] )) }) elif msg['type'] == 'run_simulation': + if self.application.verbose: + logger.info('Running new simulation for {name}'.format(name=self.config['name'])) + print(msg['data']) self.send_log('INFO.soil', 'Running new simulation for {name}'.format(name=self.config['name'])) self.config['environment_params'] = msg['data'] self.run_simulation() @@ -137,7 +139,8 @@ class SocketHandler(tornado.websocket.WebSocketHandler): thread.start() def on_close(self): - logger.info('Socket closed!') + if self.application.verbose: + logger.info('Socket closed!') def send_log(self, logger, logging): self.write_message({'type': 'log', @@ -229,5 +232,5 @@ class ModularServer(tornado.web.Application): url = 'http://127.0.0.1:{PORT}'.format(PORT=self.port) print('Interface starting at {url}'.format(url=url)) self.listen(self.port) - # webbrowser.open(url) + webbrowser.open(url) tornado.ioloop.IOLoop.instance().start()