2 # Copyright 2006,2009 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 import gnuradio_swig_python as gsp
23 _prefs_base = gsp.gr_prefs
32 def _user_prefs_filename():
33 return os.path.expanduser('~/.gnuradio/config.conf')
35 def _sys_prefs_dirname():
36 return '/etc/gnuradio/conf.d'
40 Try to coerce obj to a True or False
42 if isinstance(x, bool):
44 if isinstance(x, (float, int)):
49 class _prefs(_prefs_base):
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.
56 _prefs_base.__init__(self)
57 self.cp = ConfigParser.RawConfigParser()
58 self.__getattr__ = lambda self, name: getattr(self.cp, name)
60 def _sys_prefs_filenames(self):
61 dir = _sys_prefs_dirname()
63 fnames = os.listdir(dir)
64 except (IOError, OSError):
67 return [os.path.join(dir, f) for f in fnames]
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)
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)
81 def has_option(self, section, option):
82 return self.cp.has_option(section, option)
84 def get_string(self, section, option, default_val):
86 return self.cp.get(section, option)
90 def get_bool(self, section, option, default_val):
92 return self.cp.getboolean(section, option)
96 def get_long(self, section, option, default_val):
98 return self.cp.getint(section, option)
102 def get_double(self, section, option, default_val):
104 return self.cp.getfloat(section, option)
107 # ----------------------------------------------------------------
108 # End override of C++ virtual methods
109 # ----------------------------------------------------------------
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()
120 _prefs_base.set_singleton(_prefs_db) # tell C++ what instance to use
124 Return the global preference data base