def _edit(args): """Edit the mapping directly using an editor.""" if args: abort(USAGE + 'dtags: too many arguments') try: with TempFile( mode='w+t', delete=False, prefix='mapping.', dir=CFG_DIR ) as tfile: with io.open(MAPPING_FILE, 'rt') as mapping_file: tfile.write(EDIT_HELP_COMMENTS + mapping_file.read()) tfile.flush() except (OSError, IOError) as err: abort('dtags: failed to edit mapping: {}'.format(err), err.errno) else: editor = shlex.split(os.environ.get('EDITOR')) if not editor: abort('dtags: undefined environment variable: ' + style.bad('EDITOR')) try: sp.check_call(editor + [tfile.name]) except sp.CalledProcessError as err: abort('dtags: failed to edit mapping: {}'.format(err.message)) else: mapping, excluded = parse_mapping(tfile.name) save_mapping(mapping) rm_files(tfile.name) if excluded: print('Cleaned the following entries:\n' + excluded + '\n') finish('New entries saved successfully')
def main(): atexit.register(close_stdio) signal.signal(signal.SIGPIPE, signal.SIG_DFL) mapping, excluded = load_mapping() args = sys.argv[1:] if not args: finish(USAGE + DESCRIPTION) head, tail = args[0], args[1:] if head == '--help': finish(USAGE + DESCRIPTION) elif head == '--version': finish('Version ' + VERSION) path = expand(head) tags_removed = set() for tag in tail if tail else mapping.keys(): if path in mapping[tag]: mapping[tag].remove(path) tags_removed.add(tag) if not tags_removed: finish('Nothing to do') save_mapping(mapping) if excluded: print('Cleaned the following invalid entries:\n' + excluded + '\n') finish( style.path(path) + ' ' + ' '.join( style.sign('-') + style.tag(tag) for tag in sorted(tags_removed)))
def main(): atexit.register(close_stdio) signal.signal(signal.SIGPIPE, signal.SIG_DFL) mapping, excluded = load_mapping() args = sys.argv[1:] if not args: finish(USAGE + DESCRIPTION) head, tail = args[0], args[1:] if head == '--help': finish(USAGE + DESCRIPTION) elif head == '--version': finish('Version ' + VERSION) elif head.startswith('-'): abort(USAGE + 'u: invalid argument: ' + style.bad(head)) path = expand(head) tags_removed = set() for tag in tail if tail else mapping.keys(): if path in mapping[tag]: mapping[tag].remove(path) tags_removed.add(tag) if not tags_removed: finish('Nothing to do') save_mapping(mapping) if excluded: print('Cleaned the following invalid entries:\n' + excluded + '\n') finish(style.path(path) + ' ' + ' '.join( style.sign('-') + style.tag(tag) for tag in sorted(tags_removed) ))
def _clean(args): """Clean stale and/or invalid directories.""" if args: abort(USAGE + 'Too many arguments') mapping, excluded = load_mapping() if excluded: save_mapping(mapping) finish('Cleaned the following entries:\n' + excluded) else: finish('Nothing to clean')
def _clean(args): """Clean stale and/or invalid directories.""" if args: abort(USAGE + 'dtags: too many arguments') mapping, excluded = load_mapping() if excluded: save_mapping(mapping) finish('Cleaned the following entries:\n' + excluded) else: save_mapping(mapping) finish('Nothing to clean')
def main(): atexit.register(close_stdio) signal.signal(signal.SIGPIPE, signal.SIG_DFL) args = sys.argv[1:] if not args: finish(USAGE + DESCRIPTION) head, tail = args[0], args[1:] if head == '--help': finish(USAGE + DESCRIPTION) elif head == '--version': finish('Version ' + VERSION) elif head.startswith('-'): abort(USAGE + 't: invalid argument: ' + style.bad(head)) path = expand(head) invalid_path_chars = get_invalid_path_chars(path) if invalid_path_chars: abort('t: directory path {} contains bad characters {}'.format( style.bad(path), style.bad_chars(invalid_path_chars) )) if not os.path.isdir(path): abort('t: invalid directory: ' + style.bad(path)) mapping, excluded = load_mapping() tags_added = set() if not tail: tail.append(os.path.basename(path)) for tag in tail: if not tag[0].isalpha(): abort('t: tag name {} does not start with an alphabet' .format(style.bad(tag))) if ' ' in tag: abort('t: tag name {} contains whitespaces'.format(style.bad(tag))) invalid_tag_chars = get_invalid_tag_chars(tag) if invalid_tag_chars: abort('t: tag name {} contains bad characters {}'.format( style.bad(tag), style.bad_chars(invalid_tag_chars) )) if path not in mapping[tag]: mapping[tag].add(path) tags_added.add(tag) if tags_added or excluded: save_mapping(mapping) if excluded: print('Cleaned the following invalid entries:\n' + excluded + '\n') if not tags_added: finish('Nothing to do') else: finish(style.path(path) + ' ' + ' '.join( style.sign('+') + style.tag(tag) for tag in sorted(tags_added) ))
def main(): atexit.register(close_stdio) signal.signal(signal.SIGPIPE, signal.SIG_DFL) args = sys.argv[1:] if not args: finish(USAGE + DESCRIPTION) head, tail = args[0], args[1:] if head == '--help': finish(USAGE + DESCRIPTION) elif head == '--version': finish('Version ' + VERSION) elif head.startswith('-'): abort(USAGE + 't: invalid argument: ' + style.bad(head)) path = expand(head) invalid_path_chars = get_invalid_path_chars(path) if invalid_path_chars: abort('t: directory path {} contains bad characters {}'.format( style.bad(path), style.bad_chars(invalid_path_chars))) if not os.path.isdir(path): abort('t: invalid directory: ' + style.bad(path)) mapping, excluded = load_mapping() tags_added = set() if not tail: tail.append(os.path.basename(path)) for tag in tail: if not tag[0].isalpha(): abort('t: tag name {} does not start with an alphabet'.format( style.bad(tag))) if ' ' in tag: abort('t: tag name {} contains whitespaces'.format(style.bad(tag))) invalid_tag_chars = get_invalid_tag_chars(tag) if invalid_tag_chars: abort('t: tag name {} contains bad characters {}'.format( style.bad(tag), style.bad_chars(invalid_tag_chars))) if path not in mapping[tag]: mapping[tag].add(path) tags_added.add(tag) if tags_added or excluded: save_mapping(mapping) if excluded: print('Cleaned the following invalid entries:\n' + excluded + '\n') if not tags_added: finish('Nothing to do') else: finish( style.path(path) + ' ' + ' '.join( style.sign('+') + style.tag(tag) for tag in sorted(tags_added)))