Convert system.connected_users to the new data model.

This commit is contained in:
Fabio Manganiello 2023-04-23 01:12:07 +02:00
parent 259b42bdd6
commit 387616ea96
Signed by: blacklight
GPG key ID: D90FBA7F76362774
7 changed files with 91 additions and 47 deletions

View file

@ -8,30 +8,6 @@ class SystemResponse(Response):
pass pass
class ConnectUserResponse(SystemResponse):
def __init__(
self,
name: str,
terminal: str,
host: str,
started: datetime,
pid: Optional[int] = None,
*args,
**kwargs
):
super().__init__(
*args,
output={
'name': name,
'terminal': terminal,
'host': host,
'started': started,
'pid': pid,
},
**kwargs
)
class ProcessResponse(SystemResponse): class ProcessResponse(SystemResponse):
def __init__( def __init__(
self, self,
@ -93,11 +69,6 @@ class SystemResponseList(SystemResponse):
super().__init__(output=[r.output for r in responses], *args, **kwargs) super().__init__(output=[r.output for r in responses], *args, **kwargs)
class ConnectedUserResponseList(SystemResponseList):
def __init__(self, responses: List[ConnectUserResponse], *args, **kwargs):
super().__init__(responses=responses, *args, **kwargs)
class ProcessResponseList(SystemResponseList): class ProcessResponseList(SystemResponseList):
def __init__(self, responses: List[ProcessResponse], *args, **kwargs): def __init__(self, responses: List[ProcessResponse], *args, **kwargs):
super().__init__(responses=responses, *args, **kwargs) super().__init__(responses=responses, *args, **kwargs)

View file

@ -24,8 +24,6 @@ from platypush.entities.system import (
SystemTemperature, SystemTemperature,
) )
from platypush.message.response.system import ( from platypush.message.response.system import (
ConnectedUserResponseList,
ConnectUserResponse,
ProcessResponseList, ProcessResponseList,
ProcessResponse, ProcessResponse,
) )
@ -56,6 +54,8 @@ from platypush.schemas.system import (
SystemInfoSchema, SystemInfoSchema,
Temperature, Temperature,
TemperatureSchema, TemperatureSchema,
User,
UserSchema,
) )
@ -403,26 +403,17 @@ class SystemPlugin(SensorPlugin, EntityManager):
battery = self._sensors_battery() battery = self._sensors_battery()
return BatterySchema().dump(battery) if battery else None # type: ignore return BatterySchema().dump(battery) if battery else None # type: ignore
def _connected_users(self) -> List[User]:
return UserSchema().load(psutil.users(), many=True) # type: ignore
@action @action
def connected_users(self) -> ConnectedUserResponseList: def connected_users(self) -> List[dict]:
""" """
Get the list of connected users. Get the list of connected users.
:return: List of :class:`platypush.message.response.system.ConnectUserResponse`.
"""
users = psutil.users()
return ConnectedUserResponseList( :return: .. schema:: system.UserSchema
[ """
ConnectUserResponse( return UserSchema().dump(self._connected_users(), many=True)
name=u.name,
terminal=u.terminal,
host=u.host,
started=datetime.fromtimestamp(u.started),
pid=u.pid,
)
for u in users
]
)
@action @action
def processes(self, filter: Optional[str] = '') -> ProcessResponseList: def processes(self, filter: Optional[str] = '') -> ProcessResponseList:

View file

@ -18,6 +18,7 @@ from ._model import SystemInfo
from ._network import NetworkInterface, NetworkInterfaceSchema from ._network import NetworkInterface, NetworkInterfaceSchema
from ._schemas import SystemInfoSchema from ._schemas import SystemInfoSchema
from ._temperature import Temperature, TemperatureSchema from ._temperature import Temperature, TemperatureSchema
from ._user import User, UserSchema
__all__ = [ __all__ = [
@ -48,4 +49,6 @@ __all__ = [
"SystemInfoSchema", "SystemInfoSchema",
"Temperature", "Temperature",
"TemperatureSchema", "TemperatureSchema",
"User",
"UserSchema",
] ]

View file

@ -0,0 +1,4 @@
from ._model import User
from ._schemas import UserSchema
__all__ = ['User', 'UserSchema']

View file

@ -0,0 +1,22 @@
from datetime import datetime
from dateutil.tz import gettz
from marshmallow import pre_load
from .._base import SystemBaseSchema
class UserBaseSchema(SystemBaseSchema):
"""
Base schema for system users.
"""
@pre_load
def pre_load(self, data: dict, **_) -> dict:
data = super().pre_load(data)
started_ts = data.pop('started', None)
if started_ts is not None:
data['started'] = datetime.fromtimestamp(started_ts).replace(tzinfo=gettz())
data['username'] = data.pop('name', data.pop('username', None))
return data

View file

@ -0,0 +1,46 @@
from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional
@dataclass
class User:
"""
System user wrapper.
"""
username: str = field(
metadata={
'metadata': {
'description': 'Username',
'example': 'root',
}
}
)
terminal: Optional[str] = field(
metadata={
'metadata': {
'description': 'Identifier of the terminal the user is connected to',
'example': 'pts/1',
}
}
)
started: Optional[datetime] = field(
metadata={
'metadata': {
'description': 'When the user session started',
'example': 'pts/1',
}
}
)
pid: Optional[int] = field(
metadata={
'metadata': {
'description': 'PID of the process that holds the session',
'example': 12345,
}
}
)

View file

@ -0,0 +1,7 @@
from marshmallow_dataclass import class_schema
from ._base import UserBaseSchema
from ._model import User
UserSchema = class_schema(User, base_schema=UserBaseSchema)