Skip to content

Commit

Permalink
Merge pull request #1 from 7eXx/system_info
Browse files Browse the repository at this point in the history
System info
  • Loading branch information
7eXx authored Jan 23, 2024
2 parents 43b10d7 + 744f7bd commit 99c544f
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 63 deletions.
6 changes: 6 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Release Procedure
This guide aims to help how to release a new version of the package.

## Tests
To execute all test from a folder use:
```shell
python -m unitest discover tests/
```

## Setup
Ensure you have general installed ***build***
```shell
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "system_info_texx"
version = "0.0.5"
version = "0.0.8"
authors = [
{ name="Marco Tessari", email="[email protected]" },
]
Expand Down
15 changes: 15 additions & 0 deletions src/system_info_texx/base_sys_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from abc import abstractmethod, ABC


class _BaseSysInfo(ABC):

def get_all_attributes(self):
return self.__dict__

@abstractmethod
def serialize(self) -> str:
pass

@abstractmethod
def format_pretty(self) -> str:
pass
13 changes: 11 additions & 2 deletions src/system_info_texx/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import psutil
import json

from .base_sys_info import _BaseSysInfo

class Cpu:

class Cpu(_BaseSysInfo):

__sensor_file = '/sys/class/thermal/thermal_zone0/temp'

Expand All @@ -25,4 +27,11 @@ def __init__(self) -> None:
self.unit = "°C"

def serialize(self) -> str:
return json.dumps(self.__dict__, ensure_ascii=False)
return json.dumps(self.get_all_attributes(), ensure_ascii=False)

def format_pretty(self) -> str:
output = "CPU : \n"
output += "Temperature : " + str(self.temperature) + " " + self.unit + "\n"
output += "Percentage : " + str(self.percentage) + " %\n"

return output
14 changes: 12 additions & 2 deletions src/system_info_texx/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import json

from .utils import Utils
from .base_sys_info import _BaseSysInfo


class Disk:
class Disk(_BaseSysInfo):
def __init__(self, unit="GB") -> None:
disk_usage = psutil.disk_usage("/")

Expand All @@ -19,4 +20,13 @@ def __init__(self, unit="GB") -> None:
self.percentage = disk_usage[3]

def serialize(self) -> str:
return json.dumps(self.__dict__)
return json.dumps(self.get_all_attributes())

def format_pretty(self) -> str:
output = "Disk: \n"
output += "Total : " + str(self.total) + " " + self.unit + "\n"
output += "Used : " + str(self.used) + " " + self.unit + "\n"
output += "Free : " + str(self.free) + " " + self.unit + "\n"
output += "Percentage : " + str(self.percentage) + " %"

return output
15 changes: 13 additions & 2 deletions src/system_info_texx/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import psutil

from .utils import Utils
from .base_sys_info import _BaseSysInfo


class Memory:
class Memory(_BaseSysInfo):
def __init__(self) -> None:
memory = psutil.virtual_memory()

Expand All @@ -16,4 +17,14 @@ def __init__(self) -> None:
self.free = Utils.convert_byte_to_megabyte(memory[4])

def serialize(self) -> str:
return json.dumps(self.__dict__)
return json.dumps(self.get_all_attributes())

def format_pretty(self) -> str:
output = "Memory: \n"
output += "Total : " + str(self.total) + " " + self.unit + "\n"
output += "Available : " + str(self.available) + " " + self.unit + "\n"
output += "Used : " + str(self.used) + " " + self.unit + "\n"
output += "Free : " + str(self.free) + " " + self.unit + "\n"
output += "Percentage : " + str(self.percentage) + " %\n"

return output
82 changes: 82 additions & 0 deletions src/system_info_texx/system_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import json
from abc import ABC, abstractmethod

from .cpu import Cpu
from .disk import Disk
from .memory import Memory


class SystemInfo(ABC):
@abstractmethod
def serialize(self) -> str:
pass

@abstractmethod
def format_pretty(self) -> str:
pass


class SimpleSystemInfo(SystemInfo):
def __init__(self):
self.cpu = Cpu()
self.memory = Memory()
self.disk = Disk()

def serialize(self) -> str:
return f'{{"cpu": {self.cpu.serialize()}, "memory": {self.memory.serialize()}, "disk": {self.disk.serialize()}}}'

def format_pretty(self) -> str:
output = self.cpu.format_pretty()
output += "--------------------------------------------\n"
output += self.memory.format_pretty()
output += "--------------------------------------------\n"
output += self.disk.format_pretty()

return output


class ExpandedSystemInfo(SystemInfo):
def __init__(self):
cpu = Cpu()
memory = Memory()
disk = Disk()

self.cpu_temp = cpu.temperature
self.cpu_perc = cpu.percentage
self.cpu_unit = cpu.unit

self.mem_tot = memory.total
self.mem_ava = memory.available
self.mem_use = memory.used
self.mem_fre = memory.free
self.mem_uni = memory.unit
self.mem_per = memory.percentage

self.disk_tot = disk.total
self.disk_use = disk.used
self.disk_fre = disk.free
self.disk_uni = disk.unit
self.disk_per = disk.percentage

def serialize(self) -> str:
return json.dumps(self.__dict__, ensure_ascii=False)

def format_pretty(self) -> str:
output = "CPU: \n"
output += "Temperature : " + str(self.cpu_temp) + " " + self.cpu_unit + "\n"
output += "Percentage : " + str(self.cpu_perc) + " %\n"
output += "--------------------------------------------\n"
output += "Memory: \n"
output += "Total : " + str(self.mem_tot) + " " + self.mem_uni + "\n"
output += "Available : " + str(self.mem_ava) + " " + self.mem_uni + "\n"
output += "Used : " + str(self.mem_use) + " " + self.mem_uni + "\n"
output += "Free : " + str(self.mem_fre) + " " + self.mem_uni + "\n"
output += "Percentage : " + str(self.mem_per) + " %\n"
output += "--------------------------------------------\n"
output += "Disk: \n"
output += "Total : " + str(self.disk_tot) + " " + self.disk_uni + "\n"
output += "Used : " + str(self.disk_use) + " " + self.disk_uni + "\n"
output += "Free : " + str(self.disk_fre) + " " + self.disk_uni + "\n"
output += "Percentage : " + str(self.disk_per) + " %\n"

return output
48 changes: 48 additions & 0 deletions tests/test_cpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import unittest
from unittest.mock import patch, Mock

from src.system_info_texx.cpu import Cpu


class TestCpu(unittest.TestCase):
def setUp(self):
# Create a Cpu instance
self.cpu = Cpu()

@patch('platform.system', return_value='Linux')
@patch('io.open', create=True)
def test_init_linux(self, mock_io_open, mock_platform_system):
# Set up mock file content and open function for Linux platform
mock_io_open.return_value.__enter__ = lambda s: s
mock_io_open.return_value.__exit__ = Mock()
mock_io_open.return_value.read.return_value = '50000\n' # Assuming a temperature value of 50.000°C

# Create a Cpu instance on Linux
cpu_linux = Cpu()

# Check if the temperature is correctly set
self.assertEqual(cpu_linux.temperature, 50.0)

@patch('platform.system', return_value='Windows')
def test_init_windows(self, mock_platform_system):
# Create a Cpu instance on Windows
cpu_windows = Cpu()

# Check if the temperature is set to 0 on non-Linux platforms
self.assertEqual(cpu_windows.temperature, 0)

def test_serialize(self):
# Set up values for the Cpu instance
self.cpu.percentage = 75.0
self.cpu.temperature = 60.0
self.cpu.min_temp = 0
self.cpu.max_temp = 100
self.cpu.unit = "°C"

# Call the serialize method
result = self.cpu.serialize()

# Check if the result matches the expected JSON string
expected_result = '{"percentage": 75.0, "temperature": 60.0, "min_temp": 0, "max_temp": 100, "unit": "°C"}'
self.assertEqual(result, expected_result)

45 changes: 45 additions & 0 deletions tests/test_disk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
from unittest.mock import Mock, patch
import psutil
import json
from src.system_info_texx.disk import Disk, Utils


class TestDisk(unittest.TestCase):
@patch('psutil.disk_usage', return_value=(1024 * 1024 * 1024 * 10, 1024 * 1024 * 1024 * 5, 1024 * 1024 * 1024 * 2, 50.0))
def test_init(self, mock_disk_usage):
# Create a Disk instance
disk = Disk(unit="GB")

# Check if the attributes are set correctly
self.assertEqual(disk.unit, "GB")
self.assertEqual(disk.total, 10)
self.assertEqual(disk.used, 5)
self.assertEqual(disk.free, 2)
self.assertEqual(disk.percentage, 50.0)

def test_init_invalid_unit(self):
# Create a Disk instance with an invalid unit
disk = Disk(unit="InvalidUnit")

# Check if the unit is set to "B" as a default
self.assertEqual(disk.unit, "B")

def test_serialize(self):
# Set up values for the Disk instance
disk = Disk(unit="GB")
disk.total = 10
disk.used = 5
disk.free = 2
disk.percentage = 50.0

# Call the serialize method
result = disk.serialize()

# Check if the result matches the expected JSON string
expected_result = '{"unit": "GB", "total": 10, "used": 5, "free": 2, "percentage": 50.0}'
self.assertEqual(result, expected_result)


if __name__ == '__main__':
unittest.main()
42 changes: 42 additions & 0 deletions tests/test_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from unittest.mock import Mock, patch
import json
import psutil
from src.system_info_texx.memory import Memory, Utils


class TestMemory(unittest.TestCase):
@patch('psutil.virtual_memory', return_value=(1024 * 1024 * 1024, 512 * 1024 * 1024, 50.0, 256 * 1024 * 1024, 256 * 1024 * 1024))
def test_init(self, mock_virtual_memory):
# Create a Memory instance
memory = Memory()

# Check if the attributes are set correctly
self.assertEqual(memory.unit, "MB")
self.assertEqual(memory.total, 1024) # 1024 MB = 1 GB
self.assertEqual(memory.available, 512) # 512 MB
self.assertEqual(memory.percentage, 50.0)
self.assertEqual(memory.used, 256) # 256 MB
self.assertEqual(memory.free, 256) # 256 MB

def test_serialize(self):
# Set up values for the Memory instance
memory = Memory()
memory.unit = "MB"
memory.total = 1024
memory.available = 512
memory.percentage = 50.0
memory.used = 256
memory.free = 256

# Call the serialize method
result = memory.serialize()

# Check if the result matches the expected JSON string
expected_result = '{"unit": "MB", "total": 1024, "available": 512, "percentage": 50.0, "used": 256, "free": 256}'
self.assertEqual(result, expected_result)


if __name__ == '__main__':
unittest.main()

Loading

0 comments on commit 99c544f

Please sign in to comment.