Skip to content

Commit

Permalink
First draft of dysh shell
Browse files Browse the repository at this point in the history
  • Loading branch information
tchamberlin committed Nov 15, 2023
1 parent 7e61ea4 commit bf8b69d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Source = "https://github.com/GreenBankObservatory/dysh"
#dysh = "dysh.dysh:main"
[project.scripts]
fdr = "dysh.util.files:main_cli"
dysh = "dysh.shell:main"

[tool.hatch.version]
path = "src/dysh/__init__.py"
Expand Down
4 changes: 3 additions & 1 deletion src/dysh/fits/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
"""Classes and functions for importing SDFITS files"""
__all__ = ["gbtfitsload", "sdfitsload", "gb20mfitsload"]
from dysh.fits.gb20mfitsload import GB20MFITSLoad # noqa
from dysh.fits.gbtfitsload import GBTFITSLoad # noqa
from dysh.fits.sdfitsload import SDFITSLoad # noqa
88 changes: 88 additions & 0 deletions src/dysh/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import argparse
from pathlib import Path
from typing import List

import IPython
from traitlets.config import Config

import dysh.fits
from dysh import __version__
from dysh.fits.sdfitsload import SDFITSLoad

# TODO: Derive URLs from pyproject.toml?
BANNER = f"""--------------------------------------------------------------------------
Welcome to Dysh v{__version__}
Example usage: https://dysh.readthedocs.io/en/latest/example.html
Bug repots: https://github.com/GreenBankObservatory/dysh/issues
For help with a Dysh routine from the command line,
use the builtin 'help'. e.g.:
help(GBTFITSLoad)
The following items have been made globally available for convenience:
{{user_ns_str}}
To suppress this banner, start with --no-banner flag
--------------------------------------------------------------------------
"""


def parse_args():
parser = argparse.ArgumentParser(
description=(
"Dysh interactive shell.\n\n All CLI arguments other than those defined below are passed through "
"to ipython; see $ ipython --help for more details"
)
)
parser.add_argument("paths", help="FITS file paths to load initially", nargs="*", type=Path)
parser.add_argument("-p", "--profile", help="The IPython profile to use", default="dysh")
parser.add_argument("-L", "--fits-loader", help="The SDFITS loader class name to use", default="GBTFITSLoad")
parser.add_argument(
"--colors", help="Set the color scheme", choices=["NoColor", "Neutral", "Linux", "LightBG"], default="LightBG"
)
return parser.parse_known_args()


def init_shell(colors: str, profile: str | Path, *ipython_args, sdfits_files=None):
c = Config()
import numpy as np
import pandas as pd
from astropy.io import fits
from astropy.table import Table

from dysh.fits.gbtfitsload import GBTFITSLoad

user_ns = {"pd": pd, "np": np, "GBTFITSLoad": GBTFITSLoad, "Table": Table, "fits": fits}
if sdfits_files:
user_ns["sdfits_files"] = sdfits_files

c.BaseIPythonApplication.profile = profile
c.InteractiveShell.colors = colors
c.InteractiveShell.banner2 = BANNER.format(
user_ns_str="\n".join(f"{' '*8}{k} (from {v.__name__})" for k, v in user_ns.items())
)
IPython.start_ipython(ipython_args, config=c, user_ns=user_ns)


def get_fits_loader_class(loader_class_name: str):
try:
return getattr(dysh.fits, loader_class_name)
except AttributeError as error:
raise NotImplementedError(f"No known SDFITS Loader {loader_class_name!r}") from error


def open_sdfits_files(paths: List[Path], loader_class_name="GBTFITSLoad") -> List[SDFITSLoad]:
loader_class = get_fits_loader_class(loader_class_name)
return [loader_class(path) for path in paths]


def main():
args, remaining_args = parse_args()
sdfits_files = open_sdfits_files(args.paths, args.fits_loader)
init_shell(colors=args.colors, profile=args.profile, *remaining_args, sdfits_files=sdfits_files)


if __name__ == "__main__":
main()

0 comments on commit bf8b69d

Please sign in to comment.