From 5b7e2be6a9fa83a99e2893582b5a5c640f8394e8 Mon Sep 17 00:00:00 2001
From: James Powell <james@dutc.io>
Date: Sat, 2 Jul 2022 11:36:42 -0400
Subject: [PATCH] context managers for all `open`; ensure unit tests run

---
 pattern/graph/__init__.py    | 19 ++++++++-----------
 pattern/graph/commonsense.py | 13 +++++++------
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/pattern/graph/__init__.py b/pattern/graph/__init__.py
index 85411b8e..7c990eec 100644
--- a/pattern/graph/__init__.py
+++ b/pattern/graph/__init__.py
@@ -1601,19 +1601,15 @@ def export(self, path, encoding="utf-8"):
         # Copy compressed graph.js + canvas.js (unless a custom path is given.)
         if self.javascript is None:
             for p, f in (("..", "canvas.js"), (".", "graph.js")):
-                a = open(os.path.join(MODULE, p, f), "r")
-                b = open(os.path.join(path, f), "w")
-                b.write(minify(a.read()))
-                b.close()
+                with open(os.path.join(MODULE, p, f), "r") as a, open(os.path.join(path, f), "w") as b:
+                    b.write(minify(a.read()))
         # Create style.css.
         if self.stylesheet == DEFAULT:
-            f = open(os.path.join(path, "style.css"), "w")
-            f.write(self.style)
-            f.close()
+            with open(os.path.join(path, "style.css"), "w") as f:
+                f.write(self.style)
         # Create index.html.
-        f = open(os.path.join(path, "index.html"), "w", encoding=encoding)
-        f.write(self.html)
-        f.close()
+        with open(os.path.join(path, "index.html"), "w", encoding=encoding) as f:
+            f.write(self.html)
 
 #--- GRAPH EXPORT: GRAPHML ------------------------------------------------------------------------
 # Exports graphs as GraphML XML, which can be read by Gephi (https://gephi.org).
@@ -1627,7 +1623,8 @@ class GraphMLRenderer(GraphRenderer):
     def serialize(self, directed=False):
         p = "tmp.graphml"
         self.export(p, directed, encoding="utf-8")
-        s = open(p, encoding="utf-8").read()
+        with open(p, encoding="utf-8") as f:
+            s = f.read()
         os.unlink(p)
         return s
 
diff --git a/pattern/graph/commonsense.py b/pattern/graph/commonsense.py
index a603e664..2b6d662c 100644
--- a/pattern/graph/commonsense.py
+++ b/pattern/graph/commonsense.py
@@ -134,7 +134,8 @@ def __init__(self, data=os.path.join(MODULE, "commonsense.csv"), **kwargs):
         # Load data from the given path,
         # a CSV-file of (concept1, relation, concept2, context, weight)-items.
         if data is not None:
-            s = open(data, encoding = 'utf-8').read()
+            with open(data, encoding = 'utf-8') as f:
+                s = f.read()
             s = s.strip(BOM_UTF8)
             s = ((v.strip("\"") for v in r.split(",")) for r in s.splitlines())
             for concept1, relation, concept2, context, weight in s:
@@ -246,7 +247,8 @@ def download(path=os.path.join(MODULE, "commonsense.csv"), threshold=50):
         Saves the data as commonsense.csv which can be the input for Commonsense.load().
     """
     s = "http://nodebox.net/perception?format=txt&robots=1"
-    s = urlopen(s).read()
+    with urlopen(s) as u:
+        s = u.read()
     s = s.decode("utf-8")
     s = s.replace("\\'", "'")
     # Group relations by author.
@@ -284,10 +286,9 @@ def download(path=os.path.join(MODULE, "commonsense.csv"), threshold=50):
     for (concept1, relation, concept2), (context, weight) in r.items():
         s.append("\"%s\",\"%s\",\"%s\",\"%s\",%s" % (
             concept1, relation, concept2, context, weight))
-    f = open(path, "w", encoding = 'utf-8')
-    f.write(BOM_UTF8)
-    f.write("\n".join(s))
-    f.close()
+    with open(path, "w", encoding = 'utf-8') as f:
+        f.write(BOM_UTF8)
+        f.write("\n".join(s))
 
 
 def json():
