mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
declare variables with some common names as int instead of a union
This commit is contained in:
parent
5a094e78fa
commit
8cc6758eb1
3 changed files with 52 additions and 9 deletions
|
@ -3,7 +3,7 @@
|
|||
|
||||
import json
|
||||
import sys
|
||||
from typing import List
|
||||
from typing import List, Literal, TypedDict
|
||||
|
||||
import stringcase
|
||||
|
||||
|
@ -11,6 +11,11 @@ strings_json, outfile = sys.argv[1:]
|
|||
modules = json.load(open(strings_json))
|
||||
|
||||
|
||||
class Variable(TypedDict):
|
||||
name: str
|
||||
kind: Literal["Any", "Int"]
|
||||
|
||||
|
||||
def legacy_enum() -> str:
|
||||
out = ["class LegacyTranslationEnum(enum.Enum):"]
|
||||
for module in modules:
|
||||
|
@ -45,12 +50,24 @@ def methods() -> str:
|
|||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
def get_arg_types(args: List[str]) -> str:
|
||||
return ", ".join([f"{stringcase.snakecase(arg)}: FluentVariable" for arg in args])
|
||||
def get_arg_types(args: List[Variable]) -> str:
|
||||
|
||||
return ", ".join(
|
||||
[f"{stringcase.snakecase(arg['name'])}: {arg_kind(arg)}" for arg in args]
|
||||
)
|
||||
|
||||
|
||||
def get_args(args: List[str]) -> str:
|
||||
return ", ".join([f'"{arg}": {stringcase.snakecase(arg)}' for arg in args])
|
||||
def arg_kind(arg: Variable) -> str:
|
||||
if arg["kind"] == "Int":
|
||||
return "int"
|
||||
else:
|
||||
return "FluentVariable"
|
||||
|
||||
|
||||
def get_args(args: List[Variable]) -> str:
|
||||
return ", ".join(
|
||||
[f'"{arg["name"]}": {stringcase.snakecase(arg["name"])}' for arg in args]
|
||||
)
|
||||
|
||||
|
||||
out = ""
|
||||
|
|
|
@ -560,7 +560,8 @@ class CardLayout(QDialog):
|
|||
cards = tr.card_templates_card_count(count=card_cnt)
|
||||
msg = tr.card_templates_delete_the_as_card_type_and(
|
||||
template=template["name"],
|
||||
cards=cards,
|
||||
# unlike most cases, 'cards' is a string in this message
|
||||
cards=cards, # type: ignore[arg-type]
|
||||
)
|
||||
if not askUser(msg):
|
||||
return
|
||||
|
|
|
@ -17,10 +17,25 @@ pub struct Module {
|
|||
pub struct Translation {
|
||||
pub key: String,
|
||||
pub text: String,
|
||||
pub variables: Vec<String>,
|
||||
pub variables: Vec<Variable>,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Serialize)]
|
||||
pub struct Variable {
|
||||
name: String,
|
||||
kind: VariableKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Serialize)]
|
||||
enum VariableKind {
|
||||
Int,
|
||||
// Float,
|
||||
// Number,
|
||||
// String,
|
||||
Any,
|
||||
}
|
||||
|
||||
pub fn get_modules(data: &TranslationsByLang) -> Vec<Module> {
|
||||
let mut output = vec![];
|
||||
|
||||
|
@ -80,8 +95,8 @@ struct Visitor {
|
|||
}
|
||||
|
||||
impl Visitor {
|
||||
fn into_output(self) -> (String, Vec<String>) {
|
||||
let mut vars: Vec<_> = self.variables.into_iter().collect();
|
||||
fn into_output(self) -> (String, Vec<Variable>) {
|
||||
let mut vars: Vec<_> = self.variables.into_iter().map(Into::into).collect();
|
||||
vars.sort_unstable();
|
||||
(self.text, vars)
|
||||
}
|
||||
|
@ -118,3 +133,13 @@ impl Visitor {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Variable {
|
||||
fn from(name: String) -> Self {
|
||||
let kind = match name.as_str() {
|
||||
"cards" | "notes" | "count" | "amount" => VariableKind::Int,
|
||||
_ => VariableKind::Any,
|
||||
};
|
||||
Variable { name, kind }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue