Remove pb SortKind enum and use pb Columns instead

This commit is contained in:
RumovZ 2021-04-09 18:50:30 +02:00
parent bdd257e140
commit 055a5e8a04
5 changed files with 31 additions and 56 deletions

View File

@ -4,6 +4,7 @@ persistent = no
[TYPECHECK]
ignored-classes=
BrowserColumns,
BrowserRow,
FormatTimespanIn,
AnswerCardIn,

View File

@ -12,7 +12,6 @@ SearchNode = _pb.SearchNode
Progress = _pb.Progress
EmptyCardsReport = _pb.EmptyCardsReport
GraphPreferences = _pb.GraphPreferences
BuiltinSort = _pb.SortOrder.Builtin.SortColumn
Preferences = _pb.Preferences
UndoStatus = _pb.UndoStatus
OpChanges = _pb.OpChanges
@ -41,7 +40,7 @@ from anki.config import Config, ConfigManager
from anki.consts import *
from anki.dbproxy import DBProxy
from anki.decks import Deck, DeckConfig, DeckConfigId, DeckId, DeckManager
from anki.errors import AbortSchemaModification, DBError
from anki.errors import AbortSchemaModification, DBError, InvalidInput
from anki.lang import FormatTimeSpan
from anki.media import MediaManager, media_paths_from_col_path
from anki.models import ModelManager, Notetype, NotetypeDict, NotetypeId
@ -506,7 +505,7 @@ class Collection:
def find_cards(
self,
query: str,
order: Union[bool, str, BuiltinSort.V] = False,
order: Union[bool, str, BrowserColumns.Column] = False,
reverse: bool = False,
) -> Sequence[CardId]:
"""Return card ids matching the provided search.
@ -521,10 +520,12 @@ class Collection:
desc and vice versa when reverse is set in the collection config, eg
order="c.ivl asc, c.due desc".
If order is a BuiltinSort value, sort using that builtin sort, eg
col.find_cards("", order=BuiltinSort.DUE)
If order is a BrowserColumns.Column that supports sorting, sort using that
column. All available columns are available through col.all_browser_columns()
or browser.table._model.columns and support sorting unless column.sorting
is set to BrowserColumns.SORTING_NONE.
The reverse argument only applies when a BuiltinSort.V is provided;
The reverse argument only applies when a BrowserColumns.Column is provided;
otherwise the collection config defines whether reverse is set or not.
"""
mode = _build_sort_mode(order, reverse)
@ -535,7 +536,7 @@ class Collection:
def find_notes(
self,
query: str,
order: Union[bool, str, BuiltinSort.V] = False,
order: Union[bool, str, BrowserColumns.Column] = False,
reverse: bool = False,
) -> Sequence[NoteId]:
"""Return note ids matching the provided search.
@ -1123,15 +1124,17 @@ _UndoInfo = Union[_ReviewsUndo, LegacyCheckpoint, None]
def _build_sort_mode(
order: Union[bool, str, BuiltinSort.V],
order: Union[bool, str, BrowserColumns.Column],
reverse: bool,
) -> _pb.SortOrder:
if isinstance(order, str):
return _pb.SortOrder(custom=order)
elif isinstance(order, bool):
if isinstance(order, bool):
if order is True:
return _pb.SortOrder(from_config=_pb.Empty())
else:
return _pb.SortOrder(none=_pb.Empty())
else:
return _pb.SortOrder(builtin=_pb.SortOrder.Builtin(column=order, reverse=reverse))
if order.sorting != BrowserColumns.SORTING_NONE:
return _pb.SortOrder(
builtin=_pb.SortOrder.Builtin(column=order.key, reverse=reverse)
)
raise InvalidInput(f"{order} is not a valid sort order.")

View File

@ -1,7 +1,7 @@
# coding: utf-8
import pytest
from anki.collection import BuiltinSort, Config
from anki.collection import Config
from anki.consts import *
from tests.shared import getEmptyCol, isNearCutoff
@ -124,8 +124,15 @@ def test_findCards():
col.set_config_bool(Config.Bool.BROWSER_SORT_BACKWARDS, True)
col.flush()
assert col.findCards("", order=True)[0] in latestCardIds
assert col.find_cards("", order=BuiltinSort.DUE, reverse=False)[0] == firstCardId
assert col.find_cards("", order=BuiltinSort.DUE, reverse=True)[0] != firstCardId
sort_columns = dict(((c.key, c) for c in col.all_browser_columns()))
assert (
col.find_cards("", order=sort_columns["cardDue"], reverse=False)[0]
== firstCardId
)
assert (
col.find_cards("", order=sort_columns["cardDue"], reverse=True)[0]
!= firstCardId
)
# model
assert len(col.findCards("note:basic")) == 3
assert len(col.findCards("-note:basic")) == 2

View File

@ -796,22 +796,7 @@ message SearchOut {
message SortOrder {
message Builtin {
enum SortColumn {
CARD_MOD = 0;
CARDS = 1;
DECK = 2;
DUE = 3;
EASE = 4;
LAPSES = 5;
INTERVAL = 6;
NOTE_CREATION = 7;
NOTE_MOD = 8;
NOTETYPE = 9;
REPS = 10;
SORT_FIELD = 11;
TAGS = 12;
}
SortColumn column = 1;
string column = 1;
bool reverse = 2;
}
oneof value {

View File

@ -5,13 +5,12 @@ mod browser_table;
mod search_node;
use std::convert::TryInto;
use std::str::FromStr;
use super::Backend;
use crate::{
backend_proto as pb,
backend_proto::{
sort_order::builtin::SortColumn as SortColumnProto, sort_order::Value as SortOrderProto,
},
backend_proto::sort_order::Value as SortOrderProto,
browser_table::Column,
prelude::*,
search::{concatenate_searches, replace_search_node, write_nodes, Node, SortMode},
@ -108,26 +107,6 @@ impl SearchService for Backend {
}
}
impl From<SortColumnProto> for Column {
fn from(kind: SortColumnProto) -> Self {
match kind {
SortColumnProto::CardMod => Column::CardMod,
SortColumnProto::Cards => Column::Cards,
SortColumnProto::Deck => Column::Deck,
SortColumnProto::Due => Column::Due,
SortColumnProto::Ease => Column::Ease,
SortColumnProto::Lapses => Column::Lapses,
SortColumnProto::Interval => Column::Interval,
SortColumnProto::NoteCreation => Column::NoteCreation,
SortColumnProto::NoteMod => Column::NoteMod,
SortColumnProto::Notetype => Column::Notetype,
SortColumnProto::Reps => Column::Reps,
SortColumnProto::SortField => Column::SortField,
SortColumnProto::Tags => Column::Tags,
}
}
}
impl From<Option<SortOrderProto>> for SortMode {
fn from(order: Option<SortOrderProto>) -> Self {
use pb::sort_order::Value as V;
@ -136,7 +115,7 @@ impl From<Option<SortOrderProto>> for SortMode {
V::Custom(s) => SortMode::Custom(s),
V::FromConfig(_) => SortMode::FromConfig,
V::Builtin(b) => SortMode::Builtin {
column: b.column().into(),
column: Column::from_str(&b.column).unwrap_or_default(),
reverse: b.reverse,
},
}