From 45f295fcf87b8e7b10f6e02f5e2ec2399e58af23 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Mon, 2 Nov 2020 15:41:15 +0100 Subject: [PATCH] Add pyzebra handler * allow user to specify anatric path --- pyzebra/anatric.py | 5 ++--- pyzebra/app/app.py | 9 +-------- pyzebra/app/handler.py | 30 ++++++++++++++++++++++++++++++ pyzebra/app/panel_hdf_anatric.py | 5 +++-- pyzebra/cli.py | 12 +++++++++++- 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 pyzebra/app/handler.py diff --git a/pyzebra/anatric.py b/pyzebra/anatric.py index e841061..3dd4573 100644 --- a/pyzebra/anatric.py +++ b/pyzebra/anatric.py @@ -2,7 +2,6 @@ import subprocess import xml.etree.ElementTree as ET -ANATRIC_PATH = "/afs/psi.ch/project/sinq/rhel7/bin/anatric" DATA_FACTORY_IMPLEMENTATION = [ "trics", "morph", @@ -24,8 +23,8 @@ REFLECTION_PRINTER_FORMATS = [ ALGORITHMS = ["adaptivemaxcog", "adaptivedynamic"] -def anatric(config_file): - subprocess.run([ANATRIC_PATH, config_file], check=True) +def anatric(config_file, anatric_path="/afs/psi.ch/project/sinq/rhel7/bin/anatric"): + subprocess.run([anatric_path, config_file], check=True) class AnatricConfig: diff --git a/pyzebra/app/app.py b/pyzebra/app/app.py index 35e5bf0..64b35c1 100644 --- a/pyzebra/app/app.py +++ b/pyzebra/app/app.py @@ -1,4 +1,3 @@ -import argparse import logging import sys from io import StringIO @@ -11,14 +10,8 @@ import panel_ccl_integrate import panel_hdf_anatric import panel_hdf_viewer -parser = argparse.ArgumentParser( - prog="pyzebra", formatter_class=argparse.ArgumentDefaultsHelpFormatter -) - -args = parser.parse_args() doc = curdoc() -doc.title = "pyzebra" sys.stdout = StringIO() stdout_textareainput = TextAreaInput(title="print output:", height=150) @@ -26,7 +19,7 @@ stdout_textareainput = TextAreaInput(title="print output:", height=150) bokeh_stream = StringIO() bokeh_handler = logging.StreamHandler(bokeh_stream) bokeh_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT)) -bokeh_logger = logging.getLogger('bokeh') +bokeh_logger = logging.getLogger("bokeh") bokeh_logger.addHandler(bokeh_handler) bokeh_log_textareainput = TextAreaInput(title="server output:", height=150) diff --git a/pyzebra/app/handler.py b/pyzebra/app/handler.py new file mode 100644 index 0000000..a10dad8 --- /dev/null +++ b/pyzebra/app/handler.py @@ -0,0 +1,30 @@ +from bokeh.application.handlers import Handler + + +class PyzebraHandler(Handler): + """Provides a mechanism for generic bokeh applications to build up new streamvis documents. + """ + + def __init__(self, anatric_path): + """Initialize a pyzebra handler for bokeh applications. + + Args: + args (Namespace): Command line parsed arguments. + """ + super().__init__() # no-op + + self.anatric_path = anatric_path + + def modify_document(self, doc): + """Modify an application document with pyzebra specific features. + + Args: + doc (Document) : A bokeh Document to update in-place + + Returns: + Document + """ + doc.title = "pyzebra" + doc.anatric_path = self.anatric_path + + return doc diff --git a/pyzebra/app/panel_hdf_anatric.py b/pyzebra/app/panel_hdf_anatric.py index 5023161..529bebe 100644 --- a/pyzebra/app/panel_hdf_anatric.py +++ b/pyzebra/app/panel_hdf_anatric.py @@ -21,6 +21,7 @@ from pyzebra.anatric import DATA_FACTORY_IMPLEMENTATION, REFLECTION_PRINTER_FORM def create(): + doc = curdoc() config = pyzebra.AnatricConfig() def _load_config_file(file): @@ -345,7 +346,7 @@ def create(): with tempfile.TemporaryDirectory() as temp_dir: temp_file = temp_dir + "/temp.xml" config.save_as(temp_file) - pyzebra.anatric(temp_file) + pyzebra.anatric(temp_file, anatric_path=doc.anatric_path) with open(config.logfile) as f_log: output_log.value = f_log.read() @@ -404,6 +405,6 @@ def create(): with open("debug.xml") as f_config: output_config.value = f_config.read() - curdoc().add_periodic_callback(update_config, 1000) + doc.add_periodic_callback(update_config, 1000) return Panel(child=tab_layout, title="hdf anatric") diff --git a/pyzebra/cli.py b/pyzebra/cli.py index 8d1f38d..e3d9f49 100644 --- a/pyzebra/cli.py +++ b/pyzebra/cli.py @@ -6,6 +6,8 @@ from bokeh.application.application import Application from bokeh.application.handlers import ScriptHandler from bokeh.server.server import Server +from pyzebra.app.handler import PyzebraHandler + logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO) logger = logging.getLogger(__name__) @@ -35,6 +37,13 @@ def main(): help="hostname that can connect to the server websocket", ) + parser.add_argument( + "--anatric-path", + type=str, + default="/afs/psi.ch/project/sinq/rhel7/bin/anatric", + help="path to anatric executable", + ) + parser.add_argument( "--args", nargs=argparse.REMAINDER, @@ -46,9 +55,10 @@ def main(): logger.info(app_path) + pyzebra_handler = PyzebraHandler(args.anatric_path) handler = ScriptHandler(filename=app_path, argv=args.args) server = Server( - {"/": Application(handler)}, + {"/": Application(pyzebra_handler, handler)}, port=args.port, allow_websocket_origin=args.allow_websocket_origin, )