Skip to content

Commit 4ad5c35

Browse files
committed
refactor: update profile handling to use BaseProfile and resolve_profile for better structure
1 parent 9fc5bbb commit 4ad5c35

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

src/copia/cli/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Holds utilities for enhanced management of the config profile system"""
2-
from .models import Profile
2+
from .models import BaseProfile, FileBasedProfile, ServerBasedProfile, resolve_profile
33
from .exceptions import *
44
from .globals import *
55
from .loaders import get_profile

src/copia/cli/config/loaders.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pydantic import ValidationError
1313

1414
from .globals import ConfigScope, LOCAL_COPIA_FILE, GLOBAL_COPIA_FILE
15-
from .models import Profile
15+
from .models import BaseProfile, resolve_profile
1616
from .exceptions import (
1717
FoundProfileIsNotATableError,
1818
ProfilesKeyIsNotATableError,
@@ -71,7 +71,7 @@ def load_config(scope: ConfigScope) -> TOMLDocument:
7171
raise InvalidConfigError(f"{scope} config file is not a valid TOML file: {TOMLErr}")
7272

7373

74-
def get_profile_from_config(profile_name: str, config: dict) -> Profile:
74+
def get_profile_from_config(profile_name: str, config: dict) -> BaseProfile:
7575
"""try to get a valid profile from config
7676
7777
config must be a dict representation of the config file
@@ -89,7 +89,7 @@ def get_profile_from_config(profile_name: str, config: dict) -> Profile:
8989
InvalidProfileError: when the profile isn't valid
9090
9191
Returns:
92-
Profile: a valid Profile object
92+
BaseProfile: a valid BaseProfile object
9393
"""
9494
profiles = config.get("profiles")
9595
if profiles is None:
@@ -103,12 +103,12 @@ def get_profile_from_config(profile_name: str, config: dict) -> Profile:
103103
raise FoundProfileIsNotATableError(profile_name)
104104

105105
try:
106-
return Profile(**profile_data)
106+
return resolve_profile(profile_data)
107107
except ValidationError as Err:
108108
raise InvalidProfileError(Err)
109109

110110

111-
def get_profile(profile_name: str, scope: ConfigScope) -> Profile:
111+
def get_profile(profile_name: str, scope: ConfigScope) -> BaseProfile:
112112
"""try to get a valid profile from config based on scope
113113
114114
Args:
@@ -124,7 +124,7 @@ def get_profile(profile_name: str, scope: ConfigScope) -> Profile:
124124
InvalidProfileError: when the profile isn't valid
125125
126126
Returns:
127-
Profile: a valid Profile object
127+
BaseProfile: a valid BaseProfile object
128128
"""
129129
config = load_config(scope)
130130
return get_profile_from_config(profile_name, config)

src/copia/cli/config/models.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Annotated, Union
22

3-
from pydantic import BaseModel, Field, StringConstraints, ConfigDict
3+
from pydantic import BaseModel, Field, StringConstraints
44
from pydantic.networks import IPvAnyAddress
55

66
from .globals import Adapter, PORT_MIN_VAL, PORT_MAX_VAL
@@ -16,11 +16,12 @@
1616
strict=True,
1717
description="The Database Port"
1818
)]
19-
20-
class Profile(BaseModel):
21-
model_config = ConfigDict(extra="forbid")
2219

20+
class BaseProfile(BaseModel):
2321
adapter: Adapter = Field(description="The copia Adapter to use")
22+
23+
class ServerBasedProfile(BaseProfile):
24+
2425
host: Union[IPvAnyAddress, NotEmptyStr] = Field(description="The database hostname")
2526
port: Port
2627
database: NotEmptyStr = Field(description="The Database Port")
@@ -34,3 +35,15 @@ def __str__(self) -> str:
3435
def __repr__(self) -> str:
3536
return self.__str__()
3637

38+
class FileBasedProfile(BaseProfile):
39+
database: str
40+
41+
def __str__(self) -> str:
42+
return f"{self.database}"
43+
44+
def resolve_profile(payload: dict) -> BaseProfile:
45+
basic_info = BaseProfile(**payload)
46+
if basic_info.adapter == "mysql":
47+
return FileBasedProfile(**payload)
48+
else:
49+
return ServerBasedProfile(**payload)

0 commit comments

Comments
 (0)