Skip to content

Commit

Permalink
Merge pull request #79 from blaylockbk/blaylockbk/issue78
Browse files Browse the repository at this point in the history
Simplify config file generation on import
  • Loading branch information
blaylockbk authored Dec 19, 2023
2 parents 252df85 + 21e409b commit 775e854
Showing 1 changed file with 61 additions and 31 deletions.
92 changes: 61 additions & 31 deletions goes2go/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## Brian Blaylock
## July 8, 2021

import os
import warnings
import toml
from pathlib import Path
import os

import toml

# =======================================================================
# Load custom xarray accessors
Expand All @@ -16,36 +17,38 @@


# =======================================================================
# Append Path object with my custom expand method so user can use
# environment variables in the config file (e.g., ${HOME}).
# Overload Path object with my custom `expand` method so the user can
# set environment variables in the config file (e.g., ${HOME}).
def _expand(self):
"""
Fully expand and resolve the Path with the given environment variables.
Example
-------
>>> Path('$HOME').expand()
>>> PosixPath('/p/home/blaylock')
PosixPath('/p/home/blaylock')
"""
return Path(os.path.expandvars(self)).expanduser().resolve()


Path.expand = _expand

# =======================================================================
# goes2go configuration file
# Configuration file is save in `~/config/goes2go/config.toml`
_config_path = Path("~/.config/goes2go/config.toml").expand()
_save_dir = str(Path("~/data").expand())
# Location of goes2go configuration file
_config_path = os.getenv("GOES2GO_CONFIG_PATH", "~/.config/goes2go")
_config_path = Path(_config_path).expand()
_config_file = _config_path / "config.toml"

# Default directory goes2go saves model output
# NOTE: The `\\` is an escape character in TOML.
# For Windows paths "C:\\user\\"" needs to be "C:\\\\user\\\\""
_save_dir = str(Path("~/data").expand())
_save_dir = _save_dir.replace("\\", "\\\\")
# For Windows paths, "C:\\user\\"" needs to be "C:\\\\user\\\\""
_save_dir = os.getenv("GOES2GO_SAVE_DIR", "~/data")
_save_dir = Path(_save_dir).expand()
_save_dir = str(_save_dir).replace("\\", "\\\\")

# =======================================================================
# Default TOML Configuration
default_toml = f"""
default_toml = f""" # GOES-2-go Defaults
["default"]
save_dir = "{_save_dir}"
satellite = "noaa-goes16"
Expand All @@ -69,30 +72,57 @@ def _expand(self):
return_as = "xarray"
"""

# =======================================================================
# If a config file isn't found, make one
if not _config_path.exists():
########################################################################
# Load config file (create one if needed)
try:
# Load the goes2go config file
config = toml.load(_config_file)
except:
try:
# Create the goes2go config file
_config_path.mkdir(parents=True, exist_ok=True)
with open(_config_file, "w", encoding="utf-8") as f:
f.write(default_toml)

print(
f" ╭─goes2go──────────────────────────────────────────────╮\n"
f" │ INFO: Created a default config file. │\n"
f" │ You may view/edit goes2go's configuration here: │\n"
f" │ {str(_config_file):^53s}\n"
f" ╰──────────────────────────────────────────────────────╯\n"
)

# Load the new goes2go config file
config = toml.load(_config_file)
except (FileNotFoundError, PermissionError, IOError):
print(
f" ╭─goes2go─────────────────────────────────────────────╮\n"
f" │ WARNING: Unable to create config file │\n"
f" │ {str(_config_file):^53s}\n"
f" │ goes2go will use standard default settings. │\n"
f" │ Consider setting env variable GOES2GO_CONFIG_PATH. │\n"
f" ╰─────────────────────────────────────────────────────╯\n"
)
config = toml.loads(default_toml)


# Expand the full path for `save_dir`
config["default"]["save_dir"] = Path(config["default"]["save_dir"]).expand()

if os.getenv("GOES2GO_SAVE_DIR"):
config["default"]["save_dir"] = Path(os.getenv("GOES2GO_SAVE_DIR")).expand()
print(
f" ╭─────────────────────────────────────────────────╮\n"
f" │ I'm building goes2go's default config file. │\n"
f" ╰╥────────────────────────────────────────────────╯\n"
f" 👷🏻‍♂️"
f" ╭─goes2go──────────────────────────────────────────────╮\n"
f" │ INFO: Overriding the configured save_dir because the │\n"
f" │ environment variable GOES2GO_SAVE_DIR is set to │\n"
f" │ {os.getenv('GOES2GO_SAVE_DIR'):^53s}\n"
f" ╰──────────────────────────────────────────────────────╯\n"
)
_config_path.parent.mkdir(parents=True, exist_ok=True)
with open(_config_path, "w") as f:
toml_string = toml.dump(toml.loads(default_toml), f)
print(f"⚙ Created config file [{_config_path}] with default values.")

# =======================================================================
# Read the config file
config = toml.load(_config_path)

config["default"]["save_dir"] = Path(config["default"]["save_dir"]).expand()

# Merge default settings with overwrite settings for each download method
for i in ["timerange", "latest", "nearesttime"]:
config[i] = {**config["default"], **config[i]}


from goes2go.data import goes_latest, goes_nearesttime, goes_timerange
from goes2go.NEW import GOES
from goes2go.data import goes_nearesttime, goes_latest, goes_timerange

0 comments on commit 775e854

Please # to comment.