From 8b350a58c8e8aedae5ad0c013415a5c696fbdba6 Mon Sep 17 00:00:00 2001 From: Obijuan Date: Fri, 8 Mar 2024 13:57:49 +0100 Subject: [PATCH] util: check_dir(): refactor --- apio/managers/examples.py | 9 ++++----- apio/managers/project.py | 8 ++++---- apio/managers/scons.py | 4 ++-- apio/util.py | 15 ++++++++------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/apio/managers/examples.py b/apio/managers/examples.py index 4bd550e5..3554f54b 100644 --- a/apio/managers/examples.py +++ b/apio/managers/examples.py @@ -82,8 +82,8 @@ def copy_example_dir(self, example, project_dir, sayno): if util.check_package( self.name, self.version, self.spec_version, self.examples_dir ): - project_dir = util.check_dir(project_dir) - example_path = str(Path(project_dir) / example) + project_dir = Path(util.check_dir(project_dir)) + example_path = str(project_dir / example) local_example_path = str(Path(self.examples_dir) / example) if isdir(local_example_path): @@ -121,13 +121,12 @@ def copy_example_files(self, example, project_dir, sayno): if util.check_package( self.name, self.version, self.spec_version, self.examples_dir ): - project_dir = util.check_dir(project_dir) - example_path = project_dir + example_path = Path(util.check_dir(project_dir)) local_example_path = str(Path(self.examples_dir) / example) if isdir(local_example_path): self._copy_files( - example, local_example_path, example_path, sayno + example, local_example_path, str(example_path), sayno ) else: click.secho(EXAMPLE_NOT_FOUND_MSG, fg="yellow") diff --git a/apio/managers/project.py b/apio/managers/project.py index 11018599..12c66a4c 100644 --- a/apio/managers/project.py +++ b/apio/managers/project.py @@ -73,10 +73,10 @@ def create_sconstruct(self, project_dir="", arch=None, sayyes=False): def create_ini(self, board, top_module, project_dir="", sayyes=False): """Creates a new apio project file""" - project_dir = util.check_dir(project_dir) + project_dir = Path(util.check_dir(project_dir)) # -- Build the filename - ini_path = str(Path(project_dir) / PROJECT_FILENAME) + ini_path = str(project_dir / PROJECT_FILENAME) # Check board boards = Resources().boards @@ -108,10 +108,10 @@ def create_ini(self, board, top_module, project_dir="", sayyes=False): def update_ini(self, top_module, project_dir): """Update the current init file with the given top-module""" - project_dir = util.check_dir(project_dir) + project_dir = Path(util.check_dir(project_dir)) # -- Build the filename - ini_path = str(Path(project_dir) / PROJECT_FILENAME) + ini_path = str(project_dir / PROJECT_FILENAME) # -- Check if the apio.ini file exists if not isfile(ini_path): diff --git a/apio/managers/scons.py b/apio/managers/scons.py index c32d0d20..5504e1e3 100644 --- a/apio/managers/scons.py +++ b/apio/managers/scons.py @@ -58,10 +58,10 @@ def __init__(self, project_dir=""): if project_dir is not None: # Check if it is a correct folder # (or create a new one) - project_dir = util.check_dir(project_dir) + project_dir = Path(util.check_dir(project_dir)) # Change to that folder - os.chdir(project_dir) + os.chdir(str(project_dir)) # W0703: Catching too general exception Exception (broad-except) # pylint: disable=W0703 diff --git a/apio/util.py b/apio/util.py index c767f926..2308c2dd 100644 --- a/apio/util.py +++ b/apio/util.py @@ -18,7 +18,7 @@ import platform import subprocess from threading import Thread -from os.path import isdir, isfile, dirname, exists +from os.path import isdir, dirname, exists from pathlib import Path import click @@ -662,16 +662,17 @@ def mkdir(path): pass -def check_dir(_dir): - """Check if the given path is a folder. If no path is given - the current path is used""" +def check_dir(_dir: Path) -> str: + """Check if the given path is a folder""" # -- If no path is given, get the current working directory - if _dir is None: - _dir = os.getcwd() + if _dir: + _dir = Path(_dir) + else: + _dir = Path.cwd() # -- Check if the path is a file or a folder - if isfile(_dir): + if _dir.is_file(): # -- It is a file! Error! Exit! click.secho( f"Error: project directory is already a file: {_dir}", fg="red"