Skip to content
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
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)$"
Expand All @@ -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]
5 changes: 0 additions & 5 deletions CHANGELOG.rst

This file was deleted.

5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/Helioviewer-Project/python-api/releases>`__.
8 changes: 4 additions & 4 deletions hvpy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion hvpy/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 44 additions & 9 deletions hvpy/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from datetime import datetime

import pytest
Expand Down Expand Up @@ -71,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,
Expand All @@ -82,13 +83,47 @@ 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()
save_file(res, filename, overwrite=False)
save_file(res, filename, overwrite=True)

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(":test.png", "_test.png")
res = takeScreenshot(
date=datetime.today(),
imageScale=2.44,
layers="[10,1,100]",
x0=0,
y0=0,
width=1920,
height=1200,
display=True,
)
saved_file = save_file(res, str(filename))
assert not filename.exists()
assert saved_file == Path(clean_filename)


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,
)
saved_file = save_file(res, filename)
saved_file.unlink()
assert not Path(filename).exists()
assert saved_file == Path(clean_filename).expanduser().resolve()
17 changes: 14 additions & 3 deletions hvpy/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import re
from typing import Any, List, Union, Callable
from pathlib import Path
from datetime import datetime
Expand Down Expand Up @@ -151,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.

Expand All @@ -164,9 +166,18 @@ 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.
"""
if isinstance(filename, str):
filename = Path(filename)
filepath, filename = os.path.split(filename)
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)
return filename
Copy link
Member Author

@nabobalis nabobalis Oct 17, 2022

Choose a reason for hiding this comment

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

Was there a reason to not do this?

It is quite useful to get the return and so I can pass that filepath into another function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, I don't recall. It is better now.