2018-07-06 02:08:38 +02:00
|
|
|
import ast
|
2022-04-07 00:18:11 +02:00
|
|
|
import contextlib
|
2021-10-16 22:35:37 +02:00
|
|
|
import datetime
|
2023-10-10 01:35:01 +02:00
|
|
|
import functools
|
2018-07-05 09:15:53 +02:00
|
|
|
import hashlib
|
2017-12-18 01:10:51 +01:00
|
|
|
import importlib
|
2018-07-06 02:08:38 +02:00
|
|
|
import inspect
|
2017-12-18 01:10:51 +01:00
|
|
|
import logging
|
2018-01-27 04:31:09 +01:00
|
|
|
import os
|
2021-02-12 22:43:34 +01:00
|
|
|
import pathlib
|
2019-03-16 19:28:47 +01:00
|
|
|
import re
|
2017-12-20 20:25:08 +01:00
|
|
|
import signal
|
2019-02-05 02:30:20 +01:00
|
|
|
import socket
|
2018-11-01 23:34:14 +01:00
|
|
|
import ssl
|
2023-09-30 02:28:20 +02:00
|
|
|
import time
|
2019-02-06 11:51:44 +01:00
|
|
|
import urllib.request
|
2023-10-04 02:27:09 +02:00
|
|
|
from importlib.machinery import SourceFileLoader
|
|
|
|
from importlib.util import spec_from_loader, module_from_spec
|
2023-09-30 02:28:20 +02:00
|
|
|
from multiprocessing import Lock as PLock
|
2023-08-14 10:46:27 +02:00
|
|
|
from tempfile import gettempdir
|
2024-02-22 22:52:52 +01:00
|
|
|
from threading import Event, Lock as TLock
|
2024-05-24 20:20:25 +02:00
|
|
|
from typing import Generator, Optional, Tuple, Type, Union
|
2017-12-18 01:10:51 +01:00
|
|
|
|
2021-10-16 22:35:37 +02:00
|
|
|
from dateutil import parser, tz
|
2021-06-26 11:14:26 +02:00
|
|
|
from redis import Redis
|
2023-02-05 18:05:41 +01:00
|
|
|
from rsa.key import PublicKey, PrivateKey, newkeys
|
2021-06-26 11:14:26 +02:00
|
|
|
|
2020-09-27 01:33:38 +02:00
|
|
|
logger = logging.getLogger('utils')
|
2023-03-28 15:26:45 +02:00
|
|
|
Lock = Union[PLock, TLock] # type: ignore
|
2017-12-18 01:10:51 +01:00
|
|
|
|
2019-07-01 22:26:04 +02:00
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
def get_module_and_method_from_action(action):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Input: action=music.mpd.play
|
|
|
|
Output: ('music.mpd', 'play')
|
|
|
|
"""
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
tokens = action.split('.')
|
|
|
|
module_name = str.join('.', tokens[:-1])
|
|
|
|
method_name = tokens[-1:][0]
|
2019-07-01 22:26:04 +02:00
|
|
|
return module_name, method_name
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_message_class_by_type(msgtype):
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets the class of a message type given as string"""
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
module = importlib.import_module('platypush.message.' + msgtype)
|
2018-02-20 22:58:13 +01:00
|
|
|
except ImportError as e:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.warning('Unsupported message type %s', msgtype)
|
|
|
|
raise RuntimeError(e) from e
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
cls_name = msgtype[0].upper() + msgtype[1:]
|
|
|
|
|
|
|
|
try:
|
|
|
|
msgclass = getattr(module, cls_name)
|
|
|
|
except AttributeError as e:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.warning('No such class in %s: %s', module.__name__, cls_name)
|
|
|
|
raise RuntimeError(e) from e
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
return msgclass
|
|
|
|
|
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
def get_event_class_by_type(type): # pylint: disable=redefined-builtin
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets an event class by type name"""
|
2017-12-24 01:03:26 +01:00
|
|
|
event_module = importlib.import_module('.'.join(type.split('.')[:-1]))
|
|
|
|
return getattr(event_module, type.split('.')[-1])
|
|
|
|
|
|
|
|
|
2019-07-01 22:26:04 +02:00
|
|
|
def get_plugin_module_by_name(plugin_name):
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets the module of a plugin by name (e.g. "music.mpd" or "media.vlc")"""
|
2019-07-01 22:26:04 +02:00
|
|
|
|
|
|
|
module_name = 'platypush.plugins.' + plugin_name
|
|
|
|
try:
|
|
|
|
return importlib.import_module('platypush.plugins.' + plugin_name)
|
|
|
|
except ImportError as e:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.error('Cannot import %s: %s', module_name, e)
|
2019-07-01 22:26:04 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-05-17 00:05:22 +02:00
|
|
|
def get_backend_module_by_name(backend_name):
|
|
|
|
"""Gets the module of a backend by name (e.g. "backend.http" or "backend.mqtt")"""
|
|
|
|
|
|
|
|
module_name = 'platypush.backend.' + backend_name
|
|
|
|
try:
|
|
|
|
return importlib.import_module('platypush.backend.' + backend_name)
|
|
|
|
except ImportError as e:
|
|
|
|
logger.error('Cannot import %s: %s', module_name, e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-10-04 02:27:09 +02:00
|
|
|
def get_plugin_class_by_name(plugin_name) -> Optional[type]:
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets the class of a plugin by name (e.g. "music.mpd" or "media.vlc")"""
|
2019-07-01 22:26:04 +02:00
|
|
|
|
|
|
|
module = get_plugin_module_by_name(plugin_name)
|
|
|
|
if not module:
|
2023-07-23 19:04:01 +02:00
|
|
|
return None
|
2019-07-01 22:26:04 +02:00
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
class_name = getattr(
|
|
|
|
module, ''.join([_.capitalize() for _ in plugin_name.split('.')]) + 'Plugin'
|
|
|
|
)
|
2019-07-01 22:26:04 +02:00
|
|
|
try:
|
2022-04-07 00:18:11 +02:00
|
|
|
return getattr(
|
|
|
|
module, ''.join([_.capitalize() for _ in plugin_name.split('.')]) + 'Plugin'
|
|
|
|
)
|
2019-07-01 22:26:04 +02:00
|
|
|
except Exception as e:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.error('Cannot import class %s: %s', class_name, e)
|
2019-07-01 22:26:04 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-10-09 22:35:08 +02:00
|
|
|
def get_plugin_name_by_class(plugin) -> str:
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets the common name of a plugin (e.g. "music.mpd" or "media.vlc") given its class."""
|
2020-09-19 00:50:22 +02:00
|
|
|
|
|
|
|
from platypush.plugins import Plugin
|
|
|
|
|
|
|
|
if isinstance(plugin, Plugin):
|
|
|
|
plugin = plugin.__class__
|
|
|
|
|
|
|
|
class_name = plugin.__name__
|
|
|
|
class_tokens = [
|
2022-04-07 00:18:11 +02:00
|
|
|
token.lower()
|
|
|
|
for token in re.sub(r'([A-Z])', r' \1', class_name).split(' ')
|
2020-09-19 00:50:22 +02:00
|
|
|
if token.strip() and token != 'Plugin'
|
|
|
|
]
|
|
|
|
|
|
|
|
return '.'.join(class_tokens)
|
|
|
|
|
|
|
|
|
2023-10-04 02:27:09 +02:00
|
|
|
def get_backend_class_by_name(backend_name: str) -> Optional[type]:
|
2023-05-17 00:05:22 +02:00
|
|
|
"""Gets the class of a backend by name (e.g. "backend.http" or "backend.mqtt")"""
|
|
|
|
|
|
|
|
module = get_backend_module_by_name(backend_name)
|
|
|
|
if not module:
|
2023-07-23 19:04:01 +02:00
|
|
|
return None
|
2023-05-17 00:05:22 +02:00
|
|
|
|
|
|
|
class_name = getattr(
|
|
|
|
module,
|
|
|
|
''.join(
|
|
|
|
[
|
|
|
|
token.capitalize()
|
|
|
|
for i, token in enumerate(backend_name.split('.'))
|
|
|
|
if not (i == 0 and token == 'backend')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
+ 'Backend',
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
return getattr(
|
|
|
|
module,
|
|
|
|
''.join([_.capitalize() for _ in backend_name.split('.')]) + 'Backend',
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error('Cannot import class %s: %s', class_name, e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-07-23 19:04:01 +02:00
|
|
|
def get_backend_name_by_class(backend) -> str:
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Gets the common name of a backend (e.g. "http" or "mqtt") given its class."""
|
2020-09-27 01:33:38 +02:00
|
|
|
|
|
|
|
from platypush.backend import Backend
|
|
|
|
|
|
|
|
if isinstance(backend, Backend):
|
|
|
|
backend = backend.__class__
|
|
|
|
|
|
|
|
class_name = backend.__name__
|
|
|
|
class_tokens = [
|
2022-04-07 00:18:11 +02:00
|
|
|
token.lower()
|
|
|
|
for token in re.sub(r'([A-Z])', r' \1', class_name).split(' ')
|
2020-09-27 01:33:38 +02:00
|
|
|
if token.strip() and token != 'Backend'
|
|
|
|
]
|
|
|
|
|
|
|
|
return '.'.join(class_tokens)
|
|
|
|
|
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
def set_timeout(seconds, on_timeout):
|
|
|
|
"""
|
|
|
|
Set a function to be called if timeout expires without being cleared.
|
|
|
|
It only works on the main thread.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
seconds -- Timeout in seconds
|
|
|
|
on_timeout -- Function invoked on timeout unless clear_timeout is called before
|
|
|
|
"""
|
|
|
|
|
2021-04-05 00:58:44 +02:00
|
|
|
def _sighandler(*_):
|
2017-12-20 20:25:08 +01:00
|
|
|
on_timeout()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGALRM, _sighandler)
|
|
|
|
signal.alarm(seconds)
|
|
|
|
|
|
|
|
|
|
|
|
def clear_timeout():
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Clear any previously set timeout"""
|
2017-12-20 20:25:08 +01:00
|
|
|
signal.alarm(0)
|
|
|
|
|
2017-12-18 01:10:51 +01:00
|
|
|
|
2018-07-05 09:15:53 +02:00
|
|
|
def get_hash(s):
|
2022-04-07 00:18:11 +02:00
|
|
|
"""Get the SHA256 hash hex digest of a string input"""
|
2018-07-05 09:15:53 +02:00
|
|
|
return hashlib.sha256(s.encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
|
2018-07-17 01:23:12 +02:00
|
|
|
def get_decorators(cls, climb_class_hierarchy=False):
|
|
|
|
"""
|
|
|
|
Get the decorators of a class as a {"decorator_name": [list of methods]} dictionary
|
2021-09-16 17:53:40 +02:00
|
|
|
|
|
|
|
:param cls: Class type
|
|
|
|
:param climb_class_hierarchy: If set to True (default: False), it will search return the decorators in the parent
|
|
|
|
classes as well
|
2018-07-17 01:23:12 +02:00
|
|
|
:type climb_class_hierarchy: bool
|
|
|
|
"""
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
decorators = {}
|
|
|
|
|
|
|
|
def visit_FunctionDef(node):
|
|
|
|
for n in node.decorator_list:
|
|
|
|
if isinstance(n, ast.Call):
|
2023-07-23 19:04:01 +02:00
|
|
|
name = (
|
|
|
|
n.func.attr
|
|
|
|
if isinstance(n.func, ast.Attribute)
|
|
|
|
else n.func.id # type: ignore
|
|
|
|
)
|
2018-07-06 02:08:38 +02:00
|
|
|
else:
|
|
|
|
name = n.attr if isinstance(n, ast.Attribute) else n.id
|
|
|
|
|
2018-07-17 01:23:12 +02:00
|
|
|
decorators[name] = decorators.get(name, set())
|
|
|
|
decorators[name].add(node.name)
|
|
|
|
|
|
|
|
if climb_class_hierarchy:
|
|
|
|
targets = inspect.getmro(cls)
|
|
|
|
else:
|
|
|
|
targets = [cls]
|
2018-07-06 02:08:38 +02:00
|
|
|
|
|
|
|
node_iter = ast.NodeVisitor()
|
|
|
|
node_iter.visit_FunctionDef = visit_FunctionDef
|
2018-07-17 01:23:12 +02:00
|
|
|
|
|
|
|
for target in targets:
|
2022-04-07 00:18:11 +02:00
|
|
|
with contextlib.suppress(TypeError):
|
2018-07-17 01:23:12 +02:00
|
|
|
node_iter.visit(ast.parse(inspect.getsource(target)))
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return decorators
|
|
|
|
|
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
def get_redis_queue_name_by_message(msg) -> Optional[str]:
|
|
|
|
"""
|
|
|
|
Get the Redis queue name for the response(s) associated to a request
|
|
|
|
message.
|
2018-09-20 12:49:57 +02:00
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
:param msg: Input message, as a :class:`platypush.message.request.Request`
|
|
|
|
object.
|
|
|
|
"""
|
|
|
|
from platypush.message.request import Request
|
2018-09-20 12:49:57 +02:00
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
if not isinstance(msg, Request):
|
|
|
|
logger.warning('Not a valid request (type: %s): %s', type(msg), msg)
|
|
|
|
return None
|
|
|
|
return f'platypush/responses/{msg.id}' if msg.id else None
|
2018-09-20 12:49:57 +02:00
|
|
|
|
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
def _get_ssl_context(
|
|
|
|
context_type=None, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None
|
|
|
|
):
|
2018-11-01 23:34:14 +01:00
|
|
|
if not context_type:
|
2022-04-07 00:18:11 +02:00
|
|
|
ssl_context = ssl.create_default_context(cafile=ssl_cafile, capath=ssl_capath)
|
2018-11-01 23:34:14 +01:00
|
|
|
else:
|
2019-06-21 02:13:14 +02:00
|
|
|
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
2018-11-01 23:34:14 +01:00
|
|
|
|
2023-07-23 19:04:01 +02:00
|
|
|
assert ssl_cert, 'No certificate specified'
|
2018-11-01 23:34:14 +01:00
|
|
|
if ssl_cafile or ssl_capath:
|
2022-04-07 00:18:11 +02:00
|
|
|
ssl_context.load_verify_locations(cafile=ssl_cafile, capath=ssl_capath)
|
2018-11-01 23:34:14 +01:00
|
|
|
|
|
|
|
ssl_context.load_cert_chain(
|
|
|
|
certfile=os.path.abspath(os.path.expanduser(ssl_cert)),
|
2022-04-07 00:18:11 +02:00
|
|
|
keyfile=os.path.abspath(os.path.expanduser(ssl_key)) if ssl_key else None,
|
2018-11-01 23:34:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return ssl_context
|
|
|
|
|
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
def get_ssl_context(ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Generic builder for SSL context.
|
|
|
|
"""
|
2022-04-07 00:18:11 +02:00
|
|
|
return _get_ssl_context(
|
|
|
|
context_type=None,
|
|
|
|
ssl_cert=ssl_cert,
|
|
|
|
ssl_key=ssl_key,
|
|
|
|
ssl_cafile=ssl_cafile,
|
|
|
|
ssl_capath=ssl_capath,
|
|
|
|
)
|
2018-11-01 23:34:14 +01:00
|
|
|
|
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
def get_ssl_server_context(
|
|
|
|
ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None
|
|
|
|
):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Builder for a server-side SSL context.
|
|
|
|
"""
|
2022-04-07 00:18:11 +02:00
|
|
|
return _get_ssl_context(
|
|
|
|
context_type=ssl.PROTOCOL_TLS_SERVER,
|
|
|
|
ssl_cert=ssl_cert,
|
|
|
|
ssl_key=ssl_key,
|
|
|
|
ssl_cafile=ssl_cafile,
|
|
|
|
ssl_capath=ssl_capath,
|
|
|
|
)
|
2018-11-01 23:34:14 +01:00
|
|
|
|
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
def get_ssl_client_context(
|
|
|
|
ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None
|
|
|
|
):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Builder for a client-side SSL context.
|
|
|
|
"""
|
2022-04-07 00:18:11 +02:00
|
|
|
return _get_ssl_context(
|
|
|
|
context_type=ssl.PROTOCOL_TLS_CLIENT,
|
|
|
|
ssl_cert=ssl_cert,
|
|
|
|
ssl_key=ssl_key,
|
|
|
|
ssl_cafile=ssl_cafile,
|
|
|
|
ssl_capath=ssl_capath,
|
|
|
|
)
|
2018-11-01 23:34:14 +01:00
|
|
|
|
2021-09-16 17:53:40 +02:00
|
|
|
|
2019-02-03 17:43:30 +01:00
|
|
|
def find_bins_in_path(bin_name):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Search for a binary in the PATH variable.
|
|
|
|
"""
|
2022-02-07 01:47:38 +01:00
|
|
|
return [
|
|
|
|
os.path.join(p, bin_name)
|
|
|
|
for p in os.environ.get('PATH', '').split(':')
|
2022-04-07 00:18:11 +02:00
|
|
|
if os.path.isfile(os.path.join(p, bin_name))
|
|
|
|
and (os.name == 'nt' or os.access(os.path.join(p, bin_name), os.X_OK))
|
|
|
|
]
|
2018-11-01 23:34:14 +01:00
|
|
|
|
2017-12-18 01:10:51 +01:00
|
|
|
|
2019-02-05 00:15:36 +01:00
|
|
|
def find_files_by_ext(directory, *exts):
|
|
|
|
"""
|
2023-02-05 18:05:41 +01:00
|
|
|
Finds all the files in the given directory with the provided extensions.
|
2019-02-05 00:15:36 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not exts:
|
|
|
|
raise AttributeError('No extensions provided')
|
|
|
|
|
|
|
|
if not os.path.isdir(directory):
|
2023-02-05 18:05:41 +01:00
|
|
|
raise AttributeError(f'{directory} is not a valid directory')
|
2019-02-05 00:15:36 +01:00
|
|
|
|
|
|
|
min_len = len(min(exts, key=len))
|
|
|
|
max_len = len(max(exts, key=len))
|
|
|
|
result = []
|
|
|
|
|
2022-04-07 00:18:11 +02:00
|
|
|
for _, __, files in os.walk(directory):
|
2021-09-16 17:53:40 +02:00
|
|
|
for i in range(min_len, max_len + 1):
|
2019-02-05 00:15:36 +01:00
|
|
|
result += [f for f in files if f[-i:] in exts]
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
def is_process_alive(pid: int) -> bool:
|
|
|
|
"""
|
|
|
|
:param pid: Process ID.
|
|
|
|
:return: True if the process with the given PID is alive.
|
|
|
|
"""
|
2019-02-05 00:15:36 +01:00
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
def get_ip_or_hostname() -> str:
|
|
|
|
"""
|
|
|
|
Get the the default IP address or hostname of the machine.
|
|
|
|
"""
|
2019-02-05 02:30:20 +01:00
|
|
|
ip = socket.gethostbyname(socket.gethostname())
|
2021-07-17 22:14:15 +02:00
|
|
|
if ip.startswith('127.') or ip.startswith('::1'):
|
2021-01-14 00:15:35 +01:00
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sock.connect(('10.255.255.255', 1))
|
|
|
|
ip = sock.getsockname()[0]
|
|
|
|
sock.close()
|
2021-04-05 00:58:44 +02:00
|
|
|
except Exception as e:
|
|
|
|
logger.debug(e)
|
2021-01-14 00:15:35 +01:00
|
|
|
|
|
|
|
return ip
|
2019-02-05 02:30:20 +01:00
|
|
|
|
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
def get_mime_type(resource: str) -> Optional[str]:
|
|
|
|
"""
|
|
|
|
Get the MIME type of the given resource.
|
|
|
|
|
|
|
|
:param resource: The resource to get the MIME type for - it can be a file
|
|
|
|
path or a URL.
|
|
|
|
"""
|
2019-02-06 13:22:58 +01:00
|
|
|
import magic
|
2022-04-07 00:18:11 +02:00
|
|
|
|
2019-02-06 11:51:44 +01:00
|
|
|
if resource.startswith('file://'):
|
2023-02-05 18:05:41 +01:00
|
|
|
offset = len('file://')
|
|
|
|
resource = resource[offset:]
|
2019-02-06 11:51:44 +01:00
|
|
|
|
|
|
|
if resource.startswith('http://') or resource.startswith('https://'):
|
|
|
|
with urllib.request.urlopen(resource) as response:
|
|
|
|
return response.info().get_content_type()
|
|
|
|
else:
|
2019-06-21 13:40:45 +02:00
|
|
|
if hasattr(magic, 'detect_from_filename'):
|
2023-03-28 15:26:45 +02:00
|
|
|
mime = magic.detect_from_filename(resource) # type: ignore
|
2019-06-21 13:40:45 +02:00
|
|
|
elif hasattr(magic, 'from_file'):
|
|
|
|
mime = magic.from_file(resource, mime=True)
|
|
|
|
else:
|
2022-04-07 00:18:11 +02:00
|
|
|
raise RuntimeError(
|
|
|
|
'The installed magic version provides neither detect_from_filename nor from_file'
|
|
|
|
)
|
2019-06-21 13:40:45 +02:00
|
|
|
|
2019-06-21 02:13:14 +02:00
|
|
|
if mime:
|
2023-03-28 15:26:45 +02:00
|
|
|
return mime.mime_type if hasattr(mime, 'mime_type') else mime # type: ignore
|
2019-02-06 11:51:44 +01:00
|
|
|
|
2023-02-05 18:05:41 +01:00
|
|
|
return None
|
|
|
|
|
2020-01-17 21:15:27 +01:00
|
|
|
|
2019-03-16 19:28:47 +01:00
|
|
|
def camel_case_to_snake_case(string):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Utility function to convert CamelCase to snake_case.
|
|
|
|
"""
|
2019-03-16 19:28:47 +01:00
|
|
|
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string)
|
|
|
|
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
|
|
|
|
|
2019-02-06 11:51:44 +01:00
|
|
|
|
2020-01-17 21:15:27 +01:00
|
|
|
def grouper(n, iterable, fillvalue=None):
|
|
|
|
"""
|
|
|
|
Split an iterable in groups of max N elements.
|
|
|
|
grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
|
|
|
|
"""
|
|
|
|
from itertools import zip_longest
|
2022-04-07 00:18:11 +02:00
|
|
|
|
2020-01-17 21:15:27 +01:00
|
|
|
args = [iter(iterable)] * n
|
|
|
|
|
|
|
|
if fillvalue:
|
2022-12-10 15:57:28 +01:00
|
|
|
return zip_longest(*args, fillvalue=fillvalue)
|
2020-01-17 21:15:27 +01:00
|
|
|
|
|
|
|
for chunk in zip_longest(*args):
|
|
|
|
yield filter(None, chunk)
|
|
|
|
|
2023-07-23 19:04:01 +02:00
|
|
|
return
|
|
|
|
|
2020-01-17 21:15:27 +01:00
|
|
|
|
2020-04-08 23:22:54 +02:00
|
|
|
def is_functional_procedure(obj) -> bool:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Check if the given object is a functional procedure.
|
|
|
|
"""
|
2020-04-08 23:22:54 +02:00
|
|
|
return callable(obj) and hasattr(obj, 'procedure')
|
|
|
|
|
|
|
|
|
|
|
|
def is_functional_hook(obj) -> bool:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Check if the given object is a functional hook.
|
|
|
|
"""
|
2020-04-08 23:22:54 +02:00
|
|
|
return callable(obj) and hasattr(obj, 'hook')
|
|
|
|
|
|
|
|
|
2020-10-13 23:25:27 +02:00
|
|
|
def is_functional_cron(obj) -> bool:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Check if the given object is a functional cron.
|
|
|
|
"""
|
2020-10-13 23:25:27 +02:00
|
|
|
return callable(obj) and hasattr(obj, 'cron') and hasattr(obj, 'cron_expression')
|
|
|
|
|
|
|
|
|
2020-04-09 23:50:08 +02:00
|
|
|
def run(action, *args, **kwargs):
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Run the given action with the given arguments. Example:
|
|
|
|
|
|
|
|
>>> from platypush.utils import run
|
|
|
|
>>> run('music.mpd.play', resource='file:///home/user/music.mp3')
|
|
|
|
|
|
|
|
"""
|
2024-05-24 20:20:25 +02:00
|
|
|
from platypush.config import Config
|
2020-04-08 23:22:54 +02:00
|
|
|
from platypush.context import get_plugin
|
2024-05-24 20:20:25 +02:00
|
|
|
from platypush.procedure import Procedure
|
|
|
|
|
|
|
|
if action.startswith('procedure.'):
|
|
|
|
procedure_name = action.removeprefix('procedure.')
|
|
|
|
procedures = Config.get_procedures()
|
|
|
|
procedure = procedures.get(procedure_name)
|
|
|
|
if not procedure:
|
|
|
|
raise RuntimeError(f'No such procedure: {procedure_name}')
|
|
|
|
|
|
|
|
if isinstance(procedure, dict):
|
|
|
|
procedure = Procedure.build(
|
|
|
|
name=procedure_name,
|
|
|
|
requests=procedure.get('actions', []),
|
|
|
|
args=procedure.get('args', {}),
|
|
|
|
_async=procedure.get('async', False),
|
|
|
|
)
|
|
|
|
|
|
|
|
return procedure.execute(*args, **kwargs)
|
|
|
|
|
|
|
|
return procedure(*args, **kwargs)
|
2022-04-07 00:18:11 +02:00
|
|
|
|
2020-04-08 23:22:54 +02:00
|
|
|
(module_name, method_name) = get_module_and_method_from_action(action)
|
|
|
|
plugin = get_plugin(module_name)
|
|
|
|
method = getattr(plugin, method_name)
|
2020-04-10 00:06:36 +02:00
|
|
|
response = method(*args, **kwargs)
|
|
|
|
|
|
|
|
if response.errors:
|
|
|
|
raise RuntimeError(response.errors[0])
|
|
|
|
|
|
|
|
return response.output
|
2020-04-08 23:22:54 +02:00
|
|
|
|
|
|
|
|
2022-12-10 15:57:28 +01:00
|
|
|
def generate_rsa_key_pair(
|
|
|
|
key_file: Optional[str] = None, size: int = 2048
|
|
|
|
) -> Tuple[PublicKey, PrivateKey]:
|
2021-02-12 22:43:34 +01:00
|
|
|
"""
|
|
|
|
Generate an RSA key pair.
|
|
|
|
|
|
|
|
:param key_file: Target file for the private key (the associated public key will be stored in ``<key_file>.pub``.
|
|
|
|
If no key file is specified then the public and private keys will be returned in ASCII format in a dictionary
|
|
|
|
with the following structure:
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
|
|
|
"private": "private key here",
|
|
|
|
"public": "public key here"
|
|
|
|
}
|
|
|
|
|
|
|
|
:param size: Key size (default: 2048 bits).
|
|
|
|
:return: A tuple with the generated ``(priv_key_str, pub_key_str)``.
|
|
|
|
"""
|
2022-11-21 12:30:38 +01:00
|
|
|
logger.info('Generating RSA keypair')
|
2023-02-05 18:05:41 +01:00
|
|
|
pub_key, priv_key = newkeys(size)
|
2022-11-21 12:30:38 +01:00
|
|
|
logger.info('Generated RSA keypair')
|
|
|
|
public_key_str = pub_key.save_pkcs1('PEM').decode()
|
|
|
|
private_key_str = priv_key.save_pkcs1('PEM').decode()
|
2021-02-12 22:43:34 +01:00
|
|
|
|
|
|
|
if key_file:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.info('Saving private key to %s', key_file)
|
2022-04-07 00:18:11 +02:00
|
|
|
with open(os.path.expanduser(key_file), 'w') as f1, open(
|
|
|
|
os.path.expanduser(key_file) + '.pub', 'w'
|
|
|
|
) as f2:
|
2021-02-12 22:43:34 +01:00
|
|
|
f1.write(private_key_str)
|
|
|
|
f2.write(public_key_str)
|
|
|
|
os.chmod(key_file, 0o600)
|
|
|
|
|
2022-11-21 12:30:38 +01:00
|
|
|
return pub_key, priv_key
|
2021-02-12 22:43:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_or_generate_jwt_rsa_key_pair():
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Get or generate a JWT RSA key pair.
|
|
|
|
"""
|
2021-02-12 22:43:34 +01:00
|
|
|
from platypush.config import Config
|
|
|
|
|
2023-08-19 13:21:24 +02:00
|
|
|
key_dir = os.path.join(Config.get_workdir(), 'jwt')
|
2021-02-12 22:43:34 +01:00
|
|
|
priv_key_file = os.path.join(key_dir, 'id_rsa')
|
|
|
|
pub_key_file = priv_key_file + '.pub'
|
|
|
|
|
|
|
|
if os.path.isfile(priv_key_file) and os.path.isfile(pub_key_file):
|
2022-12-10 15:57:28 +01:00
|
|
|
with open(pub_key_file, 'r') as f1, open(priv_key_file, 'r') as f2:
|
2022-11-21 12:30:38 +01:00
|
|
|
return (
|
2023-02-05 18:05:41 +01:00
|
|
|
PublicKey.load_pkcs1(f1.read().encode()),
|
|
|
|
PrivateKey.load_pkcs1(f2.read().encode()),
|
2022-11-21 12:30:38 +01:00
|
|
|
)
|
2021-02-12 22:43:34 +01:00
|
|
|
|
|
|
|
pathlib.Path(key_dir).mkdir(parents=True, exist_ok=True, mode=0o755)
|
|
|
|
return generate_rsa_key_pair(priv_key_file, size=2048)
|
|
|
|
|
|
|
|
|
2021-02-19 02:54:12 +01:00
|
|
|
def get_enabled_plugins() -> dict:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Get the enabled plugins.
|
|
|
|
|
|
|
|
:return: A dictionary with the enabled plugins, in the format ``name`` ->
|
|
|
|
:class:`platypush.plugins.Plugin` instance.
|
|
|
|
"""
|
2021-02-19 02:54:12 +01:00
|
|
|
from platypush.config import Config
|
|
|
|
from platypush.context import get_plugin
|
|
|
|
|
|
|
|
plugins = {}
|
2022-04-07 00:18:11 +02:00
|
|
|
for name in Config.get_plugins():
|
2021-02-19 02:54:12 +01:00
|
|
|
try:
|
|
|
|
plugin = get_plugin(name)
|
|
|
|
if plugin:
|
|
|
|
plugins[name] = plugin
|
2021-04-05 00:58:44 +02:00
|
|
|
except Exception as e:
|
2023-02-05 18:05:41 +01:00
|
|
|
logger.warning('Could not initialize plugin %s', name)
|
2022-02-07 01:47:38 +01:00
|
|
|
logger.exception(e)
|
2021-02-19 02:54:12 +01:00
|
|
|
|
|
|
|
return plugins
|
|
|
|
|
|
|
|
|
2023-10-18 22:10:32 +02:00
|
|
|
def get_enabled_backends() -> dict:
|
|
|
|
"""
|
|
|
|
Get the enabled backends.
|
|
|
|
|
|
|
|
:return: A dictionary with the enabled backends, in the format ``name`` ->
|
|
|
|
:class:`platypush.backend.Backend` instance.
|
|
|
|
"""
|
|
|
|
from platypush.config import Config
|
|
|
|
from platypush.context import get_backend
|
|
|
|
|
|
|
|
backends = {}
|
|
|
|
for name in Config.get_backends():
|
|
|
|
try:
|
|
|
|
backend = get_backend(name.removeprefix('backend.'))
|
|
|
|
if backend:
|
|
|
|
backends[name] = backend
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning('Could not initialize backend %s', name)
|
|
|
|
logger.exception(e)
|
|
|
|
|
|
|
|
return backends
|
|
|
|
|
|
|
|
|
2023-07-24 01:04:13 +02:00
|
|
|
def get_redis_conf() -> dict:
|
|
|
|
"""
|
|
|
|
Get the Redis connection arguments from the configuration.
|
|
|
|
"""
|
|
|
|
from platypush.config import Config
|
|
|
|
|
|
|
|
return (
|
|
|
|
Config.get('redis')
|
|
|
|
or (Config.get('backend.redis') or {}).get('redis_args', {})
|
|
|
|
or {}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-05 22:00:50 +01:00
|
|
|
def get_redis(*args, **kwargs) -> Redis:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Get a Redis client on the basis of the Redis configuration.
|
|
|
|
|
|
|
|
The Redis configuration can be loaded from:
|
|
|
|
|
2023-07-24 01:04:13 +02:00
|
|
|
1. The ``redis`` plugin.
|
|
|
|
2. The ``backend.redis`` configuration (``redis_args`` attribute)
|
2023-02-05 18:05:41 +01:00
|
|
|
|
|
|
|
"""
|
2023-02-05 22:00:50 +01:00
|
|
|
if not (args or kwargs):
|
2023-07-24 01:04:13 +02:00
|
|
|
kwargs = get_redis_conf()
|
2023-02-05 22:00:50 +01:00
|
|
|
|
|
|
|
return Redis(*args, **kwargs)
|
2021-06-26 11:14:26 +02:00
|
|
|
|
2021-10-16 22:35:37 +02:00
|
|
|
|
|
|
|
def to_datetime(t: Union[str, int, float, datetime.datetime]) -> datetime.datetime:
|
2023-02-05 18:05:41 +01:00
|
|
|
"""
|
|
|
|
Utility function to convert a datetime/timestamp provided as a
|
|
|
|
string/integer/float/datetime to a ``datetime.datetime`` instance.
|
|
|
|
"""
|
2022-04-07 00:18:11 +02:00
|
|
|
if isinstance(t, (int, float)):
|
2021-10-16 22:35:37 +02:00
|
|
|
return datetime.datetime.fromtimestamp(t, tz=tz.tzutc())
|
|
|
|
if isinstance(t, str):
|
|
|
|
return parser.parse(t)
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2023-03-28 15:26:45 +02:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def get_lock(
|
|
|
|
lock: Lock, timeout: Optional[float] = None
|
|
|
|
) -> Generator[bool, None, None]:
|
|
|
|
"""
|
|
|
|
Get a lock with an optional timeout through a context manager construct:
|
|
|
|
|
|
|
|
>>> from threading import Lock
|
|
|
|
>>> lock = Lock()
|
|
|
|
>>> with get_lock(lock, timeout=2):
|
|
|
|
>>> ...
|
|
|
|
|
|
|
|
"""
|
|
|
|
kwargs = {'timeout': timeout} if timeout else {}
|
|
|
|
result = lock.acquire(**kwargs)
|
|
|
|
|
|
|
|
try:
|
2023-03-31 22:31:32 +02:00
|
|
|
if not result:
|
|
|
|
raise TimeoutError()
|
2023-03-28 15:26:45 +02:00
|
|
|
yield result
|
|
|
|
finally:
|
|
|
|
if result:
|
|
|
|
lock.release()
|
|
|
|
|
|
|
|
|
2023-08-14 10:46:27 +02:00
|
|
|
def get_default_pid_file() -> str:
|
|
|
|
"""
|
|
|
|
Get the default PID file path.
|
|
|
|
"""
|
|
|
|
return os.path.join(gettempdir(), 'platypush.pid')
|
|
|
|
|
|
|
|
|
2023-08-14 23:05:32 +02:00
|
|
|
def get_remaining_timeout(
|
|
|
|
timeout: Optional[float], start: float, cls: Union[Type[int], Type[float]] = float
|
|
|
|
) -> Optional[Union[int, float]]:
|
|
|
|
"""
|
|
|
|
Get the remaining timeout, given a start time.
|
|
|
|
"""
|
|
|
|
if timeout is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return cls(max(0, timeout - (time.time() - start)))
|
|
|
|
|
|
|
|
|
2023-08-23 02:53:31 +02:00
|
|
|
def get_src_root() -> str:
|
|
|
|
"""
|
|
|
|
:return: The root source folder of the application.
|
|
|
|
"""
|
|
|
|
import platypush
|
|
|
|
|
|
|
|
return os.path.dirname(inspect.getfile(platypush))
|
|
|
|
|
|
|
|
|
2023-08-31 23:19:13 +02:00
|
|
|
def is_root() -> bool:
|
|
|
|
"""
|
|
|
|
:return: True if the current user is root/administrator.
|
|
|
|
"""
|
|
|
|
return os.getuid() == 0
|
|
|
|
|
|
|
|
|
2023-09-05 13:03:30 +02:00
|
|
|
def get_message_response(msg):
|
|
|
|
"""
|
|
|
|
Get the response to the given message.
|
|
|
|
|
|
|
|
:param msg: The message to get the response for.
|
|
|
|
:return: The response to the given message.
|
|
|
|
"""
|
|
|
|
from platypush.message import Message
|
|
|
|
|
|
|
|
redis = get_redis()
|
|
|
|
redis_queue = get_redis_queue_name_by_message(msg)
|
|
|
|
if not redis_queue:
|
|
|
|
return None
|
|
|
|
|
|
|
|
response = redis.blpop(redis_queue, timeout=60)
|
|
|
|
if response and len(response) > 1:
|
|
|
|
response = Message.build(response[1])
|
|
|
|
else:
|
|
|
|
response = None
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2023-10-04 02:27:09 +02:00
|
|
|
def import_file(path: str, name: Optional[str] = None):
|
|
|
|
"""
|
|
|
|
Import a Python file as a module, even if no __init__.py is
|
|
|
|
defined in the directory.
|
|
|
|
|
|
|
|
:param path: Path of the file to import.
|
|
|
|
:param name: Custom name for the imported module (default: same as the file's basename).
|
|
|
|
:return: The imported module.
|
|
|
|
"""
|
|
|
|
name = name or re.split(r"\.py$", os.path.basename(path))[0]
|
|
|
|
loader = SourceFileLoader(name, os.path.expanduser(path))
|
|
|
|
mod_spec = spec_from_loader(name, loader)
|
|
|
|
assert mod_spec, f"Cannot create module specification for {path}"
|
|
|
|
mod = module_from_spec(mod_spec)
|
|
|
|
loader.exec_module(mod)
|
|
|
|
return mod
|
|
|
|
|
|
|
|
|
2023-10-10 01:35:01 +02:00
|
|
|
def get_defining_class(meth) -> Optional[type]:
|
|
|
|
"""
|
|
|
|
See https://stackoverflow.com/a/25959545/622364.
|
|
|
|
|
|
|
|
This is the best way I could find of answering the question "given a bound
|
|
|
|
method, get the class that defined it",
|
|
|
|
"""
|
2023-10-16 00:21:49 +02:00
|
|
|
if isinstance(meth, type):
|
|
|
|
return meth
|
|
|
|
|
2023-10-10 01:35:01 +02:00
|
|
|
if isinstance(meth, functools.partial):
|
|
|
|
return get_defining_class(meth.func)
|
|
|
|
|
|
|
|
if inspect.ismethod(meth) or (
|
|
|
|
inspect.isbuiltin(meth)
|
|
|
|
and getattr(meth, '__self__', None) is not None
|
|
|
|
and getattr(meth.__self__, '__class__', None)
|
|
|
|
):
|
|
|
|
for cls in inspect.getmro(meth.__self__.__class__):
|
|
|
|
if meth.__name__ in cls.__dict__:
|
|
|
|
return cls
|
|
|
|
meth = getattr(meth, '__func__', meth) # fallback to __qualname__ parsing
|
|
|
|
|
|
|
|
if inspect.isfunction(meth):
|
|
|
|
cls = getattr(
|
|
|
|
inspect.getmodule(meth),
|
|
|
|
meth.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
if isinstance(cls, type):
|
|
|
|
return cls
|
|
|
|
|
|
|
|
return getattr(meth, '__objclass__', None) # handle special descriptor objects
|
|
|
|
|
|
|
|
|
2023-10-31 01:39:32 +01:00
|
|
|
def is_debug_enabled() -> bool:
|
|
|
|
"""
|
|
|
|
:return: True if the debug mode is enabled.
|
|
|
|
"""
|
|
|
|
from platypush.config import Config
|
|
|
|
|
|
|
|
return (Config.get('logging') or {}).get('level') == logging.DEBUG
|
|
|
|
|
|
|
|
|
2023-11-03 18:06:09 +01:00
|
|
|
def get_default_downloads_dir() -> str:
|
|
|
|
"""
|
|
|
|
:return: The default downloads directory.
|
|
|
|
"""
|
|
|
|
return os.path.join(os.path.expanduser('~'), 'Downloads')
|
|
|
|
|
|
|
|
|
2024-02-22 22:52:52 +01:00
|
|
|
def wait_for_either(*events, timeout: Optional[float] = None, cls: Type = Event):
|
|
|
|
"""
|
|
|
|
Wait for any of the given events to be set.
|
|
|
|
|
|
|
|
:param events: The events to be checked.
|
|
|
|
:param timeout: The maximum time to wait for the event to be set. Default: None.
|
|
|
|
:param cls: The class to be used for the event. Default: threading.Event.
|
|
|
|
"""
|
|
|
|
from .threads import OrEvent
|
|
|
|
|
|
|
|
return OrEvent(*events, cls=cls).wait(timeout=timeout)
|
|
|
|
|
|
|
|
|
2024-05-31 19:52:32 +02:00
|
|
|
def utcnow():
|
|
|
|
"""
|
2024-06-01 01:34:47 +02:00
|
|
|
utcnow() without tears. It always returns a datetime object in UTC
|
|
|
|
timezone.
|
2024-05-31 19:52:32 +02:00
|
|
|
"""
|
2024-06-01 01:34:47 +02:00
|
|
|
return datetime.datetime.now(datetime.timezone.utc)
|
2024-05-31 19:52:32 +02:00
|
|
|
|
|
|
|
|
2019-02-03 17:43:30 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|