diff options
-rw-r--r-- | ranger/container/tags.py | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/ranger/container/tags.py b/ranger/container/tags.py index 50d5ff72..50525848 100644 --- a/ranger/container/tags.py +++ b/ranger/container/tags.py @@ -5,30 +5,34 @@ from __future__ import (absolute_import, division, print_function) -from os.path import isdir, exists, dirname, abspath, realpath, expanduser, sep +from os.path import exists, abspath, realpath, expanduser, sep import string from ranger import PY3 +from ranger.core.shared import FileManagerAware ALLOWED_KEYS = string.ascii_letters + string.digits + string.punctuation -class Tags(object): +class Tags(FileManagerAware): default_tag = '*' def __init__(self, filename): + # COMPAT: The intent is to get abspath/normpath's behavior of + # collapsing `symlink/..`, abspath is retained for historical reasons + # because the documentation states its behavior isn't necessarily in + # line with normpath's. self._filename = realpath(abspath(expanduser(filename))) - if isdir(dirname(self._filename)) and not exists(self._filename): - open(self._filename, 'w') - self.sync() def __contains__(self, item): return item in self.tags def add(self, *items, **others): + if len(*items) == 0: + return tag = others.get('tag', self.default_tag) self.sync() for item in items: @@ -36,6 +40,8 @@ class Tags(object): self.dump() def remove(self, *items): + if len(*items) == 0: + return self.sync() for item in items: try: @@ -45,6 +51,8 @@ class Tags(object): self.dump() def toggle(self, *items, **others): + if len(*items) == 0: + return tag = others.get('tag', self.default_tag) tag = str(tag) if tag not in ALLOWED_KEYS: @@ -71,8 +79,11 @@ class Tags(object): fobj = open(self._filename, 'r', errors='replace') else: fobj = open(self._filename, 'r') - except OSError: - pass + except OSError as err: + if exists(self._filename): + self.fm.notify(err, bad=True) + else: + self.tags = dict() else: self.tags = self._parse(fobj) fobj.close() @@ -80,8 +91,8 @@ class Tags(object): def dump(self): try: fobj = open(self._filename, 'w') - except OSError: - pass + except OSError as err: + self.fm.notify(err, bad=True) else: self._compile(fobj) fobj.close() |