]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
* gdcmPython/__init__.py doesn't crash anymore when running in
[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 InstallModePath = os.path.join(__path__[0], "Data")
45 if os.path.isfile(os.path.join(InstallModePath, "test.acr")):
46    os.environ["GDCM_DATA_PATH"] = InstallModePath
47 else:
48    PreInstallModePath = os.path.join(__path__[0], "..", "Data")
49    if os.path.isfile(os.path.join(PreInstallModePath, "test.acr")):
50       os.environ["GDCM_DATA_PATH"] = PreInstallModePath
51    else:
52       print "Unfound data path"
53       sys.exit(1)
54
55 ### Import the swig generated shadow classes.
56 try:
57    import gdcm
58 except ImportError,e:
59    print e
60    raise ImportError, "gdcm extension not imported."
61
62 ### Expose only the necessary stuff
63 gdcmHeader = gdcm.gdcmHeader
64 gdcmDictSet = gdcm.gdcmDictSet
65 gdcmFile = gdcm.gdcmFile