From 2a4a9c9b6f705ceecff75d2c303c62a9986984f1 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 23 Mar 2025 19:19:28 +0100 Subject: [PATCH] use Path instead of strings --- Gamiki/Builder.py | 21 ++++++++++----------- Gamiki/Game.py | 15 +++++++-------- Gamiki/Library.py | 19 ++++++++----------- Gamiki/Support/Support.py | 1 - Gamiki/Support/SupportDos.py | 18 +++++++++--------- Gamiki/Support/SupportGog.py | 33 ++++++++++++++------------------- Gamiki/Support/SupportWin31.py | 10 ++++++---- Main.py | 8 ++++---- 8 files changed, 58 insertions(+), 67 deletions(-) diff --git a/Gamiki/Builder.py b/Gamiki/Builder.py index 9644bb6..a9d747a 100644 --- a/Gamiki/Builder.py +++ b/Gamiki/Builder.py @@ -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 diff --git a/Gamiki/Game.py b/Gamiki/Game.py index babe18d..98dee23 100644 --- a/Gamiki/Game.py +++ b/Gamiki/Game.py @@ -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: diff --git a/Gamiki/Library.py b/Gamiki/Library.py index 47b1228..af189e3 100644 --- a/Gamiki/Library.py +++ b/Gamiki/Library.py @@ -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) diff --git a/Gamiki/Support/Support.py b/Gamiki/Support/Support.py index f22ecf6..7e6d920 100644 --- a/Gamiki/Support/Support.py +++ b/Gamiki/Support/Support.py @@ -1,6 +1,5 @@ from subprocess import run from shutil import which -#from typing import Any, Self from ..Game import Game diff --git a/Gamiki/Support/SupportDos.py b/Gamiki/Support/SupportDos.py index e89954a..d9ac410 100644 --- a/Gamiki/Support/SupportDos.py +++ b/Gamiki/Support/SupportDos.py @@ -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 diff --git a/Gamiki/Support/SupportGog.py b/Gamiki/Support/SupportGog.py index cacf212..bb353fc 100644 --- a/Gamiki/Support/SupportGog.py +++ b/Gamiki/Support/SupportGog.py @@ -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() + diff --git a/Gamiki/Support/SupportWin31.py b/Gamiki/Support/SupportWin31.py index 9a85c05..0aa5749 100644 --- a/Gamiki/Support/SupportWin31.py +++ b/Gamiki/Support/SupportWin31.py @@ -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 e4472ef..4cf8554 100755 --- 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." ) -- 2.27.0