~ubuntu-branches/ubuntu/precise/checkbox/precise

« back to all changes in this revision

Viewing changes to registries/hal.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 16:46:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090120164615-7iz6nmlef41h4vx2
Tags: 0.4
* Setup bzr-builddeb in native mode.
* Removed LGPL notice from the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2008 Canonical
 
3
#
 
4
# Written by Marc Tardif <marc@interunion.ca>
 
5
#
 
6
# This file is part of Checkbox.
 
7
#
 
8
# Checkbox is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# Checkbox is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
import re
 
22
 
 
23
from checkbox.lib.cache import cache
 
24
from checkbox.lib.update import recursive_update
 
25
 
 
26
from checkbox.registries.command import CommandRegistry
 
27
from checkbox.registries.data import DataRegistry
 
28
from checkbox.registries.map import MapRegistry
 
29
 
 
30
 
 
31
class DeviceRegistry(DataRegistry):
 
32
    """Registry for HAL device information.
 
33
 
 
34
    Each item contained in this registry consists of the properties of
 
35
    the corresponding HAL device.
 
36
    """
 
37
 
 
38
    @cache
 
39
    def items(self):
 
40
        all = {}
 
41
        current = None
 
42
        for line in self.split("\n"):
 
43
            match = re.match(r"  (.*) = (.*) \((.*?)\)", line)
 
44
            if match:
 
45
                keys = match.group(1).split(".")
 
46
                value = match.group(2).strip()
 
47
                type_name = match.group(3)
 
48
                if type_name == "bool":
 
49
                    value = bool(value == "true")
 
50
                elif type_name == "double":
 
51
                    value = float(value.split()[0])
 
52
                elif type_name == "int" or type_name == "uint64":
 
53
                    value = int(value.split()[0])
 
54
                elif type_name == "string":
 
55
                    value = str(value.strip("'"))
 
56
                elif type_name == "string list":
 
57
                    value = [v.strip("'")
 
58
                            for v in value.strip("{}").split(", ")]
 
59
                else:
 
60
                    raise Exception, "Unknown type: %s" % type_name
 
61
 
 
62
                for key in reversed(keys):
 
63
                    value = {key: value}
 
64
 
 
65
                recursive_update(all, value)
 
66
 
 
67
        items = []
 
68
        for key, value in all.items():
 
69
            value = MapRegistry(None, value)
 
70
            items.append((key, value))
 
71
 
 
72
        return items
 
73
 
 
74
 
 
75
class HalRegistry(CommandRegistry):
 
76
    """Registry for HAL information.
 
77
 
 
78
    Each item contained in this registry consists of the udi as key and
 
79
    the corresponding device registry as value.
 
80
    """
 
81
 
 
82
    @cache
 
83
    def items(self):
 
84
        items = []
 
85
        for block in self.split("\n\n"):
 
86
            lines = block.split("\n")
 
87
            while lines:
 
88
                line = lines.pop(0)
 
89
                match = re.match(r"udi = '(.*)'", line)
 
90
                if match:
 
91
                    udi = match.group(1)
 
92
                    key = udi.split("/")[-1]
 
93
                    break
 
94
 
 
95
            if lines:
 
96
                value = DeviceRegistry(None, "\n".join(lines))
 
97
                items.append((key, value))
 
98
 
 
99
        return items
 
100
 
 
101
 
 
102
factory = HalRegistry