helper
- Shared Functionality For the Everything¶
Introduction¶
We collect functions here, which should be useful in many places but would make rather empty modules left alone.
Messages¶
from promod3.core import helper
helper.MsgErrorAndExit("Something failed!", 1)
File Tests¶
The following example parses an argument (call as pm SCRIPTNAME FILENAME
) as
a file name and checks whether it is a pdb
or mmcif
file.
"""Test for file reading."""
from promod3.core import helper
from promod3.core import pm3argparse
p = pm3argparse.PM3ArgumentParser(__doc__)
p.add_argument('file', type=str)
opts = p.Parse()
helper.FileExists('Test file', 1, opts.file)
opts.name, opts.ext, opts.gz = helper.FileExtension('Test file', 2,
opts.file,
('pdb', 'mmcif'),
gzip=True)
- promod3.core.helper.FileExists(prefix, exit_status, filename)¶
Checks if a file exists, terminates if not. The error message displayed is fixed and only needs a prefix describing the specimen of file.
- Parameters:
- Returns:
No return value, exits script with value
exit_status
if file is missing.
- promod3.core.helper.FileExtension(prefix, exit_status, filename, extensions, gzip=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 gzip 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 gzip 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.
- Parameters:
prefix (
str
) – String to put in front of the failure-message “file extension not supported:filename
”.exit_status (
int
) – Exit code on missing file, ends up in$?
in the shell.0
is traditionally reserved to successful commands.filename (
str
) – Path including file name to be checked.extensions (
list
) – List of strings without a leading “.”.gzip (
bool
) – Indicates whether to check for an additional “gz” extension.
- Returns:
(base name of
filename
(str
), extension of file without a “.gz” (str
), flag to indicate an additional “.gz” (bool
)) ifgzip
is set, (base name offilename
(str
), extension of file) if not.
- promod3.core.helper.FileGzip(prefix, exit_status, filename, allowed=True)¶
See if a file is gzipped or not. This is basically done by checking for a “gz” suffix. May also be used to verify that a file is not compressed where it does not apply. That is where allowed comes in. If “gz” is not allowed, terminates the script on gzip files.
- Parameters:
prefix (
str
) – String to put in front of the failure-message where gzip files are not allowed.exit_status (
int
) – Exit code on gzip files to be avoided, ends up in$?
in the shell.0
is traditionally reserved to successful commands.filename (
str
) – Path including file name to be checked.allowed (
bool
) – Set toFalse
if gzipped files are not allowed. Then the script will terminate if a gzip file is found.
- Returns:
Flag to indicate if file is gzipped (
bool
).