Replace os.path with pathlib.Path where used

This commit is contained in:
Daniel Rojas 2021-10-07 23:28:47 +02:00
parent b513051bc2
commit 00be4747ad
3 changed files with 18 additions and 18 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os from pathlib import Path
from setuptools import setup, find_packages from setuptools import setup, find_packages
from src.wireviz import __version__, CMD_NAME, APP_URL 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 # README file and 2) it's easier to type in the README file than to put a raw
# string in below ... # string in below ...
def read(fname): def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read() return open(Path(__file__).parent / fname).read()
setup( setup(
name=CMD_NAME, name=CMD_NAME,
@ -19,7 +19,7 @@ setup(
author='Daniel Rojas', author='Daniel Rojas',
#author_email='', #author_email='',
description='Easily document cables and wiring harnesses', 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', long_description_content_type='text/markdown',
install_requires=[ install_requires=[
'pyyaml', 'pyyaml',

View File

@ -2,9 +2,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import sys
import os import os
from pathlib import Path from pathlib import Path
import sys
script_path = Path(__file__).absolute() script_path = Path(__file__).absolute()
@ -94,7 +94,7 @@ def clean_generated(groupkeys):
for filename in collect_filenames('Cleaning', key, generated_extensions): for filename in collect_filenames('Cleaning', key, generated_extensions):
if filename.is_file(): if filename.is_file():
print(f' rm "{filename}"') print(f' rm "{filename}"')
os.remove(filename) Path(filename).unlink()
def compare_generated(groupkeys, branch = '', include_graphviz_output = False): def compare_generated(groupkeys, branch = '', include_graphviz_output = False):

View File

@ -2,7 +2,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse import argparse
import os
from pathlib import Path from pathlib import Path
import sys import sys
from typing import Any, Tuple from typing import Any, Tuple
@ -10,7 +9,7 @@ from typing import Any, Tuple
import yaml import yaml
if __name__ == '__main__': 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 import __version__
from wireviz.DataClasses import Metadata, Options, Tweak 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: 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: with open_file_read(yaml_file) as file:
yaml_input = file.read() yaml_input = file.read()
if not file_out: if file_out:
fn, fext = os.path.splitext(yaml_file) file_out = Path(file_out)
file_out = fn else:
file_out = os.path.abspath(file_out) file_out = yaml_file.parent / yaml_file.stem
file_out = file_out.resolve()
parse(yaml_input, file_out=file_out) parse(yaml_input, file_out=file_out)
@ -311,7 +312,7 @@ def main():
args = parse_cmdline() 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') print(f'Error: input file {args.input_file} inaccessible or does not exist, check path')
sys.exit(1) sys.exit(1)
@ -319,7 +320,7 @@ def main():
yaml_input = fh.read() yaml_input = fh.read()
if args.prepend_file: 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') print(f'Error: prepend input file {args.prepend_file} inaccessible or does not exist, check path')
sys.exit(1) sys.exit(1)
with open_file_read(args.prepend_file) as fh: with open_file_read(args.prepend_file) as fh:
@ -327,12 +328,11 @@ def main():
yaml_input = prepend + yaml_input yaml_input = prepend + yaml_input
if not args.output_file: if not args.output_file:
file_out = args.input_file file_out = Path(args.input_file)
pre, _ = os.path.splitext(file_out) file_out = file_out.parent / file_out.stem # extension will be added by graphviz output function
file_out = pre # extension will be added by graphviz output function
else: else:
file_out = args.output_file file_out = Path(args.output_file)
file_out = os.path.abspath(file_out) file_out = file_out.resolve()
parse(yaml_input, file_out=file_out) parse(yaml_input, file_out=file_out)