From cf241b7a0efe0f345f7f919ca359a7fc11c53496 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 21 Aug 2020 14:08:49 -0600 Subject: [PATCH] Make it work with whatever version of Python I'm running here --- devel/devel-server.py | 4 ++-- devel/moth.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/devel/devel-server.py b/devel/devel-server.py index 1c91c00..57106a5 100755 --- a/devel/devel-server.py +++ b/devel/devel-server.py @@ -98,7 +98,7 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler): if not p.is_dir() or p.match(".*"): continue catName = p.parts[-1] - cat = moth.Category(str(p), self.seed) + cat = moth.Category(p, self.seed) puzzles[catName] = [[i, str(i)] for i in cat.pointvals()] puzzles[catName].append([0, ""]) if len(puzzles) <= 1: @@ -157,7 +157,7 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler): self.send_response(200) self.send_header("Content-Type", "text/html; charset=\"utf-8\"") self.end_headers() - self.wfile.write(cgitb.html(sys.exc_info())) + self.wfile.write(bytes(cgitb.html(sys.exc_info()), "utf-8")) return self.send_response(200) diff --git a/devel/moth.py b/devel/moth.py index 0d9c411..f1841f2 100644 --- a/devel/moth.py +++ b/devel/moth.py @@ -16,6 +16,7 @@ import string import sys import tempfile import shlex +import pathlib import yaml messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -55,7 +56,7 @@ def pushd(newdir): def loadmod(name, path): - abspath = os.path.abspath(path) + abspath = str(path.resolve()) loader = importlib.machinery.SourceFileLoader(name, abspath) return loader.load_module() @@ -298,8 +299,9 @@ class Puzzle: def read_directory(self, path): + path = pathlib.Path(path) try: - puzzle_mod = loadmod("puzzle", os.path.join(path, "puzzle.py")) + puzzle_mod = loadmod("puzzle", path / "puzzle.py") except FileNotFoundError: puzzle_mod = None @@ -467,12 +469,12 @@ class Puzzle: class Category: def __init__(self, path, seed): - self.path = path + self.path = pathlib.Path(path) self.seed = seed self.catmod = None try: - self.catmod = loadmod('category', os.path.join(path, 'category.py')) + self.catmod = loadmod('category', self.path / 'category.py') except FileNotFoundError: self.catmod = None @@ -482,15 +484,14 @@ class Category: pointvals = self.catmod.pointvals() else: pointvals = [] - for fpath in glob.glob(os.path.join(self.path, "[0-9]*")): - pn = os.path.basename(fpath) - points = int(pn) + for fpath in self.path.glob("[0-9]*"): + points = int(fpath.name) pointvals.append(points) return sorted(pointvals) def puzzle(self, points): puzzle = Puzzle(self.seed, points) - path = os.path.join(self.path, str(points)) + path = self.path / str(points) if self.catmod: with pushd(self.path): self.catmod.make(points, puzzle)