<s> import os <EOL> import imp <EOL> from etcaetera . adapter . base import Adapter <EOL> from etcaetera . utils import format_key <EOL> from etcaetera . constants import ( <EOL> JSON_EXTENSIONS , <EOL> YAML_EXTENSIONS , <EOL> PYTHON_EXTENSIONS <EOL> ) <EOL> class File ( Adapter ) : <EOL> def __init__ ( self , filepath , * args , ** kwargs ) : <EOL> self . filepath = filepath <EOL> super ( File , self ) . __init__ ( * args , ** kwargs ) <EOL> def strictness_check ( self ) : <EOL> if not os . path . exists ( self . filepath ) : <EOL> raise IOError ( "<STR_LIT>" . format ( self . filepath ) ) <EOL> def load ( self , formatter = None ) : <EOL> try : <EOL> fd = open ( self . filepath , '<STR_LIT:r>' ) <EOL> except IOError : <EOL> if self . strict is True : <EOL> raise <EOL> else : <EOL> return <EOL> _ , file_extension = os . path . splitext ( self . filepath ) <EOL> if file_extension . lower ( ) in JSON_EXTENSIONS : <EOL> import json <EOL> self . data = dict ( ( self . format ( k , formatter ) , v ) for k , v in json . load ( fd ) . items ( ) ) <EOL> elif file_extension . lower ( ) in YAML_EXTENSIONS : <EOL> from yaml import load as yload , dump as ydump <EOL> try : <EOL> from yaml import CLoader as Loader <EOL> except ImportError : <EOL> from yaml import Loader <EOL> self . data = dict ( ( self . format ( k , formatter ) , v ) for k , v in yload ( fd , Loader = Loader ) . items ( ) ) <EOL> elif file_extension . lower ( ) in PYTHON_EXTENSIONS : <EOL> mod = imp . load_source ( '<STR_LIT>' , self . filepath ) <EOL> self . data = dict ( ( self . format ( k , formatter ) , v ) for k , v in vars ( mod ) . items ( ) if k . isupper ( ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( file_extension ) ) <EOL> fd . close ( ) </s>
