Merge pull request #378 from agentydragon/types-utils

Add some type declarations in utils.py
This commit is contained in:
Damien Elmes 2019-12-23 09:24:20 +10:00 committed by GitHub
commit 30f5183b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,7 @@ import traceback
from contextlib import contextmanager from contextlib import contextmanager
from hashlib import sha1 from hashlib import sha1
from html.entities import name2codepoint from html.entities import name2codepoint
from typing import Any, Iterator, List, Optional, Tuple, Union from typing import Any, Iterable, Iterator, List, Optional, Tuple, Union
from anki.db import DB from anki.db import DB
from anki.lang import _, ngettext from anki.lang import _, ngettext
@ -41,7 +41,7 @@ timeTable = {
"hours": lambda n: ngettext("%s hour", "%s hours", n), "hours": lambda n: ngettext("%s hour", "%s hours", n),
"minutes": lambda n: ngettext("%s minute", "%s minutes", n), "minutes": lambda n: ngettext("%s minute", "%s minutes", n),
"seconds": lambda n: ngettext("%s second", "%s seconds", n), "seconds": lambda n: ngettext("%s second", "%s seconds", n),
} }
inTimeTable = { inTimeTable = {
"years": lambda n: ngettext("in %s year", "in %s years", n), "years": lambda n: ngettext("in %s year", "in %s years", n),
@ -50,7 +50,7 @@ inTimeTable = {
"hours": lambda n: ngettext("in %s hour", "in %s hours", n), "hours": lambda n: ngettext("in %s hour", "in %s hours", n),
"minutes": lambda n: ngettext("in %s minute", "in %s minutes", n), "minutes": lambda n: ngettext("in %s minute", "in %s minutes", n),
"seconds": lambda n: ngettext("in %s second", "in %s seconds", n), "seconds": lambda n: ngettext("in %s second", "in %s seconds", n),
} }
def shortTimeFmt(type: str) -> str: def shortTimeFmt(type: str) -> str:
return { return {
@ -68,7 +68,9 @@ def shortTimeFmt(type: str) -> str:
"seconds": _("%ss"), "seconds": _("%ss"),
}[type] }[type]
def fmtTimeSpan(time: Union[int, float], pad: int = 0, point: int = 0, short: bool = False, inTime: bool = False, unit: int = 99) -> str: def fmtTimeSpan(time: Union[int, float], pad: int = 0, point: int = 0,
short: bool = False, inTime: bool = False,
unit: int = 99) -> str:
"Return a string representing a time span (eg '2 days')." "Return a string representing a time span (eg '2 days')."
(type, point) = optimalPeriod(time, point, unit) (type, point) = optimalPeriod(time, point, unit)
time = convertSecondsTo(time, type) time = convertSecondsTo(time, type)
@ -84,7 +86,8 @@ def fmtTimeSpan(time: Union[int, float], pad: int = 0, point: int = 0, short: bo
timestr = "%%%(a)d.%(b)df" % {'a': pad, 'b': point} timestr = "%%%(a)d.%(b)df" % {'a': pad, 'b': point}
return locale.format_string(fmt % timestr, time) return locale.format_string(fmt % timestr, time)
def optimalPeriod(time: Union[int, float], point: int, unit: int) -> Tuple[str, int]: def optimalPeriod(time: Union[int, float], point: int,
unit: int) -> Tuple[str, int]:
if abs(time) < 60 or unit < 1: if abs(time) < 60 or unit < 1:
type = "seconds" type = "seconds"
point -= 1 point -= 1
@ -157,7 +160,7 @@ def stripHTMLMedia(s: str) -> str:
s = reMedia.sub(" \\1 ", s) s = reMedia.sub(" \\1 ", s)
return stripHTML(s) return stripHTML(s)
def minimizeHTML(s) -> str: def minimizeHTML(s: str) -> str:
"Correct Qt's verbose bold/underline/etc." "Correct Qt's verbose bold/underline/etc."
s = re.sub('<span style="font-weight:600;">(.*?)</span>', '<b>\\1</b>', s = re.sub('<span style="font-weight:600;">(.*?)</span>', '<b>\\1</b>',
s) s)
@ -167,7 +170,7 @@ def minimizeHTML(s) -> str:
'<u>\\1</u>', s) '<u>\\1</u>', s)
return s return s
def htmlToTextLine(s) -> str: def htmlToTextLine(s: str) -> str:
s = s.replace("<br>", " ") s = s.replace("<br>", " ")
s = s.replace("<br />", " ") s = s.replace("<br />", " ")
s = s.replace("<div>", " ") s = s.replace("<div>", " ")
@ -217,7 +220,7 @@ def hexifyID(id) -> str:
def dehexifyID(id) -> int: def dehexifyID(id) -> int:
return int(id, 16) return int(id, 16)
def ids2str(ids: Any) -> str: def ids2str(ids: Iterable[Union[int, str]]) -> str:
"""Given a list of integers, return a string '(int1,int2,...)'.""" """Given a list of integers, return a string '(int1,int2,...)'."""
return "(%s)" % ",".join(str(i) for i in ids) return "(%s)" % ",".join(str(i) for i in ids)
@ -230,7 +233,7 @@ def timestampID(db: DB, table: str) -> int:
t += 1 t += 1
return t return t
def maxID(db: DB) -> Any: def maxID(db: DB) -> int:
"Return the first safe ID to use." "Return the first safe ID to use."
now = intTime(1000) now = intTime(1000)
for tbl in "cards", "notes": for tbl in "cards", "notes":
@ -251,7 +254,7 @@ def base91(num: int) -> str:
# all printable characters minus quotes, backslash and separators # all printable characters minus quotes, backslash and separators
return base62(num, _base91_extra_chars) return base62(num, _base91_extra_chars)
def guid64() -> Any: def guid64() -> str:
"Return a base91-encoded 64bit random number." "Return a base91-encoded 64bit random number."
return base91(random.randint(0, 2**64-1)) return base91(random.randint(0, 2**64-1))
@ -295,7 +298,7 @@ def fieldChecksum(data: str) -> int:
_tmpdir = None _tmpdir = None
def tmpdir() -> Any: def tmpdir() -> str:
"A reusable temp folder which we clean out on each program invocation." "A reusable temp folder which we clean out on each program invocation."
global _tmpdir global _tmpdir
if not _tmpdir: if not _tmpdir:
@ -311,12 +314,12 @@ def tmpdir() -> Any:
pass pass
return _tmpdir return _tmpdir
def tmpfile(prefix: str = "", suffix: str = "") -> Any: def tmpfile(prefix: str = "", suffix: str = "") -> str:
(fd, name) = tempfile.mkstemp(dir=tmpdir(), prefix=prefix, suffix=suffix) (fd, name) = tempfile.mkstemp(dir=tmpdir(), prefix=prefix, suffix=suffix)
os.close(fd) os.close(fd)
return name return name
def namedtmp(name: str, rm: bool = True) -> Any: def namedtmp(name: str, rm: bool = True) -> str:
"Return tmpdir+name. Deletes any existing file." "Return tmpdir+name. Deletes any existing file."
path = os.path.join(tmpdir(), name) path = os.path.join(tmpdir(), name)
if rm: if rm: