@@ -128,8 +128,7 @@ def text(self):
128128 def __add_text (self , data , mode = 'w' , encoding = None , errors = None ):
129129 """ Allows to write/append text to the file. """
130130 if not isinstance (data , str ):
131- raise TypeError ("data must be str, not %s" %
132- data .__class__ .__name__ )
131+ raise TypeError (f"data must be str, not { data .__class__ .__name__ } " )
133132 with self .open (mode = mode , encoding = encoding , errors = errors ) as f :
134133 return f .write (str (data ))
135134
@@ -375,13 +374,11 @@ class CredentialsPath(Path):
375374 :param secret: secret
376375 """
377376 def __new__ (cls , * parts , ** kwargs ):
378- p = Path (* parts )
379- path , fn = (str (p ), "creds.txt" ) if p .suffix == "" else (p .dirname , p .filename )
380- kw = {'create' : True , 'expand' : True }
381- path = Path (* parts , ** kw )
382- kwargs ['exist_ok' ] = True
383- kwargs ['create' ] = False
384- self = super (CredentialsPath , cls ).__new__ (cls , str (path ), fn , ** kwargs )
377+ kwargs ['create' ], kwargs ['expand' ] = False , True
378+ self = super (CredentialsPath , cls ).__new__ (cls , * parts , ** kwargs )
379+ if self .exists () and self .is_dir ():
380+ self = self .joinpath ("creds.txt" )
381+ self .dirname .mkdir (exist_ok = True )
385382 self .id = kwargs .get ("id" ) or ""
386383 self .secret = kwargs .get ("secret" ) or ""
387384 d = kwargs .get ("delimiter" ) or ":"
@@ -416,12 +413,13 @@ def ask(self, id="Username:", secret="Password:"):
416413 while self .id == "" :
417414 self .id = user_input (id_prompt , required = True )
418415 if id_pattern and not re .search (id_pattern , self .id ):
419- raise ValueError ("Bad %s" % id_alias )
416+ raise ValueError (f "Bad { id_alias } " )
420417 self .secret = ""
421418 try :
422419 self .secret = (getpass if is_dict (sec_pattern ) else getrepass )(sec_prompt , None , sec_pattern ).strip ()
423420 except ValueError as e :
424- raise e ("Non-compliant %s:\n - %s" % (sec_alias , "\n - " .join (e .errors ))) if hasattr (e , "errors" ) else e
421+ d = "\n - "
422+ raise e (f"Non-compliant { sec_alias } :{ d } { d .join (e .errors )} " ) if hasattr (e , "errors" ) else e
425423
426424 def load (self , delimiter = ":" ):
427425 """ This loads credentials from the path taking a delimiter (default: ":") into account. """
@@ -433,18 +431,18 @@ def load(self, delimiter=":"):
433431 try :
434432 self .id , self .secret = c .split (delimiter )
435433 except ValueError :
436- raise ValueError ("Bad delimiter '%s ' ; it should not be a character present in the identifier or secret" % \
437- delimiter )
434+ raise ValueError (f "Bad delimiter '{ delimiter } ' ; it shall be a character not present in the identifier or "
435+ "secret" )
438436
439437 def save (self , delimiter = ":" ):
440438 """ This saves the defined credentials to the path taking a delimiter (default: ":") into account. """
441439 if self .id == "" and self .secret == "" :
442440 return
443441 self .touch (0o600 )
444442 if delimiter in self .id or delimiter in self .secret :
445- raise ValueError ("Bad delimiter '%s ' ; it should not be a character present in the identifier or secret" % \
446- delimiter )
447- self .write_text ("%s%s%s" % ( self .id , delimiter , self .secret ) )
443+ raise ValueError (f "Bad delimiter '{ delimiter } ' ; it shall be a character not present in the identifier or "
444+ "secret" )
445+ self .write_text (f" { self .id } { delimiter } { self .secret } " )
448446
449447
450448class MirrorPath (Path ):
@@ -604,7 +602,7 @@ def __init__(self, path, remove_cache=False):
604602 if e == ".pyc" and p .absolute ().dirname .parts [- 1 ] == "__pycache__" :
605603 parts = p .filename .split ("." )
606604 if len (parts ) == 3 and re .match (r".?python\-?[23]\d" , parts [- 2 ]):
607- _cached .append (str (p .absolute ().dirname .parent .joinpath ("%s.py" % parts [0 ])))
605+ _cached .append (str (p .absolute ().dirname .parent .joinpath (f" { parts [0 ]} .py" )))
608606 p = PythonPath (p )
609607 if p .loaded :
610608 self .modules .append (p .module )
0 commit comments