use Path instead of strings
authorNiki Roo <niki@nikiroo.be>
Sun, 23 Mar 2025 18:19:28 +0000 (19:19 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 23 Mar 2025 18:19:28 +0000 (19:19 +0100)
Gamiki/Builder.py
Gamiki/Game.py
Gamiki/Library.py
Gamiki/Support/Support.py
Gamiki/Support/SupportDos.py
Gamiki/Support/SupportGog.py
Gamiki/Support/SupportWin31.py
Main.py

index 9644bb6fdb130031169b24c37204efde6f9c470e..a9d747ab5151e216dbf0215311b173706823b81b 100644 (file)
@@ -1,5 +1,4 @@
-from os      import listdir
-from os.path import isfile, isdir, join, expanduser, realpath, exists
+from pathlib import Path, PurePath
 
 from .        import Library, Game
 from .Support import Support, SupportDos, SupportWin31, SupportGog
@@ -16,13 +15,14 @@ class Builder:
         supports.append(SupportDos())
         
         targets = []
-        if (exists(expanduser("~/.games.cfg"))):
-            with open(file, encoding="utf-8") as data:
+        config = Path("~/.games.cfg").expanduser()
+        if (config.exists()):
+            with open(config, encoding="utf-8") as data:
                 ln = data.strip()
                 if (not ln.startswith("#")):
-                    targets.append(ln)
+                    targets.append(Path(ln))
         else:
-            targets.append(expanduser("~/Games"))
+            targets.append(Path("~/Games").expanduser())
         
         for target in targets:
             self.load_libs(supports, target)
@@ -31,14 +31,13 @@ class Builder:
             for game in lib:
                 self.games.append(game)
     
-    def load_libs(self, supports: list[Support], dir: str):
-        for path in listdir(dir):
-            fullpath = join(dir, path)
-            if (not isdir(realpath(fullpath))):
+    def load_libs(self, supports: list[Support], dir: PurePath):
+        for path in dir.iterdir():
+            if (not path.is_dir()):
                 continue
             
             try:
-                self.libraries.append(Library(fullpath, supports))
+                self.libraries.append(Library(path, supports))
             except FileNotFoundError:
                 pass
         
index babe18da3c5da23cc7b7db4c90aa5d6f8f2be81b..98dee239614924f45b674310ec5b1f5959addf39 100644 (file)
@@ -1,19 +1,18 @@
-from os.path import basename as basename
+from pathlib import PurePath
 
 class Game(dict[str, str]):
     """Game object generated from a directory."""
-    def __init__(self, library, dir: str):
-        # TODO: doc: dir = full path
-        self.dir     = dir.removesuffix("/")
-        self.name    = basename(self.dir)
-        self.code    = basename(self.dir)
+    def __init__(self, library, dir: PurePath):
+        self.dir     = dir
+        self.name    = dir.name
+        self.code    = dir.name
         self.src     = ""
         self.tags    = []
         self.desc    = ""
         self.support = None
         self.library = library
         
-        self._read_info(self.dir + "/gameinfo.txt")
+        self._read_info(self.dir.joinpath("gameinfo.txt"))
             
         if ("Name" in self):
             self.name = self["Name"]
@@ -26,7 +25,7 @@ class Game(dict[str, str]):
         if ("Desc" in self):
             self.desc = self["Desc"].replace("\\n", "\n").strip()
     
-    def _read_info(self, file: str):
+    def _read_info(self, file: PurePath):
         try:
             with open(file, encoding="utf-8") as data:
                 for ln in data:
index 47b122857f02c5ab1a8e998778583ff3a3dc32a1..af189e35e5bfcb81625aebb4d7b5e44733f755bb 100644 (file)
@@ -1,5 +1,4 @@
-from os      import listdir
-from os.path import isdir, join, realpath, exists
+from pathlib import PurePath
 
 from .Support import Support
 from .Game    import Game
@@ -7,24 +6,22 @@ from .Game    import Game
 class Library(list[Game]):
     """Manage a library (a folder) of Games."""
     
-    def __init__(self, dir: str, supports: list[Support]):
-        self.name             = '' # TODO
+    def __init__(self, dir: PurePath, supports: list[Support]):
+        self.name             = dir.name
         self.dir              = dir
         self.supports         = supports
         self.preferredSupport = None
-        # TODO: dir = fullpath
         
-        config = join(self.dir, "games.cfg")
-        if (not exists(config)):
+        config = self.dir.joinpath("games.cfg")
+        if (not config.exists()):
             raise FileNotFoundError("Library is not configured (games.cfg)")
         
         # TODO: get name from config?
 
-        for path in listdir(self.dir):
-            fullpath = join(self.dir, path)
-            if (not isdir(realpath(fullpath))): 
+        for path in self.dir.iterdir():
+            if (not path.is_dir()):
                 continue
-            game = Game(self, fullpath)
+            game = Game(self, path)
             for support in self.supports:
                 if (support.supports(game)):
                     game.set_support(support)
index f22ecf6eb2394b28a25f041b45a78b34713cb7af..7e6d9209cbb6410b2356bb2b8c1b48414fb7cbaa 100644 (file)
@@ -1,6 +1,5 @@
 from subprocess import run
 from shutil     import which
-#from typing     import Any, Self
 
 from ..Game import Game
 
index e89954aac43370125238a5d2cc7c76c16fdd890a..d9ac410fc42ee77b60954aeaecaffcdd89a86b15 100644 (file)
@@ -1,4 +1,3 @@
-from os.path    import exists, realpath
 from subprocess import run
 from shutil     import which
 
@@ -11,27 +10,28 @@ class SupportDos(Support):
         pass
             
     def supports(self, game: Game):
-        return (exists(game.dir + "/start.conf"))
+        return game.dir.joinpath("start.conf").exists()
     
     def start(self, game: Game, params: list[str] = []):
-        dir = realpath(game.dir)
-        link_dir = realpath(self.get_link_dir(game))
+        dir = game.dir.resolve()
+        link_dir = self.get_link_dir(game).resolve()
         
         if (Support.program("app.sh") != None):
             cmd = [ 
                     Support.program("app.sh"),
-                    "--wait", "dosbox", "--link", link_dir 
+                    "--wait", "dosbox", "--link", link_dir.as_posix()
             ]
         elif (Support.program("dosbox") != None):
             cmd = [ Support.program("dosbox") ]
         else:
             raise RuntimeError("DosBox not found")
         
-        if (exists(dir + "/base.conf")):
+        base = dir.joinpath("base.conf")
+        if (base.exists()):
             cmd.append("-conf")
-            cmd.append(dir + "/base.conf")
+            cmd.append(base.as_posix())
         cmd.append("-conf")
-        cmd.append(dir + "/start.conf")
+        cmd.append(dir.joinpath("start.conf").as_posix())
         
         print("Running", game.name)
         rep = run(cmd, cwd=dir)
@@ -39,5 +39,5 @@ class SupportDos(Support):
             print("\nError when running", game.name, ": RC", rep.returncode)
 
     def get_link_dir(self, game: Game) -> str:
-        return (game.dir)
+        return game.dir
 
index cacf212e41d185b5a13dc9aab75439a212906956..bb353fcee443592f6f3b7f8ecc28da7e231a0c64 100644 (file)
@@ -1,6 +1,5 @@
 from random     import random
 from math       import floor
-from os.path    import exists, realpath
 from os         import environ
 from pathlib    import Path
 from subprocess import run
@@ -15,47 +14,43 @@ class SupportGog(Support):
         pass
             
     def supports(self, game: Game):
-        return (exists(game.dir + "/gameinfo"))
+        return game.dir.joinpath("gameinfo").exists()
     
     def start(self, game: Game, params: list[str] = []):
-        dir = realpath(game.dir)
+        dir = game.dir.resolve()
         
-        if (not exists(dir + "/start.sh")):
+        if (not dir.joinpath("start.sh").exists()):
             raise RuntimeError("GoG game without a start.sh script")
         
         env = environ.copy()
 
         ffile = None
         if (Support.program("launch.sh") != None):
-            ffile = Path(
+            tfile = Path(
                     "/tmp/shared/.game-" 
                     + "{0:6d}".format(floor(random() * 1000000))
                     + ".sh"
             )
-            with open(ffile, 'w', encoding="utf-8") as data:
+            with open(tfile, 'w', encoding="utf-8") as data:
                 data.write("#!/bin/sh\n")
-                data.write("cd '" + dir + "'\n")
+                data.write("cd '" + dir.as_posix() + "'\n")
                 data.write("./start.sh\n")
-            ffile.chmod(0o777)
-            env["OPTS"] = "--entrypoint=" + ffile.as_posix()
+            tfile.chmod(0o777)
+            env["OPTS"] = "--entrypoint=" + tfile.as_posix()
             cmd = [ 
                     Support.program("launch.sh"),
-                    "games", "--link", dir
+                    "games", "--link", dir.as_posix()
             ]
         else:
-            cmd = [ dir + "/start.sh" ]
-        
-        if (exists(dir + "/base.conf")):
-            cmd.append("-conf")
-            cmd.append(dir + "/base.conf")
-        cmd.append("-conf")
-        cmd.append(dir + "/start.conf")
+            cmd = [ dir.joinpath("start.sh").as_posix() ]
         
         # GoG launcher already does this:
         # print("Running", game.name)
         rep = run(cmd, cwd=dir, env=env)
-        if (ffile != ""):
-            ffile.unlink()
         if (rep.returncode != 0):
             print("\nError when running", game.name, ": RC", rep.returncode)
+        
+        if (tfile != ""):
+            tfile.unlink()
+        
 
index 9a85c05ad29e4c8c69f9aa7ce1577240148021c1..0aa5749e9619ddfc70ace4ce4dc2503f1994368e 100644 (file)
@@ -1,4 +1,4 @@
-from os.path    import exists, realpath
+from pathlib    import PurePath
 from subprocess import run
 from shutil     import which
 
@@ -11,9 +11,11 @@ class SupportWin31(SupportDos):
     # TODO: must be tried befoer SupportDos
             
     def supports(self, game: Game):
-        return (exists(game.dir + "/start.conf")
-            and exists(game.dir + "/C/WINDOWS"))
+        return (
+                game.dir.joinpath("start.conf").exists()
+            and game.dir.joinpath("C", "WINDOWS").exists()
+        )
     
-    def get_link_dir(self, game: Game) -> str:
+    def get_link_dir(self, game: Game) -> PurePath:
         return game.library.dir
 
diff --git a/Main.py b/Main.py
index e4472ef7f736657c1cb1d06e157cededf5df0843..4cf8554e5a65b3f7ae5317ee02d934d143f30059 100755 (executable)
--- a/Main.py
+++ b/Main.py
@@ -1,12 +1,12 @@
 #!/usr/bin/env python3
 
-import argparse
-from os.path import basename
-from sys import stderr
+from argparse import ArgumentParser
+from os.path  import basename
+from sys      import stderr
 
 from Gamiki import Builder, Library
 
-parser = argparse.ArgumentParser(
+parser = ArgumentParser(
     description="Game launcher and simple management system."
 )