remove old fmtTimeSpan() and associated strings

This commit is contained in:
Damien Elmes 2020-02-23 18:47:16 +10:00
parent 22bb4607c9
commit e73157285e
5 changed files with 19 additions and 99 deletions

View file

@ -12,7 +12,7 @@ import anki
from anki.consts import *
from anki.lang import _, ngettext
from anki.rsbackend import FString
from anki.utils import fmtTimeSpan, ids2str
from anki.utils import ids2str
# Card stats
##########################################################################
@ -631,8 +631,12 @@ group by day order by day)"""
),
)
i: List[str] = []
self._line(i, _("Average interval"), fmtTimeSpan(avg * 86400))
self._line(i, _("Longest interval"), fmtTimeSpan(max_ * 86400))
self._line(
i, _("Average interval"), self.col.backend.format_time_span(avg * 86400)
)
self._line(
i, _("Longest interval"), self.col.backend.format_time_span(max_ * 86400)
)
return txt + self._lineTbl(i)
def _ivls(self) -> Tuple[list, int]:

View file

@ -6,7 +6,6 @@ from __future__ import annotations
# some add-ons expect json to be in the utils module
import json # pylint: disable=unused-import
import locale
import math
import os
import platform
import random
@ -21,10 +20,9 @@ import traceback
from contextlib import contextmanager
from hashlib import sha1
from html.entities import name2codepoint
from typing import Iterable, Iterator, List, Optional, Tuple, Union
from typing import Iterable, Iterator, List, Optional, Union
from anki.db import DB
from anki.lang import ngettext
_tmpdir: Optional[str]
@ -37,86 +35,6 @@ def intTime(scale: int = 1) -> int:
return int(time.time() * scale)
timeTable = {
"years": lambda n: ngettext("%s year", "%s years", n),
"months": lambda n: ngettext("%s month", "%s months", n),
"days": lambda n: ngettext("%s day", "%s days", n),
"hours": lambda n: ngettext("%s hour", "%s hours", n),
"minutes": lambda n: ngettext("%s minute", "%s minutes", n),
"seconds": lambda n: ngettext("%s second", "%s seconds", n),
}
inTimeTable = {
"years": lambda n: ngettext("in %s year", "in %s years", n),
"months": lambda n: ngettext("in %s month", "in %s months", n),
"days": lambda n: ngettext("in %s day", "in %s days", n),
"hours": lambda n: ngettext("in %s hour", "in %s hours", n),
"minutes": lambda n: ngettext("in %s minute", "in %s minutes", n),
"seconds": lambda n: ngettext("in %s second", "in %s seconds", n),
}
def fmtTimeSpan(
time: Union[int, float],
pad: int = 0,
point: int = 0,
inTime: bool = False,
unit: int = 99,
) -> str:
"Return a string representing a time span (eg '2 days')."
(type, point) = optimalPeriod(time, point, unit)
time = convertSecondsTo(time, type)
if not point:
time = int(round(time))
if inTime:
fmt = inTimeTable[type](_pluralCount(time, point))
else:
fmt = timeTable[type](_pluralCount(time, point))
timestr = "%%%(a)d.%(b)df" % {"a": pad, "b": point}
return locale.format_string(fmt % timestr, time)
def optimalPeriod(time: Union[int, float], point: int, unit: int) -> Tuple[str, int]:
if abs(time) < 60 or unit < 1:
type = "seconds"
point -= 1
elif abs(time) < 3600 or unit < 2:
type = "minutes"
elif abs(time) < 60 * 60 * 24 or unit < 3:
type = "hours"
elif abs(time) < 60 * 60 * 24 * 30 or unit < 4:
type = "days"
elif abs(time) < 60 * 60 * 24 * 365 or unit < 5:
type = "months"
point += 1
else:
type = "years"
point += 1
return (type, max(point, 0))
def convertSecondsTo(seconds: Union[int, float], type: str) -> float:
if type == "seconds":
return seconds
elif type == "minutes":
return seconds / 60
elif type == "hours":
return seconds / 3600
elif type == "days":
return seconds / 86400
elif type == "months":
return seconds / 2592000
elif type == "years":
return seconds / 31536000
assert False
def _pluralCount(time: Union[int, float], point: int) -> int:
if point:
return 2
return math.floor(time)
# Locale
##############################################################################

View file

@ -1,8 +0,0 @@
# coding: utf-8
from anki.utils import fmtTimeSpan
def test_fmtTimeSpan():
assert fmtTimeSpan(5) == "5 seconds"
assert fmtTimeSpan(5, inTime=True) == "in 5 seconds"

View file

@ -25,7 +25,7 @@ from anki.lang import _, ngettext
from anki.models import NoteType
from anki.notes import Note
from anki.rsbackend import FString
from anki.utils import fmtTimeSpan, htmlToTextLine, ids2str, intTime, isMac, isWin
from anki.utils import htmlToTextLine, ids2str, intTime, isMac, isWin
from aqt import AnkiQt, gui_hooks
from aqt.editor import Editor
from aqt.exporting import ExportDialog
@ -74,7 +74,7 @@ class FindDupesDialog:
class DataModel(QAbstractTableModel):
def __init__(self, browser):
def __init__(self, browser: Browser):
QAbstractTableModel.__init__(self)
self.browser = browser
self.col = browser.col
@ -82,8 +82,8 @@ class DataModel(QAbstractTableModel):
self.activeCols = self.col.conf.get(
"activeCols", ["noteFld", "template", "cardDue", "deck"]
)
self.cards = []
self.cardObjs = {}
self.cards: List[int] = []
self.cardObjs: Dict[int, Card] = {}
def getCard(self, index):
id = self.cards[index.row()]
@ -320,7 +320,7 @@ class DataModel(QAbstractTableModel):
return _("(new)")
elif c.type == 1:
return _("(learning)")
return fmtTimeSpan(c.ivl * 86400)
return self.col.backend.format_time_span(c.ivl * 86400)
elif type == "cardEase":
if c.type == 0:
return _("(new)")

View file

@ -33,8 +33,14 @@ def stripSounds(text) -> str:
return aqt.mw.col.backend.strip_av_tags(text)
def fmtTimeSpan(time, pad=0, point=0, inTime=False, unit=99):
print("fmtTimeSpan() has become col.backend.format_time_span()")
return aqt.mw.col.backend.format_time_span(time)
def install_pylib_legacy():
anki.utils.bodyClass = bodyClass
anki.utils.fmtTimeSpan = fmtTimeSpan
anki.sound._soundReg = r"\[sound:(.*?)\]"
anki.sound.allSounds = allSounds
anki.sound.stripSounds = stripSounds