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:
StructuralMike
2021-03-27 12:43:00 +01:00
parent 9b06c6d947
commit ac46d4d8aa
2 changed files with 7 additions and 31 deletions

View File

@@ -4,33 +4,12 @@ import random
import urllib.request import urllib.request
import urllib.parse import urllib.parse
import re import re
import yaml
from DungeonRandomizer import parse_cli from DungeonRandomizer import parse_cli
from Main import main as DRMain from Main import main as DRMain
from source.classes.BabelFish import BabelFish 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(): def main():
parser = argparse.ArgumentParser(add_help=False) parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--multi', default=1, type=lambda value: min(max(int(value), 1), 255)) 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): def get_weights(path):
try: try:
if urllib.parse.urlparse(path).scheme: if urllib.parse.urlparse(path).scheme:
yaml = str(urllib.request.urlopen(path).read(), "utf-8") return yaml.load(urllib.request.urlopen(path), Loader=yaml.FullLoader)
else: with open(path, 'r', encoding='utf-8') as f:
with open(path, 'rb') as f: return yaml.load(f, Loader=yaml.FullLoader)
yaml = str(f.read(), "utf-8")
except Exception as e: except Exception as e:
print('Failed to read weights (%s)' % e) raise Exception(f'Failed to read weights file: {e}')
return
return parse_yaml(yaml)
def roll_settings(weights): def roll_settings(weights):
def get_choice(option, root=weights): def get_choice(option, root=weights):

View File

@@ -3,4 +3,5 @@ fast-enum
python-bps-continued python-bps-continued
colorama colorama
aioconsole aioconsole
websockets websockets
pyyaml