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