Skip to content

Commit

Permalink
version to 1.0.3dev2, new NoNetwork exception and handling related to…
Browse files Browse the repository at this point in the history
… that throughout casaconfig
  • Loading branch information
bgarwood committed Jan 31, 2025
1 parent e1552ab commit fa86ad1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 13 deletions.
4 changes: 4 additions & 0 deletions casaconfig/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
print("This is likely due to no network connection or bad connectivity to a remote site, wait and try again")
sys.exit(1)

except casaconfig.NoNetwork as exc:
print("No network seen, can not continue.")
sys.exit(1)

except casaconfig.BadLock as exc:
# the output produced by the update functions is sufficient, just re-echo the exception text itself
print(str(exc))
Expand Down
5 changes: 5 additions & 0 deletions casaconfig/private/CasaconfigErrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ class RemoteError(Exception):

class NotWritable(Exception):
"""Raised when the path is not writable by the user"""
pass

class UnsetMeasurespath(Exception):
"""Raised when a path argument is None"""
pass

class NoNetwork(Exception):
"""Raised when there is no network connection."""
pass
9 changes: 8 additions & 1 deletion casaconfig/private/data_available.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def data_available():
list - version names returned as list of strings
Raises
- casaconfig.RemoteError - Raised when there is an error fetching some remote content
- casaconfig.NoNetwork - Raised where there is no network seen, can not continue
- casaconfig.RemoteError - Raised when there is an error fetching some remote content for some reason other than no network
- Exception - Unexpected exception while getting list of available casarundata versions
"""
Expand All @@ -48,6 +49,12 @@ def data_available():
import certifi

from casaconfig import RemoteError
from casaconfig import NoNetwork

from .have_network import have_network

if not have_network():
raise NoNetwork("No network, can not find the list of available data.")

class LinkParser(html.parser.HTMLParser):
def reset(self):
Expand Down
7 changes: 4 additions & 3 deletions casaconfig/private/get_data_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_data_lock(path, fn_name):
This function is intended for internal casaconfig use.
If there is not network (have_network returns False) then the lock file is not
set or initialized and a RemoteError exception is raised.
set or initialized and a NoNetwork exception is raised.
Parameters
- path (str) - The location where 'data_update.log' is to be found.
Expand All @@ -40,6 +40,7 @@ def get_data_lock(path, fn_name):
- the open file descriptor holding the lock. Close this file descriptor to release the lock.
Raises:
- casaconfig.NoNetwork - raised wheren there is no network, nothing can be downloaded so nothing should be locked.
- casaconfig.BadLock - raised when the path to the lock file does not exist or the lock file is not empty as found
- Exception - an unexpected exception was seen while writing the lock information to the file
Expand All @@ -51,11 +52,11 @@ def get_data_lock(path, fn_name):
from datetime import datetime

from casaconfig import BadLock
from casaconfig import RemoteError
from casaconfig import NoNetwork
from .have_network import have_network

if not have_network():
raise RemoteError("No network, lock file has not been set, unable to continue.")
raise NoNetwork("No network, lock file has not been set, unable to continue.")

if not os.path.exists(path):
raise BadLock("path to contain lock file does not exist : %s" % path)
Expand Down
9 changes: 8 additions & 1 deletion casaconfig/private/measures_available.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ def measures_available():
version names returned as list of strings
Raises:
- casaconfig.RemoteError - raised when when a socket.gaierror is seen, likely due to no network connection
- casaconfig.NoNetwork - raised when there is no network (have_data returns False)
- casaconfig.RemoteError - raised when when a socket.gaierror is seen, unlikely due to the have_network check
- Exception: raised when any unexpected exception happens
"""
from ftplib import FTP
import socket

from casaconfig import RemoteError
from casaconfig import NoNetwork

from .have_network import have_network

if not have_network():
raise NoNetwork("can not find the list of available measures data")

