2018-07-19 21:26:45 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Util module
|
|
|
|
|
2018-09-11 00:28:46 +02:00
|
|
|
from typing import Union
|
|
|
|
|
2019-03-08 20:31:05 +01:00
|
|
|
from logging import LoggerAdapter
|
2019-01-30 20:23:16 +01:00
|
|
|
from functools import wraps
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from gajim.common import app
|
2019-02-19 19:17:56 +01:00
|
|
|
from gajim.common.const import EME_MESSAGES
|
2019-01-30 20:23:16 +01:00
|
|
|
|
2018-09-05 00:06:59 +02:00
|
|
|
|
2018-09-11 00:28:46 +02:00
|
|
|
def from_xs_boolean(value: Union[str, bool]) -> bool:
|
|
|
|
if isinstance(value, bool):
|
2018-09-05 00:06:59 +02:00
|
|
|
return value
|
|
|
|
|
2018-10-19 15:59:04 +02:00
|
|
|
if value in ('1', 'true', 'True'):
|
2018-09-05 00:06:59 +02:00
|
|
|
return True
|
|
|
|
|
2018-10-19 15:59:04 +02:00
|
|
|
if value in ('0', 'false', 'False', ''):
|
2018-09-05 00:06:59 +02:00
|
|
|
return False
|
|
|
|
|
2018-10-19 15:59:04 +02:00
|
|
|
raise ValueError('Cant convert %s to python boolean' % value)
|
2018-09-05 00:06:59 +02:00
|
|
|
|
|
|
|
|
2018-09-11 00:28:46 +02:00
|
|
|
def to_xs_boolean(value: Union[bool, None]) -> str:
|
2018-09-05 00:06:59 +02:00
|
|
|
# Convert to xs:boolean ('true', 'false')
|
|
|
|
# from a python boolean (True, False) or None
|
|
|
|
if value is True:
|
|
|
|
return 'true'
|
|
|
|
|
2018-09-11 00:28:46 +02:00
|
|
|
if value is False:
|
|
|
|
return 'false'
|
|
|
|
|
|
|
|
if value is None:
|
2018-09-05 00:06:59 +02:00
|
|
|
return 'false'
|
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
'Cant convert %s to xs:boolean' % value)
|
2019-01-30 20:23:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def event_node(node):
|
|
|
|
def event_node_decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def func_wrapper(self, _con, _stanza, properties):
|
2019-02-01 17:03:46 +01:00
|
|
|
if not properties.is_pubsub_event:
|
|
|
|
return
|
2019-01-30 20:23:16 +01:00
|
|
|
if properties.pubsub_event.node != node:
|
|
|
|
return
|
|
|
|
func(self, _con, _stanza, properties)
|
|
|
|
|
|
|
|
return func_wrapper
|
|
|
|
return event_node_decorator
|
|
|
|
|
|
|
|
|
|
|
|
def store_publish(func):
|
|
|
|
@wraps(func)
|
|
|
|
def func_wrapper(self, *args, **kwargs):
|
|
|
|
if not app.account_is_connected(self._account):
|
|
|
|
self._stored_publish = partial(func, self, *args, **kwargs)
|
|
|
|
return
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
return func_wrapper
|
2019-02-19 19:17:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_eme_message(eme_data):
|
|
|
|
try:
|
|
|
|
return EME_MESSAGES[eme_data.namespace]
|
|
|
|
except KeyError:
|
|
|
|
return EME_MESSAGES['fallback'] % eme_data.name
|
2019-03-08 20:31:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LogAdapter(LoggerAdapter):
|
|
|
|
def process(self, msg, kwargs):
|
|
|
|
return '[%s] %s' % (self.extra['account'], msg), kwargs
|