@@ -270,15 +270,17 @@ def main(argv, syserr_handler):
270
270
logger .debug (f"Valid years are: { year_list } " )
271
271
YEAR = r"(" + "|" .join (year_list ) + r")"
272
272
273
+ filenames : list [str ] = args .filenames # pyright: ignore[reportAny]
274
+
273
275
with suppress (QuitException ):
274
- for filename in args . filenames :
276
+ for filename in filenames :
275
277
filename = os .path .abspath (filename )
276
278
if not os .path .exists (filename ):
277
279
raise FatalException (
278
280
filename + " specified on the command line does not exist."
279
281
)
280
282
281
- if os .path .isdir (filename ) and args .recursive :
283
+ if os .path .isdir (filename ) and args .recursive : # pyright: ignore[reportAny]
282
284
new_filename = process_filename (filename , args )
283
285
assert new_filename is not None
284
286
walk_tree (new_filename , args )
@@ -313,8 +315,8 @@ def walk_tree(dirname: str, args):
313
315
def datetime_prefix (args , non_extension : str , filename : str ):
314
316
logger = logging .getLogger ("normfn" )
315
317
316
- def first_not_none ( list : list ):
317
- for item in list :
318
+ def first_not_none [ T ]( lst : list [ T ] ):
319
+ for item in lst :
318
320
if item is not None :
319
321
return item
320
322
@@ -477,7 +479,7 @@ def datetime_prefix(args, non_extension: str, filename: str):
477
479
if number_of_subs == 0 :
478
480
logger .debug ("Didn't find date or time" )
479
481
480
- timetouse = get_timetouse (args , filename )
482
+ timetouse = get_timetouse (args . time_option , filename )
481
483
482
484
newname_with_dash_if_needed = (
483
485
("-" + newname ) if not args .discard_existing_name else ""
@@ -519,7 +521,7 @@ def process_filename(filename: str, args):
519
521
logger .debug ("New filename would be identical, skipping." )
520
522
return original_filename
521
523
522
- validate_move (args , original_filename , filename )
524
+ validate_move (args . force , original_filename , filename )
523
525
524
526
move_it = True
525
527
@@ -532,14 +534,14 @@ def process_filename(filename: str, args):
532
534
new_filename = os .path .join (
533
535
os .path .dirname (original_filename ), new_filename
534
536
)
535
- validate_move (args , original_filename , new_filename )
537
+ validate_move (args . force , original_filename , new_filename )
536
538
filename = new_filename
537
539
else :
538
540
move_it = True if move_it == b"y" else False
539
541
540
542
if move_it :
541
543
if not args .dry_run :
542
- shiftfile (args , original_filename , filename )
544
+ shiftfile (args . undo_log_file , original_filename , filename )
543
545
return filename
544
546
else :
545
547
logger .info (
@@ -569,22 +571,22 @@ def should_exclude(filename: str, basename: str):
569
571
return (match , exclude_pattern )
570
572
571
573
572
- def get_timetouse (args , filename : str ):
574
+ def get_timetouse (time_option : str , filename : str ):
573
575
ctime = datetime .fromtimestamp (os .path .getctime (filename ))
574
576
mtime = datetime .fromtimestamp (os .path .getmtime (filename ))
575
577
576
- if args . time_option == "now" :
578
+ if time_option == "now" :
577
579
timetouse = datetime .now ()
578
- elif args . time_option == "earliest" :
580
+ elif time_option == "earliest" :
579
581
timetouse = min (ctime , mtime )
580
582
else :
581
583
timetouse = max (ctime , mtime )
582
584
583
585
return timetouse
584
586
585
587
586
- def validate_move (args , original_filename : str , filename : str ) -> None :
587
- if os .path .exists (filename ) and not args . force :
588
+ def validate_move (force : bool , original_filename : str , filename : str ) -> None :
589
+ if os .path .exists (filename ) and not force :
588
590
raise FatalException (
589
591
f"Want to move { original_filename .strip ()} to "
590
592
f"{ filename } , but it already exists."
@@ -604,7 +606,7 @@ def rlinput(prompt: str, prefill: str = ""):
604
606
readline .set_startup_hook ()
605
607
606
608
607
- def shiftfile (args , source : str , target : str ):
609
+ def shiftfile (undo_log_file : str , source : str , target : str ):
608
610
logger = logging .getLogger ("normfn" )
609
611
610
612
source = os .path .abspath (source )
@@ -623,17 +625,17 @@ def shiftfile(args, source: str, target: str):
623
625
else :
624
626
raise
625
627
626
- if args . undo_log_file :
627
- check_undo_log_file_header (args )
628
- with open (args . undo_log_file , "a" , encoding = "utf-8" ) as log_file :
628
+ if undo_log_file :
629
+ check_undo_log_file_header (undo_log_file )
630
+ with open (undo_log_file , "a" , encoding = "utf-8" ) as log_file :
629
631
_ = log_file .write (f"# { dt_now } : moving { source } to { target } \n " )
630
632
_ = log_file .write (f"mv { shlex .quote (target )} { shlex .quote (source )} \n " )
631
633
logger .info (source + " moved to " + target )
632
634
633
635
634
- def check_undo_log_file_header (args ):
635
- if not os .path .exists (args . undo_log_file ):
636
- with open (args . undo_log_file , "w" ) as log_file :
636
+ def check_undo_log_file_header (undo_log_file : str ):
637
+ if not os .path .exists (undo_log_file ):
638
+ with open (undo_log_file , "w" ) as log_file :
637
639
wrapper = textwrap .TextWrapper (initial_indent = "# " , subsequent_indent = "# " )
638
640
_ = log_file .write ("#!/bin/sh\n " )
639
641
_ = log_file .write (
@@ -649,9 +651,7 @@ def check_undo_log_file_header(args):
649
651
wrapper .fill (
650
652
"This file contains shell commands which can be run to invert (undo) the effects of "
651
653
"running normfn. They must be run in *reverse order*. You can achieve "
652
- "this by running `tac "
653
- + args .undo_log_file
654
- + " | sh`. If you wish, you can edit "
654
+ f"this by running `tac { undo_log_file } | sh`. If you wish, you can edit "
655
655
"the file first to control which actions are undone."
656
656
)
657
657
+ "\n "
@@ -706,10 +706,10 @@ def insensitiveize(string: str) -> str:
706
706
return "" .join (map (lambda char : ("[" + char .lower () + char .upper () + "]" ), string ))
707
707
708
708
709
- class FatalException (Exception ):
710
- def __init__ (self , value ):
709
+ class FatalException [ T ] (Exception ):
710
+ def __init__ (self , value : T ):
711
711
Exception .__init__ (self , value )
712
- self .value = value
712
+ self .value : T = value
713
713
714
714
def __str__ (self ):
715
715
return repr (self .value )
0 commit comments