-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from 7eXx/system_info
System info
- Loading branch information
Showing
11 changed files
with
378 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]" }, | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
Oops, something went wrong.