Skip to content

Commit

Permalink
Merge pull request #251 from chrisburr/pydantic2
Browse files Browse the repository at this point in the history
Suppport Pydantic 2
  • Loading branch information
chrisburr authored Jun 8, 2024
2 parents c06979b + d6f81c3 commit 4bfc9c2
Show file tree
Hide file tree
Showing 49 changed files with 548 additions and 473 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- pydantic==1.10.13
- pydantic
- sqlalchemy
- types-PyYAML
- types-cachetools
Expand Down
34 changes: 20 additions & 14 deletions diracx-cli/src/diracx/cli/internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import git
import typer
import yaml
from pydantic import parse_obj_as
from pydantic import TypeAdapter

from diracx.core.config import ConfigSource, ConfigSourceUrl
from diracx.core.config.schema import (
Expand All @@ -25,13 +25,11 @@


@app.command()
def generate_cs(
config_repo: str,
):
def generate_cs(config_repo: str):
"""Generate a minimal DiracX configuration repository"""
# TODO: The use of parse_obj_as should be moved in to typer itself
config_repo = parse_obj_as(ConfigSourceUrl, config_repo)
if config_repo.scheme != "git+file":
# TODO: The use of TypeAdapter should be moved in to typer itself
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
if config_repo.scheme != "git+file" or config_repo.path is None:
raise NotImplementedError("Only git+file:// URLs are supported")
repo_path = Path(config_repo.path)
if repo_path.exists() and list(repo_path.iterdir()):
Expand Down Expand Up @@ -62,8 +60,10 @@ def add_vo(
):
"""Add a registry entry (vo) to an existing configuration repository"""

# TODO: The use of parse_obj_as should be moved in to typer itself
config_repo = parse_obj_as(ConfigSourceUrl, config_repo)
# TODO: The use of TypeAdapter should be moved in to typer itself
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
if config_repo.scheme != "git+file" or config_repo.path is None:
raise NotImplementedError("Only git+file:// URLs are supported")
repo_path = Path(config_repo.path)

# A VO should at least contain a default group
Expand Down Expand Up @@ -104,8 +104,10 @@ def add_group(
):
"""Add a group to an existing vo in the configuration repository"""

# TODO: The use of parse_obj_as should be moved in to typer itself
config_repo = parse_obj_as(ConfigSourceUrl, config_repo)
# TODO: The use of TypeAdapter should be moved in to typer itself
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
if config_repo.scheme != "git+file" or config_repo.path is None:
raise NotImplementedError("Only git+file:// URLs are supported")
repo_path = Path(config_repo.path)

new_group = GroupConfig(Properties=set(properties), Quota=None, Users=set())
Expand Down Expand Up @@ -139,8 +141,10 @@ def add_user(
):
"""Add a user to an existing vo and group"""

# TODO: The use of parse_obj_as should be moved in to typer itself
config_repo = parse_obj_as(ConfigSourceUrl, config_repo)
# TODO: The use of TypeAdapter should be moved in to typer itself
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
if config_repo.scheme != "git+file" or config_repo.path is None:
raise NotImplementedError("Only git+file:// URLs are supported")

repo_path = Path(config_repo.path)

Expand Down Expand Up @@ -184,6 +188,8 @@ def update_config_and_commit(repo_path: Path, config: Config, message: str):
repo = git.Repo(repo_path)
yaml_path = repo_path / "default.yml"
typer.echo(f"Writing back configuration to {yaml_path}", err=True)
yaml_path.write_text(yaml.safe_dump(config.dict(exclude_unset=True)))
yaml_path.write_text(
yaml.safe_dump(config.model_dump(exclude_unset=True, mode="json"))
)
repo.index.add([yaml_path.relative_to(repo_path)])
repo.index.commit(message)
10 changes: 6 additions & 4 deletions diracx-cli/src/diracx/cli/internal/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ def cs_sync(old_file: Path, new_file: Path):

_apply_fixes(raw)

config = Config.parse_obj(raw)
new_file.write_text(yaml.safe_dump(config.dict(exclude_unset=True)))
config = Config.model_validate(raw)
new_file.write_text(
yaml.safe_dump(config.model_dump(exclude_unset=True, mode="json"))
)


def _apply_fixes(raw):
"""Modify raw in place to make any layout changes between the old and new structure"""

conv_config = ConversionConfig.parse_obj(raw["DiracX"]["CsSync"])
conv_config = ConversionConfig.model_validate(raw["DiracX"]["CsSync"])

raw.pop("DiracX", None)
# Remove dips specific parts from the CS
Expand Down Expand Up @@ -119,7 +121,7 @@ def _apply_fixes(raw):

for vo, vo_meta in conv_config.VOs.items():
raw["Registry"][vo] = {
"IdP": vo_meta.IdP,
"IdP": vo_meta.IdP.model_dump(),
"DefaultGroup": vo_meta.DefaultGroup,
"Users": {},
"Groups": {},
Expand Down
58 changes: 29 additions & 29 deletions diracx-cli/tests/legacy/cs_sync/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ Registry:
DefaultGroup: jenkins_user
Groups:
jenkins_fcadmin:
Properties: !!set
FileCatalogManagement: null
NormalUser: null
Users: !!set
26dbe36e-cf5c-4c52-a834-29a1c904ef74: null
a95ab678-3fa4-41b9-b863-fe62ce8064ce: null
e2cb28ec-1a1e-40ee-a56d-d899b79879ce: null
Properties:
- FileCatalogManagement
- NormalUser
Users:
- 26dbe36e-cf5c-4c52-a834-29a1c904ef74
- a95ab678-3fa4-41b9-b863-fe62ce8064ce
- e2cb28ec-1a1e-40ee-a56d-d899b79879ce
jenkins_user:
Properties: !!set
NormalUser: null
Users: !!set
26dbe36e-cf5c-4c52-a834-29a1c904ef74: null
a95ab678-3fa4-41b9-b863-fe62ce8064ce: null
e2cb28ec-1a1e-40ee-a56d-d899b79879ce: null
Properties:
- NormalUser
Users:
- 26dbe36e-cf5c-4c52-a834-29a1c904ef74
- a95ab678-3fa4-41b9-b863-fe62ce8064ce
- e2cb28ec-1a1e-40ee-a56d-d899b79879ce
IdP:
ClientID: 995ed3b9-d5bd-49d3-a7f4-7fc7dbd5a0cd
URL: https://jenkins.invalid/
Expand Down Expand Up @@ -94,23 +94,23 @@ Registry:
DefaultGroup: dirac_user
Groups:
dirac_admin:
Properties: !!set
AlarmsManagement: null
CSAdministrator: null
FullDelegation: null
JobAdministrator: null
Operator: null
ProxyManagement: null
ServiceAdministrator: null
Users: !!set
26b14fc9-6d40-4ca5-b014-6234eaf0fb6e: null
Properties:
- AlarmsManagement
- CSAdministrator
- FullDelegation
- JobAdministrator
- Operator
- ProxyManagement
- ServiceAdministrator
Users:
- 26b14fc9-6d40-4ca5-b014-6234eaf0fb6e
dirac_user:
Properties: !!set
NormalUser: null
Users: !!set
26b14fc9-6d40-4ca5-b014-6234eaf0fb6e: null
d3adc733-6588-4d6f-8581-5986b02d0c87: null
ff2152ff-34f4-4739-b106-3def37e291e3: null
Properties:
- NormalUser
Users:
- 26b14fc9-6d40-4ca5-b014-6234eaf0fb6e
- d3adc733-6588-4d6f-8581-5986b02d0c87
- ff2152ff-34f4-4739-b106-3def37e291e3
IdP:
ClientID: 072afab5-ed92-46e0-a61d-4ecbc96e0770
URL: https://vo.invalid/
Expand Down
2 changes: 1 addition & 1 deletion diracx-cli/tests/legacy/cs_sync/test_cssync.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_cs_sync(tmp_path, monkeypatch):
actual_output = yaml.safe_load(output_file.read_text())
expected_output = yaml.safe_load((file_path / "integration_test.yaml").read_text())
assert actual_output == expected_output
Config.parse_obj(actual_output)
Config.model_validate(actual_output)


def test_disabled_vos_empty(tmp_path, monkeypatch):
Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/_configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(
**kwargs: Any,
) -> None:
diracx_preferences = diracx_preferences or get_diracx_preferences()
self._endpoint = endpoint or diracx_preferences.url
self._endpoint = str(endpoint or diracx_preferences.url)
if verify is True and diracx_preferences.ca_path:
verify = str(diracx_preferences.ca_path)
kwargs["connection_verify"] = verify
Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ def _deserialize(self, target_obj, data):
elif isinstance(response, type) and issubclass(response, Enum):
return self.deserialize_enum(data, response)

if data is None:
if data is None or data is CoreNull:
return data
try:
attributes = response._attribute_map # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/_vendor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/_configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __init__(
if verify is True and diracx_preferences.ca_path:
verify = str(diracx_preferences.ca_path)
kwargs["connection_verify"] = verify
self._endpoint = endpoint or diracx_preferences.url
self._endpoint = str(endpoint or diracx_preferences.url)
self._client_id = client_id or "myDIRACClientID"

# Get .well-known configuration
Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/_vendor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion diracx-client/src/diracx/client/aio/operations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].17)
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.10.2, generator: @autorest/[email protected].19)
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
Loading

0 comments on commit 4bfc9c2

Please sign in to comment.