-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pydantic: split base and charmcraft config models (#1141)
- Loading branch information
Showing
18 changed files
with
122 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,90 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# For further info, check https://github.com/canonical/charmcraft | ||
|
||
"""Charmcraft basic pydantic model.""" | ||
|
||
import pydantic | ||
|
||
|
||
class ModelConfigDefaults( | ||
pydantic.BaseModel, extra=pydantic.Extra.forbid, frozen=True, validate_all=True | ||
): | ||
"""Define Charmcraft's defaults for the BaseModel configuration.""" | ||
|
||
|
||
class CustomStrictStr(pydantic.StrictStr): | ||
"""Generic class to create custom strict strings validated by pydantic.""" | ||
|
||
@classmethod | ||
def __get_validators__(cls): | ||
"""Yield the relevant validators.""" | ||
yield from super().__get_validators__() | ||
yield cls.custom_validate | ||
|
||
|
||
class RelativePath(CustomStrictStr): | ||
"""Constrained string which must be a relative path.""" | ||
|
||
@classmethod | ||
def custom_validate(cls, value: str) -> str: | ||
"""Validate relative path. | ||
Check if it's an absolute path using POSIX's '/' (not os.path.sep, as the charm's | ||
config is independent of the platform where charmcraft is running. | ||
""" | ||
if not value: | ||
raise ValueError(f"{value!r} must be a valid relative path (cannot be empty)") | ||
|
||
if value[0] == "/": | ||
raise ValueError(f"{value!r} must be a valid relative path (cannot start with '/')") | ||
|
||
return value | ||
|
||
|
||
class AttributeName(CustomStrictStr): | ||
"""Constrained string that must match the name of an attribute from linters.CHECKERS.""" | ||
|
||
@classmethod | ||
def custom_validate(cls, value: str) -> str: | ||
"""Validate attribute name.""" | ||
from charmcraft import linters # import here to avoid cyclic imports | ||
|
||
valid_names = [ | ||
checker.name | ||
for checker in linters.CHECKERS | ||
if checker.check_type == linters.CheckType.attribute | ||
] | ||
if value not in valid_names: | ||
raise ValueError(f"Bad attribute name {value!r}") | ||
return value | ||
|
||
|
||
class LinterName(CustomStrictStr): | ||
"""Constrained string that must match the name of a linter from linters.CHECKERS.""" | ||
|
||
@classmethod | ||
def custom_validate(cls, value: str) -> str: | ||
"""Validate attribute name.""" | ||
from charmcraft import linters # import here to avoid cyclic imports | ||
|
||
valid_names = [ | ||
checker.name | ||
for checker in linters.CHECKERS | ||
if checker.check_type == linters.CheckType.lint | ||
] | ||
if value not in valid_names: | ||
raise ValueError(f"Bad lint name {value!r}") | ||
return value |
This file contains 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.