Update mystery yaml parser to a maintained package module
Tested on both local files and url-files and works great.
This commit is contained in:
35
Mystery.py
35
Mystery.py
@@ -4,33 +4,12 @@ import random
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import re
|
||||
import yaml
|
||||
|
||||
from DungeonRandomizer import parse_cli
|
||||
from Main import main as DRMain
|
||||
from source.classes.BabelFish import BabelFish
|
||||
|
||||
def parse_yaml(txt):
|
||||
def strip(s):
|
||||
s = s.strip()
|
||||
return '' if not s else s.strip('"') if s[0] == '"' else s.strip("'") if s[0] == "'" else s
|
||||
ret = {}
|
||||
indents = {len(txt) - len(txt.lstrip(' ')): ret}
|
||||
for line in txt.splitlines():
|
||||
line = re.sub(r'#.*', '', line)
|
||||
if not line:
|
||||
continue
|
||||
name, val = line.split(':', 1)
|
||||
val = strip(val)
|
||||
spaces = len(name) - len(name.lstrip(' '))
|
||||
name = strip(name)
|
||||
if val:
|
||||
indents[spaces][name] = val
|
||||
else:
|
||||
newdict = {}
|
||||
indents[spaces][name] = newdict
|
||||
indents[spaces+2] = newdict
|
||||
return ret
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
parser.add_argument('--multi', default=1, type=lambda value: min(max(int(value), 1), 255))
|
||||
@@ -106,15 +85,11 @@ def main():
|
||||
def get_weights(path):
|
||||
try:
|
||||
if urllib.parse.urlparse(path).scheme:
|
||||
yaml = str(urllib.request.urlopen(path).read(), "utf-8")
|
||||
else:
|
||||
with open(path, 'rb') as f:
|
||||
yaml = str(f.read(), "utf-8")
|
||||
return yaml.load(urllib.request.urlopen(path), Loader=yaml.FullLoader)
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return yaml.load(f, Loader=yaml.FullLoader)
|
||||
except Exception as e:
|
||||
print('Failed to read weights (%s)' % e)
|
||||
return
|
||||
|
||||
return parse_yaml(yaml)
|
||||
raise Exception(f'Failed to read weights file: {e}')
|
||||
|
||||
def roll_settings(weights):
|
||||
def get_choice(option, root=weights):
|
||||
|
||||
Reference in New Issue
Block a user