]> Creatis software - gdcm.git/blob - gdcmPython/__init__.py
363c0bd5d3775fe568a79589ad09888748c8d3de
[gdcm.git] / gdcmPython / __init__.py
1 import os, sys
2
3 ### Setup the path to the dictionaries. WARNING: this needs to be done
4 #   BEFORE importation of gdcm. We consider we found the dictionaries
5 #   path when we encounter the standard dictionary i.e. the file dicomV3.dic.
6 # 1/ first consider the environnement variable.
7 # 2/ when this fails consider the package installation mode i.e. when the
8 #    layout is such that the directory containing the dictionaries is
9 #    BELOW (the root is at the top) the current directory.
10 # 3/ eventually consider the pre-installation mode i.e. when the
11 #    layout is such that the directory containing the dictionaries is
12 #    ABOVE (the root is at the top) the current directory.
13 #   
14 try:
15         os.environ["GDCM_DICT_PATH"]
16         if not os.path.isfile(os.path.join(os.environ["GDCM_DICT_PATH"],
17                                            "dicomV3.dic")):
18                 raise KeyError
19 except KeyError:
20         # Those pathes have to be relative to the package (hence we use __path__): 
21         InstallModePath = os.path.join(__path__[0], "Dicts/")
22         if os.path.isfile(os.path.join(InstallModePath, "dicomV3.dic")):
23                 os.environ["GDCM_DICT_PATH"] = InstallModePath
24                 PreInstallModePath = None
25         else:
26                 PreInstallModePath = os.path.join(__path__[0], "..", "Dicts/")
27                 if os.path.isfile(os.path.join(PreInstallModePath, "dicomV3.dic")):
28                         os.environ["GDCM_DICT_PATH"] = PreInstallModePath
29                         InstallModePath = None
30                 else:
31                         print "Unfound gdcm dictionaries path"
32                         sys.exit(1)
33
34 ### Import the swig generated shadow classes.
35 try:
36         from gdcm import *
37 except ImportError,e:
38         print e
39         raise ImportError, "gdcm extension not imported."
40
41 ### Set up the path to the data images (for examples and test suite)
42 if InstallModePath:
43         GDCM_DATA_PATH = os.path.join(__path__[0], "Data")
44 else:
45         if PreInstallModePath:
46                 GDCM_DATA_PATH = os.path.join(__path__[0], "..", "Data")
47         else:
48                 print "Unfound data path"
49                 sys.exit(1)
50
51 ### Get filename from command line or default it
52 try:
53         FileName = sys.argv[1]
54 except IndexError:
55         FileName = os.path.join(GDCM_DATA_PATH, "test.acr")
56
57 if not os.path.isfile(FileName):
58         print "Cannot open file ", FileName
59         sys.exit()