Rename to pypcilib.
[pypcilib.git] / pypcimap.py
1 #!/usr/bin/python -tt
2 # Copyright 2007 The Trustees of Princeton University
3 # Author: Daniel Hokka Zakrisson
4 # $Id$
5 # vim:ts=4:expandtab
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met: 
10
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 #       
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 #       
19 # * Neither the name of the copyright holder nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #       
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
27 # UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
33 # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE. 
35
36 import os
37 import re
38
39 class PCIMap:
40     """Encapsulates modules.pcimap"""
41     def __init__(self, filename):
42         self.list = []
43         self.read(filename)
44     def get(self, tuple):
45         """Returns a list of candidate modules for the PCI device specified in tuple"""
46         ret = []
47         for i in self.list:
48             if ((i[1] == tuple[0] or i[1] == 0xffffffff) and
49                 (i[2] == tuple[1] or i[2] == 0xffffffff) and
50                 (i[3] == tuple[2] or i[3] == 0xffffffff) and
51                 (i[4] == tuple[3] or i[4] == 0xffffffff) and
52                 (i[5] == (tuple[4] & i[6]))):
53                 ret.append(i[0])
54         return ret
55     def add(self, list):
56         # FIXME: check values
57         self.list.append(list)
58     def read(self, filename):
59         f = file(filename)
60         pattern = re.compile("(\\S+)\\s+0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\s0x([0-9A-Fa-f]+)\\n")
61         while True:
62             line = f.readline()
63             if line == "":
64                 break
65             if line[0] == '#' or line[0] == '\n':
66                 continue
67             match = pattern.match(line)
68             if not match:
69                 continue
70             self.add([match.group(1),
71                 int(match.group(2), 16),
72                 int(match.group(3), 16),
73                 int(match.group(4), 16),
74                 int(match.group(5), 16),
75                 int(match.group(6), 16),
76                 int(match.group(7), 16),
77                 int(match.group(8), 16)])
78         f.close()