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

« back to all changes in this revision

Viewing changes to registries/cpuinfo.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
from checkbox.lib.cache import cache
 
22
from checkbox.lib.conversion import string_to_type
 
23
 
 
24
from checkbox.registries.data import DataRegistry
 
25
from checkbox.registries.filename import FilenameRegistry
 
26
from checkbox.registries.link import LinkRegistry
 
27
 
 
28
 
 
29
class ProcessorRegistry(DataRegistry):
 
30
    """Registry for processor information.
 
31
 
 
32
    Each item contained in this registry consists of the information
 
33
    for a single processor in the /proc/cpuinfo file.
 
34
    """
 
35
 
 
36
    def items(self):
 
37
        items = []
 
38
        for line in [l.strip() for l in self.split("\n")]:
 
39
            (key, value) = line.split(":", 1)
 
40
 
 
41
            # Sanitize key so that it can be expressed as
 
42
            # an attribute
 
43
            key = key.strip()
 
44
            key = key.replace(" ", "_")
 
45
            key = key.lower()
 
46
 
 
47
            # Rename processor entry to name
 
48
            if key == "processor":
 
49
                key = "name"
 
50
 
 
51
            # Express value as a list if it is flags
 
52
            value = value.strip()
 
53
            if key == "flags":
 
54
                value = value.split()
 
55
            else:
 
56
                value = string_to_type(value)
 
57
 
 
58
            items.append((key, value))
 
59
 
 
60
        items.append(("processor", LinkRegistry(None, self)))
 
61
 
 
62
        return items
 
63
 
 
64
 
 
65
class CpuinfoRegistry(FilenameRegistry):
 
66
    """Registry for cpuinfo information.
 
67
 
 
68
    Each item contained in this registry consists of the processor number
 
69
    as key and the corresponding processor registry as value.
 
70
    """
 
71
 
 
72
    @cache
 
73
    def items(self):
 
74
        items = []
 
75
        for data in [d.strip() for d in self.split("\n\n") if d]:
 
76
            key = len(items)
 
77
            value = ProcessorRegistry(None, data)
 
78
            items.append((key, value))
 
79
 
 
80
        return items
 
81
 
 
82
 
 
83
factory = CpuinfoRegistry