def analyse(request): from common.files import getAllFilesRecursive from common.tags import DESIRED_TAGS from common.html import genFolderCode def checkDir(d): return path.isdir(d) _get = request.GET dir_name = path.realpath(_get.get('directory', getcwd())) willMakeChanges = lambda : _get.get('change', "") == CHANGE_TRUE_VALUE title = makeTitle (dir_name) d = {'title' : title} if checkDir(dir_name): directories = getAllFilesRecursive(dir_name, 'mp3') if directories: print directories folder_template = path.join(TEMPLATE_FOLDER, 'id3info', 'folder.tpl') files = genFolderCode(directories, folder_template) else: d['error'] = True files = "Directory not found: %s" % dir_name d['content'] = files d.update(dict(_get.items())) # reuse the defined variables on _get return render_to_response('id3info/main.tpl', d)
def getFiles(request): from common.files import getAllFilesRecursive, makeRenamingChanges from common.html import genFolderCode import re def checkDir(d): return path.isdir(d) def updateDictWithReplacement(regex, replacement, raw_dict): """Returns a new dictionary with the 'files' key containing the tuple (old, replaced)""" files = [] any_changes = False for f in raw_dict['files']: if type(f) is dict: folder, had_changes = updateDictWithReplacement(regex, replacement, f) any_changes = any_changes or had_changes # if any is True, remain! files.append(folder) else: replaced = regex.sub(replacement, f) if replaced != f: any_changes = True files.append([f, replaced]) else: files.append(f) return {'basename' : raw_dict['basename'], 'files' : files}, any_changes _get = request.GET dir_name = path.realpath(_get.get('directory', getcwd())) # p = 'extension'; exec('%s = _get.get("%s", "")' % (p,p)) # as that is not allowed... extension = _get.get("extension", "") search = _get.get("search", "") replacement = _get.get("replacement", "") willMakeChanges = lambda : _get.get('change', "") == CHANGE_TRUE_VALUE d = {'title' : makeTitle(dir_name)} if checkDir(dir_name): raw_dict = getAllFilesRecursive(dir_name) parsed_files, any_changes = updateDictWithReplacement(re.compile(search), replacement, raw_dict) if willMakeChanges(): makeRenamingChanges(parsed_files) elif any_changes: # don't appear "Apply Changes" if already changing d['can_change'] = any_changes d['change_value'] = CHANGE_TRUE_VALUE folder_template = path.join(TEMPLATE_FOLDER, 'regex_renom', 'folder.tpl') files = genFolderCode(parsed_files, folder_template, willMakeChanges()) else: d['error'] = True files = "Directory not found: %s" % dir_name d['content'] = files d.update(dict(_get.items())) # reuse the defined variables on _get return render_to_response('regex_renom/main.tpl', d)