Commit 9f59eb54 authored by Daniel Brötzmann's avatar Daniel Brötzmann Committed by Philipp Hörist

CommandSystem: Fix some pylint errors

parent 2257da6c
Pipeline #4196 passed with stages
in 2 minutes and 47 seconds
......@@ -13,6 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class BaseError(Exception):
"""
Common base for errors which relate to a specific command.
......@@ -34,16 +35,19 @@ class BaseError(Exception):
def __str__(self):
return self.message
class DefinitionError(BaseError):
"""
Used to indicate errors occurred on command definition.
"""
class CommandError(BaseError):
"""
Used to indicate errors occurred during command execution.
"""
class NoCommandError(BaseError):
"""
Used to indicate an inability to find the specified command.
......
......@@ -22,10 +22,16 @@ declarative way.
from types import FunctionType
from inspect import getargspec, getdoc
from gajim.command_system.dispatcher import Host, Container
from gajim.command_system.dispatcher import get_command, list_commands
from gajim.command_system.mapping import parse_arguments, adapt_arguments
from gajim.command_system.errors import DefinitionError, CommandError, NoCommandError
from gajim.command_system.dispatcher import Host
from gajim.command_system.dispatcher import Container
from gajim.command_system.dispatcher import get_command
from gajim.command_system.dispatcher import list_commands
from gajim.command_system.mapping import parse_arguments
from gajim.command_system.mapping import adapt_arguments
from gajim.command_system.errors import DefinitionError
from gajim.command_system.errors import CommandError
from gajim.command_system.errors import NoCommandError
class CommandHost(metaclass=Host):
"""
......@@ -39,6 +45,7 @@ class CommandHost(metaclass=Host):
"""
__metaclass__ = Host
class CommandContainer(metaclass=Container):
"""
Command container is an entity which holds defined commands,
......@@ -55,6 +62,7 @@ class CommandContainer(metaclass=Container):
"""
__metaclass__ = Container
class CommandProcessor:
"""
Command processor is an immediate command emitter. It does not
......@@ -142,6 +150,7 @@ class CommandProcessor:
commands = dict(commands)
return sorted(set(commands.values()), key=lambda k: k.__repr__())
class Command:
def __init__(self, handler, *names, **properties):
......@@ -217,7 +226,8 @@ class Command:
# Behavior of this code need to be checked. Might yield
# incorrect results on some rare occasions.
spec_args = names[:-len(defaults) if defaults else len(names)]
spec_kwargs = list(zip(names[-len(defaults):], defaults)) if defaults else {}
spec_kwargs = list(
zip(names[-len(defaults):], defaults)) if defaults else {}
# Removing self from arguments specification. Command handler
# should receive the processors as a first argument, which
......@@ -227,6 +237,7 @@ class Command:
return spec_args, spec_kwargs, var_args, var_kwargs
def command(*names, **properties):
"""
A decorator for defining commands in a declarative way. Provides
......@@ -287,7 +298,8 @@ def command(*names, **properties):
raise DefinitionError("Empty option can be used only with raw commands")
if extra and overlap:
raise DefinitionError("Extra and overlap options can not be used together")
raise DefinitionError("Extra and overlap options can not be used "
"together")
properties = {
'usage': usage,
......@@ -323,6 +335,7 @@ def command(*names, **properties):
return decorator
def doc(text):
"""
This decorator is used to bind a documentation (a help) to a
......
......@@ -35,8 +35,13 @@ 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
from gajim.command_system.framework import CommandContainer
from gajim.command_system.framework import command
from gajim.command_system.framework import doc
from gajim.command_system.implementation.hosts import ChatCommands
from gajim.command_system.implementation.hosts import PrivateChatCommands
from gajim.command_system.implementation.hosts import GroupChatCommands
class CustomCommonCommands(CommandContainer):
"""
......@@ -69,6 +74,7 @@ class CustomCommonCommands(CommandContainer):
"""
return "I don't dance."
class CustomChatCommands(CommandContainer):
"""
This command container bounds only to the ChatCommands command host.
......@@ -92,6 +98,7 @@ class CustomChatCommands(CommandContainer):
"""
return "Buy yourself a stereo."
class CustomPrivateChatCommands(CommandContainer):
"""
This command container bounds only to the PrivateChatCommands
......@@ -108,6 +115,7 @@ class CustomPrivateChatCommands(CommandContainer):
def make_coffee(self):
return "I'm not a coffee machine!"
class CustomGroupChatCommands(CommandContainer):
"""
This command container bounds only to the GroupChatCommands command
......
......@@ -46,6 +46,7 @@ from gajim.command_system.implementation.hosts import ChatCommands
from gajim.command_system.implementation.hosts import PrivateChatCommands
from gajim.command_system.implementation.hosts import GroupChatCommands
class Execute(CommandContainer):
AUTOMATIC = True
HOSTS = ChatCommands, PrivateChatCommands, GroupChatCommands
......@@ -117,6 +118,7 @@ class Execute(CommandContainer):
strip = chr(10) + chr(32)
return text.decode().strip(strip)
class Show(Execute):
@command("sh", raw=True)
......
......@@ -20,6 +20,7 @@ different command processors, which are the source of commands.
from gajim.command_system.framework import CommandHost
class ChatCommands(CommandHost):
"""
This command host is bound to the command processor which processes
......@@ -27,6 +28,7 @@ class ChatCommands(CommandHost):
"""
AUTOMATIC = True
class PrivateChatCommands(CommandHost):
"""
This command host is bound to the command processor which processes
......@@ -34,6 +36,7 @@ class PrivateChatCommands(CommandHost):
"""
AUTOMATIC = True
class GroupChatCommands(CommandHost):
"""
This command host is bound to the command processor which processes
......
......@@ -39,7 +39,9 @@ from gajim.common import app
from gajim.common.i18n import _
from gajim.command_system.framework import CommandProcessor
from gajim.command_system.errors import CommandError, NoCommandError
from gajim.command_system.errors import CommandError
from gajim.command_system.errors import NoCommandError
class ChatCommandProcessor(CommandProcessor):
"""
......@@ -95,12 +97,14 @@ class ChatCommandProcessor(CommandProcessor):
self.echo(help_(self, name))
return True
def command_postprocessor(self, command, name, arguments, args, kwargs, value):
def command_postprocessor(self, command, name, arguments, args, kwargs,
value):
# If command returns a string - print it to a user. A convenient
# and sufficient in most simple cases shortcut to a using echo.
if value and isinstance(value, str):
self.echo(value)
class CommandTools:
"""
Contains a set of basic tools and shortcuts you can use in your
......
......@@ -17,7 +17,8 @@
Provides an actual implementation for the standard commands.
"""
from time import localtime, strftime
from time import localtime
from time import strftime
from datetime import date
from gi.repository import GLib
......@@ -28,10 +29,14 @@ from gajim.common.i18n import _
from gajim.common.const import KindConstant
from gajim.command_system.errors import CommandError
from gajim.command_system.framework import CommandContainer, command, doc
from gajim.command_system.framework import CommandContainer
from gajim.command_system.framework import command
from gajim.command_system.framework import doc
from gajim.command_system.mapping import generate_usage
from gajim.command_system.implementation.hosts import ChatCommands, PrivateChatCommands, GroupChatCommands
from gajim.command_system.implementation.hosts import ChatCommands
from gajim.command_system.implementation.hosts import PrivateChatCommands
from gajim.command_system.implementation.hosts import GroupChatCommands
class StandardCommonCommands(CommandContainer):
......@@ -44,7 +49,8 @@ class StandardCommonCommands(CommandContainer):
HOSTS = ChatCommands, PrivateChatCommands, GroupChatCommands
@command(overlap=True)
@doc(_("Show help on a given command or a list of available commands if -a is given"))
@doc(_("Show help on a given command or a list of available commands if "
"-a is given"))
def help(self, cmd=None, all_=False):
if cmd:
cmd = self.get_command(cmd)
......@@ -111,12 +117,13 @@ class StandardCommonCommands(CommandContainer):
if date_obj == date.today():
formatted = "[%s] %s: %s" % (time_, contact, row.message)
else:
formatted = "[%s, %s] %s: %s" % (date_, time_, contact, row.message)
formatted = "[%s, %s] %s: %s" % (
date_, time_, contact, row.message)
self.echo(formatted)
@command(raw=True, empty=True)
#Do not translate online, away, chat, xa, dnd
# Do not translate online, away, chat, xa, dnd
@doc(_("""
Set the current status
......@@ -128,7 +135,7 @@ class StandardCommonCommands(CommandContainer):
raise CommandError("Invalid status given")
for connection in app.connections.values():
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
'sync_with_global_status'):
continue
if not connection.is_connected:
continue
......@@ -142,7 +149,7 @@ class StandardCommonCommands(CommandContainer):
for connection in app.connections.values():
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
'sync_with_global_status'):
continue
if not connection.is_connected:
continue
......@@ -156,12 +163,13 @@ class StandardCommonCommands(CommandContainer):
for connection in app.connections.values():
if not app.config.get_per('accounts', connection.name,
'sync_with_global_status'):
'sync_with_global_status'):
continue
if not connection.is_connected:
continue
connection.change_status('online', message)
class StandardCommonChatCommands(CommandContainer):
"""
This command container contains standard commands, which are common
......@@ -180,7 +188,8 @@ class StandardCommonChatCommands(CommandContainer):
@doc(_("Send a ping to the contact"))
def ping(self):
if self.account == app.ZEROCONF_ACC_NAME:
raise CommandError(_('Command is not supported for zeroconf accounts'))
raise CommandError(
_('Command is not supported for zeroconf accounts'))
app.connections[self.account].get_module('Ping').send_ping(self.contact)
@command
......@@ -221,6 +230,7 @@ class StandardCommonChatCommands(CommandContainer):
def attention(self, message):
self.send_message(message, process_commands=False, attention=True)
class StandardChatCommands(CommandContainer):
"""
This command container contains standard commands which are unique
......@@ -230,6 +240,7 @@ class StandardChatCommands(CommandContainer):
AUTOMATIC = True
HOSTS = (ChatCommands,)
class StandardPrivateChatCommands(CommandContainer):
"""
This command container contains standard commands which are unique
......@@ -239,6 +250,7 @@ class StandardPrivateChatCommands(CommandContainer):
AUTOMATIC = True
HOSTS = (PrivateChatCommands,)
class StandardGroupChatCommands(CommandContainer):
"""
This command container contains standard commands which are unique
......@@ -275,7 +287,8 @@ class StandardGroupChatCommands(CommandContainer):
raise CommandError(_("Nickname not found"))
@command('msg', raw=True)
@doc(_("Open a private chat window with a specified participant and send him a message"))
@doc(_("Open a private chat window with a specified participant and send "
"him a message"))
def message(self, nick, a_message):
nicks = app.contacts.get_nick_list(self.account, self.room_jid)
if nick in nicks:
......@@ -296,7 +309,8 @@ class StandardGroupChatCommands(CommandContainer):
@doc(_("Invite a user to a group chat for a reason"))
def invite(self, jid, reason):
self.connection.get_module('MUC').invite(self.room_jid, jid, reason)
return _("Invited %(jid)s to %(room_jid)s") % {'jid': jid,
return _("Invited %(jid)s to %(room_jid)s") % {
'jid': jid,
'room_jid': self.room_jid}
@command(raw=True, empty=True)
......@@ -310,7 +324,8 @@ class StandardGroupChatCommands(CommandContainer):
GLib.Variant('as', [self.account, jid]))
@command('part', 'close', raw=True, empty=True)
@doc(_("Leave the group chat, optionally giving a reason, and close tab or window"))
@doc(_("Leave the group chat, optionally giving a reason, and close tab or "
"window"))
def leave(self, reason):
self.leave(reason=reason)
......@@ -322,7 +337,8 @@ class StandardGroupChatCommands(CommandContainer):
"""))
def ban(self, who, reason=''):
if who in app.contacts.get_nick_list(self.account, self.room_jid):
contact = app.contacts.get_gc_contact(self.account, self.room_jid, who)
contact = app.contacts.get_gc_contact(
self.account, self.room_jid, who)
who = contact.jid
self.connection.get_module('MUC').set_affiliation(
self.room_jid,
......@@ -332,25 +348,25 @@ class StandardGroupChatCommands(CommandContainer):
@command(raw=True, empty=True)
@doc(_("Kick user from group chat by nickname"))
def kick(self, who, reason):
if not who in app.contacts.get_nick_list(self.account, self.room_jid):
if who not in app.contacts.get_nick_list(self.account, self.room_jid):
raise CommandError(_("Nickname not found"))
self.connection.get_module('MUC').set_role(
self.room_jid, who, 'none', reason or str())
@command(raw=True)
#Do not translate moderator, participant, visitor, none
# Do not translate moderator, participant, visitor, none
@doc(_("""Set participant role in group chat.
Role can be given as one of the following values:
moderator, participant, visitor, none"""))
def role(self, who, role):
if role not in ('moderator', 'participant', 'visitor', 'none'):
raise CommandError(_("Invalid role given"))
if not who in app.contacts.get_nick_list(self.account, self.room_jid):
if who not in app.contacts.get_nick_list(self.account, self.room_jid):
raise CommandError(_("Nickname not found"))
self.connection.get_module('MUC').set_role(self.room_jid, who, role)
@command(raw=True)
#Do not translate owner, admin, member, outcast, none
# Do not translate owner, admin, member, outcast, none
@doc(_("""Set participant affiliation in group chat.
Affiliation can be given as one of the following values:
owner, admin, member, outcast, none"""))
......@@ -401,6 +417,7 @@ class StandardGroupChatCommands(CommandContainer):
@doc(_("Send a ping to the contact"))
def ping(self, nick):
if self.account == app.ZEROCONF_ACC_NAME:
raise CommandError(_('Command is not supported for zeroconf accounts'))
raise CommandError(
_('Command is not supported for zeroconf accounts'))
gc_c = app.contacts.get_gc_contact(self.account, self.room_jid, nick)
app.connections[self.account].get_module('Ping').send_ping(gc_c)
......@@ -27,7 +27,8 @@ from operator import itemgetter
from gajim.common.i18n import _
from gajim.command_system.errors import DefinitionError, CommandError
from gajim.command_system.errors import DefinitionError
from gajim.command_system.errors import CommandError
# Quite complex piece of regular expression logic to parse options and
# arguments. Might need some tweaking along the way.
......@@ -43,6 +44,7 @@ KEY_ENCODING = 'UTF-8'
# on command handler argument specification) will be rendered.
USAGE_PATTERN = 'Usage: %s %s'
def parse_arguments(arguments):
"""
Simple yet effective and sufficient in most cases parser which
......@@ -110,6 +112,7 @@ def parse_arguments(arguments):
return args, opts
def adapt_arguments(command, arguments, args, opts):
"""
Adapt args and opts got from the parser to a specific handler by
......@@ -268,7 +271,8 @@ def adapt_arguments(command, arguments, args, opts):
initial = norm_kwargs.get(key)
if isinstance(initial, bool):
if not isinstance(value, bool):
raise CommandError("%s: Switch can not take an argument" % key, command)
raise CommandError(
"%s: Switch can not take an argument" % key, command)
# Inject the source arguments as a string as a first argument, if
# command has enabled the corresponding option.
......@@ -279,6 +283,7 @@ def adapt_arguments(command, arguments, args, opts):
# command handler and being expanded.
return tuple(args), dict(opts)
def generate_usage(command, complete=True):
"""
Extract handler's arguments specification and wrap them in a
......@@ -323,7 +328,8 @@ def generate_usage(command, complete=True):
if spec_args:
args += '<%s>' % ', '.join(spec_args)
if var_args or sp_extra:
args += (' ' if spec_args else str()) + '<<%s>>' % (var_args or sp_extra)
args += (' ' if spec_args else str()) + '<<%s>>' % (
var_args or sp_extra)
usage += args
......
......@@ -24,6 +24,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
def remove(sequence, target):
if isinstance(sequence, list):
if target in sequence:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment