From 46213f56db37daa1fa160cba6fe4f2a86fad352d Mon Sep 17 00:00:00 2001 From: Stefan Bienert <stefan.bienert@unibas.ch> Date: Thu, 2 Oct 2014 11:44:26 +0200 Subject: [PATCH] Started helper module --- actions/pm-build-rawmodel | 7 +++++-- core/doc/CMakeLists.txt | 1 + core/doc/helper.rst | 36 ++++++++++++++++++++++++++++++++++++ core/doc/index.rst | 5 ++++- core/pymod/CMakeLists.txt | 2 +- core/pymod/core/argcheck.py | 16 ++++++++-------- core/pymod/core/helper.py | 27 +++++++++++++++++++++++++++ doc/conf.py.in | 10 ++++++++-- 8 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 core/doc/helper.rst create mode 100644 core/pymod/core/helper.py diff --git a/actions/pm-build-rawmodel b/actions/pm-build-rawmodel index aed752bc..29486cf6 100755 --- a/actions/pm-build-rawmodel +++ b/actions/pm-build-rawmodel @@ -1,8 +1,8 @@ #!/usr/bin/env ost -import argparse +import os, argparse from promod3 import meld -from promod3.core import argcheck +from promod3.core import argcheck, helper ### CHANGELOG - START # 2013-11-07 - created @@ -13,6 +13,7 @@ from promod3.core import argcheck # 2 - template structure file has unsupported file extension # 3 - alignment file does not exist # 4 - alignment file has unsupported file extension +# 5 - failed to write results to file ### EXIT STATUS - END ### SETUP - START @@ -69,6 +70,8 @@ aln = io.LoadAlignment(opts.alignment_file) aln.AttachView(1, tpl.CreateFullView()) result = meld.BuildRawModel(aln) io.SavePDB(result.model, opts.model_file) +if not os.path.isfile(opts.model_file): + helper.MsgErrorAndExit("Failed to write model file '%s'." % opts.model_file, 5) ### MAIN - END diff --git a/core/doc/CMakeLists.txt b/core/doc/CMakeLists.txt index 9e2491f7..9097ded5 100644 --- a/core/doc/CMakeLists.txt +++ b/core/doc/CMakeLists.txt @@ -1,6 +1,7 @@ set(CORE_RST index.rst argcheck.rst +helper.rst ) add_doc_source(NAME core RST ${CORE_RST}) diff --git a/core/doc/helper.rst b/core/doc/helper.rst new file mode 100644 index 00000000..c790f0ab --- /dev/null +++ b/core/doc/helper.rst @@ -0,0 +1,36 @@ +:mod:`~promod3.core.helper` - Shared functionality for the everything +================================================================================ + +.. currentmodule:: promod3.core.helper + +Introduction +-------------------------------------------------------------------------------- + +We collect functions here, which should be useful in many places but would make +rather empty modules left alone. + + +Messages +-------------------------------------------------------------------------------- + +.. testcode:: helper + :hide: + + from promod3.core import helper + + try: + helper.MsgErrorAndExit("Something failed!", 1) + except SystemExit, e: + if e.code == 1: + pass + else: + raise + +.. doctest:: helper + + from promod3.core import helper + + helper.MsgErrorAndExit("Something failed!", 1) + +.. autofunction:: MsgErrorAndExit + diff --git a/core/doc/index.rst b/core/doc/index.rst index 4ec3a445..896ac2e6 100644 --- a/core/doc/index.rst +++ b/core/doc/index.rst @@ -4,11 +4,14 @@ .. module:: promod3.core :synopsis: Basic functionality, supporting standard tasks in your code. -This module gathers functions and classes which are not devoted to homology modeling per se but cover standard programming issues. +This module gathers functions and classes which are not devoted to homology +modeling per se but cover standard programming issues. .. toctree:: :maxdepth: 2 argcheck + helper + .. LocalWords: promod se toctree maxdepth argcheck diff --git a/core/pymod/CMakeLists.txt b/core/pymod/CMakeLists.txt index d1400c33..8a6a110f 100644 --- a/core/pymod/CMakeLists.txt +++ b/core/pymod/CMakeLists.txt @@ -1,4 +1,4 @@ -set(PROMOD3_CORE_FILES __init__.py argcheck.py) +set(PROMOD3_CORE_FILES __init__.py argcheck.py helper.py) pymod(NAME core PY ${PROMOD3_CORE_FILES} IN_DIR core __init__.py OUTPUT_DIR "promod3") diff --git a/core/pymod/core/argcheck.py b/core/pymod/core/argcheck.py index 86cff3c7..8907d413 100644 --- a/core/pymod/core/argcheck.py +++ b/core/pymod/core/argcheck.py @@ -4,10 +4,7 @@ Basic helpers for arguments. import os, sys import ost - -def _MsgErrorAndExit(msg, exit_status): - ost.LogError(msg) - sys.exit(exit_status) +import helper def FileExists(prefix, exit_status, file): ''' @@ -29,7 +26,7 @@ def FileExists(prefix, exit_status, file): missing. ''' if not os.path.exists(file): - _MsgErrorAndExit('%s file does not exist: %s\n' % (prefix, file), + helper.MsgErrorAndExit('%s file does not exist: %s\n' % (prefix, file), exit_status) def FileExtension(prefix, exit_status, file, extensions, gz=False): @@ -74,14 +71,16 @@ def FileExtension(prefix, exit_status, file, extensions, gz=False): filename, fileext = os.path.splitext(filename) if not gz: extension_string = ', '.join(extensions) - _MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, file)+ + helper.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)+ + helper.MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, + file)+ 'Allowed extensions are: %s\n' % extension_string, exit_status) fileext = fileext[1:].lower() @@ -94,7 +93,8 @@ def FileExtension(prefix, exit_status, file, extensions, gz=False): extension_string = ', '.join(extensions) if gz: extension_string += ', ' + '.gz, '.join(extensions) + '.gz' - _MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, file)+ + helper.MsgErrorAndExit('%s file extension not supported: %s. ' % (prefix, + file)+ 'Allowed extensions are: %s\n' % extension_string, exit_status) diff --git a/core/pymod/core/helper.py b/core/pymod/core/helper.py new file mode 100644 index 00000000..7f5871d5 --- /dev/null +++ b/core/pymod/core/helper.py @@ -0,0 +1,27 @@ +""" +Uncategorised functions which may come handy at several places. +""" + +import sys +import ost + +def MsgErrorAndExit(msg, exit_status): + ''' + Send a messages to the |ost_s| :ost_docs:`error log <base/logging/>` and exit + the Python interpreter. + + :param msg: The message. + :type msg: :class:`str` + + :param exit_status: Exit code, ends up in ``$?`` in the shell. ``0`` is + traditionally reserved to successful commands. + :type exit_status: :class:`int` + + :returns: No return value, exits script with value ``exit_status``. + ''' + ost.LogError(msg) + sys.exit(exit_status) + +__all__ = ( + 'MsgErrorAndExit', +) diff --git a/doc/conf.py.in b/doc/conf.py.in index 79fcf5ce..1cc6b9e8 100644 --- a/doc/conf.py.in +++ b/doc/conf.py.in @@ -29,7 +29,11 @@ sys.path.insert(1, '@OST_ROOT@/@LIB_DIR@/python@PYTHON_VERSION@/site-packages') # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.pngmath', 'sphinx.ext.ifconfig', 'sphinx.ext.viewcode', 'sphinx.ext.extlinks'] +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', + 'sphinx.ext.intersphinx', 'sphinx.ext.todo', + 'sphinx.ext.coverage', 'sphinx.ext.pngmath', + 'sphinx.ext.ifconfig', 'sphinx.ext.viewcode', + 'sphinx.ext.extlinks'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -252,7 +256,9 @@ intersphinx_mapping = {'python': ('@PYTHON_DOC_URL@', None), # -- ProMod3 specific configuration -------------------------------------------- extlinks = {'py_docs' : ('@PYTHON_DOC_URL@/%s', - 'Python documentation')} + 'Python documentation'), + 'ost_docs' : ('@OST_DOC_URL@/%s', + 'OpenStructure documentation')} # The _nameattr is a bit ugly: we want to have __name__ formatted as Python # attribute but Sphinx does not go with calling :attr: inside extlinks. To keep # the Python url prefix, we define sth here. -- GitLab