Skip to content

Replaced os.path with pathlib#182

Open
trag1c wants to merge 11 commits intoeconchick:masterfrom
trag1c:use-pathlib
Open

Replaced os.path with pathlib#182
trag1c wants to merge 11 commits intoeconchick:masterfrom
trag1c:use-pathlib

Conversation

@trag1c
Copy link

@trag1c trag1c commented May 22, 2024

Hey, I just made a Pull Request!

Description

Changes all occurrences of os.path to use pathlib instead.

Motivation and Context

Closes #179

Have you tested this? If so, how?

Updated unit tests :)

Checklist for PR author(s)

  • Changes are covered by unit tests (no major decrease in code coverage %).
  • All tests pass.
  • Docstring coverage is 100% via tox -e docs or interrogate -c pyproject.toml (I mean, we should set a good example 😄).
  • Updates to documentation:
    • Document any relevant additions/changes in README.rst.
    • Manually update both the README.rst and docs/index.rst for any new/changed CLI flags.
    • Any changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives. Find the appropriate next version in the project's __init__.py file.

Release note

NONE

@trag1c trag1c marked this pull request as ready for review May 22, 2024 21:48
@trag1c
Copy link
Author

trag1c commented May 22, 2024

Not sure if this deserves a release note or any versionchanged directives.

I'll take a look at Windows' fails tomorrow 👍

@trag1c trag1c changed the title Replaced os.path with `pathlib Replaced os.path with pathlib May 22, 2024
@trag1c
Copy link
Author

trag1c commented May 25, 2024

I'll take a look at Windows' fails tomorrow 👍

Took a bit longer but I'm getting OSErrors, seems like Windows can't install cairosvg 🤔 I feel like I'm doing something wrong here.

@econchick Could you approve the workflow? 😄

@msftcangoblowm
Copy link

Have been here myself.

Getting the paths to work on all platforms and only being able to test thru a gh workflow.

Here is a module that is specifically for the challenge this PR is about

https://github.com/msftcangoblowm/wreck/blob/master/src/wreck/_safe_path.py

If you hit a brick wall or feel going around in circles, this code might be helpful

Copy link

@Paebbels Paebbels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that transition to pathlib, but I found many mistakes and code parts that can be improved. See my list of suggestions.

I found this PR by accident. I'm no contributor to this repository. I'm just an excessive Python user.

buf = []
for fl in filenames:
with codecs.open(os.path.join(HERE, fl), "rb", encoding) as f:
with codecs.open(HERE / fl, "rb", encoding) as f:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with codecs.open(HERE / fl, "rb", encoding) as f:
with (HERE / fl).open("r", encoding) as f:

You should open the file in text mode, not binary mode and then apply the encoding. Path objects provide a Path.open() method.

Since when is the abbreviation of filename fl?

Comment on lines +12 to +13
here = Path(__file__).parent.absolute()
with codecs.open(str(here.joinpath(*parts)), "rb", "utf-8") as f:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
here = Path(__file__).parent.absolute()
with codecs.open(str(here.joinpath(*parts)), "rb", "utf-8") as f:
HERE = Path(__file__).parent.absolute()
with HERE.joinpath(*parts).open("r", encoding="utf-8") as f:

No need to convert to a str, almost every open function/method accepts path-like objects. Don't open a file in binary when you apply an encoding.

Other parts use HERE.

# need to write the badge as an svg first in order to convert it to
# another format
tmp_output_file = f"{os.path.splitext(output)[0]}.tmp.svg"
tmp_output_file = Path(f"{output.parent / output.stem}.tmp.svg")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tmp_output_file = Path(f"{output.parent / output.stem}.tmp.svg")
tmp_output_file = output.with_suffix(".tmp.svg")

Source: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.with_suffix


try:
badge = minidom.parse(output)
badge = minidom.parse(str(output))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
badge = minidom.parse(str(output))
badge = minidom.parse(output)

minidom accepts file-like objects: https://docs.python.org/3/library/xml.dom.minidom.html#xml.dom.minidom.parse

"""
with open(path_config, "rb") as f:
pyproject_toml = tomllib.load(f)
pyproject_toml = tomllib.loads(Path(path_config).read_text())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pyproject_toml = tomllib.loads(Path(path_config).read_text())
pyproject_toml = tomllib.loads(Path(path_config).read_text(encoding="utf-8"))

No encoding is specified.


expected_output = expected_output.replace("\n", "")
expected_output_path = FIXTURES / "expected_badge.svg"
expected_output = expected_output_path.read_text().replace("\n", "")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected_output = expected_output_path.read_text().replace("\n", "")
expected_output = expected_output_path.read_text(encoding="utf-8").replace("\n", "")

with open(expected_fixture) as f:
expected_out = f.read()
expected_fixture = FIXTURES / "windows" / exp_fixture_file
expected_out = expected_fixture.read_text()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected_out = expected_fixture.read_text()
expected_out = expected_fixture.read_text(encoding="utf-8")


with open(expected_fixture) as f:
expected_out = f.read()
expected_out = expected_fixture.read_text()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected_out = expected_fixture.read_text()
expected_out = expected_fixture.read_text(encoding="utf-8")

expected = f.read()
expected = expected.replace("\n", "").replace("\r", "")
expected_fixture = FIXTURES / "default-style" / "99.svg"
expected = expected_fixture.read_text().replace("\n", "").replace("\r", "")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected = expected_fixture.read_text().replace("\n", "").replace("\r", "")
expected = expected_fixture.read_text(encoding="utf-8").replace("\n", "").replace("\r", "")

expected_contents = (
expected_fixture.read_bytes()
if out_format == "png"
else expected_fixture.read_text()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else expected_fixture.read_text()
else expected_fixture.read_text(encoding="utf-8")

@trag1c
Copy link
Author

trag1c commented Apr 21, 2025

I like that transition to pathlib, but I found many mistakes and code parts that can be improved. See my list of suggestions.

I found this PR by accident. I'm no contributor to this repository. I'm just an excessive Python user.

Thanks, a lot of these are good suggestions!

Not sure what's up with the snark though (e.g. "Since when is the abbreviation of filename fl?"), I didn't write the original code, just touching up the os.path bits :)

I'll probably hold off on applying these until there's some interest from Lynn in this PR actually going somewhere.

@Paebbels
Copy link

@trag1c The comment about fl was not meant for you, just for the maintainer when checking this review :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace os.path with pathlib

3 participants