From 00be4747addb09fa20170f08f7a148964b18c37d Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Thu, 7 Oct 2021 23:28:47 +0200 Subject: [PATCH] Replace `os.path` with `pathlib.Path` where used --- setup.py | 6 +++--- src/wireviz/build_examples.py | 4 ++-- src/wireviz/wireviz.py | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index 4992bc5..921ec1d 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import os +from pathlib import Path from setuptools import setup, find_packages from src.wireviz import __version__, CMD_NAME, APP_URL @@ -11,7 +11,7 @@ from src.wireviz import __version__, CMD_NAME, APP_URL # README file and 2) it's easier to type in the README file than to put a raw # string in below ... def read(fname): - return open(os.path.join(os.path.dirname(__file__), fname)).read() + return open(Path(__file__).parent / fname).read() setup( name=CMD_NAME, @@ -19,7 +19,7 @@ setup( author='Daniel Rojas', #author_email='', description='Easily document cables and wiring harnesses', - long_description=read(os.path.join(os.path.dirname(__file__), 'docs/README.md')), + long_description=read(Path(__file__).parent / 'docs/README.md'), long_description_content_type='text/markdown', install_requires=[ 'pyyaml', diff --git a/src/wireviz/build_examples.py b/src/wireviz/build_examples.py index 65a10f6..f098a59 100755 --- a/src/wireviz/build_examples.py +++ b/src/wireviz/build_examples.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- import argparse -import sys import os from pathlib import Path +import sys script_path = Path(__file__).absolute() @@ -94,7 +94,7 @@ def clean_generated(groupkeys): for filename in collect_filenames('Cleaning', key, generated_extensions): if filename.is_file(): print(f' rm "{filename}"') - os.remove(filename) + Path(filename).unlink() def compare_generated(groupkeys, branch = '', include_graphviz_output = False): diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index abf7a20..55363a7 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import argparse -import os from pathlib import Path import sys from typing import Any, Tuple @@ -10,7 +9,7 @@ from typing import Any, Tuple import yaml if __name__ == '__main__': - sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + sys.path.insert(0, str(Path(__file__).parent.parent)) from wireviz import __version__ from wireviz.DataClasses import Metadata, Options, Tweak @@ -284,13 +283,15 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: + yaml_file = Path(yaml_file) with open_file_read(yaml_file) as file: yaml_input = file.read() - if not file_out: - fn, fext = os.path.splitext(yaml_file) - file_out = fn - file_out = os.path.abspath(file_out) + if file_out: + file_out = Path(file_out) + else: + file_out = yaml_file.parent / yaml_file.stem + file_out = file_out.resolve() parse(yaml_input, file_out=file_out) @@ -311,7 +312,7 @@ def main(): args = parse_cmdline() - if not os.path.exists(args.input_file): + if not Path(args.input_file).exists(): print(f'Error: input file {args.input_file} inaccessible or does not exist, check path') sys.exit(1) @@ -319,7 +320,7 @@ def main(): yaml_input = fh.read() if args.prepend_file: - if not os.path.exists(args.prepend_file): + if not Path(args.prepend_file).exists(): print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path') sys.exit(1) with open_file_read(args.prepend_file) as fh: @@ -327,12 +328,11 @@ def main(): yaml_input = prepend + yaml_input if not args.output_file: - file_out = args.input_file - pre, _ = os.path.splitext(file_out) - file_out = pre # extension will be added by graphviz output function + file_out = Path(args.input_file) + file_out = file_out.parent / file_out.stem # extension will be added by graphviz output function else: - file_out = args.output_file - file_out = os.path.abspath(file_out) + file_out = Path(args.output_file) + file_out = file_out.resolve() parse(yaml_input, file_out=file_out)