def add_extension(fn, tags, addext = None, extension=None): if not addext: return fn elif addext and (extension is not None): return fn + os.path.extsep + encode_fn(extension) else: return fn + os.path.extsep + encode_fn(tags["__ext"])
def add_extension(fn, tags, addext=None, extension=None): if not addext: return fn elif addext and (extension is not None): return fn + os.path.extsep + encode_fn(extension) else: return fn + os.path.extsep + encode_fn(tags["__ext"])
def rename_file(audio, tags): """If tags(a dictionary) contains a PATH key, then the file in self.taginfo[row] is renamed based on that. If successful, tags is returned(with the new filename as a key) otherwise {} is returned.""" test_audio = MockTag() test_audio.filepath = audio.filepath renamed = False if PATH in tags: test_audio.filepath = to_string(tags[PATH]) if FILENAME in tags: test_audio.filename = safe_name(to_string(tags[FILENAME])) if EXTENSION in tags: test_audio.ext = safe_name(to_string(tags[EXTENSION])) if DIRNAME in tags: newdir = safe_name(encode_fn(tags[DIRNAME])) newdir = os.path.join(os.path.dirname(audio.dirpath), newdir) if rename_dir(audio.filepath, audio.dirpath, newdir): audio.dirpath = newdir test_audio.dirpath = newdir renamed = True elif DIRPATH in tags: newdir = encode_fn(to_string(tags[DIRPATH])) if rename_dir(audio.filepath, audio.dirpath, newdir): audio.dirpath = newdir renamed = True test_audio.dirpath = newdir if rename(audio.filepath, test_audio.filepath): audio.filepath = test_audio.filepath renamed = True return renamed
def tag_dir(m_tags, pattern, r_tags, state = None): '''Tag to Dir, "Tag->Dir: $1" &Pattern (can be relative path), text, %artist% - %album%''' if state is None: state = {'tag_dir': set()} elif 'tag_dir' not in state: state['tag_dir'] = set() if r_tags.dirpath in state['tag_dir']: return dirpath = r_tags.dirpath if pattern.endswith(u'/') and len(pattern) > 1: pattern = pattern[:-1] filename = tag_to_filename(pattern, m_tags, r_tags, False, state, True) if filename: state['tag_dir'].add(encode_fn(filename)) return {DIRPATH: filename}
def tag_dir(m_tags, pattern, r_tags, state=None): '''Tag to Dir, "Tag->Dir: $1" &Pattern (can be relative path), text, %artist% - %album%''' if state is None: state = {'tag_dir': set()} elif 'tag_dir' not in state: state['tag_dir'] = set() if r_tags.dirpath in state['tag_dir']: return dirpath = r_tags.dirpath if pattern.endswith(u'/') and len(pattern) > 1: pattern = pattern[:-1] filename = tag_to_filename(pattern, m_tags, r_tags, False, state, True) if filename: state['tag_dir'].add(encode_fn(filename)) return {DIRPATH: filename}
def tag_to_filename(pattern, m_tags, r_tags, ext=True, state=None, is_dir=False): if not pattern: return if state is None: state = {} tags = m_tags tf = findfunc.parsefunc path_join = os.path.join if state is None: state = {} path_seps, text = tf(pattern, tags, state=state, path_sep=u"/") start_pos = 0 new_dirs = [] if path_seps: for p in path_seps: if start_pos != p: new_dirs.append(safe_name(text[start_pos:p])) start_pos = p new_dirs.append(safe_name(text[start_pos + 1:])) else: new_dirs = [safe_name(text)] if os.path.isabs(pattern): return add_extension(u'/' + u'/'.join(map(safe_name, new_dirs)), tags, ext) else: subdirs = new_dirs count = len(path_seps) dirpath = r_tags.dirpath new_fn = encode_fn(path_join(*new_dirs)) if new_fn.startswith('./'): return add_extension(path_join(dirpath, new_fn[len('./'):]), tags, ext) elif new_fn.startswith('../'): parent = dirpath while new_fn.startswith('../'): parent = os.path.dirname(parent) new_fn = new_fn[len('../'):] return add_extension(path_join(parent, new_fn), tags, ext) elif count and u'..' not in subdirs: subdirs = dirpath.split('/') if count >= len(subdirs): parent = [''] else: if is_dir: parent = subdirs[:-(count + 1)] else: parent = subdirs[:-(count)] else: if is_dir: dirpath = os.path.dirname(r_tags.dirpath) parent = dirpath.split('/') if not parent[0]: parent.insert(0, '/') return add_extension(os.path.join(*(parent + [new_fn])), tags, ext)