Skip to content

Commit 3217990

Browse files
committed
Automatically re-export sub-packages
Using a re-export macro allows us to add functions e.g. to QuantumControlBase and have those automatically be available in QuantumControl.
1 parent 25d8030 commit 3217990

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

src/QuantumControl.jl

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
11
module QuantumControl
2+
include("reexport.jl")
23

34
using QuantumPropagators
45
export propagate, propstep!, propstep
56

67
using QuantumControlBase
7-
export ControlProblem, Objective, WeightedObjective, liouvillian
8-
export discretize, discretize_on_midpoints, getcontrols, get_control_parameters
9-
export get_tlist_midpoints
10-
export propagate_objective
11-
export optimize, @optimize_or_load, optimization_savename, load_optimization
12-
export default_optimization_savename_kwargs
13-
export chain_infohooks
8+
@reexport_members(QuantumControlBase)
149

1510
module Shapes
11+
# we need `QuantumControlBase.Shapes` to be available under a name that
12+
# doesn't clash with `QuantumControl.Shapes` in order for the
13+
# `@reexport_members` macro to work correctly
14+
using QuantumControlBase: Shapes as QuantumControlBase_Shapes
1615
using QuantumControlBase.Shapes
17-
export flattop, box, blackman
16+
include("reexport.jl")
17+
@reexport_members(QuantumControlBase_Shapes)
1818
end
1919

2020
module Functionals
21+
using QuantumControlBase: Functionals as QuantumControlBase_Functionals
2122
using QuantumControlBase.Functionals
22-
export F_ss, J_T_ss, chi_ss!, F_sm, J_T_sm, chi_sm!, F_re, J_T_re, chi_re!
23-
export grad_J_T_sm!
23+
include("reexport.jl")
24+
@reexport_members(QuantumControlBase_Functionals)
2425
end
2526

2627
using Krotov
2728
using GRAPE
2829

29-
using Pkg
30-
using UUIDs
31-
32-
"""Print the versions of the packages constituting QuantumControl."""
33-
function print_versions()
34-
project_toml = Pkg.TOML.parsefile(joinpath(@__DIR__, "..", "Project.toml"))
35-
version = project_toml["version"]
36-
direct_deps = project_toml["deps"]
37-
deps = Pkg.dependencies()
38-
pkg_names = [name for name in keys(direct_deps) if name ["Pkg", "UUIDs"]]
39-
col_width = maximum([length(name) for name in pkg_names])
40-
for name in reverse(pkg_names)
41-
pkginfo = deps[UUIDs.UUID(direct_deps[name])]
42-
if pkginfo.is_tracking_path
43-
println("$(rpad(name, col_width)): $(pkginfo.version) ($(pkginfo.source))")
44-
else
45-
println("$(rpad(name, col_width)): $(pkginfo.version)")
46-
end
47-
end
48-
println("$(rpad("QuantumControl", col_width)): $version")
49-
end
30+
include("print_versions.jl")
5031

5132
end

src/print_versions.jl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Pkg
2+
using UUIDs
3+
4+
"""Print the versions of the packages constituting QuantumControl."""
5+
function print_versions()
6+
project_toml = Pkg.TOML.parsefile(joinpath(@__DIR__, "..", "Project.toml"))
7+
version = project_toml["version"]
8+
direct_deps = project_toml["deps"]
9+
deps = Pkg.dependencies()
10+
pkg_names = [name for name in keys(direct_deps) if name ["Pkg", "UUIDs"]]
11+
col_width = maximum([length(name) for name in pkg_names])
12+
for name in reverse(pkg_names)
13+
pkginfo = deps[UUIDs.UUID(direct_deps[name])]
14+
if pkginfo.is_tracking_path
15+
println("$(rpad(name, col_width)): $(pkginfo.version) ($(pkginfo.source))")
16+
else
17+
println("$(rpad(name, col_width)): $(pkginfo.version)")
18+
end
19+
end
20+
println("$(rpad("QuantumControl", col_width)): $version")
21+
end

src/reexport.jl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# I'm aware of Reexport.jl, but it doesn't work for QuantumControl.jl
2+
#
3+
# For one thing, `@reexport using QuantumControlBase` re-exports not just the
4+
# members of `QuantumControlBase`, but also `QuantumControlBase` itself. Also,
5+
# as far as I can tell `@reexport using QuantumControlBase.Shapes` does not
6+
# work when it's inside a module also called `Shapes`, as we're doing.
7+
#
8+
# Besides, the macro below is pretty trivial
9+
10+
macro reexport_members(modname::Symbol)
11+
mod = getfield(__module__, modname)
12+
member_names = _exported_names(mod)
13+
Expr(:export, member_names...)
14+
end
15+
16+
function _exported_names(m::Module)
17+
return filter!(
18+
x -> (Base.isexported(m, x) && (x != nameof(m))),
19+
names(m; all=true, imported=true)
20+
)
21+
end

0 commit comments

Comments
 (0)