From 8b0a29095aeab4b4c10de5d70a9b9b225dbce00c Mon Sep 17 00:00:00 2001 From: Hans Kallekleiv Date: Mon, 24 Aug 2020 15:29:08 +0200 Subject: [PATCH] Use built in xml parsing (#415) --- setup.py | 5 ++- .../horizon_uncertainty_viewer.py | 36 +++++++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index d924ddff1..2bdf4a312 100644 --- a/setup.py +++ b/setup.py @@ -55,9 +55,9 @@ }, install_requires=[ "dash>=1.11", + "dash_bootstrap_components>=0.10.3", + "defusedxml>=0.6.0", "fmu-ensemble>=1.2.3", - "beautifulsoup4", - "lxml", "matplotlib>=3.0", "pandas>=0.24", "pillow>=6.1", @@ -66,7 +66,6 @@ "webviz-config>=0.0.55", "webviz-subsurface-components>=0.0.27", "xtgeo>=2.8", - "dash_bootstrap_components>=0.10.3", ], tests_require=TESTS_REQUIRE, extras_require={"tests": TESTS_REQUIRE}, diff --git a/webviz_subsurface/plugins/_horizon_uncertainty_viewer/horizon_uncertainty_viewer.py b/webviz_subsurface/plugins/_horizon_uncertainty_viewer/horizon_uncertainty_viewer.py index 96b9f2cc3..75514ca16 100644 --- a/webviz_subsurface/plugins/_horizon_uncertainty_viewer/horizon_uncertainty_viewer.py +++ b/webviz_subsurface/plugins/_horizon_uncertainty_viewer/horizon_uncertainty_viewer.py @@ -3,10 +3,10 @@ import json from uuid import uuid4 from pathlib import Path +import defusedxml.ElementTree as ET import numpy as np import xtgeo -from bs4 import BeautifulSoup import dash import dash_table from dash.dependencies import Input, Output, State @@ -56,10 +56,9 @@ def __init__( self.basedir = basedir self.planned_wells_dir = planned_wells_dir - - with open(get_path(basedir / "model_file.xml"), "r") as file: - self.modelfile = BeautifulSoup(file, "xml") - self.surfaces = load_surfaces(basedir, self.modelfile) + self.modelfile_path = basedir / "model_file.xml" + self.modelfile = get_path(self.modelfile_path) + self.surfaces = load_surfaces(basedir, self.modelfile_path) self.planned_wellfiles = ( json.load(find_files(planned_wells_dir, "*.txt")) if planned_wells_dir @@ -68,6 +67,7 @@ def __init__( self.wellfiles = json.load(find_files(basedir / "input" / "welldata", "*.txt")) self.wellfiles = [str(get_path(Path(w))) for w in self.wellfiles] self.allfiles = json.load(find_files(basedir)) + self.allfiles.append(self.modelfile_path) self.allfiles += self.planned_wellfiles self.planned_wellfiles = [ str(get_path(Path(w))) for w in self.planned_wellfiles @@ -781,7 +781,10 @@ def add_webvizstore(self): ) ) functions.append( - (get_surfaces, [{"basedir": self.basedir, "modelfile": self.modelfile,}],) + ( + get_surfaces, + [{"basedir": self.basedir, "modelfile": self.modelfile_path,}], + ) ) return functions @@ -836,6 +839,7 @@ def load_surfaces(basedir: Path, modelfile): @webvizstore def get_surfaces(basedir: Path, modelfile) -> io.BytesIO: + modelfile = ET.parse(get_path(modelfile)).getroot() surface_types = { "depth": "d_", "depth-trend": "dt_", @@ -843,12 +847,12 @@ def get_surfaces(basedir: Path, modelfile) -> io.BytesIO: "depth-trend-error": "dte_", "depth-residual": "dr_", } - surface_wrappers = modelfile.find_all("surface") + surface_wrappers = modelfile.findall(".//surface") surfaces = [] for element in surface_wrappers: surface = { - "name": element.find("name").get_text(), - "topofzone": element.find("top-of-zone").get_text(), + "name": element.findtext("name"), + "topofzone": element.findtext("top-of-zone"), } for s_mtype, s_type in surface_types.items(): surface[s_type] = ( @@ -860,7 +864,7 @@ def get_surfaces(basedir: Path, modelfile) -> io.BytesIO: / f"{s_type}{surface['name']}.rxb" ) ) - if element.find(s_mtype).get_text() == "yes" + if element.find("output").findtext(s_mtype) == "yes" else None ) surfaces.append(surface) @@ -891,11 +895,12 @@ def surface_from_json(surfaceobj): def extract_topofzone_names(modelfile): - surface_wrappers = modelfile.find_all("surface") + modelfile = ET.parse(modelfile).getroot() + surface_wrappers = modelfile.findall(".//surface") topofzone_names = [] for element in surface_wrappers: - name = element.find("top-of-zone") - topofzone_names.append(name.get_text()) + name = element.findtext("top-of-zone") + topofzone_names.append(name) return topofzone_names @@ -922,8 +927,9 @@ def get_well_points(basedir: Path): def get_zonelog_name(modelfile): - zonelog_wrapper = modelfile.find("zone-log-name") - return zonelog_wrapper.get_text() + modelfile = ET.parse(modelfile).getroot() + zonelog_wrapper = modelfile.findtext(".//zone-log-name") + return zonelog_wrapper def get_zonation_status(basedir: Path):