Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / prefs.py
1 #
2 # Copyright 2006 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 import gnuradio_swig_python as gsp
23 _prefs_base = gsp.gr_prefs
24
25
26 import ConfigParser
27 import os
28 import os.path
29 import sys
30
31
32 def _user_prefs_filename():
33     return os.path.expanduser('~/.gnuradio/config.conf')
34         
35 def _sys_prefs_dirname():
36     return os.path.join(gsp.prefix(), 'etc/gnuradio/conf.d')
37
38 def _bool(x):
39     """
40     Try to coerce obj to a True or False
41     """
42     if isinstance(x, bool):
43         return x
44     if isinstance(x, (float, int)):
45         return bool(x)
46     raise TypeError, x
47         
48
49 class _prefs(_prefs_base):
50     """
51     Derive our 'real class' from the stubbed out base class that has support
52     for SWIG directors.  This allows C++ code to magically and transparently
53     invoke the methods in this python class.
54     """
55     def __init__(self):
56         _prefs_base.__init__(self)
57         self.cp = ConfigParser.RawConfigParser()
58
59     def _sys_prefs_filenames(self):
60         dir = _sys_prefs_dirname()
61         try:
62             fnames = os.listdir(dir)
63         except (IOError, OSError):
64             return []
65         fnames.sort()
66         return [os.path.join(dir, f) for f in fnames]
67
68         
69     def _read_files(self):
70         filenames = self._sys_prefs_filenames()
71         filenames.append(_user_prefs_filename())
72         #print "filenames: ", filenames
73         self.cp.read(filenames)
74
75     def __getattr__(self, name):
76         return getattr(self.cp, name)
77
78     # ----------------------------------------------------------------
79     # These methods override the C++ virtual methods of the same name
80     # ----------------------------------------------------------------
81     def has_section(self, section):
82         return self.cp.has_section(section)
83
84     def has_option(self, section, option):
85         return self.cp.has_option(section, option)
86
87     def get_string(self, section, option, default_val):
88         try:
89             return self.cp.get(section, option)
90         except:
91             return default_val
92
93     def get_bool(self, section, option, default_val):
94         try:
95             return self.cp.getboolean(section, option)
96         except:
97             return default_val
98
99     def get_long(self, section, option, default_val):
100         try:
101             return self.cp.getint(section, option)
102         except:
103             return default_val
104         
105     def get_double(self, section, option, default_val):
106         try:
107             return self.cp.getfloat(section, option)
108         except:
109             return default_val
110     # ----------------------------------------------------------------
111     #              End override of C++ virtual methods
112     # ----------------------------------------------------------------
113
114
115 _prefs_db = _prefs()
116
117 # if GR_DONT_LOAD_PREFS is set, don't load them.
118 # (make check uses this to avoid interactions.)
119 if os.getenv("GR_DONT_LOAD_PREFS", None) is None:
120     _prefs_db._read_files()
121     
122
123 _prefs_base.set_singleton(_prefs_db)    # tell C++ what instance to use
124
125 def prefs():
126     """
127     Return the global preference data base
128     """
129     return _prefs_db