files = []
try:
ftp = FTP('ftp.astron.nl')
Expand Down
10 changes: 7 additions & 3 deletions casaconfig/private/measures_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def measures_update(path=None, version=None, force=False, logger=None, auto_upda
casaconfig.BadReadme - raised when something unexpected is found in the readme or the readme changed after an update is in progress
casaconfig.NoReadme - raised when the readme.txt file is not found at path (path also may not exist)
casaconfig.NotWritable - raised when the user does not have permission to write to path
casaconfig.RemoteError - raised by measures_available when the remote list of measures could not be fetched
casaconfig.NoNetwork - raised by measuers_available or when getting the lock file if there is no network.
casaconfig.RemoteError - raised by measures_available when the remote list of measures could not be fetched, not due to no network.
casaconfig.UnsetMeasurespath - raised when path is None and has not been set in config
Exception - raised when something unexpected happened while updating measures
Expand All @@ -138,7 +139,7 @@ def measures_update(path=None, version=None, force=False, logger=None, auto_upda
import fcntl

from casaconfig import measures_available
from casaconfig import AutoUpdatesNotAllowed, UnsetMeasurespath, RemoteError, NotWritable, BadReadme, BadLock, NoReadme
from casaconfig import AutoUpdatesNotAllowed, UnsetMeasurespath, RemoteError, NotWritable, BadReadme, BadLock, NoReadme, NoNetwork

from .print_log_messages import print_log_messages
from .get_data_lock import get_data_lock
Expand Down Expand Up @@ -208,8 +209,11 @@ def measures_update(path=None, version=None, force=False, logger=None, auto_upda
# get the current most recent version
try:
checkVersion = measures_available()[-1]
except RemoteError as exc:
except NoNetwork as exc:
# no network, no point in continuing, just reraise
raise exc
except RemoteError as exc:
# bad network?, no point in continuing, just reraise
raise exc
except:
# unsure what happened, leave it at none, which will trigger an update attempt, which might work
Expand Down
14 changes: 10 additions & 4 deletions casaconfig/private/pull_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
Raises
- casaconfig.BadLock - raised when the lock file is not empty when a lock is requested
- casaconfig.BadReadme - raised when the readme.txt file found at path does not contain the expected list of installed files or there was an unexpected change while the data lock is on
- casaconfig.NoNetwork - raised where this is no network
- casaconfig.NotWritable - raised when the user does not have write permission to path
- casaconfig.RemoteError - raised by data_available when the list of available data versions could not be fetched
- casaconfig.RemoteError - raised by data_available when the list of available data versions could not be fetched for some reason other than no network
- casaconfig.UnsetMeasurespath - raised when path is None and and measurespath has not been set in config.
"""
Expand Down Expand Up @@ -168,7 +169,7 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
# the readme file looks as expected, pull if the version is different or force is true
if version is None:
# use most recent available
# this may raise a RemoteError, no need to catch that here but it may need to be caught upstream
# this may raise RemoteError or NoNetwork, no need to catch that here but it may need to be caught upstream
available_data = data_available()
version = available_data[-1]

Expand All @@ -186,7 +187,7 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
# need a version, use most recent available

if available_data is None:
# this may raise a RemoteError, no need to catch that here but it may need to be caught upstream
# this may raise RemoteError or NoNetwork, no need to catch that here but it may need to be caught upstream
available_data = data_available()

version = available_data[-1]
Expand All @@ -203,7 +204,7 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
else:
# requested version must be available
if available_data is None:
# this may raise a RemoteError, no need to catch that here but it may need to be caught upstream
# this may raise RemoteError or NoNetwork, no need to catch that here but it may need to be caught upstream
available_data = data_available()

if version not in available_data:
Expand All @@ -224,6 +225,7 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
try:
print_log_messages('pull_data using version %s, acquiring the lock ... ' % version, logger)

# attempting to get the lock will raise NoNetwork if there is no network and the lock will not be set, catch and reemit that in this try block
lock_fd = get_data_lock(path, 'pull_data')
# the BadLock exception that may happen here is caught below

Expand Down Expand Up @@ -298,6 +300,10 @@ def pull_data(path=None, version=None, force=False, logger=None, verbose=None):
print_log_messages(msgs, logger, True)
raise

except NoNetwork as exc:
# there is no network, it should be self explanatory so just re-raise it
raise

except Exception as exc:
msgs = []
msgs.append('ERROR! : Unexpected exception while populating casarundata version %s to %s' % (version, path))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "casaconfig"
version = "1.0.3dev1"
version = "1.0.3dev2"
description = "CASA Operational Configuration Package"
readme = 'README.md'
authors = [
Expand Down

0 comments on commit fa86ad1

Please sign in to comment.