@@ -39,9 +39,50 @@ class DataPackage(typing.TypedDict):
39
39
class WorldSource (typing .NamedTuple ):
40
40
path : str # typically relative path from this module
41
41
is_zip : bool = False
42
+ relative : bool = True # relative to regular world import folder
42
43
43
44
def __repr__ (self ):
44
- return f"{ self .__class__ .__name__ } ({ self .path } , is_zip={ self .is_zip } )"
45
+ return f"{ self .__class__ .__name__ } ({ self .path } , is_zip={ self .is_zip } , relative={ self .relative } )"
46
+
47
+ @property
48
+ def resolved_path (self ) -> str :
49
+ if self .relative :
50
+ return os .path .join (folder , self .path )
51
+ return self .path
52
+
53
+ def load (self ) -> bool :
54
+ try :
55
+ if self .is_zip :
56
+ importer = zipimport .zipimporter (self .resolved_path )
57
+ if hasattr (importer , "find_spec" ): # new in Python 3.10
58
+ spec = importer .find_spec (os .path .basename (self .path ).rsplit ("." , 1 )[0 ])
59
+ mod = importlib .util .module_from_spec (spec )
60
+ else : # TODO: remove with 3.8 support
61
+ mod = importer .load_module (os .path .basename (self .path ).rsplit ("." , 1 )[0 ])
62
+
63
+ mod .__package__ = f"worlds.{ mod .__package__ } "
64
+ mod .__name__ = f"worlds.{ mod .__name__ } "
65
+ sys .modules [mod .__name__ ] = mod
66
+ with warnings .catch_warnings ():
67
+ warnings .filterwarnings ("ignore" , message = "__package__ != __spec__.parent" )
68
+ # Found no equivalent for < 3.10
69
+ if hasattr (importer , "exec_module" ):
70
+ importer .exec_module (mod )
71
+ else :
72
+ importlib .import_module (f".{ self .path } " , "worlds" )
73
+ return True
74
+
75
+ except Exception as e :
76
+ # A single world failing can still mean enough is working for the user, log and carry on
77
+ import traceback
78
+ import io
79
+ file_like = io .StringIO ()
80
+ print (f"Could not load world { self } :" , file = file_like )
81
+ traceback .print_exc (file = file_like )
82
+ file_like .seek (0 )
83
+ import logging
84
+ logging .exception (file_like .read ())
85
+ return False
45
86
46
87
47
88
# find potential world containers, currently folders and zip-importable .apworld's
@@ -58,35 +99,7 @@ def __repr__(self):
58
99
# import all submodules to trigger AutoWorldRegister
59
100
world_sources .sort ()
60
101
for world_source in world_sources :
61
- try :
62
- if world_source .is_zip :
63
- importer = zipimport .zipimporter (os .path .join (folder , world_source .path ))
64
- if hasattr (importer , "find_spec" ): # new in Python 3.10
65
- spec = importer .find_spec (world_source .path .split ("." , 1 )[0 ])
66
- mod = importlib .util .module_from_spec (spec )
67
- else : # TODO: remove with 3.8 support
68
- mod = importer .load_module (world_source .path .split ("." , 1 )[0 ])
69
-
70
- mod .__package__ = f"worlds.{ mod .__package__ } "
71
- mod .__name__ = f"worlds.{ mod .__name__ } "
72
- sys .modules [mod .__name__ ] = mod
73
- with warnings .catch_warnings ():
74
- warnings .filterwarnings ("ignore" , message = "__package__ != __spec__.parent" )
75
- # Found no equivalent for < 3.10
76
- if hasattr (importer , "exec_module" ):
77
- importer .exec_module (mod )
78
- else :
79
- importlib .import_module (f".{ world_source .path } " , "worlds" )
80
- except Exception as e :
81
- # A single world failing can still mean enough is working for the user, log and carry on
82
- import traceback
83
- import io
84
- file_like = io .StringIO ()
85
- print (f"Could not load world { world_source } :" , file = file_like )
86
- traceback .print_exc (file = file_like )
87
- file_like .seek (0 )
88
- import logging
89
- logging .exception (file_like .read ())
102
+ world_source .load ()
90
103
91
104
lookup_any_item_id_to_name = {}
92
105
lookup_any_location_id_to_name = {}
0 commit comments