From bbe91a2eb92c3c846359442b6d81d7719bd2c9a2 Mon Sep 17 00:00:00 2001 From: Isaac Waldron Date: Mon, 2 Dec 2024 11:44:51 -0500 Subject: [PATCH 1/2] Introduce import error tests for optionals Introduce import error fences for optional plotting behavior in pyedb.common.nets.CommonNets.plot. --- src/pyedb/common/nets.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/pyedb/common/nets.py b/src/pyedb/common/nets.py index 96378e4938..66f3d93cb5 100644 --- a/src/pyedb/common/nets.py +++ b/src/pyedb/common/nets.py @@ -2,8 +2,6 @@ import os import time -import shapely - from pyedb.generic.constants import CSS4_COLORS @@ -120,22 +118,30 @@ def mirror_poly(poly): sign = -1 return [[sign * i[0], i[1]] for i in poly] - import matplotlib.pyplot as plt + try: + import matplotlib.pyplot as plt + except ImportError: + self._pedb.logger.error("Matplotlib is needed. Please, install it first.") + return False dpi = 100.0 figsize = (size[0] / dpi, size[1] / dpi) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(1, 1, 1) - from shapely import affinity - from shapely.geometry import ( - LinearRing, - MultiLineString, - MultiPolygon, - Point, - Polygon, - ) - from shapely.plotting import plot_line, plot_polygon + try: + from shapely import affinity, union_all + from shapely.geometry import ( + LinearRing, + MultiLineString, + MultiPolygon, + Point, + Polygon, + ) + from shapely.plotting import plot_line, plot_polygon + except ImportError: + self._pedb.logger.error("Shapely is needed. Please, install it first.") + return False start_time = time.time() if not nets: @@ -317,7 +323,7 @@ def create_poly(prim, polys, lines): h1 = mirror_poly([(i, j) for i, j in zip(xvt, yvt)]) holes.append(h1) if len(holes) > 1: - holes = shapely.union_all([Polygon(i) for i in holes]) + holes = union_all([Polygon(i) for i in holes]) if isinstance(holes, MultiPolygon): holes = [i.boundary for i in list(holes.geoms)] else: From c87974f213188128ac2a38c41b22756b652029f0 Mon Sep 17 00:00:00 2001 From: Isaac Waldron Date: Mon, 2 Dec 2024 15:03:45 -0500 Subject: [PATCH 2/2] Skip coverage for ImportError handling --- src/pyedb/common/nets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pyedb/common/nets.py b/src/pyedb/common/nets.py index 66f3d93cb5..dec1a6d3d1 100644 --- a/src/pyedb/common/nets.py +++ b/src/pyedb/common/nets.py @@ -120,7 +120,7 @@ def mirror_poly(poly): try: import matplotlib.pyplot as plt - except ImportError: + except ImportError: # pragma: no cover self._pedb.logger.error("Matplotlib is needed. Please, install it first.") return False @@ -139,7 +139,7 @@ def mirror_poly(poly): Polygon, ) from shapely.plotting import plot_line, plot_polygon - except ImportError: + except ImportError: # pragma: no cover self._pedb.logger.error("Shapely is needed. Please, install it first.") return False