From 6733bf672d7e0143b8bc5d3206d4f914b1c0037e Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Mon, 17 Oct 2022 16:18:17 -0700 Subject: [PATCH 1/4] fix for filename on windows and expand user --- .pre-commit-config.yaml | 6 +++--- hvpy/tests/test_utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ hvpy/utils.py | 9 +++++++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0f87cdc..9479788 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,13 +5,13 @@ repos: - id: docformatter args: [--in-place, --pre-summary-newline, --make-summary-multi] - repo: https://github.com/myint/autoflake - rev: v1.5.3 + rev: v1.7.6 hooks: - id: autoflake args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variable'] exclude: ".*(.fits|.fts|.fit|.txt|tca.*|extern.*|.rst|.md|__init__.py)$" - repo: https://github.com/psf/black - rev: 22.8.0 + rev: 22.10.0 hooks: - id: black exclude: ".*(.fits|.fts|.fit|.txt|.csv)$" @@ -34,7 +34,7 @@ repos: - id: check-yaml - id: debug-statements - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v0.971' + rev: 'v0.982' hooks: - id: mypy additional_dependencies: [types-requests==2.28.0] diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index eddc1a0..587cc9c 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -1,3 +1,4 @@ +from pathlib import Path from datetime import datetime import pytest @@ -92,3 +93,42 @@ def test_save_file(tmp_path): f2 = tmp_path / "test2.png" save_file(res, str(f2), overwrite=False) assert f2.exists() + + +def test_save_file_cleans(tmp_path): + # Clean the filename for Windows filepaths + filename = tmp_path / ":test.png" + clean_filename = str(filename).replace(":", "_") + res = takeScreenshot( + date=datetime.today(), + imageScale=2.44, + layers="[10,1,100]", + x0=0, + y0=0, + width=1920, + height=1200, + display=True, + ) + save_file(res, str(filename)) + assert not filename.exists() + assert Path(clean_filename).exists() + + +def test_save_file_expands(): + # Check that ~/ expands + filename = "~/:test.png" + clean_filename = str(filename).replace(":", "_") + res = takeScreenshot( + date=datetime.today(), + imageScale=2.44, + layers="[10,1,100]", + x0=0, + y0=0, + width=1920, + height=1200, + display=True, + ) + save_file(res, filename) + assert not Path(filename).exists() + assert Path(clean_filename).expanduser().exists() + Path(clean_filename).expanduser().unlink() diff --git a/hvpy/utils.py b/hvpy/utils.py index 484d914..0c1d4b6 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -1,3 +1,5 @@ +import os +import re from typing import Any, List, Union, Callable from pathlib import Path from datetime import datetime @@ -165,8 +167,11 @@ def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = Fal Whether to overwrite the file if it already exists. Default is `False`. """ - if isinstance(filename, str): - filename = Path(filename) + filename = Path(filename).expanduser().resolve().absolute() + filepath, filename = os.path.split(filename) + # Sanitize the filename - Only works for strings + filename = re.sub(r"[^\w\-_\. ]", "_", filename) + filename = Path(filepath) / Path(filename) if filename.exists() and not overwrite: raise FileExistsError(f"{filename} already exists. Use overwrite=True to overwrite.") filename.write_bytes(data) From 7f20275bf9e4b613fde731ecde650d9edccd11ea Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Mon, 17 Oct 2022 16:20:44 -0700 Subject: [PATCH 2/4] changelog note in docs --- CHANGELOG.rst | 5 ----- docs/index.rst | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) delete mode 100644 CHANGELOG.rst diff --git a/CHANGELOG.rst b/CHANGELOG.rst deleted file mode 100644 index 6507979..0000000 --- a/CHANGELOG.rst +++ /dev/null @@ -1,5 +0,0 @@ -v0.0.1 (2022-11-11) -=================== - -Features --------- diff --git a/docs/index.rst b/docs/index.rst index 7dfa922..8520778 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -30,3 +30,8 @@ You need to install the development version of the package in order to make and See the :ref:`obtaining_the_source` section for details. You may also want to familiarize yourself with the :ref:`dev_guide` for ``hvpy``. + +Changelog +--------- + +The changelog for ``hvpy`` is available on the `GitHub releases page `__. From c342b21c026f7b2e7602b0b238bb63c6e4be3227 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Mon, 17 Oct 2022 16:29:27 -0700 Subject: [PATCH 3/4] return the filepath from the save --- hvpy/helpers.py | 8 ++++---- hvpy/tests/test_helper.py | 2 +- hvpy/utils.py | 8 +++++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/hvpy/helpers.py b/hvpy/helpers.py index 069a097..cb9f566 100644 --- a/hvpy/helpers.py +++ b/hvpy/helpers.py @@ -122,12 +122,12 @@ def createMovie( filename = f"{title}.{format}" else: filename = f"{filename}.{format}" - save_file( + filename = save_file( data=binary_data, filename=filename, overwrite=overwrite, ) - return Path(filename) + return filename @_add_shared_docstring(takeScreenshotInputParameters) @@ -203,9 +203,9 @@ def createScreenshot( filename = f"{res['id']}_{date.date()}.png" else: filename = f"{filename}.png" - save_file( + filename = save_file( data=binary_data, filename=filename, overwrite=overwrite, ) - return Path(filename) + return filename diff --git a/hvpy/tests/test_helper.py b/hvpy/tests/test_helper.py index ddf1ff3..5e50038 100644 --- a/hvpy/tests/test_helper.py +++ b/hvpy/tests/test_helper.py @@ -38,7 +38,7 @@ def test_createMovie(start_time, end_time, tmp_path): assert result == tmp_path / "movie.mp4" -def test_createMovie_with_none_filename(start_time, end_time): +def test_createMovie_with_no_filename(start_time, end_time): result = createMovie( startTime=start_time, endTime=end_time, diff --git a/hvpy/utils.py b/hvpy/utils.py index 0c1d4b6..86f3145 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -153,7 +153,7 @@ def create_events(events: List[Union[EventType, str, tuple]]) -> str: return constructed_events[:-1] -def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = False) -> None: +def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = False) -> Path: """ Saves a file to the specified path. @@ -166,6 +166,11 @@ def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = Fal overwrite Whether to overwrite the file if it already exists. Default is `False`. + + Returns + ------- + `~pathlib.Path` + The path to the saved file. """ filename = Path(filename).expanduser().resolve().absolute() filepath, filename = os.path.split(filename) @@ -175,3 +180,4 @@ def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = Fal if filename.exists() and not overwrite: raise FileExistsError(f"{filename} already exists. Use overwrite=True to overwrite.") filename.write_bytes(data) + return filename From d97c16c6f22971b9269c5e6d98872af0278bf431 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Mon, 17 Oct 2022 16:51:09 -0700 Subject: [PATCH 4/4] try it this way for windows --- hvpy/tests/test_utils.py | 27 +++++++++++---------------- hvpy/utils.py | 4 ++-- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/hvpy/tests/test_utils.py b/hvpy/tests/test_utils.py index 587cc9c..c567380 100644 --- a/hvpy/tests/test_utils.py +++ b/hvpy/tests/test_utils.py @@ -72,7 +72,7 @@ def test_create_events_string(): def test_save_file(tmp_path): - f1 = tmp_path / "test.png" + filename = tmp_path / "test.png" res = takeScreenshot( date=datetime.today(), imageScale=2.44, @@ -83,22 +83,17 @@ def test_save_file(tmp_path): height=1200, display=True, ) - save_file(res, f1, overwrite=False) - assert f1.exists() + saved_file = save_file(res, filename, overwrite=False) + assert saved_file == filename with pytest.raises(FileExistsError, match="already exists"): - save_file(res, f1, overwrite=False) - save_file(res, f1, overwrite=True) - assert f1.exists() - - f2 = tmp_path / "test2.png" - save_file(res, str(f2), overwrite=False) - assert f2.exists() + save_file(res, filename, overwrite=False) + save_file(res, filename, overwrite=True) def test_save_file_cleans(tmp_path): # Clean the filename for Windows filepaths filename = tmp_path / ":test.png" - clean_filename = str(filename).replace(":", "_") + clean_filename = str(filename).replace(":test.png", "_test.png") res = takeScreenshot( date=datetime.today(), imageScale=2.44, @@ -109,9 +104,9 @@ def test_save_file_cleans(tmp_path): height=1200, display=True, ) - save_file(res, str(filename)) + saved_file = save_file(res, str(filename)) assert not filename.exists() - assert Path(clean_filename).exists() + assert saved_file == Path(clean_filename) def test_save_file_expands(): @@ -128,7 +123,7 @@ def test_save_file_expands(): height=1200, display=True, ) - save_file(res, filename) + saved_file = save_file(res, filename) + saved_file.unlink() assert not Path(filename).exists() - assert Path(clean_filename).expanduser().exists() - Path(clean_filename).expanduser().unlink() + assert saved_file == Path(clean_filename).expanduser().resolve() diff --git a/hvpy/utils.py b/hvpy/utils.py index 86f3145..66de562 100644 --- a/hvpy/utils.py +++ b/hvpy/utils.py @@ -172,11 +172,11 @@ def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = Fal `~pathlib.Path` The path to the saved file. """ - filename = Path(filename).expanduser().resolve().absolute() filepath, filename = os.path.split(filename) - # Sanitize the filename - Only works for strings filename = re.sub(r"[^\w\-_\. ]", "_", filename) filename = Path(filepath) / Path(filename) + filename = Path(filename).expanduser().resolve().absolute() + # Sanitize the filename - Only works for strings if filename.exists() and not overwrite: raise FileExistsError(f"{filename} already exists. Use overwrite=True to overwrite.") filename.write_bytes(data)