comms.py update markup/PEP 8/whitespace,

Also removed redundant code
This commit is contained in:
BlauFeuer 2017-02-12 19:21:40 -05:00 committed by GitHub
parent 2079504c07
commit a687cb7b26

View file

@ -72,7 +72,7 @@ class CmdAddCom(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement the command"
"""Implement the command"""
caller = self.caller
args = self.args
@ -148,7 +148,7 @@ class CmdDelCom(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implementing the command. "
"""Implementing the command. """
caller = self.caller
player = caller
@ -212,7 +212,7 @@ class CmdAllCom(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Runs the function"
"""Runs the function"""
caller = self.caller
args = self.args
@ -229,7 +229,7 @@ class CmdAllCom(COMMAND_DEFAULT_CLASS):
for channel in channels:
self.execute_cmd("addcom %s" % channel.key)
elif args == "off":
#get names all subscribed channels and disconnect from them all
# get names all subscribed channels and disconnect from them all
channels = ChannelDB.objects.get_subscriptions(caller)
for channel in channels:
self.execute_cmd("delcom %s" % channel.key)
@ -241,13 +241,13 @@ class CmdAllCom(COMMAND_DEFAULT_CLASS):
self.execute_cmd("@cdestroy %s" % channel.key)
elif args == "who":
# run a who, listing the subscribers on visible channels.
string = "\n{CChannel subscriptions{n"
string = "\n|CChannel subscriptions|n"
channels = [chan for chan in ChannelDB.objects.get_all_channels()
if chan.access(caller, 'listen')]
if not channels:
string += "No channels."
for channel in channels:
string += "\n{w%s:{n\n %s" % (channel.key, channel.wholist)
string += "\n|w%s:|n\n %s" % (channel.key, channel.wholist)
self.msg(string.strip())
else:
# wrong input
@ -276,7 +276,7 @@ class CmdChannels(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement function"
"""Implement function"""
caller = self.caller
@ -291,41 +291,43 @@ class CmdChannels(COMMAND_DEFAULT_CLASS):
if self.cmdstring == "comlist":
# just display the subscribed channels with no extra info
comtable = evtable.EvTable("{wchannel{n", "{wmy aliases{n", "{wdescription{n", align="l", maxwidth=_DEFAULT_WIDTH)
#comtable = prettytable.PrettyTable(["{wchannel", "{wmy aliases", "{wdescription"])
comtable = evtable.EvTable("|wchannel|n", "|wmy aliases|n",
"|wdescription|n", align="l", maxwidth=_DEFAULT_WIDTH)
for chan in subs:
clower = chan.key.lower()
nicks = caller.nicks.get(category="channel", return_obj=True)
comtable.add_row(*["%s%s" % (chan.key, chan.aliases.all() and
"(%s)" % ",".join(chan.aliases.all()) or ""),
"%s" % ",".join(nick.db_key for nick in make_iter(nicks)
if nick and nick.value[3].lower() == clower),
chan.db.desc])
self.msg("\n{wChannel subscriptions{n (use {w@channels{n to list all, {waddcom{n/{wdelcom{n to sub/unsub):{n\n%s" % comtable)
"(%s)" % ",".join(chan.aliases.all()) or ""),
"%s" % ",".join(nick.db_key for nick in make_iter(nicks)
if nick and nick.value[3].lower() == clower),
chan.db.desc])
self.msg("\n|wChannel subscriptions|n (use |w@channels|n to list all,"
" |waddcom|n/|wdelcom|n to sub/unsub):|n\n%s" % comtable)
else:
# full listing (of channels caller is able to listen to)
comtable = evtable.EvTable("{wsub{n", "{wchannel{n", "{wmy aliases{n", "{wlocks{n", "{wdescription{n", maxwidth=_DEFAULT_WIDTH)
#comtable = prettytable.PrettyTable(["{wsub", "{wchannel", "{wmy aliases", "{wlocks", "{wdescription"])
comtable = evtable.EvTable("|wsub|n", "|wchannel|n", "|wmy aliases|n",
"|wlocks|n", "|wdescription|n", maxwidth=_DEFAULT_WIDTH)
for chan in channels:
clower = chan.key.lower()
nicks = caller.nicks.get(category="channel", return_obj=True)
nicks = nicks or []
if chan not in subs:
substatus = "{rNo{n"
substatus = "|rNo|n"
elif caller in chan.mutelist:
substatus = "{rMuted{n"
substatus = "|rMuted|n"
else:
substatus = "{gYes{n"
substatus = "|gYes|n"
comtable.add_row(*[substatus,
"%s%s" % (chan.key, chan.aliases.all() and
"(%s)" % ",".join(chan.aliases.all()) or ""),
"%s" % ",".join(nick.db_key for nick in make_iter(nicks)
if nick.value[3].lower() == clower),
str(chan.locks),
chan.db.desc])
"%s%s" % (chan.key, chan.aliases.all() and
"(%s)" % ",".join(chan.aliases.all()) or ""),
"%s" % ",".join(nick.db_key for nick in make_iter(nicks)
if nick.value[3].lower() == clower),
str(chan.locks),
chan.db.desc])
comtable.reformat_column(0, width=9)
comtable.reformat_column(3, width=14)
self.msg("\n{wAvailable channels{n (use {wcomlist{n,{waddcom{n and {wdelcom{n to manage subscriptions):\n%s" % comtable)
self.msg("\n|wAvailable channels|n (use |wcomlist|n,|waddcom|n and |wdelcom|n"
" to manage subscriptions):\n%s" % comtable)
class CmdCdestroy(COMMAND_DEFAULT_CLASS):
@ -346,7 +348,7 @@ class CmdCdestroy(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Destroy objects cleanly."
"""Destroy objects cleanly."""
caller = self.caller
if not self.args:
@ -390,7 +392,7 @@ class CmdCBoot(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"implement the function"
"""implement the function"""
if not self.args or not self.rhs:
string = "Usage: @cboot[/quiet] <channel> = <player> [:reason]"
@ -415,11 +417,11 @@ class CmdCBoot(COMMAND_DEFAULT_CLASS):
string = "You don't control this channel."
self.msg(string)
return
if not player in channel.db_subscriptions.all():
if player not in channel.db_subscriptions.all():
string = "Player %s is not connected to channel %s." % (player.key, channel.key)
self.msg(string)
return
if not "quiet" in self.switches:
if "quiet" not in self.switches:
string = "%s boots %s from channel.%s" % (self.caller, player.key, reason)
channel.msg(string)
# find all player's nicks linked to this channel and delete them
@ -458,7 +460,7 @@ class CmdCemit(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement function"
"""Implement function"""
if not self.args or not self.rhs:
string = "Usage: @cemit[/switches] <channel> = <message>"
@ -475,7 +477,7 @@ class CmdCemit(COMMAND_DEFAULT_CLASS):
if "sendername" in self.switches:
message = "%s: %s" % (self.caller.key, message)
channel.msg(message)
if not "quiet" in self.switches:
if "quiet" not in self.switches:
string = "Sent to channel %s: %s" % (channel.key, message)
self.msg(string)
@ -497,7 +499,7 @@ class CmdCWho(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"implement function"
"""implement function"""
if not self.args:
string = "Usage: @cwho <channel>"
@ -511,8 +513,8 @@ class CmdCWho(COMMAND_DEFAULT_CLASS):
string = "You can't access this channel."
self.msg(string)
return
string = "\n{CChannel subscriptions{n"
string += "\n{w%s:{n\n %s" % (channel.key, channel.wholist)
string = "\n|CChannel subscriptions|n"
string += "\n|w%s:|n\n %s" % (channel.key, channel.wholist)
self.msg(string.strip())
@ -535,7 +537,7 @@ class CmdChannelCreate(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement the command"
"""Implement the command"""
caller = self.caller
@ -588,7 +590,7 @@ class CmdClock(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"run the function"
"""run the function"""
if not self.args:
string = "Usage: @clock channel [= lockstring]"
@ -640,7 +642,7 @@ class CmdCdesc(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement command"
"""Implement command"""
caller = self.caller
@ -651,7 +653,7 @@ class CmdCdesc(COMMAND_DEFAULT_CLASS):
if not channel:
self.msg("Channel '%s' not found." % self.lhs)
return
#check permissions
# check permissions
if not channel.access(caller, 'control'):
self.msg("You cannot admin this channel.")
return
@ -688,22 +690,20 @@ class CmdPage(COMMAND_DEFAULT_CLASS):
player_caller = True
def func(self):
"Implement function using the Msg methods"
"""Implement function using the Msg methods"""
# Since player_caller is set above, this will be a Player.
caller = self.caller
# get the messages we've sent (not to channels)
pages_we_sent = Msg.objects.get_messages_by_sender(caller,
exclude_channel_messages=True)
pages_we_sent = Msg.objects.get_messages_by_sender(caller, exclude_channel_messages=True)
# get last messages we've got
pages_we_got = Msg.objects.get_messages_by_receiver(caller)
if 'last' in self.switches:
if pages_we_sent:
recv = ",".join(obj.key for obj in pages_we_sent[-1].receivers)
self.msg("You last paged {c%s{n:%s" % (recv,
pages_we_sent[-1].message))
self.msg("You last paged |c%s|n:%s" % (recv, pages_we_sent[-1].message))
return
else:
self.msg("You haven't paged anyone yet.")
@ -725,11 +725,11 @@ class CmdPage(COMMAND_DEFAULT_CLASS):
lastpages = pages[-number:]
else:
lastpages = pages
template = "{w%s{n {c%s{n to {c%s{n: %s"
template = "|w%s|n |c%s|n to |c%s|n: %s"
lastpages = "\n ".join(template %
(utils.datetime_format(page.date_created),
",".join(obj.key for obj in page.senders),
"{n,{c ".join([obj.name for obj in page.receivers]),
"|n,|c ".join([obj.name for obj in page.receivers]),
page.message) for page in lastpages)
if lastpages:
@ -767,7 +767,7 @@ class CmdPage(COMMAND_DEFAULT_CLASS):
self.msg("Noone found to page.")
return
header = "{wPlayer{n {c%s{n {wpages:{n" % caller.key
header = "|wPlayer|n |c%s|n |wpages:|n" % caller.key
message = self.rhs
# if message begins with a :, we assume it is a 'page-pose'
@ -787,10 +787,11 @@ class CmdPage(COMMAND_DEFAULT_CLASS):
continue
pobj.msg("%s %s" % (header, message))
if hasattr(pobj, 'sessions') and not pobj.sessions.count():
received.append("{C%s{n" % pobj.name)
rstrings.append("%s is offline. They will see your message if they list their pages later." % received[-1])
received.append("|C%s|n" % pobj.name)
rstrings.append("%s is offline. They will see your message if they list their pages later."
% received[-1])
else:
received.append("{c%s{n" % pobj.name)
received.append("|c%s|n" % pobj.name)
if rstrings:
self.msg("\n".join(rstrings))
self.msg("You paged %s with: '%s'." % (", ".join(received), message))
@ -807,14 +808,15 @@ def _list_bots():
ircbots = [bot for bot in PlayerDB.objects.filter(db_is_bot=True, username__startswith="ircbot-")]
if ircbots:
from evennia.utils.evtable import EvTable
table = EvTable("{w#dbref{n", "{wbotname{n", "{wev-channel{n", "{wirc-channel{n", "{wSSL{n", maxwidth=_DEFAULT_WIDTH)
table = EvTable("|w#dbref|n", "|wbotname|n", "|wev-channel|n",
"|wirc-channel|n", "|wSSL|n", maxwidth=_DEFAULT_WIDTH)
for ircbot in ircbots:
ircinfo = "%s (%s:%s)" % (ircbot.db.irc_channel, ircbot.db.irc_network, ircbot.db.irc_port)
table.add_row("#%i" % ircbot.id, ircbot.db.irc_botname, ircbot.db.ev_channel, ircinfo, ircbot.db.irc_ssl)
return table
else:
return "No irc bots found."
return
class CmdIRC2Chan(COMMAND_DEFAULT_CLASS):
"""
@ -852,7 +854,7 @@ class CmdIRC2Chan(COMMAND_DEFAULT_CLASS):
help_category = "Comms"
def func(self):
"Setup the irc-channel mapping"
"""Setup the irc-channel mapping"""
if not settings.IRC_ENABLED:
string = """IRC is not enabled. You need to activate it in game/settings.py."""
@ -864,8 +866,7 @@ class CmdIRC2Chan(COMMAND_DEFAULT_CLASS):
self.msg(_list_bots())
return
if('disconnect' in self.switches or 'remove' in self.switches or
'delete' in self.switches):
if 'disconnect' in self.switches or 'remove' in self.switches or 'delete' in self.switches:
botname = "ircbot-%s" % self.lhs
matches = PlayerDB.objects.filter(db_is_bot=True, username=botname)
dbref = utils.dbref(self.lhs)
@ -880,12 +881,13 @@ class CmdIRC2Chan(COMMAND_DEFAULT_CLASS):
return
if not self.args or not self.rhs:
string = "Usage: @irc2chan[/switches] <evennia_channel> = <ircnetwork> <port> <#irchannel> <botname>[:typeclass]"
string = "Usage: @irc2chan[/switches] <evennia_channel> =" \
" <ircnetwork> <port> <#irchannel> <botname>[:typeclass]"
self.msg(string)
return
channel = self.lhs
self.rhs = self.rhs.replace('#', ' ') # to avoid Python comment issues
self.rhs = self.rhs.replace('#', ' ') # to avoid Python comment issues
try:
irc_network, irc_port, irc_channel, irc_botname = \
[part.strip() for part in self.rhs.split(None, 4)]
@ -945,7 +947,7 @@ class CmdIRCStatus(COMMAND_DEFAULT_CLASS):
help_category = "Comms"
def func(self):
"Handles the functioning of the command."
"""Handles the functioning of the command."""
if not self.args:
self.msg(_list_bots())
@ -987,8 +989,6 @@ class CmdIRCStatus(COMMAND_DEFAULT_CLASS):
self.caller.msg("You don't have permission to force-reload the IRC bot.")
# RSS connection
class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
"""
@ -1020,7 +1020,7 @@ class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
help_category = "Comms"
def func(self):
"Setup the rss-channel mapping"
"""Setup the rss-channel mapping"""
# checking we have all we need
if not settings.RSS_ENABLED:
@ -1029,10 +1029,10 @@ class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
return
try:
import feedparser
assert(feedparser) # to avoid checker error of not being used
assert feedparser # to avoid checker error of not being used
except ImportError:
string = ("RSS requires python-feedparser (https://pypi.python.org/pypi/feedparser). "
"Install before continuing.")
string = "RSS requires python-feedparser (https://pypi.python.org/pypi/feedparser)." \
" Install before continuing."
self.msg(string)
return
@ -1041,8 +1041,8 @@ class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
rssbots = [bot for bot in PlayerDB.objects.filter(db_is_bot=True, username__startswith="rssbot-")]
if rssbots:
from evennia.utils.evtable import EvTable
table = EvTable("{wdbid{n", "{wupdate rate{n", "{wev-channel",
"{wRSS feed URL{n", border="cells", maxwidth=_DEFAULT_WIDTH)
table = EvTable("|wdbid|n", "|wupdate rate|n", "|wev-channel",
"|wRSS feed URL|n", border="cells", maxwidth=_DEFAULT_WIDTH)
for rssbot in rssbots:
table.add_row(rssbot.id, rssbot.db.rss_rate, rssbot.db.ev_channel, rssbot.db.rss_url)
self.msg(table)
@ -1050,8 +1050,7 @@ class CmdRSS2Chan(COMMAND_DEFAULT_CLASS):
self.msg("No rss bots found.")
return
if('disconnect' in self.switches or 'remove' in self.switches or
'delete' in self.switches):
if 'disconnect' in self.switches or 'remove' in self.switches or 'delete' in self.switches:
botname = "rssbot-%s" % self.lhs
matches = PlayerDB.objects.filter(db_is_bot=True, db_key=botname)
if not matches: