]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
Minor cleanup for toplevel CMakeLists
[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 GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
60 if not GDCM_DATA_PATH:
61    print "GDCM_DATA_PATH is not setup properly: unfound Test directory"
62
63 ### Set up the path to the data images of the test suite.
64 GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData")
65
66 ### Import the swig generated shadow classes.
67 try:
68    import gdcm
69 except ImportError,e:
70    print e
71    raise ImportError, "gdcm extension not imported."
72
73 ### Expose only the necessary stuff
74 gdcmGlobal          = gdcm.gdcmGlobal
75 gdcmDictSet         = gdcm.gdcmDictSet
76 gdcmDicomDir        = gdcm.gdcmDicomDir
77 gdcmHeader          = gdcm.gdcmHeader
78 gdcmHeaderHelper    = gdcm.gdcmHeaderHelper
79 gdcmFile            = gdcm.gdcmFile
80
81 gdcmDicomDirMeta    = gdcm.gdcmDicomDirMeta
82 gdcmDicomDirPatient = gdcm.gdcmDicomDirPatient
83 gdcmDicomDirStudy   = gdcm.gdcmDicomDirStudy
84 gdcmDicomDirSerie   = gdcm.gdcmDicomDirSerie
85 gdcmDicomDirImage   = gdcm.gdcmDicomDirImage
86
87 def GetPubDictEntryNames():
88    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictEntryNames()
89
90 def GetPubDictEntryNamesByCategory():
91    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictEntryNamesByCategory()