Changing yaml output to show hex numbers as hex and not as decimal

This commit is contained in:
codemann8
2023-08-02 15:01:37 -05:00
parent 35f3abc1c1
commit 1566813f81
2 changed files with 10 additions and 1 deletions

View File

@@ -737,6 +737,13 @@ class bidict(dict):
super(bidict, self).__delitem__(key) super(bidict, self).__delitem__(key)
class HexInt(int): pass
def hex_representer(dumper, data):
import yaml
return yaml.ScalarNode('tag:yaml.org,2002:int', f"{data:#0{4}x}")
if __name__ == '__main__': if __name__ == '__main__':
# make_new_base2current() # make_new_base2current()
# read_entrance_data(old_rom=sys.argv[1]) # read_entrance_data(old_rom=sys.argv[1])

View File

@@ -4,6 +4,7 @@ import urllib.parse
import yaml import yaml
from typing import Any from typing import Any
from yaml.representer import Representer from yaml.representer import Representer
from Utils import HexInt, hex_representer
from collections import defaultdict from collections import defaultdict
from pathlib import Path from pathlib import Path
@@ -350,7 +351,7 @@ class CustomSettings(object):
for p in self.player_range: for p in self.player_range:
if p in world.owswaps and len(world.owswaps[p][0]) > 0: if p in world.owswaps and len(world.owswaps[p][0]) > 0:
flips[p] = {} flips[p] = {}
flips[p]['force_flip'] = list(f for f in world.owswaps[p][0] if f < 0x40 or f >= 0x80) flips[p]['force_flip'] = list(HexInt(f) for f in world.owswaps[p][0] if f < 0x40 or f >= 0x80)
flips[p]['force_flip'].sort() flips[p]['force_flip'].sort()
flips[p]['undefined_chance'] = 0 flips[p]['undefined_chance'] = 0
@@ -416,6 +417,7 @@ class CustomSettings(object):
def write_to_file(self, destination): def write_to_file(self, destination):
yaml.add_representer(defaultdict, Representer.represent_dict) yaml.add_representer(defaultdict, Representer.represent_dict)
yaml.add_representer(HexInt, hex_representer)
with open(destination, 'w') as file: with open(destination, 'w') as file:
yaml.dump(self.world_rep, file) yaml.dump(self.world_rep, file)