From 24bd3a6412d0207202a06e57379939805cf347e6 Mon Sep 17 00:00:00 2001 From: BlauFeuer Date: Sat, 18 Feb 2017 20:56:08 -0500 Subject: [PATCH] markup, whitespace and PEP 8 Indent and variable name updates, also. --- evennia/commands/default/player.py | 208 +++++++++++++++-------------- 1 file changed, 106 insertions(+), 102 deletions(-) diff --git a/evennia/commands/default/player.py b/evennia/commands/default/player.py index b2d18b625a..2a0d192086 100644 --- a/evennia/commands/default/player.py +++ b/evennia/commands/default/player.py @@ -13,7 +13,7 @@ The property self.character can be used to access the character when these commands are triggered with a connected character (such as the case of the @ooc command), it is None if we are OOC. -Note that under MULTISESSION_MODE > 2, Player- commands should use +Note that under MULTISESSION_MODE > 2, Player commands should use self.msg() and similar methods to reroute returns to the correct method. Otherwise all text will be returned to all connected sessions. @@ -44,7 +44,7 @@ class MuxPlayerLookCommand(COMMAND_DEFAULT_CLASS): """ def parse(self): - "Custom parsing" + """Custom parsing""" super(MuxPlayerLookCommand, self).parse() @@ -62,7 +62,7 @@ class MuxPlayerLookCommand(COMMAND_DEFAULT_CLASS): # store playable property if self.args: self.playable = dict((utils.to_str(char.key.lower()), char) - for char in playable).get(self.args.lower(), None) + for char in playable).get(self.args.lower(), None) else: self.playable = playable @@ -83,10 +83,10 @@ class CmdOOCLook(MuxPlayerLookCommand): Look in the ooc state. """ - #This is an OOC version of the look command. Since a - #Player doesn't have an in-game existence, there is no - #concept of location or "self". If we are controlling - #a character, pass control over to normal look. + # This is an OOC version of the look command. Since a + # Player doesn't have an in-game existence, there is no + # concept of location or "self". If we are controlling + # a character, pass control over to normal look. key = "look" aliases = ["l", "ls"] @@ -97,11 +97,11 @@ class CmdOOCLook(MuxPlayerLookCommand): player_caller = True def func(self): - "implement the ooc look command" + """implement the ooc look command""" if _MULTISESSION_MODE < 2: # only one character allowed - self.msg("You are out-of-character (OOC).\nUse {w@ic{n to get back into the game.") + self.msg("You are out-of-character (OOC).\nUse |w@ic|n to get back into the game.") return # call on-player look helper method @@ -128,7 +128,7 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS): player_caller = True def func(self): - "create the new character" + """create the new character""" player = self.player if not self.args: self.msg("Usage: @charcreate [= description]") @@ -139,8 +139,8 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS): charmax = _MAX_NR_CHARACTERS if _MULTISESSION_MODE > 1 else 1 if not player.is_superuser and \ - (player.db._playable_characters and - len(player.db._playable_characters) >= charmax): + (player.db._playable_characters and + len(player.db._playable_characters) >= charmax): self.msg("You may only create a maximum of %i characters." % charmax) return from evennia.objects.models import ObjectDB @@ -150,15 +150,13 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS): # check if this Character already exists. Note that we are only # searching the base character typeclass here, not any child # classes. - self.msg("{rA character named '{w%s{r' already exists.{n" % key) + self.msg("|rA character named '|w%s|r' already exists.|n" % key) return # create the character start_location = ObjectDB.objects.get_id(settings.START_LOCATION) default_home = ObjectDB.objects.get_id(settings.DEFAULT_HOME) permissions = settings.PERMISSION_PLAYER_DEFAULT - - new_character = create.create_object(typeclass, key=key, location=start_location, home=default_home, @@ -171,7 +169,8 @@ class CmdCharCreate(COMMAND_DEFAULT_CLASS): new_character.db.desc = desc elif not new_character.db.desc: new_character.db.desc = "This is a Player." - self.msg("Created new character %s. Use {w@ic %s{n to enter the game as this character." % (new_character.key, new_character.key)) + self.msg("Created new character %s. Use |w@ic %s|n to enter the game as this character." + % (new_character.key, new_character.key)) class CmdCharDelete(COMMAND_DEFAULT_CLASS): @@ -188,7 +187,7 @@ class CmdCharDelete(COMMAND_DEFAULT_CLASS): help_category = "General" def func(self): - "delete the character" + """delete the character""" player = self.player if not self.args: @@ -196,23 +195,23 @@ class CmdCharDelete(COMMAND_DEFAULT_CLASS): return # use the playable_characters list to search - match = [char for char in utils.make_iter(player.db._playable_characters) if char.key.lower() == self.args.lower()] + match = [char for char in utils.make_iter(player.db._playable_characters) + if char.key.lower() == self.args.lower()] if not match: self.msg("You have no such character to delete.") return elif len(match) > 1: self.msg("Aborting - there are two characters with the same name. Ask an admin to delete the right one.") return - else: # one match + else: # one match from evennia.utils.evmenu import get_input - def _callback(caller, prompt, result): + def _callback(caller, callback_prompt, result): if result.lower() == "yes": # only take action delobj = caller.ndb._char_to_delete key = delobj.key - caller.db._playable_characters = [char for char - in caller.db._playable_characters if char != delobj] + caller.db._playable_characters = [pc for pc in caller.db._playable_characters if pc != delobj] delobj.delete() self.msg("Character '%s' was permanently deleted." % key) else: @@ -272,7 +271,8 @@ class CmdIC(COMMAND_DEFAULT_CLASS): self.msg("That is not a valid character choice.") return if len(new_character) > 1: - self.msg("Multiple targets with the same name:\n %s" % ", ".join("%s(#%s)" % (obj.key, obj.id) for obj in new_character)) + self.msg("Multiple targets with the same name:\n %s" + % ", ".join("%s(#%s)" % (obj.key, obj.id) for obj in new_character)) return else: new_character = new_character[0] @@ -280,7 +280,7 @@ class CmdIC(COMMAND_DEFAULT_CLASS): player.puppet_object(session, new_character) player.db._last_puppet = new_character except RuntimeError as exc: - self.msg("{rYou cannot become {C%s{n: %s" % (new_character.name, exc)) + self.msg("|rYou cannot become |C%s|n: %s" % (new_character.name, exc)) # note that this is inheriting from MuxPlayerLookCommand, @@ -306,7 +306,7 @@ class CmdOOC(MuxPlayerLookCommand): player_caller = True def func(self): - "Implement function" + """Implement function""" player = self.player session = self.session @@ -322,17 +322,18 @@ class CmdOOC(MuxPlayerLookCommand): # disconnect try: player.unpuppet_object(session) - self.msg("\n{GYou go OOC.{n\n") + self.msg("\n|GYou go OOC.|n\n") if _MULTISESSION_MODE < 2: # only one character allowed - self.msg("You are out-of-character (OOC).\nUse {w@ic{n to get back into the game.") + self.msg("You are out-of-character (OOC).\nUse |w@ic|n to get back into the game.") return self.msg(player.at_look(target=self.playable, session=session)) except RuntimeError as exc: - self.msg("{rCould not unpuppet from {c%s{n: %s" % (old_char, exc)) + self.msg("|rCould not unpuppet from |c%s|n: %s" % (old_char, exc)) + class CmdSessions(COMMAND_DEFAULT_CLASS): """ @@ -352,22 +353,22 @@ class CmdSessions(COMMAND_DEFAULT_CLASS): player_caller = True def func(self): - "Implement function" + """Implement function""" player = self.player sessions = player.sessions.all() - table = prettytable.PrettyTable(["{wsessid", - "{wprotocol", - "{whost", - "{wpuppet/character", - "{wlocation"]) + table = prettytable.PrettyTable(["|wsessid", + "|wprotocol", + "|whost", + "|wpuppet/character", + "|wlocation"]) for sess in sorted(sessions, key=lambda x: x.sessid): char = player.get_puppet(sess) table.add_row([str(sess.sessid), str(sess.protocol_key), type(sess.address) == tuple and sess.address[0] or sess.address, char and str(char) or "None", char and str(char.location) or "N/A"]) - string = "{wYour current session(s):{n\n%s" % table + string = "|wYour current session(s):|n\n%s" % table self.msg(string) @@ -408,16 +409,17 @@ class CmdWho(COMMAND_DEFAULT_CLASS): nplayers = (SESSIONS.player_count()) if show_session_data: # privileged info - table = prettytable.PrettyTable(["{wPlayer Name", - "{wOn for", - "{wIdle", - "{wPuppeting", - "{wRoom", - "{wCmds", - "{wProtocol", - "{wHost"]) + table = prettytable.PrettyTable(["|wPlayer Name", + "|wOn for", + "|wIdle", + "|wPuppeting", + "|wRoom", + "|wCmds", + "|wProtocol", + "|wHost"]) for session in session_list: - if not session.logged_in: continue + if not session.logged_in: + continue delta_cmd = time.time() - session.cmd_last_visible delta_conn = time.time() - session.conn_time player = session.get_player() @@ -433,7 +435,7 @@ class CmdWho(COMMAND_DEFAULT_CLASS): isinstance(session.address, tuple) and session.address[0] or session.address]) else: # unprivileged - table = prettytable.PrettyTable(["{wPlayer name", "{wOn for", "{wIdle"]) + table = prettytable.PrettyTable(["|wPlayer name", "|wOn for", "|wIdle"]) for session in session_list: if not session.logged_in: continue @@ -444,8 +446,9 @@ class CmdWho(COMMAND_DEFAULT_CLASS): utils.time_format(delta_conn, 0), utils.time_format(delta_cmd, 1)]) - isone = nplayers == 1 - string = "{wPlayers:{n\n%s\n%s unique account%s logged in." % (table, "One" if isone else nplayers, "" if isone else "s") + is_one = nplayers == 1 + string = "|wPlayers:|n\n%s\n%s unique account%s logged in." \ + % (table, "One" if is_one else nplayers, "" if is_one else "s") self.msg(string) @@ -489,13 +492,13 @@ class CmdOption(COMMAND_DEFAULT_CLASS): if "save" in self.switches: # save all options self.caller.db._saved_protocol_flags = flags - self.msg("{gSaved all options. Use @option/clear to remove.{n") + self.msg("|gSaved all options. Use @option/clear to remove.|n") if "clear" in self.switches: # clear all saves self.caller.db._saved_protocol_flags = {} - self.msg("{gCleared all saved options.") + self.msg("|gCleared all saved options.") - options = dict(flags) # make a copy of the flag dict + options = dict(flags) # make a copy of the flag dict saved_options = dict(self.caller.attributes.get("_saved_protocol_flags", default={})) if "SCREENWIDTH" in options: @@ -503,7 +506,7 @@ class CmdOption(COMMAND_DEFAULT_CLASS): options["SCREENWIDTH"] = options["SCREENWIDTH"][0] else: options["SCREENWIDTH"] = " \n".join("%s : %s" % (screenid, size) - for screenid, size in options["SCREENWIDTH"].iteritems()) + for screenid, size in options["SCREENWIDTH"].iteritems()) if "SCREENHEIGHT" in options: if len(options["SCREENHEIGHT"]) == 1: options["SCREENHEIGHT"] = options["SCREENHEIGHT"][0] @@ -522,7 +525,7 @@ class CmdOption(COMMAND_DEFAULT_CLASS): row.append("%s%s" % (saved, changed)) table.add_row(*row) - self.msg("{wClient settings (%s):|n\n%s|n" % (self.session.protocol_key, table)) + self.msg("|wClient settings (%s):|n\n%s|n" % (self.session.protocol_key, table)) return @@ -532,30 +535,30 @@ class CmdOption(COMMAND_DEFAULT_CLASS): # Try to assign new values - def validate_encoding(val): + def validate_encoding(new_encoding): # helper: change encoding try: - utils.to_str(utils.to_unicode("test-string"), encoding=val) + utils.to_str(utils.to_unicode("test-string"), encoding=new_encoding) except LookupError: - raise RuntimeError("The encoding '|w%s|n' is invalid. " % val) + raise RuntimeError("The encoding '|w%s|n' is invalid. " % new_encoding) return val - def validate_size(val): - return {0: int(val)} + def validate_size(new_size): + return {0: int(new_size)} - def validate_bool(val): - return True if val.lower() in ("true", "on", "1") else False + def validate_bool(new_bool): + return True if new_bool.lower() in ("true", "on", "1") else False - def update(name, val, validator): + def update(new_name, new_val, validator): # helper: update property and report errors try: - old_val = flags.get(name, False) - new_val = validator(val) - flags[name] = new_val - self.msg("Option |w%s|n was changed from '|w%s|n' to '|w%s|n'." % (name, old_val, new_val)) - return {name: new_val} + old_val = flags.get(new_name, False) + new_val = validator(new_val) + flags[new_name] = new_val + self.msg("Option |w%s|n was changed from '|w%s|n' to '|w%s|n'." % (new_name, old_val, new_val)) + return {new_name: new_val} except Exception, err: - self.msg("|rCould not set option |w%s|r:|n %s" % (name, err)) + self.msg("|rCould not set option |w%s|r:|n %s" % (new_name, err)) return False validators = {"ANSI": validate_bool, @@ -590,12 +593,12 @@ class CmdOption(COMMAND_DEFAULT_CLASS): saved_options.update(optiondict) self.player.attributes.add("_saved_protocol_flags", saved_options) for key in optiondict: - self.msg("{gSaved option %s.{n" % key) + self.msg("|gSaved option %s.|n" % key) if "clear" in self.switches: # clear this save for key in optiondict: self.player.attributes.get("_saved_protocol_flags", {}).pop(key, None) - self.msg("{gCleared saved %s." % key) + self.msg("|gCleared saved %s." % key) self.session.update_flags(**optiondict) @@ -616,14 +619,14 @@ class CmdPassword(COMMAND_DEFAULT_CLASS): player_caller = True def func(self): - "hook function." + """hook function.""" player = self.player if not self.rhs: self.msg("Usage: @password = ") return - oldpass = self.lhslist[0] # this is already stripped by parse() - newpass = self.rhslist[0] # '' + oldpass = self.lhslist[0] # Both of these are + newpass = self.rhslist[0] # already stripped by parse() if not player.check_password(oldpass): self.msg("The specified old password isn't correct.") elif len(newpass) < 3: @@ -655,11 +658,11 @@ class CmdQuit(COMMAND_DEFAULT_CLASS): player_caller = True def func(self): - "hook function" + """hook function""" player = self.player if 'all' in self.switches: - player.msg("{RQuitting{n all sessions. Hope to see you soon again.", session=self.session) + player.msg("|RQuitting|n all sessions. Hope to see you soon again.", session=self.session) for session in player.sessions.all(): player.disconnect_session_from_player(session) else: @@ -674,7 +677,6 @@ class CmdQuit(COMMAND_DEFAULT_CLASS): player.disconnect_session_from_player(self.session) - class CmdColorTest(COMMAND_DEFAULT_CLASS): """ testing which colors your client support @@ -697,23 +699,23 @@ class CmdColorTest(COMMAND_DEFAULT_CLASS): player_caller = True def table_format(self, table): - """ - Helper method to format the ansi/xterm256 tables. - Takes a table of columns [[val,val,...],[val,val,...],...] - """ - if not table: - return [[]] + """ + Helper method to format the ansi/xterm256 tables. + Takes a table of columns [[val,val,...],[val,val,...],...] + """ + if not table: + return [[]] - extra_space = 1 - max_widths = [max([len(str(val)) for val in col]) for col in table] - ftable = [] - for irow in range(len(table[0])): - ftable.append([str(col[irow]).ljust(max_widths[icol]) + " " * extra_space - for icol, col in enumerate(table)]) - return ftable + extra_space = 1 + max_widths = [max([len(str(val)) for val in col]) for col in table] + ftable = [] + for irow in range(len(table[0])): + ftable.append([str(col[irow]).ljust(max_widths[icol]) + " " * + extra_space for icol, col in enumerate(table)]) + return ftable def func(self): - "Show color tables" + """Show color tables""" if self.args.startswith("a"): # show ansi 16-color table @@ -723,9 +725,11 @@ class CmdColorTest(COMMAND_DEFAULT_CLASS): # show all ansi color-related codes col1 = ["%s%s|n" % (code, code.replace("|", "||")) for code, _ in ap.ext_ansi_map[48:56]] col2 = ["%s%s|n" % (code, code.replace("|", "||")) for code, _ in ap.ext_ansi_map[56:64]] - col3 = ["%s%s|n" % (code.replace("\\",""), code.replace("|", "||").replace("\\", "")) for code, _ in ap.ext_ansi_map[-8:]] - col4 = ["%s%s|n" % (code.replace("\\",""), code.replace("|", "||").replace("\\", "")) for code, _ in ap.ansi_bright_bgs[-8:]] - col2.extend(["" for i in range(len(col1)-len(col2))]) + col3 = ["%s%s|n" % (code.replace("\\", ""), code.replace("|", "||").replace("\\", "")) + for code, _ in ap.ext_ansi_map[-8:]] + col4 = ["%s%s|n" % (code.replace("\\", ""), code.replace("|", "||").replace("\\", "")) + for code, _ in ap.ansi_bright_bgs[-8:]] + col2.extend(["" for _ in range(len(col1)-len(col2))]) table = utils.format_table([col1, col2, col4, col3]) string = "ANSI colors:" for row in table: @@ -744,9 +748,8 @@ class CmdColorTest(COMMAND_DEFAULT_CLASS): # foreground table table[ir].append("|%i%i%i%s|n" % (ir, ig, ib, "||%i%i%i" % (ir, ig, ib))) # background table - table[6+ir].append("|%i%i%i|[%i%i%i%s|n" % (5 - ir, 5 - ig, 5 - ib, - ir, ig, ib, - "||[%i%i%i" % (ir, ig, ib))) + table[6+ir].append("|%i%i%i|[%i%i%i%s|n" + % (5 - ir, 5 - ig, 5 - ib, ir, ig, ib, "||[%i%i%i" % (ir, ig, ib))) table = self.table_format(table) string = "Xterm256 colors (if not all hues show, your client might not report that it can handle xterm256):" string += "\n" + "\n".join("".join(row) for row in table) @@ -755,15 +758,15 @@ class CmdColorTest(COMMAND_DEFAULT_CLASS): for igray in range(6): letter = chr(97 + (ibatch*6 + igray)) inverse = chr(122 - (ibatch*6 + igray)) - table[0 + igray].append("|=%s%s |n" % (letter, "||=%s" % (letter))) - table[6 + igray].append("|=%s|[=%s%s |n" % (inverse, letter, "||[=%s" % (letter))) + table[0 + igray].append("|=%s%s |n" % (letter, "||=%s" % letter)) + table[6 + igray].append("|=%s|[=%s%s |n" % (inverse, letter, "||[=%s" % letter)) for igray in range(6): # the last row (y, z) has empty columns if igray < 2: letter = chr(121 + igray) inverse = chr(98 - igray) - fg = "|=%s%s |n" % (letter, "||=%s" % (letter)) - bg = "|=%s|[=%s%s |n" % (inverse, letter, "||[=%s" % (letter)) + fg = "|=%s%s |n" % (letter, "||=%s" % letter) + bg = "|=%s|[=%s%s |n" % (inverse, letter, "||[=%s" % letter) else: fg, bg = " ", " " table[0 + igray].append(fg) @@ -802,7 +805,7 @@ class CmdQuell(COMMAND_DEFAULT_CLASS): player_caller = True def _recache_locks(self, player): - "Helper method to reset the lockhandler on an already puppeted object" + """Helper method to reset the lockhandler on an already puppeted object""" if self.session: char = self.session.puppet if char: @@ -813,7 +816,7 @@ class CmdQuell(COMMAND_DEFAULT_CLASS): player.locks.reset() def func(self): - "Perform the command" + """Perform the command""" player = self.player permstr = player.is_superuser and " (superuser)" or " (%s)" % (", ".join(player.permissions.all())) if self.cmdstring == '@unquell': @@ -829,9 +832,10 @@ class CmdQuell(COMMAND_DEFAULT_CLASS): player.attributes.add('_quell', True) puppet = self.session.puppet if puppet: - cpermstr = " (%s)" % ", ".join(puppet.permissions.all()) + cpermstr = "(%s)" % ", ".join(puppet.permissions.all()) cpermstr = "Quelling to current puppet's permissions%s." % cpermstr - cpermstr += "\n(Note: If this is higher than Player permissions%s, the lowest of the two will be used.)" % permstr + cpermstr += "\n(Note: If this is higher than Player permissions %s," \ + " the lowest of the two will be used.)" % permstr cpermstr += "\nUse @unquell to return to normal permission usage." self.msg(cpermstr) else: