MythTV  master
uuiddb.py
Go to the documentation of this file.
1 # smolt - Fedora hardware profiler
2 #
3 # Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 import ConfigParser
20 import logging
21 import os
22 
23 _SECTION = 'MAIN'
24 
25 
26 def _get_option_name(hw_uuid, host):
27  return '%s__%s' % (hw_uuid, host)
28 
29 
30 class _UuidDb:
31  def __init__(self, database_filename):
32  self._database_filename = database_filename
33  self._config = ConfigParser.RawConfigParser()
35  if not self._config.has_section(_SECTION):
36  self._config.add_section(_SECTION)
37 
38  def _flush(self):
39  try:
40  smolt_user_config_dir = os.path.expanduser('~/.smolt/')
41  if not os.path.exists(smolt_user_config_dir):
42  os.mkdir(smolt_user_config_dir, 0700)
43  f = open(self._database_filename, 'w')
44  self._config.write(f)
45  f.close()
46  except:
47  logging.error('Flushing UUID database failed')
48 
49  def get_pub_uuid(self, hw_uuid, host):
50  try:
51  pub_uuid = self._config.get(_SECTION, _get_option_name(hw_uuid, host))
52  logging.info('Public UUID "%s" read from database' % pub_uuid)
53  return pub_uuid
54  except ConfigParser.NoOptionError:
55  return None
56 
57  def set_pub_uuid(self, hw_uuid, host, pub_uuid):
58  for i in (hw_uuid, host, pub_uuid):
59  if not i:
60  raise Exception('No paramater allowed to be None.')
61  self._config.set(_SECTION, _get_option_name(hw_uuid, host), pub_uuid)
62  logging.info('Public UUID "%s" written to database' % pub_uuid)
63  self._flush()
64 
65 
67  from smolt_config import get_config_attr
68  _database_filename = get_config_attr("UUID_DB", os.path.expanduser('~/.smolt/uuiddb.cfg'))
69  return _UuidDb(_database_filename)
def write(text, progress=True)
Definition: mythburn.py:279
def __init__(self, database_filename)
Definition: uuiddb.py:31
def read(device=None, features=[])
Definition: disc.py:35
def create_default_uuiddb()
Definition: uuiddb.py:66
def _get_option_name(hw_uuid, host)
Definition: uuiddb.py:26
def set_pub_uuid(self, hw_uuid, host, pub_uuid)
Definition: uuiddb.py:57
def get_config_attr(attr, default="")
Definition: smolt_config.py:22
def get_pub_uuid(self, hw_uuid, host)
Definition: uuiddb.py:49