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