Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / prefs.py
1 #
2 # Copyright 2006,2009 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 3, 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         self.__getattr__ = lambda self, name: getattr(self.cp, name)
59
60     def _sys_prefs_filenames(self):
61         dir = _sys_prefs_dirname()
62         try:
63             fnames = os.listdir(dir)
64         except (IOError, OSError):
65             return []
66         fnames.sort()
67         return [os.path.join(dir, f) for f in fnames]
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     # ----------------------------------------------------------------
76     # These methods override the C++ virtual methods of the same name
77     # ----------------------------------------------------------------
78     def has_section(self, section):
79         return self.cp.has_section(section)
80
81     def has_option(self, section, option):
82         return self.cp.has_option(section, option)
83
84     def get_string(self, section, option, default_val):
85         try:
86             return self.cp.get(section, option)
87         except:
88             return default_val
89
90     def get_bool(self, section, option, default_val):
91         try:
92             return self.cp.getboolean(section, option)
93         except:
94             return default_val
95
96     def get_long(self, section, option, default_val):
97         try:
98             return self.cp.getint(section, option)
99         except:
100             return default_val
101         
102     def get_double(self, section, option, default_val):
103         try:
104             return self.cp.getfloat(section, option)
105         except:
106             return default_val
107     # ----------------------------------------------------------------
108     #              End override of C++ virtual methods
109     # ----------------------------------------------------------------
110
111
112 _prefs_db = _prefs()
113
114 # if GR_DONT_LOAD_PREFS is set, don't load them.
115 # (make check uses this to avoid interactions.)
116 if os.getenv("GR_DONT_LOAD_PREFS", None) is None:
117     _prefs_db._read_files()
118     
119
120 _prefs_base.set_singleton(_prefs_db)    # tell C++ what instance to use
121
122 def prefs():
123     """
124     Return the global preference data base
125     """
126     return _prefs_db