Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace os.path with pathlib.Path #246

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#!/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

# Utility function to read the README file.
# Used for the long_description. It's nice, because now 1) we have a top level
# 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()
README_PATH = Path(__file__).parent / 'docs' / 'README.md'

setup(
name=CMD_NAME,
version=__version__,
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=open(README_PATH).read(),
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
long_description_content_type='text/markdown',
install_requires=[
'pyyaml',
Expand Down
4 changes: 2 additions & 2 deletions src/wireviz/build_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-

import argparse
import sys
import os
from pathlib import Path
import sys

script_path = Path(__file__).absolute()

Expand Down Expand Up @@ -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()
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved


def compare_generated(groupkeys, branch = '', include_graphviz_output = False):
Expand Down
31 changes: 16 additions & 15 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
# -*- coding: utf-8 -*-

import argparse
import os
from pathlib import Path
import sys
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)) # add src/wireviz to PATH

from wireviz import __version__
from wireviz.DataClasses import Metadata, Options, Tweak
Expand Down Expand Up @@ -284,13 +283,15 @@ def alternate_type(): # flip between connector and cable/arrow


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
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
file_out = file_out.resolve()

parse(yaml_input, file_out=file_out)

Expand All @@ -311,28 +312,28 @@ def main():

args = parse_cmdline()

if not os.path.exists(args.input_file):
print(f'Error: input file {args.input_file} inaccessible or does not exist, check path')
file_in = Path(args.input_file)

if not file_in.exists():
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
print(f'Error: input file {file_in} inaccessible or does not exist, check path')
sys.exit(1)

with open_file_read(args.input_file) as fh:
with open_file_read(file_in) as fh:
yaml_input = fh.read()

if args.prepend_file:
if not os.path.exists(args.prepend_file):
if not Path(args.prepend_file).exists():
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
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:
prepend = fh.read()
yaml_input = prepend + yaml_input

if not args.output_file:
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
file_out = args.input_file
pre, _ = os.path.splitext(file_out)
file_out = pre # extension will be added by graphviz output function
file_out = file_in.parent / file_in.stem # extension will be added by graphviz output function
formatc1702 marked this conversation as resolved.
Show resolved Hide resolved
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)

Expand Down