Skip to content

Python pydantic #1082

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
python3-justbytes
python3-packaging
python3-psutil
python3-pydantic
python3-wcwidth
task: >
PATH=${PATH}:/github/home/.local/bin PYTHONPATH=./src
Expand Down Expand Up @@ -128,6 +129,7 @@ jobs:
python3-dateutil
python3-packaging
python3-psutil
python3-pydantic
python3-wcwidth
systemd-devel
- uses: dtolnay/rust-toolchain@master
Expand Down Expand Up @@ -164,6 +166,7 @@ jobs:
python3-dateutil
python3-packaging
python3-psutil
python3-pydantic
python3-wcwidth
task: make -f Makefile legacy-package
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
python3-justbytes
python3-packaging
python3-psutil
python3-pydantic
python3-wcwidth
task: >
PATH=${PATH}:/github/home/.local/bin PYTHONPATH=./src
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ install_requires =
justbytes>=0.14
packaging
psutil
pydantic
python-dateutil
wcwidth

Expand Down
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_list_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def display(self):

(pool_uuid, pool) = stopped_pool

self._print_detail_view(pool_uuid, StoppedPool(pool))
self._print_detail_view(pool_uuid, StoppedPool(**pool))


class StoppedTable(Stopped): # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -582,7 +582,7 @@ def key_description_str(value):
clevis_str(sp.clevis_info),
)
for pool_uuid, sp in (
(pool_uuid, StoppedPool(info))
(pool_uuid, StoppedPool(**info))
for pool_uuid, info in stopped_pools.items()
)
]
Expand Down
50 changes: 26 additions & 24 deletions src/stratis_cli/_actions/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

# isort: STDLIB
import json
from typing import List, Optional
from uuid import UUID

# isort: THIRDPARTY
from pydantic import BaseModel, validator

from .._constants import Clevis
from .._stratisd_constants import (
CLEVIS_KEY_TANG_TRUST_URL,
Expand Down Expand Up @@ -141,43 +145,41 @@ def __init__(self, info):
self.value = str(value)


class Device: # pylint: disable=too-few-public-methods
class Device(BaseModel):
"""
A representation of a device in a stopped pool.
"""

def __init__(self, mapping):
self.uuid = UUID(mapping["uuid"])
self.devnode = str(mapping["devnode"])
uuid: UUID
devnode: str


class StoppedPool: # pylint: disable=too-few-public-methods
class StoppedPool(BaseModel):
"""
A representation of a single stopped pool.
A representaton of a single stopped pool.
"""

def __init__(self, pool_info):
devs: List[Device]
clevis_info: Optional[EncryptionInfoClevis]
key_description: Optional[EncryptionInfoKeyDescription]
name: Optional[str]

@validator("key_description", allow_reuse=True)
def validate_key_description(cls, v): # pylint: disable=no-self-argument
"""
Initializer.
:param pool_info: a D-Bus structure
Validate the key description.
"""
return EncryptionInfoKeyDescription(v)

self.devs = [Device(info) for info in pool_info["devs"]]

clevis_info = pool_info.get("clevis_info")
self.clevis_info = (
None if clevis_info is None else EncryptionInfoClevis(clevis_info)
)

key_description = pool_info.get("key_description")
self.key_description = (
None
if key_description is None
else EncryptionInfoKeyDescription(key_description)
)
@validator("clevis_info", allow_reuse=True)
def validate_clevis_info(cls, v): # pylint: disable=no-self-argument
"""
Validate the Clevis info.
"""
return EncryptionInfoClevis(v)

name = pool_info.get("name")
self.name = None if name is None else str(name)
class Config: # pylint: disable=too-few-public-methods,missing-docstring
arbitrary_types_allowed = True


class PoolSelector:
Expand Down
Loading