"""
Basic helpers for arguments.
"""
import os, sys
import ost
def _MsgErrorAndExit(msg, exit_status):
ost.LogError(msg)
sys.exit(exit_status)
[docs]def FileExists(prefix, exit_status, file):
'''
Checks if a file exists, terminates if not. The error message displayed is
fixed and only needs a *prefix* describing the specimen of file.
:param prefix: String to put in front of the failure-message
"file does not exist: ``file``".
:type prefix: :class:`str`
:param exit_status: Exit code on missing file, ends up in ``$?`` in the
shell. ``0`` is traditionally reserved to successful commands.
:type exit_status: :class:`int`
:param file: Path including file name to be checked.
:type file: :class:`str`
:returns: No return value, exits script with value ``exit_status`` if file is
missing.
'''
if not os.path.exists(file):
_MsgErrorAndExit('%s file does not exist: %s\n' % (prefix, file),
exit_status)
[docs]def FileExtension(prefix, exit_status, file, extensions, gz=False):
'''
Checks a file to carry a known extension given by a list of strings. Since
files are very often compressed these days, an additional "gz" suffix can be
tracked automatically by this function. Thus, the list of *extensions* only
needs to contain what you are really looking for, e.g. ("pdb") instead of
("pdb", "pdb.gz"). The *gz* flag also determines the output of this function.
If enabled, a triple is returned: name of the file without extension, its
extension and a Boolean to tell whether the file carries the gzip extension
or not. If *gz* detection is turned of, only a tuple is returned: file name
and extension. If the tested file name has an unrecognised extension, this
function terminates the script.
:param prefix: String to put in front of the failure-message
"file extension not supported: ``file``".
:type prefix: :class:`str`
:param exit_status: Exit code on missing file, ends up in ``$?`` in the
shell. ``0`` is traditionally reserved to successful commands.
:type exit_status: :class:`int`
:param file: Path including file name to be checked.
:type file: :class:`str`
:param extensions: List of strings without a leading ".".
:type extensions: :class:`list`
:param gz: Indicates whether to check for an additional "gz" extension.
:type gz: :class:`bool`
:returns: (base name of ``file`` (:class:`str`), extension of file without a
".gz" (:class:`str`), flag to indicate an additional ".gz"
(:class:`bool`)) **if** ``gz`` is set, (base name of ``file``
(:class:`str`), extension of file) **if not**.
'''
filename, fileext = os.path.splitext(file)
is_gz = False
if fileext.lower() == '.gz':
is_gz = True
filename, fileext = os.path.splitext(filename)
if not gz:
extension_string = ', '.join(extensions)
_MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, file)+
'Allowed extensions are: %s\n' % extension_string,
exit_status)
if fileext == '':
extension_string = ', '.join(extensions)
if gz:
extension_string += ', ' + '.gz, '.join(extensions) + '.gz'
_MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, file)+
'Allowed extensions are: %s\n' % extension_string,
exit_status)
fileext = fileext[1:].lower()
for ext in extensions:
if fileext == ext.lower():
if gz:
return os.path.basename(filename), fileext, is_gz
else:
return os.path.basename(filename), fileext
extension_string = ', '.join(extensions)
if gz:
extension_string += ', ' + '.gz, '.join(extensions) + '.gz'
_MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, file)+
'Allowed extensions are: %s\n' % extension_string,
exit_status)
__all__ = (
'FileExists',
'FileExtension',
)
# LocalWords: gz pdb gzip bool os sys FileExists FileExtension filename
# LocalWords: fileext