MythTV  master
gate.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # smolt - Fedora hardware profiler
3 #
4 # Copyright (C) 2009 Sebastian Pipping <sebastian@pipping.org>
5 #
6 # This program 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 of the License, or
9 # (at your option) any later version.
10 #
11 # This program 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 this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 from builtins import str
21 from builtins import object
22 import configparser
23 import os
24 import logging
25 
26 
27 class _GateBase(object):
28  def process(self, data_set, value_if_granted, value_else):
29  if self.grants(data_set):
30  return value_if_granted
31  else:
32  return value_else
33 
34 
36  def __init__(self, config_files=None):
37  if config_files is None:
38  config_files = [
39  '/etc/smolt/client.cfg',
40  os.path.expanduser('~/.smolt/client.cfg'),
41  ]
42  self.config = configparser.ConfigParser()
43  self.config.read(config_files)
44 
45  def grants(*args):
46  assert 2 <= len(args) <= 3
47  if len(args) == 2:
48  self, data_set = args
49  distro = 'any'
50  else:
51  self, distro, data_set = args
52  res = self._grants(distro, data_set)
53  logging.debug('Section "%s", key "%s" --> "%s"' % (distro, data_set, str(res)))
54  return res
55 
56  def _grants(self, distro, data_set):
57  try:
58  return self.config.getboolean(distro, data_set)
59  except (ValueError,
60  configparser.NoOptionError,
61  configparser.NoSectionError):
62  # TODO warn about error?
63  # Allow if in doubt - backwards compat
64  return True
65 
66 
68  def __init__(self, grant):
69  """
70  >>> gate = _FakeGate(grant=True)
71  >>> gate.grants("whatever")
72  True
73  >>> gate = _FakeGate(grant=False)
74  >>> gate.grants("whatever")
75  False
76  """
77  self._granted = grant
78 
79  def grants(self, *args):
80  return self._granted
81 
82 
84  return _Gate()
85 
86 
87 def create_gate_from_file(filename):
88  return _Gate([filename, ])
89 
90 
92  """
93  >>> create_passing_gate().grants("whatever")
94  True
95  """
96  return _FakeGate(grant=True)
97 
98 
100  """
101  >>> create_blocking_gate().grants("whatever")
102  False
103  """
104  return _FakeGate(grant=False)
hardwareprofile.gate._Gate
Definition: gate.py:35
discid.disc.read
def read(device=None, features=[])
Definition: disc.py:35
hardwareprofile.gate._FakeGate.__init__
def __init__(self, grant)
Definition: gate.py:68
hardwareprofile.gate._GateBase
Definition: gate.py:27
hardwareprofile.gate._FakeGate.grants
def grants(self, *args)
Definition: gate.py:79
hardwareprofile.gate._Gate.__init__
def __init__(self, config_files=None)
Definition: gate.py:36
hardwareprofile.gate.create_gate_from_file
def create_gate_from_file(filename)
Definition: gate.py:87
hardwareprofile.gate.create_passing_gate
def create_passing_gate()
Definition: gate.py:91
hardwareprofile.gate._FakeGate._granted
_granted
Definition: gate.py:77
hardwareprofile.gate._GateBase.process
def process(self, data_set, value_if_granted, value_else)
Definition: gate.py:28
hardwareprofile.gate._FakeGate
Definition: gate.py:67
hardwareprofile.gate._Gate.grants
def grants(*args)
Definition: gate.py:45
hardwareprofile.gate._Gate._grants
def _grants(self, distro, data_set)
Definition: gate.py:56
hardwareprofile.gate._Gate.config
config
Definition: gate.py:42
hardwareprofile.gate.create_blocking_gate
def create_blocking_gate()
Definition: gate.py:99
hardwareprofile.gate.create_default_gate
def create_default_gate()
Definition: gate.py:83