Monkeytype anki/tags.py

This commit is contained in:
Alan Du 2020-02-26 20:20:48 -05:00
parent ac76c61a2f
commit 78a002d666

View file

@ -13,7 +13,7 @@ from __future__ import annotations
import json import json
import re import re
from typing import Callable, Dict, List, Tuple from typing import Callable, Collection, Dict, List, Optional, Tuple
import anki # pylint: disable=unused-import import anki # pylint: disable=unused-import
from anki import hooks from anki import hooks
@ -29,7 +29,7 @@ class TagManager:
self.col = col self.col = col
self.tags: Dict[str, int] = {} self.tags: Dict[str, int] = {}
def load(self, json_) -> None: def load(self, json_: str) -> None:
self.tags = json.loads(json_) self.tags = json.loads(json_)
self.changed = False self.changed = False
@ -41,7 +41,7 @@ class TagManager:
# Registering and fetching tags # Registering and fetching tags
############################################################# #############################################################
def register(self, tags, usn=None) -> None: def register(self, tags: Collection[str], usn: Optional[int] = None) -> None:
"Given a list of tags, add any missing ones to tag registry." "Given a list of tags, add any missing ones to tag registry."
found = False found = False
for t in tags: for t in tags:
@ -55,7 +55,7 @@ class TagManager:
def all(self) -> List: def all(self) -> List:
return list(self.tags.keys()) return list(self.tags.keys())
def registerNotes(self, nids=None) -> None: def registerNotes(self, nids: Optional[List[int]] = None) -> None:
"Add any missing tags from notes to the tags list." "Add any missing tags from notes to the tags list."
# when called without an argument, the old list is cleared first. # when called without an argument, the old list is cleared first.
if nids: if nids:
@ -94,7 +94,7 @@ class TagManager:
# Bulk addition/removal from notes # Bulk addition/removal from notes
############################################################# #############################################################
def bulkAdd(self, ids, tags, add=True) -> None: def bulkAdd(self, ids: List[int], tags: str, add: bool = True) -> None:
"Add tags in bulk. TAGS is space-separated." "Add tags in bulk. TAGS is space-separated."
newTags = self.split(tags) newTags = self.split(tags)
if not newTags: if not newTags:
@ -137,23 +137,23 @@ class TagManager:
[fix(row) for row in res], [fix(row) for row in res],
) )
def bulkRem(self, ids, tags) -> None: def bulkRem(self, ids: List[int], tags: str) -> None:
self.bulkAdd(ids, tags, False) self.bulkAdd(ids, tags, False)
# String-based utilities # String-based utilities
########################################################################## ##########################################################################
def split(self, tags) -> List[str]: def split(self, tags: str) -> List[str]:
"Parse a string and return a list of tags." "Parse a string and return a list of tags."
return [t for t in tags.replace("\u3000", " ").split(" ") if t] return [t for t in tags.replace("\u3000", " ").split(" ") if t]
def join(self, tags) -> str: def join(self, tags: List[str]) -> str:
"Join tags into a single string, with leading and trailing spaces." "Join tags into a single string, with leading and trailing spaces."
if not tags: if not tags:
return "" return ""
return " %s " % " ".join(tags) return " %s " % " ".join(tags)
def addToStr(self, addtags, tags) -> str: def addToStr(self, addtags: str, tags: str) -> str:
"Add tags if they don't exist, and canonify." "Add tags if they don't exist, and canonify."
currentTags = self.split(tags) currentTags = self.split(tags)
for tag in self.split(addtags): for tag in self.split(addtags):
@ -161,7 +161,7 @@ class TagManager:
currentTags.append(tag) currentTags.append(tag)
return self.join(self.canonify(currentTags)) return self.join(self.canonify(currentTags))
def remFromStr(self, deltags, tags) -> str: def remFromStr(self, deltags: str, tags: str) -> str:
"Delete tags if they exist." "Delete tags if they exist."
def wildcard(pat, str): def wildcard(pat, str):
@ -183,7 +183,7 @@ class TagManager:
# List-based utilities # List-based utilities
########################################################################## ##########################################################################
def canonify(self, tagList) -> List[str]: def canonify(self, tagList: List[str]) -> List[str]:
"Strip duplicates, adjust case to match existing tags, and sort." "Strip duplicates, adjust case to match existing tags, and sort."
strippedTags = [] strippedTags = []
for t in tagList: for t in tagList:
@ -194,7 +194,7 @@ class TagManager:
strippedTags.append(s) strippedTags.append(s)
return sorted(set(strippedTags)) return sorted(set(strippedTags))
def inList(self, tag, tags) -> bool: def inList(self, tag: str, tags: List[str]) -> bool:
"True if TAG is in TAGS. Ignore case." "True if TAG is in TAGS. Ignore case."
return tag.lower() in [t.lower() for t in tags] return tag.lower() in [t.lower() for t in tags]