]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
* src/gdcmFile.h : bug fix. Variable type and variable name had same name
[gdcm.git] / gdcmPython / __init__.py
1 import os, sys
2
3 try:
4         from gdcmVersion import *
5 except:
6         print "gdcmVersion not imported"
7
8 def BuildInstallOrPreinstallPath(DirName, FileName = None):
9    # Builds a path to the DirName directory. This should work both when:
10    # - the package is properly installed in which case DirName is a subdir
11    #   of the package,
12    # - when in pre-installation mode (basically inside a CVS hierarchy)
13    #   in which case DirName and the package itself lie at the same
14    #   filesystem level.
15    # In both cases we need to express the full path to DirName relatively
16    # to the path to this __init__.py. For this we rely on __path__ variable.
17    # In order to make sure we got the correct Path, we check for the
18    # existence of FileName if it is setted otherwise for the existence of the
19    # DirName.
20    InstallModePath = os.path.join(__path__[0], DirName + "/")
21    if(FileName):
22       if os.path.isfile(os.path.join(InstallModePath, FileName)):
23          return InstallModePath
24    else:
25       if os.path.isdir(InstallModePath):
26          return InstallModePath
27
28    PreInstallModePath = os.path.join(__path__[0], "..", DirName + "/")
29    if(FileName):
30       if os.path.isfile(os.path.join(PreInstallModePath, FileName)):
31          return PreInstallModePath
32    else:
33       if os.path.isdir(PreInstallModePath):
34          return PreInstallModePath
35    return None
36
37 ### Setup the path to the dictionaries. WARNING: this needs to be done
38 #   BEFORE importation of the shadow classes generated by swig as found
39 #   in gdcm, since the dynamic library loads the standard dictionary
40 #   when dynamically loaded.
41 #   We consider we succefully found the dictionaries path when we encounter
42 #   the standard dictionary i.e. the file dicomV3.dic.
43
44 try:
45    ### First consider the environnement variable.
46    os.environ["GDCM_DICT_PATH"]
47    if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
48                                       "dicomV3.dic")):
49       raise KeyError
50 except KeyError:
51    Path = BuildInstallOrPreinstallPath("Dicts", "dicomV3.dic")
52    if not Path:
53       print "Path to dictionaries is mandatory. Exiting"
54       sys.exit(1)
55    os.environ["GDCM_DICT_PATH"] = Path
56 GDCM_DICT_PATH = os.environ["GDCM_DICT_PATH"]
57
58 ### Set up the path to the data images for the demos.
59 if os.environ["GDCM_DATA_PATH"]:
60    GDCM_DATA_PATH = BuildInstallOrPreinstallPath(os.environ["GDCM_DATA_PATH"],
61                                                  "test.acr")
62 else:
63    GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
64 if not GDCM_DATA_PATH:
65    print "GDCM_DATA_PATH is not setup properly: unfound Test directory"
66
67 ### Set up the path to the data images of the test suite.
68 GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData")
69
70 ### Import the swig generated shadow classes.
71 try:
72    import gdcm
73 except ImportError,e:
74    print e
75    raise ImportError, "gdcm extension not imported."
76
77 ### Expose only the necessary stuff
78 gdcmGlobal          = gdcm.gdcmGlobal
79 gdcmDictSet         = gdcm.gdcmDictSet
80 gdcmDicomDir        = gdcm.gdcmDicomDir
81 gdcmHeader          = gdcm.gdcmHeader
82 gdcmHeaderHelper    = gdcm.gdcmHeaderHelper
83 gdcmFile            = gdcm.gdcmFile
84
85 gdcmDicomDirMeta    = gdcm.gdcmDicomDirMeta
86 gdcmDicomDirPatient = gdcm.gdcmDicomDirPatient
87 gdcmDicomDirStudy   = gdcm.gdcmDicomDirStudy
88 gdcmDicomDirSerie   = gdcm.gdcmDicomDirSerie
89 gdcmDicomDirImage   = gdcm.gdcmDicomDirImage
90
91 def GetPubDictEntryNames():
92    return gdcmGlobal.GetDicts().GetPubDictEntryNames()
93
94 def GetPubDictEntryNamesByCategory():
95    return gdcmGlobal.GetDicts().GetPubDictEntryNamesByCategory()