Skip to content

Commit

Permalink
Code refactoring and cleanup. Organized imports. Added temperature-re…
Browse files Browse the repository at this point in the history
…ader classes. Fixed bug,
  • Loading branch information
eterey committed Jul 25, 2016
1 parent d836f01 commit 5c8bd18
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 76 deletions.
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ Class "CPU"

.. code-block:: python
>>> from pyspectator.processor import CPU
>>> from pyspectator.processor import Cpu
>>> from time import sleep
>>> cpu = CPU(monitoring_latency=1)
>>> cpu = Cpu(monitoring_latency=1)
>>> with cpu:
... for _ in range(8):
... cpu.load, cpu.temperature
Expand All @@ -97,4 +97,3 @@ Class "CPU"
(7.0, 54)
(10.2, 54)
(6.6, 54)
3 changes: 3 additions & 0 deletions pyspectator/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ def __iter__(self):

def __contains__(self, item):
return self.__storage.__contains__(item)


__all__ = ['LimitedTimeTable']
7 changes: 5 additions & 2 deletions pyspectator/computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime
from pyspectator.monitoring import AbcMonitor
from pyspectator.memory import NonvolatileMemory, VirtualMemory, SwapMemory
from pyspectator.processor import CPU
from pyspectator.processor import Cpu
from pyspectator.network import NetworkInterface


