-
Notifications
You must be signed in to change notification settings - Fork 28
ci: add local pre-commit hook to write datasets.yaml #1145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
149d64a
ci: add pre-commit hook to write datasets.yaml
dkrako 6166228
mess up datasets.yaml
dkrako 5e71967
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e9080c
add tests
dkrako 0ff04e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a12bdd0
make pylint happy
dkrako e2d74f2
make pylint happy
dkrako da91ad3
make mypy happy
dkrako ccad07e
Merge branch 'main' into ci/pre-commit-write-datasets-yaml
dkrako 01d3079
add coverage ignore
dkrako dff6b55
Merge branch 'main' into ci/pre-commit-write-datasets-yaml
dkrako File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) 2025 The pymovements Project Authors | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (c) 2025 The pymovements Project Authors | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
"""Write datasets.yaml for DatasetLibrary.""" | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
|
||
def main( | ||
datasets_dirpath: str | Path = './src/pymovements/datasets', | ||
datasets_yaml_filename: str = 'datasets.yaml', | ||
) -> int: | ||
"""Write datasets yaml file for DatasetLibrary. | ||
|
||
Parameters | ||
---------- | ||
datasets_dirpath: str | Path | ||
The path to the directory containing dataset definition yaml files. | ||
(default: './src/pymovements/datasets') | ||
datasets_yaml_filename: str | ||
The filename of the datasets yaml file. (default: 'datasets.yaml') | ||
|
||
Returns | ||
------- | ||
int | ||
``0`` if no changes needed, ``1`` otherwise. | ||
""" | ||
datasets_dirpath = Path(datasets_dirpath) | ||
|
||
dataset_filename_stems = sorted( | ||
[ | ||
filepath.stem for filepath in datasets_dirpath.glob('*.yaml') | ||
if filepath.name != datasets_yaml_filename # Ignore datasets.yaml file. | ||
], | ||
) | ||
|
||
try: | ||
with open(datasets_dirpath / datasets_yaml_filename, encoding='utf-8') as f: | ||
dataset_yaml_content = yaml.safe_load(f) | ||
except FileNotFoundError: | ||
dataset_yaml_content = None | ||
|
||
# File content matches. Exit successfully. | ||
if dataset_filename_stems == dataset_yaml_content: | ||
return 0 | ||
|
||
# We have some updates in the datasets directory. Update the datasets yaml file. | ||
with open(datasets_dirpath / datasets_yaml_filename, 'w', encoding='utf-8') as f: | ||
yaml.dump(dataset_filename_stems, f) | ||
|
||
return 1 | ||
|
||
|
||
if __name__ == '__main__': # pragma: no cover | ||
raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright (c) 2025 The pymovements Project Authors | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
"""Test write_datasets_yaml script.""" | ||
from pathlib import Path | ||
|
||
import pytest | ||
import yaml | ||
|
||
from pymovements._scripts import write_datasets_yaml | ||
|
||
|
||
@pytest.fixture(name='make_datasets_directory') | ||
def make_datasets_directory_fixture(tmp_path): | ||
def _make_datasets_directory(param: str, make_yaml: bool) -> Path: | ||
datasets = [] | ||
if param in {'single', 'two'}: | ||
filepath = tmp_path / 'first.yaml' | ||
filepath.touch() | ||
datasets.append('first') | ||
if param == 'two': | ||
filepath = tmp_path / 'second.yaml' | ||
filepath.touch() | ||
datasets.append('second') | ||
if make_yaml: | ||
with open(tmp_path / 'datasets.yaml', 'w', encoding='utf-8') as f: | ||
yaml.dump(datasets, f) | ||
return tmp_path | ||
|
||
yield _make_datasets_directory | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('fixture_args', 'expected_return', 'expected_yaml'), | ||
[ | ||
pytest.param( | ||
['empty', False], | ||
1, | ||
[], | ||
id='empty_new', | ||
), | ||
|
||
pytest.param( | ||
['empty', True], | ||
0, | ||
[], | ||
id='empty_exist', | ||
), | ||
|
||
pytest.param( | ||
['single', False], | ||
1, | ||
['first'], | ||
id='single_new', | ||
), | ||
|
||
pytest.param( | ||
['single', True], | ||
0, | ||
['first'], | ||
id='single_exist', | ||
), | ||
|
||
pytest.param( | ||
['two', False], | ||
1, | ||
['first', 'second'], | ||
id='two_new', | ||
), | ||
|
||
pytest.param( | ||
['two', True], | ||
0, | ||
['first', 'second'], | ||
id='two_exist', | ||
), | ||
], | ||
) | ||
def test_write_datasets_yaml( | ||
fixture_args, expected_return, expected_yaml, make_datasets_directory, | ||
): | ||
datasets_dirpath = make_datasets_directory(*fixture_args) | ||
datasets_yaml_filename = 'datasets.yaml' | ||
|
||
return_value = write_datasets_yaml.main( | ||
datasets_dirpath=datasets_dirpath, | ||
datasets_yaml_filename=datasets_yaml_filename, | ||
) | ||
|
||
with open(datasets_dirpath / datasets_yaml_filename, encoding='utf-8') as f: | ||
yaml_content = yaml.safe_load(f) | ||
|
||
assert return_value == expected_return | ||
assert yaml_content == expected_yaml |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.