Skip to content

Commit

Permalink
Fix download windows paths (#29)
Browse files Browse the repository at this point in the history
* Fix downloading paths that have windows style path separators

* Fix tests so that they can run on windows too

Co-authored-by: David Sternlicht <[email protected]>
  • Loading branch information
d1618033 and David Sternlicht authored Apr 12, 2020
1 parent 5b16872 commit dd7650f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scottypy/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .exc import NotOverwriting
from .types import JSON
from .utils import raise_for_status
from .utils import fix_path_sep_for_current_platform, raise_for_status

if typing.TYPE_CHECKING:
from requests import Session
Expand Down Expand Up @@ -76,7 +76,7 @@ def stream_to(self, fileobj: "typing.BinaryIO") -> None:

def download(self, directory: str = ".", overwrite: bool = False) -> None:
"""Download the file to the specified directory, retaining its name"""
subdir, file_ = os.path.split(self.file_name)
subdir, file_ = os.path.split(fix_path_sep_for_current_platform(self.file_name))
subdir = os.path.join(directory, subdir)
file_ = os.path.join(subdir, file_)

Expand Down
6 changes: 6 additions & 0 deletions scottypy/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import requests


Expand All @@ -21,3 +23,7 @@ def raise_for_status(response: requests.Response) -> None:
),
response=response,
)


def fix_path_sep_for_current_platform(file_name: str) -> str:
return file_name.replace("\\", os.path.sep).replace("/", os.path.sep)
13 changes: 13 additions & 0 deletions unittests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from http import HTTPStatus

import pytest
Expand Down Expand Up @@ -30,3 +31,15 @@ def test_raise_for_status_server_error():

def test_raise_for_status_no_error():
utils.raise_for_status(MockResponse(status_code=HTTPStatus.OK))


def test_fix_path_sep_for_current_platform_windows_path():
assert utils.fix_path_sep_for_current_platform(r"a\b\c") == os.path.join(
"a", "b", "c"
)


def test_fix_path_sep_for_current_platform_linux_path():
assert utils.fix_path_sep_for_current_platform("a/b/c") == os.path.join(
"a", "b", "c"
)

0 comments on commit dd7650f

Please sign in to comment.