now all errors are given with a messagedialog, instead of a terminal print
[gtkrename.git] / rename.py
blobe3e43aac49b164e5f6d4732258f92d118c19d6b3
1 # gtkrename - a simple file renamer
2 # Copyright (C) 2008, Kristian Rumberg (kristianrumberg@gmail.com)
4 # Permission to use, copy, modify, and/or distribute this software for any
5 # purpose with or without fee is hereby granted, provided that the above
6 # copyright notice and this permission notice appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 import os
17 import re
18 from sets import Set
20 class File:
21 def __init__(self, filestr):
22 self.filename = os.path.basename(filestr)
23 self.dir = os.path.dirname(filestr)
25 def _full_path(self):
26 return os.path.join(self.dir, self.filename)
28 def get_filename(self):
29 return self.filename
31 def invalid_filename(self):
32 return len(self.filename) == 0
34 def filename_matches(self, regexp_obj):
35 if regexp_obj.search( self.get_filename() ):
36 return True
37 else:
38 return False
40 def mv(self, dest_file):
41 os.rename(self._full_path(), dest_file._full_path())
43 def file_exists(self):
44 return os.path.exists(self._full_path())
46 def sed_filename(self, regexp_obj, regexp_str_replace_with):
47 newname = regexp_obj.sub(regexp_str_replace_with, self.filename)
48 return File( os.path.join(self.dir, newname) )
50 class FileList:
51 def __init__(self, filestrlist):
52 self.filelist = []
53 for fs in filestrlist:
54 self.filelist.append( File(fs) )
56 def grep(self, regexp_str):
57 new_filelist = []
58 try:
59 regexp_obj = re.compile(regexp_str)
60 except:
61 return FileList([])
63 for f in self.filelist:
64 if f.filename_matches(regexp_obj):
65 new_filelist.append(f)
67 out_fl = FileList([])
68 out_fl.filelist = new_filelist
69 return out_fl
71 def sed(self, regexp_str_search, regexp_str_replace_with):
72 new_filelist = FileList([])
73 if 0 == len(self.filelist):
74 return new_filelist
76 regexp_obj = re.compile(regexp_str_search)
77 for f in self.filelist:
78 new_filelist.filelist.append( f.sed_filename(regexp_obj, regexp_str_replace_with) )
80 return new_filelist
82 # returns True on success and False on failure
83 def mv(self, dest_filelist, bool_simulate):
84 srclist = self.filelist
85 dstlist = dest_filelist.filelist
87 # make sure all resulting filenames are unique
88 if len(dstlist) != len(list(Set(dstlist))):
89 return False
91 for f in dest_filelist:
92 if f.invalid_filename():
93 return False
94 elif f.file_exists():
95 return False
97 if False == bool_simulate:
98 for s,d in zip(srclist, dstlist):
99 s.mv(d)
101 return True
103 def __iter__(self):
104 return self.filelist.__iter__()