Commit 77c9b3a9 authored by Philipp Hörist's avatar Philipp Hörist Committed by Philipp Hörist
Browse files

Add annotations and fix pylint/mypy errors

parent 226c42a9
......@@ -273,7 +273,8 @@ def _handle_remote_options(self, application, command_line):
self.activate()
return 0
def _handle_local_options(self, application,
def _handle_local_options(self,
application: Gtk.Application,
options: GLib.VariantDict) -> int:
# Parse all options that have to be executed before ::startup
if options.contains('profile'):
......
......@@ -413,7 +413,7 @@ def update_pep(self, pep_type):
img = self._pep_images[pep_type]
if pep_type in pep:
img.set_from_pixbuf(gtkgui_helpers.get_pep_as_pixbuf(pep[pep_type]))
img.set_tooltip_markup(pep[pep_type].asMarkupText())
img.set_tooltip_markup(pep[pep_type].as_markup_text())
img.show()
else:
img.hide()
......
......@@ -31,62 +31,77 @@
over the process.
"""
from typing import Any # pylint: disable=unused-import
from typing import Dict # pylint: disable=unused-import
from gajim.command_system.tools import remove
COMMANDS = {}
CONTAINERS = {}
COMMANDS = {} # type: Dict[Any, Any]
CONTAINERS = {} # type: Dict[Any, Any]
def add_host(host):
CONTAINERS[host] = []
def remove_host(host):
remove(CONTAINERS, host)
def add_container(container):
for host in container.HOSTS:
CONTAINERS[host].append(container)
def remove_container(container):
for host in container.HOSTS:
remove(CONTAINERS[host], container)
def add_commands(container):
commands = COMMANDS.setdefault(container, {})
for command in traverse_commands(container):
for name in command.names:
commands[name] = command
def remove_commands(container):
remove(COMMANDS, container)
def traverse_commands(container):
for name in dir(container):
attribute = getattr(container, name)
if is_command(attribute):
yield attribute
def is_command(attribute):
from gajim.command_system.framework import Command
return isinstance(attribute, Command)
def is_root(namespace):
metaclass = namespace.get("__metaclass__", None)
if not metaclass:
return False
return issubclass(metaclass, Dispatchable)
def get_command(host, name):
for container in CONTAINERS[host]:
command = COMMANDS[container].get(name)
if command:
return command
def list_commands(host):
for container in CONTAINERS[host]:
commands = COMMANDS[container]
for name, command in commands.items():
yield name, command
class Dispatchable(type):
# pylint: disable=no-value-for-parameter
def __init__(self, name, bases, namespace):
......@@ -99,6 +114,7 @@ def dispatch(self):
if self.AUTOMATIC:
self.enable()
class Host(Dispatchable):
def enable(self):
......@@ -107,6 +123,7 @@ def enable(self):
def disable(self):
remove_host(self)
class Container(Dispatchable):
def enable(self):
......
......@@ -34,6 +34,7 @@
detected.
"""
from gajim.common.i18n import _
from gajim.command_system.framework import CommandContainer, command, doc
from gajim.command_system.implementation.hosts import ChatCommands, PrivateChatCommands, GroupChatCommands
......
......@@ -37,6 +37,7 @@
from gi.repository import GLib
from gajim.common.i18n import _
from gajim.command_system.framework import CommandContainer, command, doc
from gajim.command_system.implementation.hosts import ChatCommands, PrivateChatCommands, GroupChatCommands
......
......@@ -23,6 +23,7 @@
from gajim import dialogs
from gajim.common import app
from gajim.common import helpers
from gajim.common.i18n import _
from gajim.common.exceptions import GajimGeneralException
from gajim.common.const import KindConstant
......
......@@ -42,7 +42,7 @@
from gajim.common.events import Events
from gajim.common.css_config import CSSConfig
interface = None # The actual interface (the gtk one for the moment)
interface = None # type: gajim.interface.Interface
thread_interface = lambda *args: None # Interface to run a thread and then a callback
config = c_config.Config()
version = gajim.__version__
......@@ -52,10 +52,10 @@
app = None # Gtk.Application
ged = ged_module.GlobalEventsDispatcher() # Global Events Dispatcher
nec = None # Network Events Controller
nec = None # type: gajim.common.nec.NetworkEventsController
plugin_manager = None # Plugins Manager
logger = None
logger = None # type: gajim.common.logger.Logger
# For backwards compatibility needed
# some plugins use that
......@@ -122,7 +122,7 @@
ZEROCONF_ACC_NAME = 'Local'
# These will be set in app.gui_interface.
idlequeue = None
idlequeue = None # type: nbxmpp.idlequeue.IdleQueue
socks5queue = None
gajim_identity = {'type': 'pc', 'category': 'client', 'name': 'Gajim'}
......
......@@ -32,6 +32,7 @@
from enum import IntEnum, unique
import gajim
from gajim.common.i18n import _
@unique
......
......@@ -19,82 +19,90 @@
# 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 Dict # pylint: disable=unused-import
from typing import List
from typing import Generator
from typing import Optional # pylint: disable=unused-import
from typing import Tuple
from typing import Union
import os
import sys
import tempfile
from pathlib import Path
import gajim
from gajim.common.i18n import _
from gajim.common.const import PathType, PathLocation
from gajim.common.types import PathTuple
def get(key):
def get(key: str) -> Union[str, List[str]]:
if key == 'PLUGINS_DIRS':
if gajim.IS_FLATPAK:
return ['/app/plugins',
_paths['PLUGINS_BASE']]
else:
return [_paths['PLUGINS_BASE'],
_paths['PLUGINS_USER']]
return [_paths['PLUGINS_BASE'],
_paths['PLUGINS_USER']]
return _paths[key]
def get_paths(type_):
def get_paths(type_: PathType) -> Generator[str, None, None]:
for key, value in _paths.items():
location, path, path_type = value
path_type = value[2]
if type_ != path_type:
continue
yield _paths[key]
def override_path(*args, **kwargs):
_paths._add(*args, **kwargs)
_paths.add(*args, **kwargs)
def set_separation(active: bool):
def set_separation(active: bool) -> None:
_paths.profile_separation = active
def set_profile(profile: str):
def set_profile(profile: str) -> None:
_paths.profile = profile
def set_config_root(config_root: str):
def set_config_root(config_root: str) -> None:
_paths.custom_config_root = config_root
def init():
def init() -> None:
_paths.init()
def create_paths():
def create_paths() -> None:
for path in get_paths(PathType.FOLDER):
if not isinstance(path, Path):
path = Path(path)
path_ = Path(path)
if path.is_file():
print(_('%s is a file but it should be a directory') % path)
if path_.is_file():
print(_('%s is a file but it should be a directory') % path_)
print(_('Gajim will now exit'))
sys.exit()
if not path.exists():
for parent_path in reversed(path.parents):
if not path_.exists():
for parent_path in reversed(path_.parents):
# Create all parent folders
# don't use mkdir(parent=True), as it ignores `mode`
# when creating the parents
if not parent_path.exists():
print(('creating %s directory') % parent_path)
parent_path.mkdir(mode=0o700)
print(('creating %s directory') % path)
path.mkdir(mode=0o700)
print(('creating %s directory') % path_)
path_.mkdir(mode=0o700)
class ConfigPaths:
def __init__(self):
self._paths = {}
def __init__(self) -> None:
self._paths = {} # type: Dict[str, PathTuple]
self.profile = ''
self.profile_separation = False
self.custom_config_root = None
self.custom_config_root = None # type: Optional[str]
if os.name == 'nt':
try:
......@@ -133,23 +141,23 @@ def __init__(self):
]
for path in source_paths:
self._add(*path)
self.add(*path)
def __getitem__(self, key):
def __getitem__(self, key: str) -> str:
location, path, _ = self._paths[key]
if location == PathLocation.CONFIG:
return os.path.join(self.config_root, path)
elif location == PathLocation.CACHE:
if location == PathLocation.CACHE:
return os.path.join(self.cache_root, path)
elif location == PathLocation.DATA:
if location == PathLocation.DATA:
return os.path.join(self.data_root, path)
return path
def items(self):
def items(self) -> Generator[Tuple[str, PathTuple], None, None]:
for key, value in self._paths.items():
yield (key, value)
def _prepare(self, path, unique):
def _prepare(self, path: str, unique: bool) -> str:
if os.name == 'nt':
path = path.capitalize()
if self.profile:
......@@ -157,7 +165,12 @@ def _prepare(self, path, unique):
return '%s.%s' % (path, self.profile)
return path
def _add(self, name, path, location=None, path_type=None, unique=False):
def add(self,
name: str,
path: str,
location: PathLocation = None,
path_type: PathType = None,
unique: bool = False) -> None:
if path and location is not None:
path = self._prepare(path, unique)
self._paths[name] = (location, path, path_type)
......@@ -175,7 +188,7 @@ def init(self):
]
for path in user_dir_paths:
self._add(*path)
self.add(*path)
# These paths are unique per profile
unique_profile_paths = [
......@@ -191,7 +204,7 @@ def init(self):
]
for path in unique_profile_paths:
self._add(*path, unique=True)
self.add(*path, unique=True)
# These paths are only unique per profile if the commandline arg
# `separate` is passed
......@@ -219,7 +232,7 @@ def init(self):
]
for path in paths:
self._add(*path)
self.add(*path)
_paths = ConfigPaths()
......
......@@ -176,7 +176,6 @@ def _is_muc_pm(self, message):
class IqErrorReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
name = 'iq-error-received'
base_network_events = []
def generate(self):
self.get_id()
......@@ -187,7 +186,6 @@ def generate(self):
class StreamReceivedEvent(nec.NetworkIncomingEvent):
name = 'stream-received'
base_network_events = []
class StreamConflictReceivedEvent(nec.NetworkIncomingEvent):
name = 'stream-conflict-received'
......@@ -321,7 +319,6 @@ def generate(self):
class ZeroconfPresenceReceivedEvent(nec.NetworkIncomingEvent):
name = 'presence-received'
base_network_events = []
def generate(self):
self.jid, self.resource = app.get_room_and_nick_from_fjid(self.fjid)
......@@ -348,7 +345,6 @@ def generate(self):
class GcPresenceReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
name = 'gc-presence-received'
base_network_events = []
def generate(self):
self.ptype = self.presence_obj.ptype
......@@ -436,15 +432,12 @@ def generate(self):
class OurShowEvent(nec.NetworkIncomingEvent):
name = 'our-show'
base_network_events = []
class BeforeChangeShowEvent(nec.NetworkIncomingEvent):
name = 'before-change-show'
base_network_events = []
class ChatstateReceivedEvent(nec.NetworkIncomingEvent):
name = 'chatstate-received'
base_network_events = []
def generate(self):
self.stanza = self.msg_obj.stanza
......@@ -456,7 +449,6 @@ def generate(self):
class GcMessageReceivedEvent(nec.NetworkIncomingEvent):
name = 'gc-message-received'
base_network_events = []
def generate(self):
self.stanza = self.msg_obj.stanza
......@@ -556,7 +548,6 @@ def generate(self):
class GcConfigChangedReceivedEvent(nec.NetworkIncomingEvent):
name = 'gc-config-changed-received'
base_network_events = []
def generate(self):
self.conn = self.msg_event.conn
......@@ -567,7 +558,6 @@ def generate(self):
class MessageSentEvent(nec.NetworkIncomingEvent):
name = 'message-sent'
base_network_events = []
def generate(self):
if not self.automatic_message:
......@@ -579,11 +569,9 @@ def generate(self):
class MessageNotSentEvent(nec.NetworkIncomingEvent):
name = 'message-not-sent'
base_network_events = []
class MessageErrorEvent(nec.NetworkIncomingEvent, HelperEvent):
name = 'message-error'
base_network_events = []
def init(self):
self.zeroconf = False
......@@ -600,11 +588,9 @@ def generate(self):
class AnonymousAuthEvent(nec.NetworkIncomingEvent):
name = 'anonymous-auth'
base_network_events = []
class JingleRequestReceivedEvent(nec.NetworkIncomingEvent):
name = 'jingle-request-received'
base_network_events = []
def generate(self):
self.fjid = self.jingle_session.peerjid
......@@ -614,7 +600,6 @@ def generate(self):
class JingleConnectedReceivedEvent(nec.NetworkIncomingEvent):
name = 'jingle-connected-received'
base_network_events = []
def generate(self):
self.fjid = self.jingle_session.peerjid
......@@ -624,7 +609,6 @@ def generate(self):
class JingleDisconnectedReceivedEvent(nec.NetworkIncomingEvent):
name = 'jingle-disconnected-received'
base_network_events = []
def generate(self):
self.fjid = self.jingle_session.peerjid
......@@ -634,7 +618,6 @@ def generate(self):
class JingleTransferCancelledEvent(nec.NetworkIncomingEvent):
name = 'jingleFT-cancelled-received'
base_network_events = []
def generate(self):
self.fjid = self.jingle_session.peerjid
......@@ -644,7 +627,6 @@ def generate(self):
class JingleErrorReceivedEvent(nec.NetworkIncomingEvent):
name = 'jingle-error-received'
base_network_events = []
def generate(self):
self.fjid = self.jingle_session.peerjid
......@@ -654,15 +636,12 @@ def generate(self):
class AccountCreatedEvent(nec.NetworkIncomingEvent):
name = 'account-created'
base_network_events = []
class AccountNotCreatedEvent(nec.NetworkIncomingEvent):
name = 'account-not-created'
base_network_events = []
class NewAccountConnectedEvent(nec.NetworkIncomingEvent):
name = 'new-account-connected'
base_network_events = []
def generate(self):
try:
......@@ -686,15 +665,12 @@ def generate(self):
class NewAccountNotConnectedEvent(nec.NetworkIncomingEvent):
name = 'new-account-not-connected'
base_network_events = []
class ConnectionTypeEvent(nec.NetworkIncomingEvent):
name = 'connection-type'
base_network_events = []
class StanzaReceivedEvent(nec.NetworkIncomingEvent):
name = 'stanza-received'
base_network_events = []
def init(self):
self.additional_data = {}
......@@ -704,14 +680,12 @@ def generate(self):
class StanzaSentEvent(nec.NetworkIncomingEvent):
name = 'stanza-sent'
base_network_events = []
def init(self):
self.additional_data = {}
class AgentRemovedEvent(nec.NetworkIncomingEvent):
name = 'agent-removed'
base_network_events = []
def generate(self):
self.jid_list = []
......@@ -722,7 +696,6 @@ def generate(self):
class BadGPGPassphraseEvent(nec.NetworkIncomingEvent):
name = 'bad-gpg-passphrase'
base_network_events = []
def generate(self):
self.account = self.conn.name
......@@ -732,7 +705,6 @@ def generate(self):
class ConnectionLostEvent(nec.NetworkIncomingEvent):
name = 'connection-lost'
base_network_events = []
def generate(self):
app.nec.push_incoming_event(OurShowEvent(None, conn=self.conn,
......@@ -741,11 +713,9 @@ def generate(self):
class GPGTrustKeyEvent(nec.NetworkIncomingEvent):
name = 'gpg-trust-key'
base_network_events = []
class GPGPasswordRequiredEvent(nec.NetworkIncomingEvent):
name = 'gpg-password-required'
base_network_events = []
def generate(self):
self.keyid = app.config.get_per('accounts', self.conn.name, 'keyid')
......@@ -753,7 +723,6 @@ def generate(self):
class PEPReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
name = 'pep-received'
base_network_events = []
def generate(self):
if not self.stanza.getTag('event'):
......@@ -778,72 +747,57 @@ def generate(self):
class PlainConnectionEvent(nec.NetworkIncomingEvent):
name = 'plain-connection'
base_network_events = []
class InsecurePasswordEvent(nec.NetworkIncomingEvent):
name = 'insecure-password'
base_network_events = []
class InsecureSSLConnectionEvent(nec.NetworkIncomingEvent):
name = 'insecure-ssl-connection'
base_network_events = []
class SSLErrorEvent(nec.NetworkIncomingEvent):
name = 'ssl-error'
base_network_events = []
class UniqueRoomIdSupportedEvent(nec.NetworkIncomingEvent):
name = 'unique-room-id-supported'
base_network_events = []
class UniqueRoomIdNotSupportedEvent(nec.NetworkIncomingEvent):
name = 'unique-room-id-not-supported'
base_network_events = []
class NonAnonymousServerErrorEvent(nec.NetworkIncomingEvent):
name = 'non-anonymous-server-error'
base_network_events = []
class UpdateGCAvatarEvent(nec.NetworkIncomingEvent):
name = 'update-gc-avatar'
base_network_events = []
def generate(self):
return True
class UpdateRosterAvatarEvent(nec.NetworkIncomingEvent):
name = 'update-roster-avatar'
base_network_events = []
def generate(self):
return True
class UpdateRoomAvatarEvent(nec.NetworkIncomingEvent):
name = 'update-room-avatar'
base_network_events = []
def generate(self):
return True
class ZeroconfNameConflictEvent(nec.NetworkIncomingEvent):
name = 'zeroconf-name-conflict'