def rename_dirs(tags, state, pattern): '''Rename Directory, "Rename dir: $1" &Pattern:, text''' dirname = safe_name(format_value(tags, pattern)) old_path = tags['__dirpath'] dirpath = path.join(path.dirname(old_path), dirname) if 'rename_dirs' in state: state['rename_dirs'][old_path] = dirpath else: state['rename_dirs'] = {old_path: dirpath}
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 load_macro_from_name(name): filename = os.path.join(ACTIONDIR, safe_name(name) + '.action') return Macro(filename)
def validate(text, to=None, chars=None): from puddleobjects import safe_name if chars is None: return safe_name(text, to=to) else: return safe_name(text, chars, to=to)
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)