From 2b9dc5969dd0b819c0f483dcda693f8bc439c354 Mon Sep 17 00:00:00 2001 From: ReimarBauer Date: Tue, 8 Nov 2022 13:10:29 +0100 Subject: [PATCH] argument plot_types added --- mslib/mswms/gallery_builder.py | 191 +++++++++++++++++---------------- mslib/mswms/mswms.py | 10 +- mslib/mswms/wms.py | 10 +- 3 files changed, 115 insertions(+), 96 deletions(-) diff --git a/mslib/mswms/gallery_builder.py b/mslib/mswms/gallery_builder.py index 3ff5911a4..cdd31c258 100644 --- a/mslib/mswms/gallery_builder.py +++ b/mslib/mswms/gallery_builder.py @@ -23,6 +23,8 @@ limitations under the License. """ +# ToDo refactor to use Jinja2 templates + import os from PIL import Image import io @@ -73,12 +75,12 @@ - - - + {section} @@ -431,21 +431,29 @@ def write_code_pages(path, sphinx=False, url_prefix=None): f.write(f""".. raw:: html\n\n{plot_htmls[layer]}""") -def write_html(path, sphinx=False): +def write_html(path, sphinx=False, plot_types=None): + if plot_types is None: + plot_types = plots.keys() """ Writes the plots.html file containing the gallery """ - html = begin + section = [] + for name in plot_types: + section.append(f""""""") + section = ".\n".join(section) + html = begin.format(section=section) if sphinx: html = html.replace("

Plot Gallery

", "") for l_type in plots: - style = "" - if l_type == "Top": - style = "style=\"display: block;\"" - html += f"
" - html += "\n".join(plots[l_type]) - html += "
" + if l_type in plot_types: + style = "" + if l_type == "Top": + style = "style=\"display: block;\"" + html += f"
" + html += "\n".join(plots[l_type]) + html += "
" with open(os.path.join(path, "plots.html"), "w+") as file: file.write(html + end) @@ -666,10 +674,13 @@ def create_linear_plot(xml, file_location): def add_image(path, plot, plot_object, generate_code=False, sphinx=False, url_prefix="", - dataset=None, level=None, itime=None, vtime=None, simple_naming=False): + dataset=None, level=None, itime=None, vtime=None, simple_naming=False, plot_types=None): """ Adds the images to the plots folder and generates the html codes to display them """ + if plot_types is None: + plot_types = plots.keys() + global end # Import here due to some circular import issue if imported too soon from mslib.index import SCRIPT_NAME @@ -679,45 +690,45 @@ def add_image(path, plot, plot_object, generate_code=False, sphinx=False, url_pr l_type = "Linear" if isinstance(plot_object, AbstractLinearSectionStyle) else \ "Side" if isinstance(plot_object, AbstractVerticalSectionStyle) else "Top" - - filename = f"{l_type}_{dataset}{plot_object.name}-" + ( - f"{level}it{itime}vt{vtime}".replace(" ", "_").replace(":", "_").replace("-", "_") - if not simple_naming else "") - - if plot: - if not os.path.exists(os.path.join(path, "plots")): - os.mkdir(os.path.join(path, "plots")) - if l_type == "Linear": - create_linear_plot(etree.fromstring(plot), os.path.join(path, "plots", filename + ".png")) - else: - with Image.open(io.BytesIO(plot)) as image: - image.save(os.path.join(path, "plots", filename + ".png"), - format="PNG") - - end = end.replace("files = [", f"files = [\"{filename}.png\",")\ - .replace(",];", "];") - img_path = f"../_static/{filename}.png" if sphinx \ - else f"{url_prefix}/static/plots/{filename}.png" - code_path = f"code/{l_type}_{dataset}{plot_object.name}.html" if sphinx \ - else f"{url_prefix if url_prefix else ''}{SCRIPT_NAME}mss/code/{l_type}_{dataset}{plot_object.name}.md" - - if generate_code: - if f"{l_type}_{dataset}{plot_object.name}" not in plot_htmls: - plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = \ - plot_html_begin + get_plot_details(path, plot_object, l_type, sphinx, img_path, code_path, dataset) - markdown = plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] - if level: - markdown = add_levels([level], None, markdown) - if vtime: - markdown = add_times(itime, [vtime], markdown) - plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = markdown - - id = img_path.split("-" + f"{level}".replace(" ", "_").replace(":", "_").replace("-", "_"))[0] - if not any([id in html for html in plots[l_type]]): - plots[l_type].append(image_md( - img_path, plot_object.name, code_path if generate_code else None, - f"{plot_object.title}" + (f"
{plot_object.abstract}" - if plot_object.abstract else ""))) + if l_type in plot_types: + filename = f"{l_type}_{dataset}{plot_object.name}-" + ( + f"{level}it{itime}vt{vtime}".replace(" ", "_").replace(":", "_").replace("-", "_") + if not simple_naming else "") + + if plot: + if not os.path.exists(os.path.join(path, "plots")): + os.mkdir(os.path.join(path, "plots")) + if l_type == "Linear": + create_linear_plot(etree.fromstring(plot), os.path.join(path, "plots", filename + ".png")) + else: + with Image.open(io.BytesIO(plot)) as image: + image.save(os.path.join(path, "plots", filename + ".png"), + format="PNG") + + end = end.replace("files = [", f"files = [\"{filename}.png\",")\ + .replace(",];", "];") + img_path = f"../_static/{filename}.png" if sphinx \ + else f"{url_prefix}/static/plots/{filename}.png" + code_path = f"code/{l_type}_{dataset}{plot_object.name}.html" if sphinx \ + else f"{url_prefix if url_prefix else ''}{SCRIPT_NAME}mss/code/{l_type}_{dataset}{plot_object.name}.md" + + if generate_code: + if f"{l_type}_{dataset}{plot_object.name}" not in plot_htmls: + plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = \ + plot_html_begin + get_plot_details(path, plot_object, l_type, sphinx, img_path, code_path, dataset) + markdown = plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] + if level: + markdown = add_levels([level], None, markdown) + if vtime: + markdown = add_times(itime, [vtime], markdown) + plot_htmls[f"{l_type}_{dataset}{plot_object.name}"] = markdown + + id = img_path.split("-" + f"{level}".replace(" ", "_").replace(":", "_").replace("-", "_"))[0] + if not any([id in html for html in plots[l_type]]): + plots[l_type].append(image_md( + img_path, plot_object.name, code_path if generate_code else None, + f"{plot_object.title}" + (f"
{plot_object.abstract}" + if plot_object.abstract else ""))) def add_levels(levels, l_type=None, text=None): diff --git a/mslib/mswms/mswms.py b/mslib/mswms/mswms.py index 2b8d01dc3..da1ba5e84 100644 --- a/mslib/mswms/mswms.py +++ b/mslib/mswms/mswms.py @@ -75,9 +75,15 @@ def main(): help="Normally the plot images should appear at the relative url /static/plots/*.png.\n" "In case they are prefixed by something, e.g. /demo/static/plots/*.png," " please provide the prefix /demo here.") + gallery.add_argument("--plot_types", default=None, + help='A comma-separated list of all plot_types. \n' + 'Default is ["Top", "Side", "Linear"]') args = parser.parse_args() - + if args.plot_types is None: + plot_types = ["Top", "Side", "Linear"] + else: + plot_types = [name.strip() for name in args.plot_types.split(',')] if args.version: print("***********************************************************************") print("\n Mission Support System (MSS)\n") @@ -102,7 +108,7 @@ def main(): create = args.create or args.refresh clear = args.clear or args.refresh server.generate_gallery(create, clear, args.show_code, url_prefix=args.url_prefix, levels=args.levels, - itimes=args.itimes, vtimes=args.vtimes) + itimes=args.itimes, vtimes=args.vtimes, plot_types=plot_types) logging.info("Gallery generation done.") sys.exit() diff --git a/mslib/mswms/wms.py b/mslib/mswms/wms.py index 595c29585..b44dbca42 100644 --- a/mslib/mswms/wms.py +++ b/mslib/mswms/wms.py @@ -220,7 +220,8 @@ def __init__(self): self.register_lsec_layer(layer[1], layer_class=layer[0]) def generate_gallery(self, create=False, clear=False, generate_code=False, sphinx=False, plot_list=None, - all_plots=False, url_prefix="", levels="", itimes="", vtimes="", simple_naming=False): + all_plots=False, url_prefix="", levels="", itimes="", vtimes="", simple_naming=False, + plot_types=None): """ Iterates through all registered layers, draws their plots and puts them in the gallery """ @@ -393,7 +394,8 @@ def generate_gallery(self, create=False, clear=False, generate_code=False, sphin generate_code, sphinx, url_prefix=url_prefix, dataset=dataset if multiple_datasets else "", level=f"{level}" + f"{vert_units}", - itime=str(itime), vtime=str(vtime), simple_naming=simple_naming) + itime=str(itime), vtime=str(vtime), simple_naming=simple_naming, + plot_types=plot_types) if level: add_levels([f"{level} {vert_units}"], vert_units) else: @@ -407,14 +409,14 @@ def generate_gallery(self, create=False, clear=False, generate_code=False, sphin None if exists else plot_driver.plot(), plot_object, generate_code, sphinx, url_prefix=url_prefix, dataset=dataset if multiple_datasets else "", itime=str(itime), - vtime=str(vtime), simple_naming=simple_naming) + vtime=str(vtime), simple_naming=simple_naming, plot_types=plot_types) add_times(itime, [vtime]) except Exception as e: traceback.print_exc() logging.error("%s %s %s", plot_object.name, type(e), e) - write_html(tmp_path, sphinx) + write_html(tmp_path, sphinx, plot_types=plot_types) if generate_code: write_code_pages(tmp_path, sphinx, url_prefix) if sphinx: