]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
a4c36f33062ebd1fdd2d24a85fa9cc286001f6b5
[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    print "Unfound directory ", DirName
21    return None
22
23 ### Setup the path to the dictionaries. WARNING: this needs to be done
24 #   BEFORE importation of the shadow classse generated by swig as found
25 #   in gdcm, since the dynamic library loads the standard dictionary
26 #   when dynamically loaded.
27 #   We consider we succefully found the dictionaries path when we encounter
28 #   the standard dictionary i.e. the file dicomV3.dic.
29
30 try:
31    ### First consider the environnement variable.
32    os.environ["GDCM_DICT_PATH"]
33    if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
34                                       "dicomV3.dic")):
35       raise KeyError
36 except KeyError:
37    Path = BuildInstallOrPreinstallPath("Dicts", "dicomV3.dic")
38    if not Path:
39       print "Path to dictionaries is mandatory. Exiting"
40       sys.exit(1)
41    os.environ["GDCM_DICT_PATH"] = Path
42
43 ### Set up the path to the data images for the demos.
44 GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
45
46 ### Set up the path to the data images of the test suite.
47 GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData", "test.acr")
48
49 ### Import the swig generated shadow classes.
50 try:
51    import gdcm
52 except ImportError,e:
53    print e
54    raise ImportError, "gdcm extension not imported."
55
56 ### Expose only the necessary stuff
57 gdcmHeader = gdcm.gdcmHeader
58 gdcmDictSet = gdcm.gdcmDictSet
59 gdcmFile = gdcm.gdcmFile
60
61 def GetPubDictTagNames():
62    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNames()
63
64 def GetPubDictTagNamesByCategory():
65    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNamesByCategory()