<s> import sys <EOL> from pathlib import Path <EOL> from . pycloader import get_module <EOL> def is_package ( full_name ) : <EOL> module_path = '<STR_LIT:/>' . join ( full_name . split ( '<STR_LIT:.>' ) ) <EOL> for load_path in sys . path : <EOL> load_path = Path ( load_path ) . absolute ( ) <EOL> target_path = Path ( '<STR_LIT>' <EOL> % ( load_path , module_path ) ) <EOL> if target_path . exists ( ) : <EOL> return True <EOL> return False <EOL> class Loader ( object ) : <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> def load_module ( self , full_name ) : <EOL> if full_name in sys . modules : <EOL> return sys . modules [ full_name ] <EOL> absolute_file_path = str ( self . path . absolute ( ) ) <EOL> mod = get_module ( full_name , absolute_file_path ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( absolute_file_path ) <EOL> print ( "<STR_LIT>" ) <EOL> mod . __file__ = absolute_file_path <EOL> mod . __loader__ = self <EOL> mod . __name__ = full_name <EOL> if is_package ( full_name ) : <EOL> mod . __path__ = [ ] <EOL> mod . __package__ = full_name <EOL> else : <EOL> mod . __package__ = full_name . rpartition ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> sys . modules [ full_name ] = mod <EOL> return mod <EOL> def find_file ( full_name ) : <EOL> target_files = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> module_path = '<STR_LIT:/>' . join ( full_name . split ( '<STR_LIT:.>' ) ) <EOL> for load_path in sys . path : <EOL> load_path = Path ( load_path ) . absolute ( ) <EOL> for target_file in target_files : <EOL> target_path = Path ( target_file % ( '<STR_LIT>' <EOL> % ( load_path , module_path ) ) ) <EOL> if target_path . exists ( ) : <EOL> return target_path <EOL> class Importer ( object ) : <EOL> def find_module ( self , fullname , path = None ) : <EOL> file_path = find_file ( fullname ) <EOL> if file_path : <EOL> pyc_file_path = Path ( file_path . with_suffix ( '<STR_LIT>' ) ) <EOL> if pyc_file_path . exists ( ) : <EOL> pyc_file_mtime = pyc_file_path . stat ( ) . st_mtime <EOL> mochi_file_mtime = file_path . stat ( ) . st_mtime <EOL> if mochi_file_mtime > pyc_file_mtime : <EOL> return Loader ( file_path ) <EOL> else : <EOL> return Loader ( file_path ) <EOL> def set_importer ( ) : <EOL> sys . meta_path . insert ( <NUM_LIT:0> , Importer ( ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT>' ) </s>
