#!/usr/bin/python -tt
# -*- coding: UTF-8 -*-
# vim: sw=4 ts=4 et:
#
# Copyright (C) 2007 Andy Shevchenko
#
# Licensed under the Academic Free License version 2.1
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

"""
This example of the lspci tool.
All printed data are got via HAL.
Used class is ListBus.
"""

__author__ = "Andy Shevchenko <andy@smile.org.ua>"
__revision__ = "$Id: lspci 23 2007-10-16 05:57:33Z andy $"

from minihallib.ListBus import ListBus

HARDWARE_BUS = 'pci'

def main():
    """ Main rountine """
    list_bus = ListBus()

    # Fill internal buffer with PCI devices by their info.bus tag
    list_bus.fill(HARDWARE_BUS)

    for udi in list_bus.lsdev(HARDWARE_BUS):
        hal_device = list_bus.get(udi)

        # Get several parameters of the given device """
        syspath = hal_device.get('pci.linux.sysfs_path', '')
        cls = hal_device.get('pci.device_class', 0)
        subcls = hal_device.get('pci.device_subclass', 0)
        vendor = hal_device.get('pci.vendor', '')
        product = hal_device.get('pci.product', '')

        # PCI bus and device numbers are got through small trick
        bus_str = "%s" % ':'.join(syspath.split('/')[-1].split(':')[1:])
        # Class string is derived from PCI class and subclass values
        cls_str = "Class %02x%02x:" % (cls, subcls)
        # The PCI vendor and PCI priduct are represented as string
        vp_str = "%s %s" % (vendor, product)

        print bus_str, cls_str, vp_str

if __name__ == "__main__":
    main()
