99
1010from awesomeversion import AwesomeVersion , AwesomeVersionStrategy , AwesomeVersionStrategyException
1111import click
12- import pydantic
1312import toml
1413
1514from cs_tools import datastructures , utils
1615from cs_tools .sync import base
1716
18- log = logging .getLogger (__name__ )
17+ _LOG = logging .getLogger (__name__ )
1918
2019
2120class CustomType (click .ParamType ):
@@ -113,7 +112,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
113112class Directory (CustomType ):
114113 """Convert STR to DIRECTORY PATH."""
115114
116- def __init__ (self , exists : bool = False , make : bool = False ):
115+ def __init__ (self , exists : bool = True , make : bool = False ):
117116 self .exists = exists
118117 self .make = make
119118
@@ -124,14 +123,15 @@ def get_metavar(self, param: click.Parameter) -> str: # noqa: ARG002
124123 def convert (self , value : Any , param : Optional [click .Parameter ], ctx : Optional [click .Context ]) -> pathlib .Path :
125124 """Coerce string into a pathlib.Path.is_dir()."""
126125 try :
127- path = pydantic . TypeAdapter ( pydantic . DirectoryPath ). validate_python (value )
128- except pydantic . ValidationError as e :
129- self .fail (message = "\n " . join ( _ [ "msg" ] for _ in e . errors ()) , param = param , ctx = ctx )
126+ path = pathlib . Path (value )
127+ except TypeError :
128+ self .fail (message = "Not a valid path" , param = param , ctx = ctx )
130129
131130 if self .exists and not path .exists ():
132131 self .fail (message = "Directory does not exist" , param = param , ctx = ctx )
133132
134- if self .make :
133+ if not path .exists () and self .make :
134+ _LOG .warning (f"The directory '{ path } ' does not yet exist, creating it.." )
135135 path .mkdir (parents = True , exist_ok = True )
136136
137137 return path .resolve ()
@@ -159,7 +159,7 @@ def _parse_syncer_configuration(
159159 self .fail (message = f"Syncer definition file does not exist at '{ definition_spec } '." , param = param , ctx = ctx )
160160
161161 except toml .TomlDecodeError :
162- log .debug (f"Syncer definition file '{ definition_spec } ' is invalid TOML." , exc_info = True )
162+ _LOG .debug (f"Syncer definition file '{ definition_spec } ' is invalid TOML." , exc_info = True )
163163 self .fail (message = f"Syncer definition file '{ definition_spec } ' is invalid TOML." , param = param , ctx = ctx )
164164
165165 return options
@@ -175,7 +175,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
175175 protocol , _ , definition_spec = value .partition ("://" )
176176
177177 # fmt: off
178- log .debug (f"Registering syncer: { protocol .lower ()} " )
178+ _LOG .debug (f"Registering syncer: { protocol .lower ()} " )
179179 syncer_base_dir = CS_TOOLS_PKG_DIR / "sync" / protocol
180180 syncer_manifest = base .SyncerManifest .model_validate_json (syncer_base_dir .joinpath ("MANIFEST.json" ).read_text ())
181181 syncer_options = self ._parse_syncer_configuration (definition_spec , param = param , ctx = ctx )
@@ -186,7 +186,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
186186 if issubclass (SyncerClass , base .DatabaseSyncer ) and self .models is not None :
187187 syncer_options ["models" ] = self .models
188188
189- log .info (f"Initializing syncer: { SyncerClass } " )
189+ _LOG .info (f"Initializing syncer: { SyncerClass } " )
190190 syncer = SyncerClass (** syncer_options )
191191
192192 # CLEAN UP DATABASE RESOURCES.
@@ -236,7 +236,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
236236 )
237237
238238 except Exception :
239- log .debug (f"Could not coerce all values to '{ self .type_caster } ', { values } " , exc_info = True )
239+ _LOG .debug (f"Could not coerce all values to '{ self .type_caster } ', { values } " , exc_info = True )
240240 self .fail (message = f"Could not coerce all values to '{ self .type_caster } ', { values } " , param = param , ctx = ctx )
241241
242242 return values
0 commit comments