@@ -34,7 +34,7 @@ class RenamePDFsByTitle(object):
3434 def __init__ (self , args ):
3535 self .pdf_files = args .files
3636 self .dry_run = args .dry_run
37- # self.interactive = args.interactive
37+ self .interactive = args .interactive
3838 self .destination = None
3939 if args .destination :
4040 if os .path .isdir (args .destination ):
@@ -49,10 +49,14 @@ def main(self):
4949 title , author = self ._get_info (f )
5050 if title :
5151 g = os .path .join (path , self ._new_filename (title , author ))
52- print ('moving' , '\" %s\" ' % f , 'to' , '\" %s\" ' % g )
52+ print ('--> moving' , '\" %s\" ' % f , 'to' , '\" %s\" ' % g )
5353 if self .dry_run :
5454 continue
55- os .rename (f , g )
55+ try :
56+ os .rename (f , g )
57+ except OSError :
58+ print ('--> error renaming file, maybe it moved?' )
59+ continue
5660 if self .destination is not None :
5761 ret = subprocess .call (['mv' , g , self .destination ])
5862 if ret == 0 :
@@ -88,8 +92,29 @@ def _get_info(self, filename):
8892 title = ti
8993 if au :
9094 author = au
95+ if self .interactive :
96+ title , author = self ._interactive_info_query (filename , title , author )
9197 return title , author
9298
99+ def _interactive_info_query (self , fn , t , a ):
100+ print ('-' * 60 )
101+ print ('Filename:' .ljust (20 ), fn )
102+ print (' * Found (t)itle:' .ljust (20 ), '\" %s\" ' % str (t ))
103+ print (' * Found (a)uthors:' .ljust (20 ), '\" %s\" ' % str (a ))
104+ ri = lambda p : raw_input (p ).lower ().strip ()
105+ ans = ri ('Change (t/a) or open (o) or keep (k)? (t/a/o/k) ' )
106+ while ans != 'k' :
107+ if ans == 't' :
108+ t = raw_input ('New title: ' ).strip ()
109+ elif ans == 'a' :
110+ a = raw_input ('New author string: ' ).strip ()
111+ elif ans == 'o' :
112+ subprocess .call (['open' , fn ])
113+ else :
114+ print ('Bad option, please choose again:' )
115+ ans = ri ('(t/a/o/k) ' )
116+ return t , a
117+ 93118 def _get_metadata (self , h ):
94119 parser = PDFParser (h )
95120 try :
@@ -137,8 +162,8 @@ def _au_last_name(self, name):
137162 help = 'list of pdf files to rename' )
138163 parser .add_argument ('-n' , dest = 'dry_run' , action = 'store_true' ,
139164 help = 'dry-run listing of filename changes' )
140- # parser.add_argument('-i', dest='interactive', action='store_true',
141- # help='interactive mode')
165+ parser .add_argument ('-i' , dest = 'interactive' , action = 'store_true' ,
166+ help = 'interactive mode' )
142167 parser .add_argument ('-d' , '--dest' , dest = 'destination' ,
143168 help = 'destination folder for renamed files' )
144169 args = parser .parse_args ()
0 commit comments