declare variables with some common names as int instead of a union

This commit is contained in:
Damien Elmes 2021-03-26 16:33:53 +10:00
parent 5a094e78fa
commit 8cc6758eb1
3 changed files with 52 additions and 9 deletions

View file

@ -3,7 +3,7 @@
import json import json
import sys import sys
from typing import List from typing import List, Literal, TypedDict
import stringcase import stringcase
@ -11,6 +11,11 @@ strings_json, outfile = sys.argv[1:]
modules = json.load(open(strings_json)) modules = json.load(open(strings_json))
class Variable(TypedDict):
name: str
kind: Literal["Any", "Int"]
def legacy_enum() -> str: def legacy_enum() -> str:
out = ["class LegacyTranslationEnum(enum.Enum):"] out = ["class LegacyTranslationEnum(enum.Enum):"]
for module in modules: for module in modules:
@ -45,12 +50,24 @@ def methods() -> str:
return "\n".join(out) + "\n" return "\n".join(out) + "\n"
def get_arg_types(args: List[str]) -> str: def get_arg_types(args: List[Variable]) -> str:
return ", ".join([f"{stringcase.snakecase(arg)}: FluentVariable" for arg in args])
return ", ".join(
[f"{stringcase.snakecase(arg['name'])}: {arg_kind(arg)}" for arg in args]
)
def get_args(args: List[str]) -> str: def arg_kind(arg: Variable) -> str:
return ", ".join([f'"{arg}": {stringcase.snakecase(arg)}' for arg in args]) 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 = "" out = ""

View file

@ -560,7 +560,8 @@ class CardLayout(QDialog):
cards = tr.card_templates_card_count(count=card_cnt) cards = tr.card_templates_card_count(count=card_cnt)
msg = tr.card_templates_delete_the_as_card_type_and( msg = tr.card_templates_delete_the_as_card_type_and(
template=template["name"], 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): if not askUser(msg):
return return

View file

@ -17,10 +17,25 @@ pub struct Module {
pub struct Translation { pub struct Translation {
pub key: String, pub key: String,
pub text: String, pub text: String,
pub variables: Vec<String>, pub variables: Vec<Variable>,
pub index: usize, 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> { pub fn get_modules(data: &TranslationsByLang) -> Vec<Module> {
let mut output = vec![]; let mut output = vec![];
@ -80,8 +95,8 @@ struct Visitor {
} }
impl Visitor { impl Visitor {
fn into_output(self) -> (String, Vec<String>) { fn into_output(self) -> (String, Vec<Variable>) {
let mut vars: Vec<_> = self.variables.into_iter().collect(); let mut vars: Vec<_> = self.variables.into_iter().map(Into::into).collect();
vars.sort_unstable(); vars.sort_unstable();
(self.text, vars) (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 }
}
}