]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
* src/gdcmElValSet.cxx, gdcmFile.cxx: JPR bug fix, removal of
[gdcm.git] / gdcmPython / __init__.py
1 import os, sys
2
3 def BuildInstallOrPreinstallPath(DirName, FileName):
4    # Builds a path to the DirName directory. This should work both when:
5    # - the package is properly installed in which case DirName is a subdir
6    #   of the package,
7    # - when in pre-installation mode (basically inside a CVS hierarchy)
8    #   in which case DirName and the package itself lie at the same
9    #   filesystem level.
10    # In both cases we need to express the full path to DirName relatively
11    # to the path to this __init__.py. For this we rely on __path__ variable.
12    # In order to make sure we got the correct Path, we check for the
13    # existence of FileName.
14    InstallModePath = os.path.join(__path__[0], DirName + "/")
15    if os.path.isfile(os.path.join(InstallModePath, FileName)):
16       return InstallModePath
17    PreInstallModePath = os.path.join(__path__[0], "..", DirName + "/")
18    if os.path.isfile(os.path.join(PreInstallModePath, FileName)):
19       return PreInstallModePath
20    return None
21
22 ### Setup the path to the dictionaries. WARNING: this needs to be done
23 #   BEFORE importation of the shadow classes generated by swig as found
24 #   in gdcm, since the dynamic library loads the standard dictionary
25 #   when dynamically loaded.
26 #   We consider we succefully found the dictionaries path when we encounter
27 #   the standard dictionary i.e. the file dicomV3.dic.
28
29 try:
30    ### First consider the environnement variable.
31    os.environ["GDCM_DICT_PATH"]
32    if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
33                                       "dicomV3.dic")):
34       raise KeyError
35 except KeyError:
36    Path = BuildInstallOrPreinstallPath("Dicts", "dicomV3.dic")
37    if not Path:
38       print "Path to dictionaries is mandatory. Exiting"
39       sys.exit(1)
40    os.environ["GDCM_DICT_PATH"] = Path
41
42 ### Set up the path to the data images for the demos.
43 GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
44 if not GDCM_DATA_PATH:
45    print "GDCM_DATA_PATH is not setup properly: unfound Test directory"
46
47 ### Set up the path to the data images of the test suite.
48 GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData", "test.acr")
49
50 ### Import the swig generated shadow classes.
51 try:
52    import gdcm
53 except ImportError,e:
54    print e
55    raise ImportError, "gdcm extension not imported."
56
57 ### Expose only the necessary stuff
58 gdcmHeader = gdcm.gdcmHeader
59 gdcmDictSet = gdcm.gdcmDictSet
60 gdcmFile = gdcm.gdcmFile
61
62 def GetPubDictTagNames():
63    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNames()
64
65 def GetPubDictTagNamesByCategory():
66    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNamesByCategory()