home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / hal.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  3.2 KB  |  105 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox 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 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import re
  20.  
  21. from checkbox.lib.cache import cache
  22. from checkbox.lib.update import recursive_update
  23.  
  24. from checkbox.properties import String
  25. from checkbox.registries.command import CommandRegistry
  26. from checkbox.registries.data import DataRegistry
  27. from checkbox.registries.map import MapRegistry
  28.  
  29.  
  30. class DeviceRegistry(DataRegistry):
  31.     """Registry for HAL device information.
  32.  
  33.     Each item contained in this registry consists of the properties of
  34.     the corresponding HAL device.
  35.     """
  36.  
  37.     @cache
  38.     def items(self):
  39.         all = {}
  40.         current = None
  41.         for line in self.split("\n"):
  42.             match = re.match(r"  (.*) = (.*) \((.*?)\)", line)
  43.             if match:
  44.                 keys = match.group(1).split(".")
  45.                 value = match.group(2).strip()
  46.                 type_name = match.group(3)
  47.                 if type_name == "bool":
  48.                     value = bool(value == "true")
  49.                 elif type_name == "double":
  50.                     value = float(value.split()[0])
  51.                 elif type_name == "int" or type_name == "uint64":
  52.                     value = int(value.split()[0])
  53.                 elif type_name == "string":
  54.                     value = str(value.strip("'"))
  55.                 elif type_name == "string list":
  56.                     value = [v.strip("'")
  57.                             for v in value.strip("{}").split(", ")]
  58.                 else:
  59.                     raise Exception, "Unknown type: %s" % type_name
  60.  
  61.                 for key in reversed(keys):
  62.                     value = {key: value}
  63.  
  64.                 recursive_update(all, value)
  65.  
  66.         items = []
  67.         for key, value in all.items():
  68.             value = MapRegistry(value)
  69.             items.append((key, value))
  70.  
  71.         return items
  72.  
  73.  
  74. class HalRegistry(CommandRegistry):
  75.     """Registry for HAL information.
  76.  
  77.     Each item contained in this registry consists of the udi as key and
  78.     the corresponding device registry as value.
  79.     """
  80.  
  81.     # Command to retrieve hal information.
  82.     command = String(default="lshal")
  83.  
  84.     @cache
  85.     def items(self):
  86.         items = []
  87.         for block in self.split("\n\n"):
  88.             lines = block.split("\n")
  89.             while lines:
  90.                 line = lines.pop(0)
  91.                 match = re.match(r"udi = '(.*)'", line)
  92.                 if match:
  93.                     udi = match.group(1)
  94.                     key = udi.split("/")[-1]
  95.                     break
  96.  
  97.             if lines:
  98.                 value = DeviceRegistry("\n".join(lines))
  99.                 items.append((key, value))
  100.  
  101.         return items
  102.  
  103.  
  104. factory = HalRegistry
  105.