Applications/CMakeFiles/pythonlib.dir/install/mirtk/__init__.py
1 ##############################################################################
2 # Medical Image Registration ToolKit (MIRTK)
3 #
4 # Copyright 2016-2017 Imperial College London
5 # Copyright 2016-2017 Andreas Schuh
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ##############################################################################
19 
20 """Medical Image Registration ToolKit
21 
22 This is the Python package of MIRTK. It can be used to execute the MIRTK
23 commands as subprocesses within a image processing pipeline written in Python.
24 Moreover, some MIRTK modules may provide loadable Python modules.
25 
26 To call a MIRTK command, either use the functions of the mirtk.subprocess module,
27 or simply execute a command using mirtk.<command>(args, option=value).
28 This will invoke the mirtk.subprocess.run function with name=<command> and
29 the provided positional and keyword arguments.
30 """
31 
32 from __future__ import absolute_import
33 
34 import sys
35 from types import ModuleType
36 from mirtk import subprocess
37 
38 # ==============================================================================
39 # Set up environment for MIRTK command execution
40 # ==============================================================================
41 
42 def _putenvs():
43  """Modify shared library search paths for MIRTK command execution."""
44  import os
45  from mirtk.subprocess import config, libexec_dir
46  ldpaths = []
47  if config != '':
48  ldpaths.append(os.path.join(libexec_dir, config))
49  ldpaths.append(libexec_dir)
50  for p in ['/Users/as12312/Software/VTK/Xcode/lib/Release', '/System/Library/Frameworks', '/usr/local/Cellar/flann/1.9.1_3/lib']:
51  if p != '': ldpaths.append(p)
52  if sys.platform.startswith('linux'):
53  ldpaths.extend(os.environ.get('LD_LIBRARY_PATH', '').split(':'))
54  os.environ['LD_LIBRARY_PATH'] = ':'.join(ldpaths)
55  elif sys.platform.startswith('darwin'):
56  ldpaths.extend(os.environ.get('DYLD_LIBRARY_PATH', '').split(':'))
57  os.environ['DYLD_LIBRARY_PATH'] = ':'.join(ldpaths)
58  elif sys.platform.startswith('win'):
59  ldpaths.extend(os.environ.get('PATH', '').split(';'))
60  os.environ['PATH'] = ';'.join(ldpaths)
61 _putenvs()
62 del _putenvs
63 
64 # ==============================================================================
65 # Wrap module into new ModuleType with dynamic subprocess.run methods
66 # ==============================================================================
67 
68 class Module(ModuleType):
69  """Module with dynamic creation of MIRTK command methods."""
70 
71  def __getattr__(self, name):
72  """Get method for execution of named MIRTK command."""
73  if name == 'subprocess':
74  return subprocess
75  if len(name) == 0 or name[0] == '_':
76  return ModuleType.__getattribute__(self, name)
77  if name in ["path", "run", "call", "check_call", "check_output"]:
78  return getattr(subprocess, name)
79  if not subprocess.path(name, quiet=True):
80  command = name.replace('_', '-')
81  if subprocess.path(command, quiet=True):
82  name = command
83  else:
84  return ModuleType.__getattribute__(self, name)
85  def run(*args, **kwargs):
86  return subprocess.run(name, *args, **kwargs)
87  setattr(self, name, run)
88  return run
89 
90 # keep a reference to this module so that it's not garbage collected
91 old_module = sys.modules[__name__]
92 
93 # setup the new module and patch it into the dict of loaded modules
94 new_module = sys.modules[__name__] = Module(__name__)
95 for attr in dict(old_module.__dict__):
96  if attr[0] == '_':
97  new_module.__dict__[attr] = old_module.__dict__[attr]