<s> import contextlib <EOL> import imp <EOL> import json <EOL> import os <EOL> import sys <EOL> class DottedDict ( dict ) : <EOL> def __getattr__ ( self , attr ) : <EOL> try : <EOL> return self [ attr ] <EOL> except KeyError : <EOL> raise AttributeError ( "<STR_LIT>" . format ( attr ) ) <EOL> __setattr__ = dict . __setitem__ <EOL> __delattr__ = dict . __delitem__ <EOL> def get_json_path ( directory , name ) : <EOL> json_path = os . path . join ( directory , '<STR_LIT>' . format ( name = name ) ) <EOL> if os . path . isfile ( json_path ) : <EOL> return json_path <EOL> class SempaiLoader ( object ) : <EOL> def __init__ ( self , json_path ) : <EOL> self . json_path = json_path <EOL> @ classmethod <EOL> def find_module ( cls , name , path = None ) : <EOL> for d in sys . path : <EOL> json_path = get_json_path ( d , name ) <EOL> if json_path is not None : <EOL> return cls ( json_path ) <EOL> if path is not None : <EOL> name = name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> for d in path : <EOL> json_path = get_json_path ( d , name ) <EOL> if json_path is not None : <EOL> return cls ( json_path ) <EOL> def load_module ( self , name ) : <EOL> if name in sys . modules : <EOL> return sys . modules [ name ] <EOL> mod = imp . new_module ( name ) <EOL> mod . __file__ = self . json_path <EOL> mod . __loader__ = self <EOL> decoder = json . JSONDecoder ( object_hook = DottedDict ) <EOL> try : <EOL> with open ( self . json_path ) as f : <EOL> d = decoder . decode ( f . read ( ) ) <EOL> except ValueError : <EOL> raise ImportError ( <EOL> '<STR_LIT>' . format ( name = self . json_path ) ) <EOL> except : <EOL> raise ImportError ( <EOL> '<STR_LIT>' . format ( name = self . json_path ) ) <EOL> mod . __dict__ . update ( d ) <EOL> sys . modules [ name ] = mod <EOL> return mod <EOL> @ contextlib . contextmanager <EOL> def imports ( ) : <EOL> try : <EOL> sys . meta_path . append ( SempaiLoader ) <EOL> yield <EOL> finally : <EOL> sys . meta_path . remove ( SempaiLoader ) </s>
