Skip to content

Instantly share code, notes, and snippets.

@lahwaacz
Created January 26, 2013 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahwaacz/4642562 to your computer and use it in GitHub Desktop.
Save lahwaacz/4642562 to your computer and use it in GitHub Desktop.
import itertools
languages = ["en", "cs", "eng", "cze"] # list of language codes
language_separators = [".", "-"] # list of separators (between file name and extension)
subtitle_extensions = ["srt", "sub"] # list of subtitle extensions
def rename_file(old, new):
p("rename %s to %s" % (old, new))
stat = os.stat(old)
os.rename(old, new)
os.utime(new, (stat.st_atime, stat.st_mtime))
def copy_file(old, new):
p("copy %s to %s" % (old, new))
shutil.copyfile(old, new)
shutil.copystat(old, new)
def symlink_file(target, name):
p("symlink %s to %s" % (name, target))
os.symlink(target, name)
def _with_subtitles(old, new, action="rename"):
if action == "rename":
func = rename_file
elif action == "copy":
func = copy_file
elif action == "delete":
func = delete_file
elif action == "symlink":
func = symlink_file
else:
raise ValueError("Unknown action: " + action)
func(old, new)
base_old = os.path.splitext(old)[0]
base_new = os.path.splitext(new)[0]
for ext in itertools.product(language_separators, languages, ["."], subtitle_extensions):
ext = "".join(ext)
if os.path.isfile(base_old + ext):
func(base_old + ext, base_new + ext)
class Renamer(object):
"""Deals with renaming of files
"""
def __init__(self, filename):
self.filename = os.path.abspath(filename)
def newName(self, newName, force = False, leave_symlink = False):
"""Renames a file, keeping the path the same.
"""
filepath, filename = os.path.split(self.filename)
filename, _ = os.path.splitext(filename)
newpath = os.path.join(filepath, newName)
if os.path.isfile(newpath):
# If the destination exists, raise exception unless force is True
if not force:
raise OSError("File %s already exists, not forcefully renaming %s" % (
newpath, self.filename))
_with_subtitles(self.filename, newpath, action="rename")
# Leave a symlink behind if configured to do so
if leave_symlink:
_with_subtitles(newpath, self.filename)
self.filename = newpath
def newPath(self, new_path = None, new_fullpath = None, force = False, always_copy = False, always_move = False, leave_symlink = False, create_dirs = True, getPathPreview = False):
"""Moves the file to a new path.
If it is on the same partition, it will be moved (unless always_copy is True)
If it is on a different partition, it will be copied, and the original
only deleted if always_move is True.
If the target file already exists, it will raise OSError unless force is True.
If it was moved, a symlink will be left behind with the original name
pointing to the file's new destination if leave_symlink is True.
"""
if always_copy and always_move:
raise ValueError("Both always_copy and always_move cannot be specified")
if (new_path is None and new_fullpath is None) or (new_path is not None and new_fullpath is not None):
raise ValueError("Specify only new_dir or new_fullpath")
if new_path is not None:
old_dir, old_filename = os.path.split(self.filename)
# Join new filepath to old one (to handle realtive dirs)
new_dir = os.path.abspath(os.path.join(old_dir, new_path))
# Join new filename onto new filepath
new_fullpath = os.path.join(new_dir, old_filename)
else:
old_dir, old_filename = os.path.split(self.filename)
# Join new filepath to old one (to handle realtive dirs)
new_fullpath = os.path.abspath(os.path.join(old_dir, new_fullpath))
new_dir = os.path.dirname(new_fullpath)
if len(Config['move_files_fullpath_replacements']) > 0:
p("Before custom full path replacements: %s" % (new_fullpath))
new_fullpath = applyCustomFullpathReplacements(new_fullpath)
new_dir = os.path.dirname(new_fullpath)
p("New path: %s" % new_fullpath)
if getPathPreview:
return new_fullpath
if create_dirs:
p("Creating directory %s" % new_dir)
try:
os.makedirs(new_dir)
except OSError, e:
if e.errno != 17:
raise
if os.path.isfile(new_fullpath):
# If the destination exists, raise exception unless force is True
if not force:
raise OSError("File %s already exists, not forcefully moving %s" % (
new_fullpath, self.filename))
if same_partition(self.filename, new_dir):
if always_copy:
# Same partition, but forced to copy
_with_subtitles(self.filename, new_fullpath, action="copy")
else:
# Same partition, just rename the file to move it
_with_subtitles(self.filename, new_fullpath, action="rename")
# Leave a symlink behind if configured to do so
if leave_symlink:
_with_subtitles(new_fullpath, self.filename, action="symlink")
else:
# File is on different partition (different disc), copy it
_with_subtitles(self.filename, new_fullpath, action="move")
if always_move:
# Forced to move file, we just trash old file
p("Deleting %s" % (self.filename))
delete_file(self.filename)
# Leave a symlink behind if configured to do so
if leave_symlink:
_with_subtitles(new_fullpath, self.filename, action="symlink")
self.filename = new_fullpath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment