Skip to content

Commit

Permalink
Added function to set the same version for both the pyproject.toml an…
Browse files Browse the repository at this point in the history
…d CITATION.cff files. Added test and updated usage in README for this function. Updated version.
  • Loading branch information
willynilly committed Apr 1, 2024
1 parent e982624 commit 9c2ae85
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ keywords:
- pyproject.toml
- CITATION.cff
license: Apache-2.0
version: "1.0.0"
version: "1.1.0"
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,70 @@ pip install cff2toml
```python
from cff2toml import update_pyproject_toml_with_citation_cff

# update the pyproject.toml file with metadata
# from CITATION.cff file
# update pyproject.toml with metadata
# from CITATION.cff
# where both files are located in the working directory
update_pyproject_toml_with_citation_cff()
```

```python
from cff2toml import update_pyproject_toml_with_citation_cff

# update the pyproject.toml file with metadata
# from CITATION.cff file
# where both files are located in the working directory
# update pyproject.toml with metadata
# from CITATION.cff
# with custom file paths
pyproject_toml_file_path: str = os.path.join('somepath', 'pyproject.toml')
citation_cff_file_path: str = os.path.join('somepath', 'CITATION.cff')
citation_cff_file_path: str = os.path.join('someotherpath', 'CITATION.cff')

update_pyproject_toml_with_citation_cff(pyproject_toml_file_path=pyproject_toml_file_path, citation_cff_file_path=citation_cff_file_path)
```

### Updating CITATION.toml with metadata from pyprojects.toml
### Updating CITATION.cff with metadata from pyprojects.toml

```python
from cff2toml import update_citation_cff_with_pyproject_toml

# update the CITATION.cff file with metadata
# from pyprojects.cff file
# update CITATION.cff with metadata
# from pyprojects.cff
# where both files are located in the working directory
update_citation_cff_with_pyproject_toml()
```

```python
from cff2toml import update_pyproject_toml_with_citation_cff

# update the CITATION.cff file with metadata
# from pyprojects.cff file
# from specific locations
# update CITATION.cff with metadata
# from pyprojects.cff
# with custom file paths
pyproject_toml_file_path: str = os.path.join('somepath', 'pyproject.toml')
citation_cff_file_path: str = os.path.join('somepath', 'CITATION.cff')
citation_cff_file_path: str = os.path.join('someotherpath', 'CITATION.cff')

update_citation_cff_with_pyproject_toml(citation_cff_file_path=citation_cff_file_path, pyproject_toml_file_path=pyproject_toml_file_path)
```

### Setting the same version for both the pyprojects.toml file and CITATION.cff file

```python
from cff2toml import set_version_for_pyproject_toml_with_citation_cff

# set same version for pyproject.toml
# and CITATION.cff where both files are
# located in the working directory
set_version_for_pyproject_toml_with_citation_cff(version="2.0.0")
```

```python
from cff2toml import update_pyproject_toml_with_citation_cff

# set same version for pyproject.toml
# and CITATION.cff where both files
# have custom file paths
pyproject_toml_file_path: str = os.path.join('somepath', 'pyproject.toml')
citation_cff_file_path: str = os.path.join('someotherpath', 'CITATION.cff')

set_version_for_pyproject_toml_with_citation_cff(version="2.0.0", pyproject_toml_file_path=pyproject_toml_file_path, citation_cff_file_path=citation_cff_file_path)
```

### Updating TOML file with metadata from CFF file

