From: Michael Tremer Date: Sun, 26 Dec 2010 17:54:00 +0000 (+0100) Subject: Add some code commenting. X-Git-Tag: v2.0.0~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e8221cde1a1016e843f8a13b4f3b876bfbd0b3a7;p=oddments%2Ffireinfo.git Add some code commenting. No functional changes. --- diff --git a/fireinfo/cpu.py b/fireinfo/cpu.py index 1fbe055..6b92b93 100644 --- a/fireinfo/cpu.py +++ b/fireinfo/cpu.py @@ -19,7 +19,7 @@ # # ############################################################################### -import os +import os.path import _fireinfo @@ -27,9 +27,19 @@ PROC_CPUINFO = "/proc/cpuinfo" SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d" class CPU(object): + """ + A class that represents the first CPU in a system. + + We get all information form the first CPU (or core) and assume that + all other ones are equal. + """ + __cpuinfo = {} def __init__(self): + """ + Initialize this class by reading all data from /proc/cpuinfo. + """ self.read_cpuinfo() def read_cpuinfo(self): @@ -59,34 +69,58 @@ class CPU(object): @property def bogomips(self): + """ + Return the bogomips of this CPU. + """ return float(self.__cpuinfo["bogomips"]) @property def model(self): + """ + Return the model id of this CPU. + """ return int(self.__cpuinfo["model"]) @property def model_string(self): + """ + Return the model string of this CPU. + """ return self.__cpuinfo["model_name"] @property def vendor(self): + """ + Return the vendor string of this CPU. + """ return self.__cpuinfo["vendor_id"] @property def stepping(self): + """ + Return the stepping id of this CPU. + """ return int(self.__cpuinfo["stepping"]) @property def flags(self): + """ + Return all flags of this CPU. + """ return self.__cpuinfo["flags"].split() @property def speed(self): + """ + Return the speed (in MHz) of this CPU. + """ return float(self.__cpuinfo["cpu_MHz"]) @property def family(self): + """ + Return the family id of this CPU. + """ return int(self.__cpuinfo["cpu_family"]) @property diff --git a/fireinfo/device.py b/fireinfo/device.py index 64d5e5f..01338b2 100644 --- a/fireinfo/device.py +++ b/fireinfo/device.py @@ -19,68 +19,115 @@ # # ############################################################################### -import os -import string - +import os.path class Device(object): + """ + This is an abstract class that represents all devices in the system. + Every single device has its own instance of this class. + """ + def __init__(self, path): - self.path=path - self._uevent={} + """ + Collect all information about the device by reading the + "uevent" file and parsing it. + """ + + # Save the path in /sys to the device. + self.path = path + + # Datastructure to store information we read. + self._uevent = {} + + # Open the uevent file and parse all lines. with open(os.path.join(path, "uevent")) as f: for line in f.readlines(): key, val = line.split("=") - self._uevent[key]=val.rstrip().lower() - + self._uevent[key] = val.rstrip().lower() + @property def driver(self): + """ + Get the driver/kernel module that device is driven by or return None. + """ return self._uevent.get("DRIVER", None) class PCIDevice(Device): + """ + A class that represents all PCI (and PCIe) devices in a system. + """ + subsystem = "pci" - + @property def model(self): + """ + Return the PCI model id of this device. + """ return self._uevent['PCI_ID'].split(":")[1] @property def vendor(self): + """ + Return the PCI vendor id of this device. + """ return self._uevent['PCI_ID'].split(":")[0] @property def deviceclass(self): + """ + Return the PCI device class of this device. + """ return self._uevent['PCI_CLASS'] @property def sub_vendor(self): + """ + Return the PCI vendor sub id of this device. + """ return self._uevent["PCI_SUBSYS_ID"].split(":")[0] @property def sub_model(self): + """ + Return the PCI model sub id of this device. + """ return self._uevent["PCI_SUBSYS_ID"].split(":")[1] class USBDevice(Device): + """ + A class that represents all USB devices in a system. + """ + subsystem = "usb" def pad(self, s): + """ + A function to pad ids that have no leading zeroes. + """ while len(s) < 4: s = "0"+s return s - - + @property def vendor(self): + """ + Return the USB vendor id of this device. + """ return self.pad(self._uevent['PRODUCT'].split("/")[0]) @property def model(self): + """ + Return the USB model id of this device. + """ return self.pad(self._uevent['PRODUCT'].split("/")[1]) @property def deviceclass(self): + """ + Return the USB device class of this device. + """ return self._uevent.get("INTERFACE", None) - - - diff --git a/fireinfo/system.py b/fireinfo/system.py index dce1b64..862aace 100644 --- a/fireinfo/system.py +++ b/fireinfo/system.py @@ -244,6 +244,9 @@ class System(object): @property def language(self): + """ + Return the language code of IPFire or "unknown" if we cannot get it. + """ # Return "unknown" if settings file does not exist. filename = "/var/ipfire/main/settings" if not os.path.exists(filename): @@ -252,19 +255,28 @@ class System(object): with open(filename, "r") as f: for line in f.readlines(): key, val = line.split("=", 1) - if key=="LANGUAGE": + if key == "LANGUAGE": return val.strip() @property def release(self): + """ + Return the system release string. + """ return read_from_file("/etc/system-release") @property def bios_vendor(self): + """ + Return the bios vendor name. + """ return read_from_file("/sys/class/dmi/id/bios_vendor") @property def vendor(self): + """ + Return the vendor string of this system (if any). + """ ret = None for file in ("sys_vendor", "board_vendor", "chassis_vendor",): ret = read_from_file(os.path.join(SYS_CLASS_DMI, file)) @@ -275,6 +287,9 @@ class System(object): @property def model(self): + """ + Return the model string of this system (if any). + """ ret = None for file in ("product_name", "board_model", "chassis_model",): ret = read_from_file(os.path.join(SYS_CLASS_DMI, file)) @@ -285,35 +300,53 @@ class System(object): @property def memory(self): + """ + Return the amount of memory in kilobytes. + """ with open("/proc/meminfo", "r") as f: firstline = f.readline().strip() return int(firstline.split()[1]) @property def kernel_release(self): + """ + Return the kernel release string. + """ return os.uname()[2] @property def root_disk(self): + """ + Return the dev node of the root disk. + """ with open("/etc/mtab", "r") as f: - dev, mountpoint, rest = f.readline().split(" ",2) + dev, mountpoint, rest = f.readline().split(" ", 2) if mountpoint == "/": + # Cut off /dev dev = dev[5:] - # cut off all digits at end of string + # Cut off all digits at end of string while dev[-1] in string.digits: - dev = dev[:-1] - return dev + dev = dev[:-1] + + return dev @property def root_size(self): - path="/sys/block/%s/size" %self.root_disk + """ + Return the size of the root disk in kilobytes. + """ + path = "/sys/block/%s/size" % self.root_disk if not os.path.exists(path): return + with open(path, "r") as f: - return int(f.readline())*512/1024 + return int(f.readline()) * 512 / 1024 @property def root_disk_serial(self): + """ + Return the serial number of the root disk (if any). + """ serial = _fireinfo.get_harddisk_serial("/dev/%s" % self.root_disk) if serial: @@ -321,8 +354,16 @@ class System(object): return serial.strip() def scan(self): - toscan = (("/sys/bus/pci/devices", device.PCIDevice), - ("/sys/bus/usb/devices", device.USBDevice)) + """ + Scan for all devices (PCI/USB) in the system and append them + to our list. + """ + self.devices = [] + + toscan = ( + ("/sys/bus/pci/devices", device.PCIDevice), + ("/sys/bus/usb/devices", device.USBDevice) + ) for path, cls in toscan: if not os.path.exists(path): continue