Implement prelim enums

This commit is contained in:
Mike A. Trethewey
2020-03-12 16:16:15 -07:00
parent d8ea5b0fd0
commit 4b0f30a85f
9 changed files with 160 additions and 40 deletions

View File

@@ -155,9 +155,9 @@
"randomizer.generation.spoiler": "Create Spoiler Log", "randomizer.generation.spoiler": "Create Spoiler Log",
"randomizer.generation.suppressrom": "Do not create patched ROM", "randomizer.generation.suppressrom": "Do Not create Patched ROM",
"randomizer.generation.usestartinventory": "Use starting inventory", "randomizer.generation.usestartinventory": "Use Starting Inventory",
"randomizer.generation.usecustompool": "Use custom item pool", "randomizer.generation.usecustompool": "Use Custom Item Pool",
"randomizer.generation.saveonexit": "Save Settings on Exit", "randomizer.generation.saveonexit": "Save Settings on Exit",
"randomizer.generation.saveonexit.ask": "Ask Me", "randomizer.generation.saveonexit.ask": "Ask Me",

View File

@@ -1,6 +1,8 @@
{ {
"mapshuffle": { "type": "checkbox" }, "keysanity": {
"compassshuffle": { "type": "checkbox" }, "mapshuffle": { "type": "checkbox" },
"smallkeyshuffle": { "type": "checkbox" }, "compassshuffle": { "type": "checkbox" },
"bigkeyshuffle": { "type": "checkbox" } "smallkeyshuffle": { "type": "checkbox" },
"bigkeyshuffle": { "type": "checkbox" }
}
} }

View File

@@ -1,22 +1,24 @@
{ {
"dungeondoorshuffle": { "widgets": {
"type": "selectbox", "dungeondoorshuffle": {
"default": "basic", "type": "selectbox",
"options": [ "default": "basic",
"vanilla", "options": [
"basic", "vanilla",
"crossed" "basic",
] "crossed"
}, ]
"experimental": { "type": "checkbox" }, },
"dungeon_counters": { "experimental": { "type": "checkbox" },
"type": "selectbox", "dungeon_counters": {
"default": "default", "type": "selectbox",
"options": [ "default": "default",
"default", "options": [
"off", "default",
"on", "off",
"pickup" "on",
] "pickup"
]
}
} }
} }

View File

@@ -1,6 +1,8 @@
{ {
"spoiler": { "type": "checkbox" }, "checkboxes": {
"suppressrom": { "type": "checkbox" }, "spoiler": { "type": "checkbox" },
"usestartinventory": { "type": "checkbox" }, "suppressrom": { "type": "checkbox" },
"usecustompool": { "type": "checkbox" } "usestartinventory": { "type": "checkbox" },
"usecustompool": { "type": "checkbox" }
}
} }

View File

@@ -1,10 +1,12 @@
{ {
"saveonexit": { "widgets": {
"type": "selectbox", "saveonexit": {
"options": [ "type": "selectbox",
"ask", "options": [
"always", "ask",
"never" "always",
] "never"
]
}
} }
} }

View File

@@ -1,3 +1,5 @@
{ {
"worlds": { "type": "spinbox" } "widgets": {
"worlds": { "type": "spinbox" }
}
} }

View File

@@ -0,0 +1 @@
aenum

View File

@@ -38,7 +38,7 @@ class BabelFish():
pass pass
# print(langs_filename + " not found for translation!") # print(langs_filename + " not found for translation!")
def translate(self, domain="", key="", subkey=""): #three levels of keys def translate(self, domain="", key="", subkey="", uselang=None): #three levels of keys
# start with nothing # start with nothing
display_text = "" display_text = ""
@@ -94,7 +94,7 @@ class BabelFish():
subkey = " - ".join(tmp) subkey = " - ".join(tmp)
subkey = subkey.strip() subkey = subkey.strip()
my_lang = self.lang_defns[self.locale] #handle for localization my_lang = self.lang_defns[uselang if uselang is not None else self.locale ] #handle for localization
en_lang = self.lang_defns["en"] #handle for English en_lang = self.lang_defns["en"] #handle for English
if domain in my_lang and key in my_lang[domain] and subkey in my_lang[domain][key] and not my_lang[domain][key][subkey] == "": #get localization first if domain in my_lang and key in my_lang[domain] and subkey in my_lang[domain][key] and not my_lang[domain][key][subkey] == "": #get localization first

109
test-options.py Normal file
View File

@@ -0,0 +1,109 @@
from __future__ import annotations
from aenum import Enum, IntEnum, extend_enum
from source.classes.BabelFish import BabelFish
import json
import os
fish = BabelFish(lang="en")
def tokenize(token):
for search,replace in (
('(', ""),
(')',""),
("'", ""),
('-'," "),
('/',""),
("\\","")
):
token = token.replace(search, replace)
tokens = token.split(" ")
i = 0
for check in tokens:
if check.lower() == check:
tokens[i] = ""
i += 1
return " ".join(tokens).replace(" ","")
class Toggle(IntEnum):
Off = 0
On = 1
@classmethod
def from_text(cls, text: str) -> Toggle:
if text.lower() in {"off", "0", "false", "none", "null"}:
return Toggle.Off
else:
return Toggle.On
class Choice(IntEnum):
@classmethod
def from_text(cls, text: str) -> Choice:
for option in cls:
if option.name == text.upper():
return option
raise KeyError(
'KeyError: Could not find option "%s" for "%s", known options are %s' %
(
text,
cls.__name__,
", ".join(option.name for option in cls)
)
)
def create_choice(option_name,option_vals):
option = type(option_name,(Choice,),{})
for name in option_vals:
extend_enum(option,str(name).upper(),len(option))
return option
def load_options(filepath):
theseCompiled = {}
with open(filepath) as widgetsDefn:
filepath = filepath.split(os.sep)
domain = filepath[3]
key = filepath[4]
theseOptions = json.load(widgetsDefn)
for section in theseOptions:
widgets = theseOptions[section]
for widget in widgets:
thisWidget = widgets[widget]
if domain == "randomize":
domain = "randomizer"
if key == "entrando":
key = "entrance"
fish_key = domain + '.' + key + '.' + widget
option_name = tokenize(fish.translate("gui","gui",fish_key,"en"))
if thisWidget["type"] == "checkbox":
theseCompiled[option_name] = Toggle
elif thisWidget["type"] == "selectbox":
option_vals = thisWidget["options"]
theseCompiled[option_name] = create_choice(option_name,option_vals)
return theseCompiled
if __name__ == "__main__":
import argparse
compiledOptions = {}
notebooks = {
"randomize": [ "dungeon", "enemizer", "entrando", "gameoptions", "generation", "item", "multiworld" ]
}
for notebook in notebooks:
for page in notebooks[notebook]:
for filename in ["keysanity","checkboxes","widgets"]:
defn = os.path.join("resources", "app", "gui", notebook, page, filename + ".json")
if os.path.isfile(defn):
compiledOptions.update(load_options(defn))
test = argparse.Namespace()
test.logic = compiledOptions["LogicLevel"].from_text("nologic")
test.mapshuffle = compiledOptions["Maps"].from_text("ON")
try:
test.logic = compiledOptions["LogicLevel"].from_text("overworldglitches")
except KeyError as e:
print(e)
if test.mapshuffle:
print("Map Shuffle is on")
print(test)
for option in compiledOptions:
print("%s: %s" % (option, list(compiledOptions[option])))