Skip to content

Commit 9c204d7

Browse files
committed
make Helper.py mostly circular import safe
1 parent 002debd commit 9c204d7

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/Data.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import json
21
import logging
3-
import os
4-
import pkgutil
52

63
from .DataValidation import DataValidation, ValidationError
4+
from .Helpers import load_data_file
75

86
from .hooks.Data import \
97
after_load_game_file, \
@@ -12,16 +10,6 @@
1210
after_load_meta_file
1311

1412

15-
# blatantly copied from the minecraft ap world because why not
16-
def load_data_file(*args) -> dict:
17-
fname = os.path.join("data", *args)
18-
19-
try:
20-
filedata = json.loads(pkgutil.get_data(__name__, fname).decode())
21-
except:
22-
filedata = []
23-
24-
return filedata
2513

2614
def convert_to_list(data, property_name: str) -> list:
2715
if isinstance(data, dict):
@@ -32,17 +20,17 @@ def convert_to_list(data, property_name: str) -> list:
3220
class ManualFile:
3321
filename: str
3422
data_type: dict|list
35-
23+
3624
def __init__(self, filename, data_type):
3725
self.filename = filename
3826
self.data_type = data_type
3927

4028
def load(self):
4129
contents = load_data_file(self.filename)
42-
30+
4331
if not contents and type(contents) != self.data_type:
4432
return self.data_type()
45-
33+
4634
return contents
4735

4836

src/Helpers.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
from BaseClasses import MultiWorld, Item
1+
import os
2+
import pkgutil
3+
import json
4+
5+
from BaseClasses import MultiWorld, Item, Location
26
from typing import Optional, List
37
from worlds.AutoWorld import World
4-
from .Data import category_table
5-
from .Items import ManualItem
6-
from .Locations import ManualLocation
78
from .hooks.Helpers import before_is_category_enabled, before_is_item_enabled, before_is_location_enabled
89

910
from typing import Union
1011

12+
# blatantly copied from the minecraft ap world because why not
13+
def load_data_file(*args) -> dict:
14+
fname = os.path.join("data", *args)
15+
16+
try:
17+
filedata = json.loads(pkgutil.get_data(__name__, fname).decode())
18+
except:
19+
filedata = []
20+
21+
return filedata
22+
1123
def is_option_enabled(multiworld: MultiWorld, player: int, name: str) -> bool:
1224
return get_option_value(multiworld, player, name) > 0
1325

@@ -28,6 +40,7 @@ def clamp(value, min, max):
2840
return value
2941

3042
def is_category_enabled(multiworld: MultiWorld, player: int, category_name: str) -> bool:
43+
from .Data import category_table
3144
"""Check if a category has been disabled by a yaml option."""
3245
hook_result = before_is_category_enabled(multiworld, player, category_name)
3346
if hook_result is not None:
@@ -56,7 +69,7 @@ def is_item_name_enabled(multiworld: MultiWorld, player: int, item_name: str) ->
5669

5770
return is_item_enabled(multiworld, player, item)
5871

59-
def is_item_enabled(multiworld: MultiWorld, player: int, item: ManualItem) -> bool:
72+
def is_item_enabled(multiworld: MultiWorld, player: int, item: Item) -> bool:
6073
"""Check if an item has been disabled by a yaml option."""
6174
hook_result = before_is_item_enabled(multiworld, player, item)
6275
if hook_result is not None:
@@ -72,7 +85,7 @@ def is_location_name_enabled(multiworld: MultiWorld, player: int, location_name:
7285

7386
return is_location_enabled(multiworld, player, location)
7487

75-
def is_location_enabled(multiworld: MultiWorld, player: int, location: ManualLocation) -> bool:
88+
def is_location_enabled(multiworld: MultiWorld, player: int, location: Location) -> bool:
7689
"""Check if a location has been disabled by a yaml option."""
7790
hook_result = before_is_location_enabled(multiworld, player, location)
7891
if hook_result is not None:

src/hooks/Helpers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from typing import Optional
2-
from BaseClasses import MultiWorld
3-
from ..Locations import ManualLocation
4-
from ..Items import ManualItem
5-
2+
from BaseClasses import MultiWorld, Item, Location
63

74
# Use this if you want to override the default behavior of is_option_enabled
85
# Return True to enable the category, False to disable it, or None to use the default behavior
@@ -11,10 +8,10 @@ def before_is_category_enabled(multiworld: MultiWorld, player: int, category_nam
118

129
# Use this if you want to override the default behavior of is_option_enabled
1310
# Return True to enable the item, False to disable it, or None to use the default behavior
14-
def before_is_item_enabled(multiworld: MultiWorld, player: int, item: ManualItem) -> Optional[bool]:
11+
def before_is_item_enabled(multiworld: MultiWorld, player: int, item: Item) -> Optional[bool]:
1512
return None
1613

1714
# Use this if you want to override the default behavior of is_option_enabled
1815
# Return True to enable the location, False to disable it, or None to use the default behavior
19-
def before_is_location_enabled(multiworld: MultiWorld, player: int, location: ManualLocation) -> Optional[bool]:
16+
def before_is_location_enabled(multiworld: MultiWorld, player: int, location: Location) -> Optional[bool]:
2017
return None

0 commit comments

Comments
 (0)