Skip to content

Commit

Permalink
Merge pull request #279 from GEOS-ESM/hotfix/pchakrab/pickle-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mathomp4 authored May 1, 2024
2 parents 1d54e5e + 809d1ae commit 1afb682
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/mepo/state/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from mepo.config.config_file import ConfigFile
from mepo.state.component import MepoComponent
from mepo.utilities import shellcmd
from mepo.utilities import colors
from mepo.state.exceptions import StateDoesNotExistError, StateAlreadyInitializedError

class MepoState(object):
Expand Down Expand Up @@ -71,12 +72,32 @@ def initialize(cls, project_config_file, directory_style):
complist.append(MepoComponent().to_component(name, comp, directory_style))
cls.write_state(complist)

@staticmethod
def __mepo1_patch():
"""
mepo1 to mepo2 includes reanming of directories
mepo.d/state/state.py -> src/mepo/state/state.py
Since pickle requires that "the class definition must be importable
and live in the same module as when the object was stored", we need to
patch sys.modules to be able to read mepo1 state
"""
print(colors.YELLOW + 'Converting mepo1 state to mepo2 state' + colors.RESET)
import mepo.state
import mepo.utilities
sys.modules['state'] = mepo.state
sys.modules['utilities'] = mepo.utilities

@classmethod
def read_state(cls):
if not cls.exists():
raise StateDoesNotExistError('Error! mepo state does not exist')
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
try:
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
except ModuleNotFoundError:
cls.__mepo1_patch()
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
return allcomps

@classmethod
Expand Down

0 comments on commit 1afb682

Please sign in to comment.