def __init__(self, key, text="", links=None, linktexts=None, keywords=None, cols=1, helptext=None, selectcmds=None, callback=None, code="", nodefaultcmds=False, separator=""): """ key - the unique identifier of this node. text - is the text that will be displayed at top when viewing this node. links - a list of keys for unique menunodes this is connected to. The actual keys will not printed - keywords will be used (or a number) linktexts - an optional list of texts to describe the links. Must match link list if defined. Entries can be None to not generate any extra text for a particular link. keywords - an optional list of unique keys for choosing links. Must match links list. If not given, index numbers will be used. Also individual list entries can be None and will be replaed by indices. If CMD_NOMATCH or CMD_NOENTRY, no text will be generated to indicate the option exists. cols - how many columns to use for displaying options. helptext - if defined, this is shown when using the help command instead of the normal help index. selectcmds- a list of custom cmdclasses for handling each option. Must match links list, but some entries may be set to None to use default menu cmds. The given command's key will be used for the menu list entry unless it's CMD_NOMATCH or CMD_NOENTRY, in which case no text will be generated. These commands have access to self.menutree and so can be used to select nodes. code - functional code. Deprecated. This will be executed just before this node is loaded (i.e. as soon after it's been selected from another node). self.caller is available to call from this code block, as well as ev. callback - function callback. This will be called as callback(currentnode) just before this node is loaded (i.e. as soon as possible as it's been selected from another node). currentnode.caller is available. nodefaultcmds - if true, don't offer the default help and look commands in the node separator - this string will be put on the line between menu nodes. """ self.key = key self.cmdset = None self.links = links self.linktexts = linktexts self.keywords = keywords self.cols = cols self.selectcmds = selectcmds self.code = code self.callback = MethodType(callback, self, MenuNode) if callback else None self.nodefaultcmds = nodefaultcmds self.separator = separator Nlinks = len(self.links) if code: ev.logger.log_depmsg("menusystem.code is deprecated. Use menusystem.callback.") # validate the input if not self.links: self.links = [] if not self.linktexts or (len(self.linktexts) != Nlinks): self.linktexts = [None for i in range(Nlinks)] if not self.keywords or (len(self.keywords) != Nlinks): self.keywords = [None for i in range(Nlinks)] if not selectcmds or (len(self.selectcmds) != Nlinks): self.selectcmds = [None for i in range(Nlinks)] # Format default text for the menu-help command if not helptext: helptext = "Select one of the valid options (" for i in range(Nlinks): if self.keywords[i]: if self.keywords[i] not in (CMD_NOMATCH, CMD_NOINPUT): helptext += "%s, " % self.keywords[i] else: helptext += "%s, " % (i + 1) helptext = helptext.rstrip(", ") + ")" self.helptext = helptext # Format text display string = "" if text: string += "%s\n" % text # format the choices into as many collumns as specified choices = [] for ilink, link in enumerate(self.links): choice = "" if self.keywords[ilink]: if self.keywords[ilink] not in (CMD_NOMATCH, CMD_NOINPUT): choice += "{g{lc%s{lt%s{le{n" % (self.keywords[ilink], self.keywords[ilink]) else: choice += "{g {lc%i{lt%i{le{n" % ((ilink + 1), (ilink + 1)) if self.linktexts[ilink]: choice += " - %s" % self.linktexts[ilink] choices.append(choice) cols = [[] for i in range(min(len(choices), cols))] while True: for i in range(len(cols)): if not choices: cols[i].append("") else: cols[i].append(choices.pop(0)) if not choices: break ftable = utils.format_table(cols) for row in ftable: string += "\n" + "".join(row) # store text self.text = self.separator + "\n" + string.rstrip()
def func(self): "implements the functionality." caller = self.caller if not self.args: caller.msg("Usage: @code <obj>[/<type> [= <codestring>]]") return codetype = None objname = self.lhs if '/' in self.lhs: objname, codetype = [part.strip() for part in self.lhs.rsplit("/", 1)] obj = self.caller.search(objname) if not obj: return # get the dicts from db storage for easy referencing evlang_scripts = obj.db.evlang_scripts evlang_locks = obj.db.evlang_locks if not (evlang_scripts != None and evlang_locks and obj.ndb.evlang): caller.msg("Object %s can not be scripted." % obj.key) return if 'delete' in self.switches: # clearing a code snippet if not codetype: caller.msg("You must specify a code type.") return if not codetype in evlang_scripts: caller.msg("Code type '%s' not found on %s." % (codetype, obj.key)) return # this will also update the database obj.ndb.evlang.delete(codetype) caller.msg("Code for type '%s' cleared on %s." % (codetype, obj.key)) return if not self.rhs: if codetype: scripts = [(name, tup[1], utils.crop(tup[0])) for name, tup in evlang_scripts.items() if name==codetype] scripts.extend([(name, "--", "--") for name in evlang_locks if name not in evlang_scripts if name==codetype]) else: # no type specified. List all scripts/slots on object print evlang_scripts scripts = [(name, tup[1], utils.crop(tup[0])) for name, tup in evlang_scripts.items()] scripts.extend([(name, "--", "--") for name in evlang_locks if name not in evlang_scripts]) scripts = sorted(scripts, key=lambda p: p[0]) table = [["type"] + [tup[0] for tup in scripts], ["creator"] + [tup[1] for tup in scripts], ["code"] + [tup[2] for tup in scripts]] ftable = utils.format_table(table, extra_space=5) string = "{wEvlang scripts on %s:{n" % obj.key for irow, row in enumerate(ftable): if irow == 0: string += "\n" + "".join("{w%s{n" % col for col in row) else: string += "\n" + "".join(col for col in row) caller.msg(string) return # we have rhs codestring = self.rhs if not codetype in evlang_locks: caller.msg("Code type '%s' cannot be coded on %s." % (codetype, obj.key)) return # check access with the locktype "code" if not obj.ndb.evlang.lockhandler.check(caller, "code"): caller.msg("You are not permitted to add code of type %s." % codetype) return # we have code access to this type. oldcode = None if codetype in evlang_scripts: oldcode = str(evlang_scripts[codetype][0]) # this updates the database right away too obj.ndb.evlang.add(codetype, codestring, scripter=caller) if oldcode: caller.msg("{wReplaced{n\n %s\n{wWith{n\n %s" % (oldcode, codestring)) else: caller.msg("Code added in '%s':\n %s" % (codetype, codestring)) if "debug" in self.switches: # debug mode caller.msg("{wDebug: running script (look out for errors below) ...{n\n" + "-"*68) obj.ndb.evlang.run_by_name(codetype, caller, quiet=False)
def __init__(self, key, text="", links=None, linktexts=None, keywords=None, cols=1, helptext=None, selectcmds=None, callback=None, code="", nodefaultcmds=False, separator=""): """ key - the unique identifier of this node. text - is the text that will be displayed at top when viewing this node. links - a list of keys for unique menunodes this is connected to. The actual keys will not printed - keywords will be used (or a number) linktexts - an optional list of texts to describe the links. Must match link list if defined. Entries can be None to not generate any extra text for a particular link. keywords - an optional list of unique keys for choosing links. Must match links list. If not given, index numbers will be used. Also individual list entries can be None and will be replaed by indices. If CMD_NOMATCH or CMD_NOENTRY, no text will be generated to indicate the option exists. cols - how many columns to use for displaying options. helptext - if defined, this is shown when using the help command instead of the normal help index. selectcmds- a list of custom cmdclasses for handling each option. Must match links list, but some entries may be set to None to use default menu cmds. The given command's key will be used for the menu list entry unless it's CMD_NOMATCH or CMD_NOENTRY, in which case no text will be generated. These commands have access to self.menutree and so can be used to select nodes. code - functional code. Deprecated. This will be executed just before this node is loaded (i.e. as soon after it's been selected from another node). self.caller is available to call from this code block, as well as ev. callback - function callback. This will be called as callback(currentnode) just before this node is loaded (i.e. as soon as possible as it's been selected from another node). currentnode.caller is available. nodefaultcmds - if true, don't offer the default help and look commands in the node separator - this string will be put on the line between menu nodes. """ self.key = key self.cmdset = None self.links = links self.linktexts = linktexts self.keywords = keywords self.cols = cols self.selectcmds = selectcmds self.code = code self.callback = MethodType(callback, self, MenuNode) if callback else None self.nodefaultcmds = nodefaultcmds self.separator = separator Nlinks = len(self.links) if code: ev.logger.log_depmsg( "menusystem.code is deprecated. Use menusystem.callback.") # validate the input if not self.links: self.links = [] if not self.linktexts or (len(self.linktexts) != Nlinks): self.linktexts = [None for i in range(Nlinks)] if not self.keywords or (len(self.keywords) != Nlinks): self.keywords = [None for i in range(Nlinks)] if not selectcmds or (len(self.selectcmds) != Nlinks): self.selectcmds = [None for i in range(Nlinks)] # Format default text for the menu-help command if not helptext: helptext = "Select one of the valid options (" for i in range(Nlinks): if self.keywords[i]: if self.keywords[i] not in (CMD_NOMATCH, CMD_NOINPUT): helptext += "%s, " % self.keywords[i] else: helptext += "%s, " % (i + 1) helptext = helptext.rstrip(", ") + ")" self.helptext = helptext # Format text display string = "" if text: string += "%s\n" % text # format the choices into as many collumns as specified choices = [] for ilink, link in enumerate(self.links): choice = "" if self.keywords[ilink]: if self.keywords[ilink] not in (CMD_NOMATCH, CMD_NOINPUT): choice += "{g{lc%s{lt%s{le{n" % (self.keywords[ilink], self.keywords[ilink]) else: choice += "{g {lc%i{lt%i{le{n" % ((ilink + 1), (ilink + 1)) if self.linktexts[ilink]: choice += " - %s" % self.linktexts[ilink] choices.append(choice) cols = [[] for i in range(min(len(choices), cols))] while True: for i in range(len(cols)): if not choices: cols[i].append("") else: cols[i].append(choices.pop(0)) if not choices: break ftable = utils.format_table(cols) for row in ftable: string += "\n" + "".join(row) # store text self.text = self.separator + "\n" + string.rstrip()