gajim-plural/gajim/common/modules/__init__.py

127 lines
3.6 KiB
Python
Raw Normal View History

# 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/>.
from typing import Any
from typing import Dict # pylint: disable=unused-import
from typing import List
from typing import Tuple
import logging
from importlib import import_module
from pathlib import Path
2018-07-26 00:12:04 +02:00
from unittest.mock import MagicMock
from gajim.common.types import ConnectionT
log = logging.getLogger('gajim.c.m')
2019-01-02 23:13:07 +01:00
ZEROCONF_MODULES = ['iq',
'adhoc_commands',
'receipts',
'discovery',
'chatstates']
2018-07-08 18:37:53 +02:00
2018-09-11 20:00:34 +02:00
_imported_modules = [] # type: List[tuple]
_modules = {} # type: Dict[str, Dict[str, Any]]
for file in Path(__file__).parent.iterdir():
if file.stem == '__init__':
continue
2018-09-11 20:00:34 +02:00
_module = import_module('.%s' % file.stem, package='gajim.common.modules')
if hasattr(_module, 'get_instance'):
log.info('Load module: %s', file.stem)
if file.stem == 'pep':
# Register the PEP module first, because other modules
# depend on it
2018-09-11 20:00:34 +02:00
_imported_modules.insert(0, (_module, file.stem))
else:
2018-09-11 20:00:34 +02:00
_imported_modules.append((_module, file.stem))
class ModuleMock:
def __init__(self, name: str) -> None:
2018-07-08 18:37:53 +02:00
self._name = name
# HTTPUpload, ..
2018-07-08 18:37:53 +02:00
self.available = False
# Blocking
self.blocked = [] # type: List[Any]
2018-07-08 18:37:53 +02:00
# Privacy Lists
self.blocked_contacts = [] # type: List[Any]
self.blocked_groups = [] # type: List[Any]
2018-07-08 18:37:53 +02:00
self.blocked_all = False
# Delimiter
self.delimiter = '::'
# Bookmarks
self.bookmarks = {} # type: Dict[Any, Any]
# Various Modules
self.supported = False
def __getattr__(self, key: str) -> MagicMock:
2018-07-26 00:12:04 +02:00
return MagicMock()
def register(con: ConnectionT, *args: Any, **kwargs: Any) -> None:
if con in _modules:
return
_modules[con.name] = {}
2018-09-11 20:00:34 +02:00
for module in _imported_modules:
2018-07-08 18:37:53 +02:00
mod, name = module
if con.name == 'Local':
if name not in ZEROCONF_MODULES:
continue
instance, name = mod.get_instance(con, *args, **kwargs)
_modules[con.name][name] = instance
def register_single(con: ConnectionT, instance: Any, name: str) -> None:
2018-08-24 21:39:03 +02:00
if con.name not in _modules:
raise ValueError('Unknown account name: %s' % con.name)
_modules[con.name][name] = instance
def unregister(con: ConnectionT) -> None:
2018-08-24 21:39:03 +02:00
for instance in _modules[con.name].values():
if hasattr(instance, 'cleanup'):
instance.cleanup()
del _modules[con.name]
def unregister_single(con: ConnectionT, name: str) -> None:
2018-08-24 21:39:03 +02:00
if con.name not in _modules:
return
if name not in _modules[con.name]:
return
del _modules[con.name][name]
def get(account: str, name: str) -> Any:
try:
return _modules[account][name]
except KeyError:
2018-07-08 18:37:53 +02:00
return ModuleMock(name)
def get_handlers(con: ConnectionT) -> List[Tuple[Any, ...]]:
handlers = [] # type: List[Tuple[Any, ...]]
for module in _modules[con.name].values():
handlers += module.handlers
return handlers