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.
21 def __init__(self
, filestr
):
22 self
.filename
= os
.path
.basename(filestr
)
23 self
.dir = os
.path
.dirname(filestr
)
26 return os
.path
.join(self
.dir, self
.filename
)
28 def get_filename(self
):
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() ):
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
) )
51 def __init__(self
, filestrlist
):
53 for fs
in filestrlist
:
54 self
.filelist
.append( File(fs
) )
56 def grep(self
, regexp_str
):
59 regexp_obj
= re
.compile(regexp_str
)
63 for f
in self
.filelist
:
64 if f
.filename_matches(regexp_obj
):
65 new_filelist
.append(f
)
68 out_fl
.filelist
= new_filelist
71 def sed(self
, regexp_str_search
, regexp_str_replace_with
):
72 new_filelist
= FileList([])
73 if 0 == len(self
.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
) )
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
))):
91 for f
in dest_filelist
:
92 if f
.invalid_filename():
97 if False == bool_simulate
:
98 for s
,d
in zip(srclist
, dstlist
):
104 return self
.filelist
.__iter
__()