]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
f454cdb11588aea64801f45d87923e65654962ad
[gdcm.git] / gdcmPython / __init__.py
1 import os, sys
2
3 ### Setup the path to the dictionaries. WARNING: this needs to be done
4 #   BEFORE importation of the shadow classse generated by swig as found
5 #   in gdcm, since the dynamic library loads the standard dictionary
6 #   when dynamically loaded.
7 #   We consider we succefully found the dictionaries path when we encounter
8 #   the standard dictionary i.e. the file dicomV3.dic.
9
10 try:
11         ### First consider the environnement variable.
12         os.environ["GDCM_DICT_PATH"]
13         if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
14                                            "dicomV3.dic")):
15                 raise KeyError
16 except KeyError:
17         # When environnement variable is unavailable assume the package was
18         # properly installed i.e. the layout is such that the directory containing
19         # the dictionaries is BELOW (the root is at the top) the current file.
20         # Note: when importing this __init__ file the pathes are relative to the
21         #       invocation directory. In order to get them relative to the package
22         #       (or more simply to this file) we rely on the __path__ variable.
23         InstallModePath = os.path.join(__path__[0], "Dicts/")
24         if os.path.isfile(os.path.join(InstallModePath, "dicomV3.dic")):
25                 os.environ["GDCM_DICT_PATH"] = InstallModePath
26                 PreInstallModePath = None
27         else:
28                 # When both environnement variable and proper package installation 
29                 # strategy fail we eventually consider the pre-installation mode
30                 # i.e. when developpers are toying in a CVS tree. In this case
31                 # the layout is such that the directory containing the dictionaries is
32                 # ABOVE (the root is at the top) the current directory.
33                 PreInstallModePath = os.path.join(__path__[0], "..", "Dicts/")
34                 if os.path.isfile(os.path.join(PreInstallModePath, "dicomV3.dic")):
35                         os.environ["GDCM_DICT_PATH"] = PreInstallModePath
36                         InstallModePath = None
37                 else:
38                         print "Unfound gdcm dictionaries path"
39                         sys.exit(1)
40
41 ### Set up the path to the data images (for the test suite and the demo
42 #   examples). As for GDCM_DICT_PATH we offer both proper python package
43 #   installation scheme and the pre-install mode (see above).
44 if InstallModePath:
45         GDCM_DATA_PATH = os.path.join(__path__[0], "Data")
46 else:
47         if PreInstallModePath:
48                 GDCM_DATA_PATH = os.path.join(__path__[0], "..","Data")
49         else:
50                 print "Unfound data path"
51                 sys.exit(1)
52
53 ### Import the swig generated shadow classes.
54 try:
55         import gdcm
56 except ImportError,e:
57         print e
58         raise ImportError, "gdcm extension not imported."
59
60 ### Expose only the necessary stuff
61 gdcmHeader = gdcm.gdcmHeader
62 gdcmDictSet = gdcm.gdcmDictSet
63 gdcmFile = gdcm.gdcmFile