OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
__init__.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2020 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 import os
20 
21 __all__=['CharList','Correl', 'FileLogSink', 'FloatList', 'FloatMatrix', 'FloatMatrix3', 'FloatMatrix4', 'GetCurrentLogSink', 'GetPrefixPath', 'GetSharedDataPath', 'GetVerbosityLevel', 'Histogram', 'IntList', 'LogDebug', 'LogError', 'LogInfo', 'LogScript', 'LogSink', 'LogTrace', 'LogVerbose', 'LogWarning', 'Max', 'Mean', 'Median', 'Min', 'MultiLogSink', 'PopLogSink', 'PopVerbosityLevel', 'PushLogSink', 'PushVerbosityLevel', 'Range', 'SetPrefixPath', 'StdDev', 'StreamLogSink', 'StringList', 'StringLogSink', 'Units', 'VERSION', 'VERSION_MAJOR', 'VERSION_MINOR', 'VERSION_PATCH', 'WITH_NUMPY', 'conop', 'geom', 'io', 'mol', 'seq', 'stutil' ,'gui','gfx','img','info']
22 
23 from ._ost_base import *
24 from .stutil import *
25 from ost import conop
26 
27 __version__ = _ost_base.VERSION
28 
29 class StreamLogSink(LogSink):
30  def __init__(self, stream):
31  LogSink.__init__(self)
32  self._stream=stream
33  def LogMessage(self, message, level):
34  self._stream.write(message)
35 
36 
37 def _GetPrefixFromPythonPath():
38  """Get the prefix from this file's path"""
39  prefix_path = __file__
40  for _ in range(5):
41  prefix_path = os.path.dirname(prefix_path)
42  return prefix_path
43 
44 
45 def _SetupOstPrefix():
46  """ This function attempts to set the OST prefix path if $OST_ROOT was not
47  set already.
48 
49  It does so by assuming that the __init__.py file is located under
50  $OST_ROOT/lib(64)?/pythonX.XX/site-packages/ost. This might not be the case
51  in all settings (conda, pip?). In that case this function should be
52  adapted.
53  """
54 
55  try:
56  # $OST_ROOT was set
57  # Note: this doesn't mean set to a valid prefix
59  except RuntimeError:
60  # Setup from this file's path
61  prefix_path = _GetPrefixFromPythonPath()
62  SetPrefixPath(prefix_path)
63 
64 
65 def _TrySetCompoundsLib(path):
66  """Tries to set the compound lib to 'path', or raises a ValueError."""
67  if path is not None and os.path.exists(path):
68  compound_lib = conop.CompoundLib.Load(path)
69  if compound_lib is not None:
70  conop.SetDefaultLib(compound_lib)
71  return
72  raise ValueError("Could not load %s" % path)
73 
74 
75 def _SetupCompoundsLib():
76  """ This function sets up the compound lib.
77 
78  By order of priority, the following heuristics are used:
79 
80  1. The $OST_COMPOUNDS_CHEMLIB environment variable
81  2. The 'compounds.chemlib' file in the shared path under the folder pointed
82  by the $OST_ROOT environment variable
83  3. The 'compounds.chemlib' file in the shared path under the OST prefix where
84  the ost python module is installed.
85 
86  If no compound library can be loaded with any of these strategies, a warning
87  message is issued.
88 
89  """
90  # Try with the $OST_COMPOUNDS_CHEMLIB environment variable
91  compound_lib_path = os.getenv("OST_COMPOUNDS_CHEMLIB")
92  if compound_lib_path:
93  compound_lib = conop.CompoundLib.Load(compound_lib_path)
94  if compound_lib is None:
95  raise RuntimeError("Could not load $OST_COMPOUNDS_CHEMLIB as a compound "
96  "library: '%s'." % compound_lib_path)
97  else:
98  conop.SetDefaultLib(compound_lib)
99  return
100 
101  # Try from GetSharedDataPath() - requires $OST_ROOT to be set.
102  try:
103  compound_lib_path = os.path.join(GetSharedDataPath(), 'compounds.chemlib')
104  _TrySetCompoundsLib(compound_lib_path)
105  except (RuntimeError, ValueError):
106  pass
107  else:
108  return
109 
110  # If OST_ROOT was pointing to a root with no compound lib or to an invalid
111  # root, try
112  try:
113  old_prefix = GetPrefixPath() # This is set by now and won't raise
114  prefix_path = _GetPrefixFromPythonPath()
115  SetPrefixPath(prefix_path)
116  compound_lib_path = os.path.join(GetSharedDataPath(), 'compounds.chemlib')
117  _TrySetCompoundsLib(compound_lib_path)
118  except (RuntimeError, ValueError):
119  SetPrefixPath(old_prefix)
120  else:
121  # If we're here, OST root was set but the prefix did not contain a compound
122  # lib, but the root obtained from the python path contained one.
123  # Most likely OST_ROOT was set incorrectly
124  LogWarning("$OST_ROOT is pointing to an invalid OST prefix: '%s'" % (
125  os.getenv("OST_ROOT")))
126  return
127 
128  LogWarning("Compound library not available. Some functionality may not " \
129  "work as expected.")
130 
131 
132 # Setup OST
133 PushVerbosityLevel(LogLevel.Script)
134 _SetupOstPrefix()
135 _SetupCompoundsLib()
virtual void LogMessage(const String &message, int severity)
Definition: log_sink.hh:47
String DLLEXPORT_OST_BASE GetPrefixPath()
get the path prefix
def PushVerbosityLevel
Definition: __init__.py:229
void DLLEXPORT_OST_BASE SetPrefixPath(const String &prefix)
set path prefix
String DLLEXPORT_OST_BASE GetSharedDataPath()