Skip to content

Commit

Permalink
Merge pull request #170 from djhoese/dep-env-var
Browse files Browse the repository at this point in the history
Deprecate PPP_CONFIG_DIR for specifying config path
  • Loading branch information
pnuu authored Nov 29, 2024
2 parents 38f5716 + 805d2cf commit d582531
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 71 deletions.
65 changes: 18 additions & 47 deletions pyorbital/tests/test_tlefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import unittest
from contextlib import suppress
from pathlib import Path
from tempfile import mkstemp
from unittest import mock

import pytest
Expand Down Expand Up @@ -286,41 +287,20 @@ def test_check_is_platform_supported_unknown(caplog):
assert expected3 in logoutput_lines[2]


#def test_get_config_path_ppp_config_set_but_not_pyorbital_future(mock, caplog, monkeypatch):
# """Test getting the config path."""
# monkeypatch.setenv("SATPY_CONFIG_PATH", "/path/to/satpy/etc")
# monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir")
#
# with caplog.at_level(logging.WARNING):
# res = _get_config_path()
#
# log_output = ("The use of PPP_CONFIG_DIR is no longer supported! " +
# "Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!")
# assert log_output in caplog.text
# assert res == PKG_CONFIG_DIR


def test_get_config_path_ppp_config_set_but_not_pyorbital_is_deprecated(caplog, monkeypatch):
"""Test getting the config path.
Here the case is tested when the new Pyorbital environment variable is not
set but the deprecated (old) Satpy/MPOP one is set.
"""
from pyorbital.tlefile import _get_config_path
def test_get_config_path_ppp_config_set_but_not_pyorbital_future(caplog, monkeypatch):
"""Test getting the config path."""
from pyorbital.tlefile import PKG_CONFIG_DIR, _get_config_path

monkeypatch.setenv("SATPY_CONFIG_PATH", "/path/to/satpy/etc")
monkeypatch.setenv("PPP_CONFIG_DIR", "/path/to/old/mpop/config/dir")

with caplog.at_level(logging.WARNING):
res = _get_config_path()

assert res == "/path/to/old/mpop/config/dir"

log_output = ("The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!" +
" Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!")

log_output = ("The use of PPP_CONFIG_DIR is no longer supported! " +
"Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!")
assert log_output in caplog.text
assert res == PKG_CONFIG_DIR


def test_get_config_path_ppp_config_set_and_pyorbital(caplog, monkeypatch):
Expand Down Expand Up @@ -452,51 +432,42 @@ def test_from_line(self):

def test_from_file(self):
"""Test reading and parsing from a file."""
from os import close, remove, write
from tempfile import mkstemp

from pyorbital.tlefile import Tle

filehandle, filename = mkstemp()
try:
write(filehandle, "\n".join([LINE0, LINE1, LINE2]).encode("utf-8"))
close(filehandle)
os.write(filehandle, "\n".join([LINE0, LINE1, LINE2]).encode("utf-8"))
os.close(filehandle)
tle = Tle("ISS (ZARYA)", filename)
self.check_example(tle)
finally:
remove(filename)
os.remove(filename)

def test_from_file_with_hyphenated_platform_name(self):
"""Test reading and parsing from a file with a slightly different name."""
from os import close, remove, write
from tempfile import mkstemp

from pyorbital.tlefile import Tle

filehandle, filename = mkstemp()
try:
write(filehandle, NOAA19_3LINES.encode("utf-8"))
close(filehandle)
os.write(filehandle, NOAA19_3LINES.encode("utf-8"))
os.close(filehandle)
tle = Tle("NOAA-19", filename)
assert tle.satnumber == "33591"
finally:
remove(filename)
os.remove(filename)

def test_from_file_with_no_platform_name(self):
"""Test reading and parsing from a file with a slightly different name."""
from os import close, remove, write
from tempfile import mkstemp

from pyorbital.tlefile import Tle

filehandle, filename = mkstemp()
try:
write(filehandle, NOAA19_2LINES.encode("utf-8"))
close(filehandle)
os.write(filehandle, NOAA19_2LINES.encode("utf-8"))
os.close(filehandle)
tle = Tle("NOAA-19", filename)
assert tle.satnumber == "33591"
finally:
remove(filename)
os.remove(filename)

def test_from_mmam_xml(self):
"""Test reading from an MMAM XML file."""
Expand Down Expand Up @@ -769,7 +740,7 @@ def test_init(self):

def test_update_db(self):
"""Test updating database with new data."""
from pyorbital.tlefile import ISO_TIME_FORMAT, SATID_TABLE, table_exists
from pyorbital.tlefile import ISO_TIME_FORMAT, SATID_TABLE, _utcnow, table_exists