Expand All @@ -22,7 +22,7 @@ def __init__(self):
self.__python_version = '{} ver. {}'.format(
platform.python_implementation(), platform.python_version()
)
self.__processor = CPU(monitoring_latency=1)
self.__processor = Cpu(monitoring_latency=1)
self.__nonvolatile_memory = NonvolatileMemory.instances_connected_devices(monitoring_latency=10)
self.__nonvolatile_memory_devices = set(
[dev_info.device for dev_info in self.__nonvolatile_memory]
Expand Down Expand Up @@ -140,3 +140,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
# endregion

pass


__all__ = ['Computer']
3 changes: 3 additions & 0 deletions pyspectator/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ def get_name_reduction(unitbyte):
if unitbyte in UnitByte:
reduction = reductions_en[unitbyte]
return reduction


__all__ = ['UnitByte']
9 changes: 9 additions & 0 deletions pyspectator/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,12 @@ def names_connected_devices():

class DeviceNotFoundException(Exception):
pass


__all__ = [
'AbsMemory',
'VirtualMemory',
'SwapMemory',
'NonvolatileMemory',
'DeviceNotFoundException'
]
3 changes: 3 additions & 0 deletions pyspectator/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ def _monitoring_action(self):
# endregion

pass


__all__ = ['AbsMonitor']
3 changes: 3 additions & 0 deletions pyspectator/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,6 @@ def _monitoring_action(self):
# endregion

pass


__all__ = ['NetworkInterface']
78 changes: 12 additions & 66 deletions pyspectator/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from datetime import datetime, timedelta
from pyspectator.monitoring import AbcMonitor
from pyspectator.collection import LimitedTimeTable
from pyspectator.temperature_reader import LinuxCpuTemperatureReader
from pyspectator.temperature_reader import WindowsCpuTemperatureReader


class CPU(AbcMonitor):
class Cpu(AbcMonitor):
"""Monitoring system of the Central Processing Unit.
:param monitoring_latency: time interval (in seconds) between calls of
Expand All @@ -23,9 +25,9 @@ class CPU(AbcMonitor):
.. code-block:: python
>>> from pyspectator.processor import CPU
>>> from pyspectator.processor import Cpu
>>> from time import sleep
>>> cpu = CPU(monitoring_latency=1)
>>> cpu = Cpu(monitoring_latency=1)
>>> cpu.name
'Intel(R) Core(TM)2 Duo CPU T6570 @ 2.10GHz'
>>> cpu.count
Expand All @@ -50,12 +52,12 @@ class CPU(AbcMonitor):

def __init__(self, monitoring_latency, stats_interval=None):
super().__init__(monitoring_latency)
self.__name = CPU.__get_processor_name()
self.__name = Cpu.__get_processor_name()
self.__count = psutil.cpu_count()
self.__load = None
self.__temperature = None
# Init temperature reader
self.__temperature_reader = CPU.__get_processor_temperature_reader()
self.__temperature_reader = Cpu.__get_processor_temperature_reader()
# Prepare to collect statistics
if stats_interval is None:
stats_interval = timedelta(hours=1)
Expand Down Expand Up @@ -169,73 +171,17 @@ def __get_processor_temperature_reader(cls):
reader = None
os_name = platform.system()
if os_name == 'Windows':
reader = cls.__windows_processor_temperature_reader()
reader = WindowsCpuTemperatureReader.get_reader()
elif os_name == 'Darwin':
# TODO: try to use C lib - https://github.com/lavoiesl/osx-cpu-temp
pass
elif os_name == 'Linux':
reader = cls.__linux__processor_temperature_reader()
return reader

@classmethod
def __windows_processor_temperature_reader(cls):
import wmi
import pythoncom

def temperature_reader():
pythoncom.CoInitialize()
w = wmi.WMI(namespace='root\\wmi')
temperature = w.MSAcpi_ThermalZoneTemperature()[0]
temperature = int(temperature.CurrentTemperature / 10.0 - 273.15)
return temperature
return temperature_reader

@classmethod
def __linux__processor_temperature_reader(cls):

def temperature_reader1(file):
temperature = open(file).read().strip()
temperature = int(temperature) // 1000
return temperature

def temperature_reader2(file):
temperature = open(file).read().strip()
temperature = int(temperature) // 1000
return temperature

def temperature_reader3(file):
temperature = open(file).read().strip()
temperature = temperature.lstrip('temperature :').rstrip(' C')
return int(temperature)

def temperature_reader4(file):
temperature = open(file).read().strip()
temperature = temperature.lstrip('temperature :').rstrip(' C')
return int(temperature)

def temperature_reader5(file):
temperature = open(file).read().strip()
temperature = temperature.lstrip('temperature :').rstrip(' C')
return int(temperature)

readers = {
'/sys/devices/LNXSYSTM:00/LNXTHERM:00/LNXTHERM:01/thermal_zone/temp':
temperature_reader1,
'/sys/bus/acpi/devices/LNXTHERM:00/thermal_zone/temp':
temperature_reader2,
'/proc/acpi/thermal_zone/THM0/temperature': temperature_reader3,
'/proc/acpi/thermal_zone/THRM/temperature': temperature_reader4,
'/proc/acpi/thermal_zone/THR1/temperature': temperature_reader5
}

for file, reader in readers.items():
if os.path.exists(file):
reader = lambda: reader(file)
break
else:
reader = None
reader = LinuxCpuTemperatureReader.get_reader()
return reader

# endregion

pass


__all__ = ['Cpu']
62 changes: 62 additions & 0 deletions pyspectator/temperature_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from functools import partial
from os import path


class LinuxCpuTemperatureReader():
files = [
'/sys/devices/LNXSYSTM:00/LNXTHERM:00/LNXTHERM:01/thermal_zone/temp',
'/sys/bus/acpi/devices/LNXTHERM:00/thermal_zone/temp',
'/proc/acpi/thermal_zone/THM0/temperature',
'/proc/acpi/thermal_zone/THRM/temperature',
'/proc/acpi/thermal_zone/THR1/temperature'
]

@classmethod
def get_reader(cls):
readers = {
cls.files[0]: cls.reader1,
cls.files[1]: cls.reader1,
cls.files[2]: cls.reader2,
cls.files[3]: cls.reader2,
cls.files[4]: cls.reader2
}
for file in cls.files:
if path.exists(file):
reader = readers.get(file)
if reader:
return reader(file)

@classmethod
def reader1(cls, file):
def reader(file):
temperature = open(file).read().strip()
temperature = int(temperature) // 1000
return temperature
return partial(reader, file)

@classmethod
def reader2(cls, file):
def reader(file):
temperature = open(file).read().strip()
temperature = temperature.lstrip('temperature :').rstrip(' C')
return int(temperature)
return partial(reader, file)


class WindowsCpuTemperatureReader():

@classmethod
def get_reader():
import wmi
import pythoncom

def temperature_reader():
pythoncom.CoInitialize()
w = wmi.WMI(namespace='root\\wmi')
temperature = w.MSAcpi_ThermalZoneTemperature()[0]
temperature = int(temperature.CurrentTemperature / 10.0 - 273.15)
return temperature
return temperature_reader


__all__ = ['LinuxCpuTemperatureReader', 'WindowsCpuTemperatureReader']
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ def main():
# Describe installer
settings = {
'name': 'pyspectator',
'version': '1.0.8',
'version': '1.1.0',
'author': 'Maxim Grischuk, Vova Sirenko',
'author_email': '[email protected]',
'maintainer': 'Maxim Grischuk',
'maintainer_email': '[email protected]',
'packages': ['pyspectator'],
'url': 'https://github.com/opium999/pyspectator',
'download_url': 'https://github.com/opium999/pyspectator/releases',
'url': 'https://github.com/uzumaxy/pyspectator',
'download_url': 'https://github.com/uzumaxy/pyspectator/releases',
'license': 'BSD',
'description': 'pyspectator is a cross-platform tool for retrieving '
'full information about computer.',
'description': 'pyspectator is a Python cross-platform tool for '
'monitoring OS resources.',
'long_description': open('README.rst').read(),
'install_requires': requires,
'keywords': [
Expand Down

0 comments on commit 5c8bd18

Please sign in to comment.