2 # Copyright 2006 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 os.path.join(gsp.prefix(), '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()
59 def _sys_prefs_filenames(self):
60 dir = _sys_prefs_dirname()
62 fnames = os.listdir(dir)
63 except (IOError, OSError):
66 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 def __getattr__(self, name):
76 return getattr(self.cp, name)
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)
84 def has_option(self, section, option):
85 return self.cp.has_option(section, option)
87 def get_string(self, section, option, default_val):
89 return self.cp.get(section, option)
93 def get_bool(self, section, option, default_val):
95 return self.cp.getboolean(section, option)
99 def get_long(self, section, option, default_val):
101 return self.cp.getint(section, option)
105 def get_double(self, section, option, default_val):
107 return self.cp.getfloat(section, option)
110 # ----------------------------------------------------------------
111 # End override of C++ virtual methods
112 # ----------------------------------------------------------------
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()
123 _prefs_base.set_singleton(_prefs_db) # tell C++ what instance to use
127 Return the global preference data base