#NEWLINE# def load_obj_from_path( import_path , prefix = None , ld = dict ( ) ) : #NEWLINE# #INDENT# "\n    import a python object from an import path\n\n    `import_path` - a python import path.  For instance:\n            mypackage.module.func\n        or\n            mypackage.module.class\n    `prefix` (str) - a value to prepend to the import path\n        if it isn't already there.  For instance:\n            load_obj_from_path('module.func', prefix='mypackage')\n        is the same as\n            load_obj_from_path('mypackage.module.func')\n    `ld` (dict) key:value data to pass to the logger if an error occurs\n    " #NEWLINE# if ( prefix and ( not import_path . startswith ( prefix ) ) ) : #NEWLINE# #INDENT# import_path = '.' . join ( [ prefix , import_path ] ) #NEWLINE# #UNINDENT# log . debug ( 'attempting to load a python object from an import path' , extra = dict ( import_path = import_path , ** ld ) ) #NEWLINE# try : #NEWLINE# #INDENT# mod = importlib . import_module ( import_path ) #NEWLINE# return mod #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# ( path , obj_name ) = import_path . rsplit ( '.' , 1 ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# log_raise ( 'import path needs at least 1 period in your import path. An example import path is something like: module.obj' , dict ( import_path = import_path , ** ld ) , InvalidImportPath ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# mod = importlib . import_module ( path ) #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# newpath = path . replace ( prefix , '' , 1 ) . lstrip ( '.' ) #NEWLINE# log . debug ( 'Could not load import path.  Trying a different one' , extra = dict ( oldpath = path , newpath = newpath ) ) #NEWLINE# path = newpath #NEWLINE# mod = importlib . import_module ( path ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# obj = getattr ( mod , obj_name ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# log_raise ( 'object does not exist in given module. Your import path is not properly defined because the given `obj_name` does not exist' , dict ( import_path = path , obj_name = obj_name , ** ld ) , InvalidImportPath ) #NEWLINE# #UNINDENT# return obj