class CustomRenameTemplate(Template):

    def __init__(self, *args, **kws):
        super(CustomRenameTemplate, self).__init__(*args, **kws)
self.orig_template = self.template
self.template = self.template.lower()

def do_template_based_capitalization(self, mapping):
    matches = self.pattern.findall(self.orig_template)
for match in matches:
    keyword = match[self.pattern.groupindex['braced'] - 1]
if keyword[0] in uppercase:
    if keyword == keyword.upper():
        mapping[keyword.lower()] = mapping[keyword.lower()].upper()
else:
    mapping[keyword.lower()] = mapping[keyword.lower()].capitalize()

def safe_substitute(self, *args, **kws):
    mapping = get_mapping_from_args(*args, **kws)
self.do_template_based_capitalization(mapping)
return super(CustomRenameTemplate, self).safe_substitute(mapping)

def substitute(self, *args, **kws):
    mapping = get_mapping_from_args(*args, **kws)
self.do_template_based_capitalization(mapping)
return super(CustomRenameTemplate, self).substitute(mapping)
Python String Templating with Case Sensitivity