2015-02-21 18:30:32 -08:00
|
|
|
"""Functions for renaming files based on metadata"""
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Copyright 2012-2014 Anthony Beville
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2012-12-14 21:54:12 -08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2013-01-24 22:14:11 -08:00
|
|
|
import datetime
|
2019-09-03 00:36:36 -07:00
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
|
|
|
|
from pathvalidate import sanitize_filepath
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2018-09-19 13:05:39 -07:00
|
|
|
from . import utils
|
|
|
|
from .issuestring import IssueString
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
class MetadataFormatter(string.Formatter):
|
|
|
|
def __init__(self, smart_cleanup=False):
|
|
|
|
super().__init__()
|
|
|
|
self.smart_cleanup = smart_cleanup
|
|
|
|
|
|
|
|
def format_field(self, value, format_spec):
|
|
|
|
if value is None or value == "":
|
|
|
|
return ""
|
|
|
|
return super().format_field(value, format_spec)
|
|
|
|
|
|
|
|
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
|
|
|
|
auto_arg_index=0):
|
|
|
|
if recursion_depth < 0:
|
|
|
|
raise ValueError('Max string recursion exceeded')
|
|
|
|
result = []
|
|
|
|
lstrip = False
|
|
|
|
for literal_text, field_name, format_spec, conversion in \
|
|
|
|
self.parse(format_string):
|
|
|
|
|
|
|
|
# output the literal text
|
|
|
|
if literal_text:
|
|
|
|
if lstrip:
|
|
|
|
result.append(literal_text.lstrip("-_)}]#"))
|
|
|
|
else:
|
|
|
|
result.append(literal_text)
|
|
|
|
lstrip = False
|
|
|
|
# if there's a field, output it
|
|
|
|
if field_name is not None:
|
|
|
|
# this is some markup, find the object and do
|
|
|
|
# the formatting
|
|
|
|
|
|
|
|
# handle arg indexing when empty field_names are given.
|
|
|
|
if field_name == '':
|
|
|
|
if auto_arg_index is False:
|
|
|
|
raise ValueError('cannot switch from manual field '
|
|
|
|
'specification to automatic field '
|
|
|
|
'numbering')
|
|
|
|
field_name = str(auto_arg_index)
|
|
|
|
auto_arg_index += 1
|
|
|
|
elif field_name.isdigit():
|
|
|
|
if auto_arg_index:
|
|
|
|
raise ValueError('cannot switch from manual field '
|
|
|
|
'specification to automatic field '
|
|
|
|
'numbering')
|
|
|
|
# disable auto arg incrementing, if it gets
|
|
|
|
# used later on, then an exception will be raised
|
|
|
|
auto_arg_index = False
|
|
|
|
|
|
|
|
# given the field_name, find the object it references
|
|
|
|
# and the argument it came from
|
|
|
|
obj, arg_used = self.get_field(field_name, args, kwargs)
|
|
|
|
used_args.add(arg_used)
|
|
|
|
|
|
|
|
# do any conversion on the resulting object
|
|
|
|
obj = self.convert_field(obj, conversion)
|
|
|
|
|
|
|
|
# expand the format spec, if needed
|
|
|
|
format_spec, auto_arg_index = self._vformat(
|
|
|
|
format_spec, args, kwargs,
|
|
|
|
used_args, recursion_depth-1,
|
|
|
|
auto_arg_index=auto_arg_index)
|
|
|
|
|
|
|
|
# format the object and append to the result
|
|
|
|
fmtObj = self.format_field(obj, format_spec)
|
|
|
|
if fmtObj == "" and len(result) > 0 and self.smart_cleanup:
|
|
|
|
lstrip = True
|
|
|
|
result.pop()
|
|
|
|
result.append(fmtObj)
|
|
|
|
|
|
|
|
return ''.join(result), auto_arg_index
|
|
|
|
|
|
|
|
|
2012-12-14 21:54:12 -08:00
|
|
|
class FileRenamer:
|
2015-02-15 02:44:00 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def __init__(self, metadata):
|
|
|
|
self.setMetadata(metadata)
|
2015-02-15 02:44:00 -08:00
|
|
|
self.setTemplate(
|
2019-09-03 00:36:36 -07:00
|
|
|
"{publisher}/{series}/{series} v{volume} #{issue} (of {issueCount}) ({year})")
|
2015-02-12 14:57:46 -08:00
|
|
|
self.smart_cleanup = True
|
|
|
|
self.issue_zero_padding = 3
|
2019-09-03 00:36:36 -07:00
|
|
|
self.move = False
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def setMetadata(self, metadata):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.metdata = metadata
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def setIssueZeroPadding(self, count):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.issue_zero_padding = count
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def setSmartCleanup(self, on):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.smart_cleanup = on
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
def setTemplate(self, template):
|
2015-02-12 14:57:46 -08:00
|
|
|
self.template = template
|
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
def determineName(self, filename, ext=None):
|
|
|
|
class Default(dict):
|
|
|
|
def __missing__(self, key):
|
|
|
|
return "{" + key + "}"
|
|
|
|
md = self.metdata
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
# padding for issue
|
|
|
|
md.issue = IssueString(md.issue).asString(pad=self.issue_zero_padding)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
template = self.template
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
pathComponents = template.split(os.sep)
|
|
|
|
new_name = ""
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
fmt = MetadataFormatter(self.smart_cleanup)
|
|
|
|
for Component in pathComponents:
|
|
|
|
new_name = os.path.join(new_name, fmt.vformat(Component, args=[], kwargs=Default(vars(md))).replace("/", "-"))
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
if ext is None or ext == "":
|
2015-02-13 15:08:07 -08:00
|
|
|
ext = os.path.splitext(filename)[1]
|
2015-02-12 14:57:46 -08:00
|
|
|
|
|
|
|
new_name += ext
|
|
|
|
|
|
|
|
# some tweaks to keep various filesystems happy
|
2015-02-15 03:44:09 -08:00
|
|
|
new_name = new_name.replace(": ", " - ")
|
|
|
|
new_name = new_name.replace(":", "-")
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2019-09-03 00:36:36 -07:00
|
|
|
# remove padding
|
|
|
|
md.issue = IssueString(md.issue).asString()
|
|
|
|
if self.move:
|
|
|
|
return sanitize_filepath(new_name.strip())
|
|
|
|
else:
|
|
|
|
return os.path.basename(sanitize_filepath(new_name.strip()))
|