```python
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "cff2toml"
version = "1.0.0"
version = "1.0.1"
description = "A module to synchronize metadata between TOML and CFF files, including between pyproject.toml and CITATION.cff files."
readme = "README.md"
requires-python = ">=3.8"
Expand Down
18 changes: 17 additions & 1 deletion src/cff2toml/cff2toml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, Union
from typing import Any, Callable, Dict, Tuple, Union
import toml
import yaml

Expand Down Expand Up @@ -122,3 +122,19 @@ def transformer(citation_cff_object: CFFObject, pyproject_toml_object: TOMLObjec

return citation_cff_object
return update_cff_with_toml(toml_file_path=pyproject_toml_file_path, cff_file_path=citation_cff_file_path, transform_cff_object_func=transformer)


def set_version_for_pyproject_toml_and_citation_cff(version: str, pyproject_toml_file_path: str, citation_cff_file_path: str) -> Tuple[TOMLObject, CFFObject]:
pyproject_toml_object: TOMLObject = load_toml_object(
toml_file_path=pyproject_toml_file_path)
citation_cff_object: CFFObject = load_cff_object(
cff_file_path=citation_cff_file_path)

citation_cff_object['version'] = version
pyproject_toml_object['project']['version'] = version

save_cff_object(citation_cff_object, cff_file_path=citation_cff_file_path)
save_toml_object(toml_object=pyproject_toml_object,
toml_file_path=pyproject_toml_file_path)

return pyproject_toml_object, citation_cff_object
57 changes: 56 additions & 1 deletion tests/test_cff2toml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import os
from src.cff2toml.cff2toml import load_cff_object, load_toml_object, CFFObject, TOMLObject, TransformCFFObjectWithTOMLObjectFunction, TransformTOMLObjectWithCFFObjectFunction, update_cff_with_toml, update_citation_cff_with_pyproject_toml, update_pyproject_toml_with_citation_cff, update_toml_with_cff
from src.cff2toml.cff2toml import load_cff_object, load_toml_object, CFFObject, TOMLObject, TransformCFFObjectWithTOMLObjectFunction, TransformTOMLObjectWithCFFObjectFunction, set_version_for_pyproject_toml_and_citation_cff, update_cff_with_toml, update_citation_cff_with_pyproject_toml, update_pyproject_toml_with_citation_cff, update_toml_with_cff

import tempfile
import shutil
Expand Down Expand Up @@ -205,3 +205,58 @@ def test_update_citation_cff_with_pyproject_toml(dummy_citation_cff_file_path, d
assert cff_object['license'] == 'Apache-1.0'
assert cff_object['abstract'] == 'A module that does something uncool.'
assert cff_object['repository-code'] == 'https://github.com/willnilly/somewhereuncool'


def test_set_version_for_citation_cff_with_pyproject_toml(dummy_citation_cff_file_path, dummy_pyproject_toml_file_path):
expected_version: str = '10.1.1'
with TempCopiedFile(source_file_path=dummy_citation_cff_file_path) as tmp_dummy_citation_cff_file:
with TempCopiedFile(source_file_path=dummy_pyproject_toml_file_path) as tmp_dummy_pyproject_toml_file:

toml_object: TOMLObject = load_toml_object(
toml_file_path=tmp_dummy_pyproject_toml_file.file_path)
assert toml_object['project']['version'] == '0.0.1'
assert toml_object['project']['name'] == 'someuncooltool'
assert toml_object['project']['description'] == 'A module that does something uncool.'
assert toml_object['project']['license'] == 'Apache-1.0'
assert toml_object['project']['urls']['Source'] == 'https://github.com/willnilly/somewhereuncool'

cff_object: CFFObject = load_cff_object(
cff_file_path=tmp_dummy_citation_cff_file.file_path)
assert cff_object['version'] == '0.0.2'
assert cff_object['title'] == 'somecooltool'
assert cff_object['license'] == 'Apache-2.0'
assert cff_object['abstract'] == 'A module that does something cool.'
assert cff_object['repository-code'] == 'https://github.com/willynilly/somewherecool'

toml_object, cff_object = set_version_for_pyproject_toml_and_citation_cff(
version=expected_version,
pyproject_toml_file_path=tmp_dummy_pyproject_toml_file.file_path,
citation_cff_file_path=tmp_dummy_citation_cff_file.file_path,)

assert toml_object['project']['version'] == expected_version
assert toml_object['project']['name'] == 'someuncooltool'
assert toml_object['project']['description'] == 'A module that does something uncool.'
assert toml_object['project']['license'] == 'Apache-1.0'
assert toml_object['project']['urls']['Source'] == 'https://github.com/willnilly/somewhereuncool'

assert cff_object['version'] == expected_version
assert cff_object['title'] == 'somecooltool'
assert cff_object['license'] == 'Apache-2.0'
assert cff_object['abstract'] == 'A module that does something cool.'
assert cff_object['repository-code'] == 'https://github.com/willynilly/somewherecool'

toml_object = load_toml_object(
toml_file_path=tmp_dummy_pyproject_toml_file.file_path)
assert toml_object['project']['version'] == expected_version
assert toml_object['project']['name'] == 'someuncooltool'
assert toml_object['project']['description'] == 'A module that does something uncool.'
assert toml_object['project']['license'] == 'Apache-1.0'
assert toml_object['project']['urls']['Source'] == 'https://github.com/willnilly/somewhereuncool'

cff_object = load_cff_object(
cff_file_path=tmp_dummy_citation_cff_file.file_path)
assert cff_object['version'] == expected_version
assert cff_object['title'] == 'somecooltool'
assert cff_object['license'] == 'Apache-2.0'
assert cff_object['abstract'] == 'A module that does something cool.'
assert cff_object['repository-code'] == 'https://github.com/willynilly/somewherecool'

0 comments on commit 9c2ae85

Please sign in to comment.