+class tagview(object):
+ def __init__(self, manga):
+ self.manga = manga
+ self.profile = manga.profile
+
+ def add(self, *tags):
+ mt = self.getall(self.profile)
+ ctags = mt.setdefault((self.manga.libnm, self.manga.id), set())
+ ctags |= set(tags)
+ self.save(self.profile, mt)
+
+ def remove(self, *tags):
+ mt = self.getall(self.profile)
+ ctags = mt.get((self.manga.libnm, self.manga.id), set())
+ ctags -= set(tags)
+ if len(ctags) < 1:
+ try:
+ del mt[self.manga.libnm, self.manga.id]
+ except KeyError:
+ pass
+ self.save(self.profile, mt)
+
+ def __iter__(self):
+ return iter(self.getall(self.profile).get((self.manga.libnm, self.manga.id), set()))
+
+ @staticmethod
+ def getall(profile):
+ ret = {}
+ try:
+ with profile.file("tags") as fp:
+ for words in splitlines(fp):
+ libnm, id = words[0:2]
+ tags = set(words[2:])
+ ret[libnm, id] = tags
+ except IOError:
+ pass
+ return ret
+
+ @staticmethod
+ def save(profile, m):
+ with profile.file("tags", "W") as fp:
+ for (libnm, id), tags in m.items():
+ fp.write(consline(libnm, id, *tags) + "\n")
+
+ @staticmethod
+ def bytag(profile, tag):
+ try:
+ with profile.file("tags") as fp:
+ for words in splitlines(fp):
+ libnm, id = words[0:2]
+ tags = words[2:]
+ if tag in tags:
+ yield profile.getmanga(libnm, id)
+ except IOError:
+ pass
+