# Get the column names
columns = [col.strip() for col in
Expand Down Expand Up @@ -804,7 +775,7 @@ def test_update_db(self):
assert data[0][1] == "\n".join((LINE1, LINE2))
# Date when the data were added should be close to current time
date_added = datetime.datetime.strptime(data[0][2], ISO_TIME_FORMAT)
now = datetime.datetime.utcnow()
now = _utcnow()
assert (now - date_added).total_seconds() < 1.0
# Source of the data
assert data[0][3] == "foo"
Expand Down
54 changes: 30 additions & 24 deletions pyorbital/tlefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Classes and functions for handling TLE files."""


import contextlib
import datetime as dt
import glob
import io
Expand Down Expand Up @@ -56,16 +55,11 @@ class TleDownloadTimeoutError(Exception):
def _get_config_path():
"""Get the config path for Pyorbital."""
if "PPP_CONFIG_DIR" in os.environ and "PYORBITAL_CONFIG_PATH" not in os.environ:
# XXX: Swap when pyorbital 1.9 is released
#LOGGER.warning(
# "The use of PPP_CONFIG_DIR is no longer supported!" +
# " Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!")
#LOGGER.debug("Using the package default for configuration: %s", PKG_CONFIG_DIR)
#return PKG_CONFIG_DIR
LOGGER.warning(
"The use of PPP_CONFIG_DIR is deprecated and will be removed in version 1.9!" +
"The use of PPP_CONFIG_DIR is no longer supported!" +
" Please use PYORBITAL_CONFIG_PATH if you need a custom config path for pyorbital!")
pyorbital_config_path = os.getenv("PPP_CONFIG_DIR", PKG_CONFIG_DIR)
LOGGER.debug("Using the package default for configuration: %s", PKG_CONFIG_DIR)
return PKG_CONFIG_DIR
else:
pyorbital_config_path = os.getenv("PYORBITAL_CONFIG_PATH", PKG_CONFIG_DIR)

Expand Down Expand Up @@ -345,17 +339,27 @@ def _get_tles_from_uris(uris, open_func, platform="", only_first=True):
return tles


@contextlib.contextmanager
def _uri_open(uri, open_func):
file_obj = open_func(uri)
try:
yield file_obj
finally:
if hasattr(file_obj, "close"):
file_obj.close()


def _get_tles_from_url(url, open_func, platform, only_first):
fid = open_func(url)
open_is_dummy = open_func == _dummy_open_stringio
tles = []
for l_0 in fid:
tle = _decode_lines(fid, l_0, platform, only_first, open_is_dummy=open_is_dummy)
if tle:
if only_first:
return [tle]
tles.append(tle)
return tles
with _uri_open(url, open_func) as fid:
open_is_dummy = open_func == _dummy_open_stringio
tles = []
for l_0 in fid:
tle = _decode_lines(fid, l_0, platform, only_first, open_is_dummy=open_is_dummy)
if tle:
if only_first:
return [tle]
tles.append(tle)
return tles


def _decode(itm):
Expand Down Expand Up @@ -577,7 +581,7 @@ def update_db(self, tle, source):
cmd = SATID_VALUES.format(num)
epoch = tle.epoch.item().isoformat()
tle = "\n".join([tle.line1, tle.line2])
now = dt.datetime.utcnow().isoformat()
now = _utcnow().isoformat()
try:
with self.db:
self.db.execute(cmd, (epoch, tle, now, source))
Expand All @@ -594,7 +598,7 @@ def write_tle_txt(self):
return
pattern = os.path.join(self.writer_config["output_dir"],
self.writer_config["filename_pattern"])
now = dt.datetime.utcnow()
now = _utcnow()
fname = now.strftime(pattern)
out_dir = os.path.dirname(fname)
if not os.path.exists(out_dir):
Expand All @@ -608,8 +612,7 @@ def write_tle_txt(self):
query = f"SELECT epoch, tle FROM '{satid:d}' ORDER BY epoch DESC LIMIT 1" # nosec
epoch, tle = self.db.execute(query).fetchone() # nosec
date_epoch = dt.datetime.strptime(epoch, ISO_TIME_FORMAT)
tle_age = (
dt.datetime.utcnow() - date_epoch).total_seconds() / 3600.
tle_age = (_utcnow() - date_epoch).total_seconds() / 3600.
logging.info("Latest TLE for '%s' (%s) is %d hours old.",
satid, platform_name, int(tle_age))
data.append(tle)
Expand All @@ -631,6 +634,9 @@ def table_exists(db, name):
return db.execute(query, (name,)).fetchone() is not None # nosec


def _utcnow():
return dt.datetime.now(tz=dt.timezone.utc).replace(tzinfo=None)

def main():
"""Run a test TLE reading."""
tle_data = read("Noaa-19")
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ max-complexity = 10
[tool.coverage.run]
relative_files = true
omit = ["pyorbital/version.py"]

[tool.pytest.ini_options]
filterwarnings = [
"error",
"ignore:numpy.ndarray size changed:RuntimeWarning",
]

0 comments on commit d582531

Please sign in to comment.