From 90d49355fae101a5923d5521967821fb1bc11de7 Mon Sep 17 00:00:00 2001 From: Alexander Zaft Date: Mon, 26 Aug 2024 14:30:36 +0200 Subject: [PATCH] all: start using pathlib Change-Id: I2b0d6ff8f534382913414fa9b35150d6f697acb4 Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/34463 Reviewed-by: Alexander Zaft Tested-by: Jenkins Automated Tests --- bin/frappy-cli | 6 +++--- bin/frappy-gui | 4 ++-- bin/frappy-play | 4 ++-- bin/frappy-server | 4 ++-- bin/sim-server | 4 ++-- frappy/client/interactive.py | 4 ++-- frappy/gui/util.py | 6 +++--- frappy/version.py | 11 +++++------ setup.py | 15 ++++++++------- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/bin/frappy-cli b/bin/frappy-cli index 3e581200..f6081e95 100755 --- a/bin/frappy-cli +++ b/bin/frappy-cli @@ -24,10 +24,10 @@ import sys import argparse -from os import path +from pathlib import Path # Add import path for inplace usage -sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..'))) +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) from frappy.client.interactive import init, run, clientenv, interact @@ -36,7 +36,7 @@ def parseArgv(argv): parser = argparse.ArgumentParser() parser.add_argument('-i', '--include', help='file to execute after connecting to the clients', metavar='file', - type=str, action='append', default=[]) + type=Path, action='append', default=[]) parser.add_argument('-o', '--only-execute', help='Do not go into interactive mode after executing files. \ Has no effect without --include.', action='store_true') diff --git a/bin/frappy-gui b/bin/frappy-gui index 77eb652b..eafb54dd 100755 --- a/bin/frappy-gui +++ b/bin/frappy-gui @@ -26,10 +26,10 @@ from __future__ import print_function import sys import argparse -from os import path +from pathlib import Path # Add import path for inplace usage -sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..'))) +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) import logging from mlzlog import ColoredConsoleHandler diff --git a/bin/frappy-play b/bin/frappy-play index 21260f63..4f1767e4 100755 --- a/bin/frappy-play +++ b/bin/frappy-play @@ -22,10 +22,10 @@ # ***************************************************************************** import sys -from os import path +from pathlib import Path # Add import path for inplace usage -sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..'))) +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) from frappy.client.interactive import Console from frappy.playground import play, USAGE diff --git a/bin/frappy-server b/bin/frappy-server index d1c5cf94..d5307d0b 100755 --- a/bin/frappy-server +++ b/bin/frappy-server @@ -24,10 +24,10 @@ import argparse import sys -from os import path +from pathlib import Path # Add import path for inplace usage -sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..'))) +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) from frappy.lib import generalConfig from frappy.logging import logger diff --git a/bin/sim-server b/bin/sim-server index 34636c4d..05bb4bd6 100755 --- a/bin/sim-server +++ b/bin/sim-server @@ -34,13 +34,13 @@ Use cases, mainly for test purposes: import sys import argparse -from os import path +from pathlib import Path import asyncore import socket import time # Add import path for inplace usage -sys.path.insert(0, path.abspath(path.join(path.dirname(__file__), '..'))) +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) from frappy.lib import get_class, formatException, mkthread diff --git a/frappy/client/interactive.py b/frappy/client/interactive.py index 062e1386..882e448d 100644 --- a/frappy/client/interactive.py +++ b/frappy/client/interactive.py @@ -348,7 +348,7 @@ def watch(*args, **kwds): def close_node(online, state): if online and state != 'shutdown': - return + return None close_event.set() return UnregisterCallback @@ -446,7 +446,7 @@ def run(filepath): "__file__": filepath, "__name__": "__main__", }) - with open(filepath, 'rb') as file: + with filepath.open('rb') as file: # pylint: disable=exec-used exec(compile(file.read(), filepath, 'exec'), clientenv.namespace, None) diff --git a/frappy/gui/util.py b/frappy/gui/util.py index 529533d8..f4928e44 100644 --- a/frappy/gui/util.py +++ b/frappy/gui/util.py @@ -21,15 +21,15 @@ # ***************************************************************************** -from os import path +from pathlib import Path from frappy.gui.qt import QColor, uic -uipath = path.dirname(__file__) +uipath = Path(__file__).parent def loadUi(widget, uiname, subdir='ui'): - uic.loadUi(path.join(uipath, subdir, uiname), widget) + uic.loadUi(uipath / subdir / uiname, widget) def is_light_theme(palette): diff --git a/frappy/version.py b/frappy/version.py index 45c27ecb..4448dd43 100644 --- a/frappy/version.py +++ b/frappy/version.py @@ -21,14 +21,13 @@ # # ***************************************************************************** -import os.path +from pathlib import Path from subprocess import PIPE, Popen __all__ = ['get_version'] -RELEASE_VERSION_FILE = os.path.join(os.path.dirname(__file__), - 'RELEASE-VERSION') -GIT_REPO = os.path.join(os.path.dirname(__file__), '..', '.git') +RELEASE_VERSION_FILE = Path(__file__).parent / 'RELEASE-VERSION' +GIT_REPO = (Path(__file__).parents[1] / '.git').resolve() def translate_version(ver): @@ -49,14 +48,14 @@ def get_git_version(abbrev=4): def read_release_version(): try: - with open(RELEASE_VERSION_FILE, encoding='utf-8') as f: + with RELEASE_VERSION_FILE.open(encoding='utf-8') as f: return f.readline().strip() except Exception: return None def write_release_version(version): - with open(RELEASE_VERSION_FILE, 'w', encoding='utf-8') as f: + with RELEASE_VERSION_FILE.open('w', encoding='utf-8') as f: f.write(f'{version}\n') diff --git a/setup.py b/setup.py index a9ff09f6..46007f73 100755 --- a/setup.py +++ b/setup.py @@ -23,19 +23,20 @@ # ***************************************************************************** -from glob import glob -from os import listdir, path +from pathlib import Path from setuptools import find_packages, setup import frappy.version # cfg-editor is currently not functional -scripts = [script for script in glob(path.join('bin', 'frappy-*')) - if not script.endswith('cfg-editor')] +scripts = [str(script) for script in Path('bin').glob('frappy-*') + if not str(script).endswith('cfg-editor')] -uidir = path.join(path.dirname(__file__), 'frappy', 'gui', 'ui') -uis = [path.join('gui', 'ui', entry) for entry in listdir(uidir)] + +frappydir = Path(__file__).parent / 'frappy' +uidir = frappydir / 'gui' / 'ui' +uis = [str(f.relative_to(frappydir)) for f in uidir.iterdir()] setup( name='frappy-core', @@ -57,7 +58,7 @@ setup( ('/lib/systemd/system-generators', ['etc/frappy-generator']), ('/lib/systemd/system', ['etc/frappy@.service', 'etc/frappy.target', - ]), + ]), ('/var/log/frappy', []), ], scripts=scripts,