+ The ARGMAP is a dictionary mapping placeholder letters to lists of
+ arguments. These are substituted cartesian-product style into the
+ command words.
+ """
+
+ ## No command map, so assume someone's already done the hard word.
+ if argmap is None: return cmd
+
+ ## Start on building a list of arguments.
+ ww = []
+
+ ## Work through each template argument in turn...
+ for w in cmd:
+
+ ## Firstly, build a list of lists. We'll then take the cartesian
+ ## product of these, and concatenate each of the results.
+ pc = []
+ last = 0
+ for m in me.R_PAT.finditer(w):
+ start, end = m.start(0), m.end(0)
+ if start > last: pc.append([w[last:start]])
+ ch = m.group(1)
+ if ch == '%':
+ pc.append(['%'])
+ else:
+ try: pc.append(argmap[m.group(1)])
+ except KeyError: raise U.ExpectedError, (
+ 500, "Unknown placeholder `%%%s' in command `%s'" % (ch, cmd))
+ last = end
+ if last < len(w): pc.append([w[last:]])
+
+ ## If any of the components is empty then there's nothing to do for
+ ## this word.
+ if not all(pc): continue
+
+ ## Now do all the substitutions.
+ ii = len(pc)*[0]
+ while True:
+ ww.append(''.join(map(lambda v, i: v[i], pc, ii)))
+ i = len(ii) - 1
+ while i >= 0:
+ ii[i] += 1
+ if ii[i] < len(pc[i]): break
+ ii[i] = 0
+ i -= 1
+ else:
+ break
+
+ ## And finally we're done.
+ return ww
+
+ def _dispatch(me, func, op, args, input = None):
+ """
+ Work out how to invoke a particular command.
+
+ Invoke FUNC, which works like `_run', with appropriate arguments. The OP
+ is a remote command name; ARGS is a sequence of (C, ARG) pairs, where C
+ is a placeholder character and ARG is a list of string values; INPUT is
+ the text to provide to the command on standard input.
+ """
+ try:
+ cmd = me._opmap[op]
+ except KeyError:
+ cmd = me._default + [op] + reduce(lambda x, y: x + y,
+ [v for k, v in args], [])
+ map = None
+ else:
+ map = dict(args)
+ return func(cmd, input = input, state = map)