Skip to content

Commit

Permalink
feat: Lazily load project submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
Beforerr committed Nov 1, 2024
1 parent c585725 commit a618f66
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ dependencies = [
"hapiclient>=0.2.2",
"pytplot-mpl-temp>=2.2.49",
"viresclient",
"lazy-loader"
]

[project.urls]
Expand Down
53 changes: 6 additions & 47 deletions pyspedas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,70 +85,29 @@
# The code below is needed for backward compatibility, so users can continue to do things
# like "from pyspedas.mms import mec" even after mms has been moved to the projects directory.

import sys
from importlib import import_module

# List of submodules we want to make available under the pyspedas namespace
submodules = ['ace', 'akebono', 'barrel', 'cluster', 'cnofs', 'csswe', 'de2', 'dscovr',
'elfin', 'equator_s', 'erg', 'fast', 'geotail', 'goes', 'image', 'kompsat',
'kyoto', 'lanl', 'maven', 'mica', 'mms', 'noaa', 'omni', 'poes', 'polar', 'psp',
'rbsp', 'secs', 'soho', 'solo', 'st5', 'stereo', 'swarm', 'themis', 'themis.state', 'twins',
'ulysses'
'ulysses', "wind"
]

for submodule in submodules:
# Import the module from the new path
full_module_path = f"pyspedas.projects.{submodule}"
imported_module = import_module(full_module_path)

# Add it to sys.modules under the old path
sys.modules[f"pyspedas.{submodule}"] = imported_module

def __getattr__(name):
if name in submodules:
return import_module(".projects." + name, __name__)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

# This set of imports is still needed for backward compatibility, when using fully-qualified
# routine names in function calls, like "pyspedas.mms.mec()" rather than "pyspedas.projects.mms.mec()"

# Make mission-specific namespaces available under pyspedas
from .projects import ace
from .projects import akebono
from .projects import barrel
from .projects import cluster
from .projects import cnofs
from .projects import csswe
from .projects import de2
from .projects import dscovr
from .projects import elfin
from .projects import equator_s
from .projects import erg
from .projects import fast
from .projects import geotail
from .projects import goes
from .projects import image
from .projects import kompsat
# for backward compatibility
from .projects.kompsat.load import load as kompsat_load
from .projects import kyoto
from .projects import lanl
from .projects import maven
# for backward compatibility
from .projects.maven import maven_load
from .projects import mica
from .projects import mms
from .projects import noaa
from .projects import poes
from .projects import polar
from .projects import psp
from .projects import rbsp
from .projects import secs
from .projects import soho
from .projects import solo
from .projects import st5
from .projects import stereo
from .projects import swarm
from .projects import themis
from .projects import twins
from .projects import ulysses
from . import vires
from .projects import wind

# set up logging/console output
import logging
Expand Down
43 changes: 43 additions & 0 deletions pyspedas/projects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import lazy_loader as lazy

submodules = [
"ace",
"akebono",
"barrel",
"cluster",
"cnofs",
"csswe",
"de2",
"dscovr",
"elfin",
"equator_s",
"erg",
"fast",
"geotail",
"goes",
"image",
"kompsat",
"kyoto",
"lanl",
"maven",
"mica",
"mms",
"noaa",
"omni",
"poes",
"polar",
"psp",
"rbsp",
"secs",
"soho",
"solo",
"st5",
"stereo",
"swarm",
"themis",
"themis.state",
"twins",
"ulysses",
]

__getattr__, __dir__, _ = lazy.attach(__name__, submodules)

0 comments on commit a618f66

Please sign in to comment.