]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
Benoit Regrain : Modify the rules for checking the paths
[gdcm.git] / gdcmPython / __init__.py
1 import os, sys
2
3 def BuildInstallOrPreinstallPath(DirName, FileName = None):
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 if it is setted otherwise for the existence of the
14    # DirName.
15    InstallModePath = os.path.join(__path__[0], DirName + "/")
16    if(FileName):
17       if os.path.isfile(os.path.join(InstallModePath, FileName)):
18          return InstallModePath
19    else:
20       if os.path.isdir(InstallModePath):
21          return InstallModePath
22
23    PreInstallModePath = os.path.join(__path__[0], "..", DirName + "/")
24    if(FileName):
25       if os.path.isfile(os.path.join(PreInstallModePath, FileName)):
26          return PreInstallModePath
27    else:
28       if os.path.isdir(PreInstallModePath):
29          return PreInstallModePath
30    return None
31
32 ### Setup the path to the dictionaries. WARNING: this needs to be done
33 #   BEFORE importation of the shadow classes generated by swig as found
34 #   in gdcm, since the dynamic library loads the standard dictionary
35 #   when dynamically loaded.
36 #   We consider we succefully found the dictionaries path when we encounter
37 #   the standard dictionary i.e. the file dicomV3.dic.
38
39 try:
40    ### First consider the environnement variable.
41    os.environ["GDCM_DICT_PATH"]
42    if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
43                                       "dicomV3.dic")):
44       raise KeyError
45 except KeyError:
46    Path = BuildInstallOrPreinstallPath("Dicts", "dicomV3.dic")
47    if not Path:
48       print "Path to dictionaries is mandatory. Exiting"
49       sys.exit(1)
50    os.environ["GDCM_DICT_PATH"] = Path
51
52 ### Set up the path to the data images for the demos.
53 GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
54 if not GDCM_DATA_PATH:
55    print "GDCM_DATA_PATH is not setup properly: unfound Test directory"
56
57 ### Set up the path to the data images of the test suite.
58 GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData")
59
60 ### Import the swig generated shadow classes.
61 try:
62    import gdcm
63 except ImportError,e:
64    print e
65    raise ImportError, "gdcm extension not imported."
66
67 ### Expose only the necessary stuff
68 gdcmHeader = gdcm.gdcmHeader
69 gdcmDictSet = gdcm.gdcmDictSet
70 gdcmFile = gdcm.gdcmFile
71
72 def GetPubDictTagNames():
73    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNames()
74
75 def GetPubDictTagNamesByCategory():
76    return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNamesByCategory()