diff --git a/src/loading.jl b/src/loading.jl index cda08b993..843bcdfef 100644 --- a/src/loading.jl +++ b/src/loading.jl @@ -153,23 +153,7 @@ function try_to_get_package(model::String) return (pop!(pkgs), true) end -macro load(model_ex) - model_ = string(model_ex) - - # get rid brackets, as in "(MLJModels.Clustering).KMedoids": - model = filter(model_) do c !(c in ['(',')']) end - - if model in string.(MLJBase.finaltypes(Model)) - @info "A model named \"$model\" is already loaded.\n"* - "Nothing new loaded. " - return - end - - pkg, success = try_to_get_package(model) - if !success # pkg is then a message - error(pkg*"Use @load $model pkg=\"PackageName\". ") - end - +function _load(model, pkg, mdl) # get load path: info = METADATA[pkg][model] path = info[:load_path] @@ -179,7 +163,7 @@ macro load(model_ex) # is already loaded into MLJ's namespace): if path_components[1] == "MLJModels" print("import MLJModels ") - __module__.eval(:(import MLJModels)) + mdl.eval(:(import MLJModels)) println('\u2714') end @@ -187,51 +171,41 @@ macro load(model_ex) # this is in MLJModels): pkg_ex = Symbol(pkg) print("import $pkg_ex ") - __module__.eval(:(import $pkg_ex)) + mdl.eval(:(import $pkg_ex)) println('\u2714') # load the model: load_ex = Meta.parse("import $path") print(string(load_ex, " ")) - __module__.eval(load_ex) + mdl.eval(load_ex) println('\u2714') - end +macro load(model_ex) + model_ = string(model_ex) -# TODO: elminate duplicate code (above and below are same after "# get -# load path:" - -macro load(model_ex, pkg_ex) - model = string(model_ex) - pkg = string(pkg_ex.args[2]) + # get rid brackets, as in "(MLJModels.Clustering).KMedoids": + model = filter(model_) do c !(c in ['(',')']) end - # get load path: - info = METADATA[pkg][model] - path = info[:load_path] - path_components = split(path, '.') + if model in string.(MLJBase.finaltypes(Model)) + @info "A model named \"$model\" is already loaded.\n"* + "Nothing new loaded. " + return + end - # if needed, put MLJModels in the calling module's namespace (it - # is already loaded into MLJ's namespace): - if path_components[1] == "MLJModels" - print("import MLJModels ") - __module__.eval(:(import MLJModels)) - println('\u2714') + pkg, success = try_to_get_package(model) + if !success # pkg is then a message + error(pkg*"Use @load $model pkg=\"PackageName\". ") end - # load the package (triggering lazy-load of implementation code if - # this is in MLJModels): - pkg_ex = Symbol(pkg) - print("import $pkg_ex ") - __module__.eval(:(import $pkg_ex)) - println('\u2714') + _load(model, pkg, __module__) +end - # load the model: - load_ex = Meta.parse("import $path") - print(string(load_ex, " ")) - __module__.eval(load_ex) - println('\u2714') +macro load(model_ex, pkg_ex) + model = string(model_ex) + pkg = string(pkg_ex.args[2]) + _load(model, pkg, __module__) end