]> Creatis software - gdcm.git/blobdiff - gdcmPython/__init__.py
Benoit Regrain : Modify the rules for checking the paths
[gdcm.git] / gdcmPython / __init__.py
index 363c0bd5d3775fe568a79589ad09888748c8d3de..dcad484cb23efa8365c080bd12deb841451ba881 100644 (file)
@@ -1,59 +1,76 @@
 import os, sys
 
+def BuildInstallOrPreinstallPath(DirName, FileName = None):
+   # Builds a path to the DirName directory. This should work both when:
+   # - the package is properly installed in which case DirName is a subdir
+   #   of the package,
+   # - when in pre-installation mode (basically inside a CVS hierarchy)
+   #   in which case DirName and the package itself lie at the same
+   #   filesystem level.
+   # In both cases we need to express the full path to DirName relatively
+   # to the path to this __init__.py. For this we rely on __path__ variable.
+   # In order to make sure we got the correct Path, we check for the
+   # existence of FileName if it is setted otherwise for the existence of the
+   # DirName.
+   InstallModePath = os.path.join(__path__[0], DirName + "/")
+   if(FileName):
+      if os.path.isfile(os.path.join(InstallModePath, FileName)):
+         return InstallModePath
+   else:
+      if os.path.isdir(InstallModePath):
+         return InstallModePath
+
+   PreInstallModePath = os.path.join(__path__[0], "..", DirName + "/")
+   if(FileName):
+      if os.path.isfile(os.path.join(PreInstallModePath, FileName)):
+         return PreInstallModePath
+   else:
+      if os.path.isdir(PreInstallModePath):
+         return PreInstallModePath
+   return None
+
 ### Setup the path to the dictionaries. WARNING: this needs to be done
-#   BEFORE importation of gdcm. We consider we found the dictionaries
-#   path when we encounter the standard dictionary i.e. the file dicomV3.dic.
-# 1/ first consider the environnement variable.
-# 2/ when this fails consider the package installation mode i.e. when the
-#    layout is such that the directory containing the dictionaries is
-#    BELOW (the root is at the top) the current directory.
-# 3/ eventually consider the pre-installation mode i.e. when the
-#    layout is such that the directory containing the dictionaries is
-#    ABOVE (the root is at the top) the current directory.
-#   
+#   BEFORE importation of the shadow classes generated by swig as found
+#   in gdcm, since the dynamic library loads the standard dictionary
+#   when dynamically loaded.
+#   We consider we succefully found the dictionaries path when we encounter
+#   the standard dictionary i.e. the file dicomV3.dic.
+
 try:
-       os.environ["GDCM_DICT_PATH"]
-       if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
-                                          "dicomV3.dic")):
-               raise KeyError
+   ### First consider the environnement variable.
+   os.environ["GDCM_DICT_PATH"]
+   if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
+                                      "dicomV3.dic")):
+      raise KeyError
 except KeyError:
-       # Those pathes have to be relative to the package (hence we use __path__): 
-       InstallModePath = os.path.join(__path__[0], "Dicts/")
-       if os.path.isfile(os.path.join(InstallModePath, "dicomV3.dic")):
-               os.environ["GDCM_DICT_PATH"] = InstallModePath
-               PreInstallModePath = None
-       else:
-               PreInstallModePath = os.path.join(__path__[0], "..", "Dicts/")
-               if os.path.isfile(os.path.join(PreInstallModePath, "dicomV3.dic")):
-                       os.environ["GDCM_DICT_PATH"] = PreInstallModePath
-                       InstallModePath = None
-               else:
-                       print "Unfound gdcm dictionaries path"
-                       sys.exit(1)
+   Path = BuildInstallOrPreinstallPath("Dicts", "dicomV3.dic")
+   if not Path:
+      print "Path to dictionaries is mandatory. Exiting"
+      sys.exit(1)
+   os.environ["GDCM_DICT_PATH"] = Path
+
+### Set up the path to the data images for the demos.
+GDCM_DATA_PATH = BuildInstallOrPreinstallPath("Test", "test.acr")
+if not GDCM_DATA_PATH:
+   print "GDCM_DATA_PATH is not setup properly: unfound Test directory"
+
+### Set up the path to the data images of the test suite.
+GDCM_TEST_DATA_PATH = BuildInstallOrPreinstallPath("gdcmData")
 
 ### Import the swig generated shadow classes.
 try:
-       from gdcm import *
+   import gdcm
 except ImportError,e:
-       print e
-       raise ImportError, "gdcm extension not imported."
-
-### Set up the path to the data images (for examples and test suite)
-if InstallModePath:
-       GDCM_DATA_PATH = os.path.join(__path__[0], "Data")
-else:
-       if PreInstallModePath:
-               GDCM_DATA_PATH = os.path.join(__path__[0], "..", "Data")
-       else:
-               print "Unfound data path"
-               sys.exit(1)
-
-### Get filename from command line or default it
-try:
-       FileName = sys.argv[1]
-except IndexError:
-       FileName = os.path.join(GDCM_DATA_PATH, "test.acr")
+   print e
+   raise ImportError, "gdcm extension not imported."
+
+### Expose only the necessary stuff
+gdcmHeader = gdcm.gdcmHeader
+gdcmDictSet = gdcm.gdcmDictSet
+gdcmFile = gdcm.gdcmFile
+
+def GetPubDictTagNames():
+   return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNames()
 
-if not os.path.isfile(FileName):
-       print "Cannot open file ", FileName
-       sys.exit()
+def GetPubDictTagNamesByCategory():
+   return gdcm.cvar.gdcmGlob.GetDicts().GetPubDictTagNamesByCategory()