quick
unread,Mar 26, 2008, 8:49:55 PM3/26/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Hotwire Shell
I'm trying to write a builtin_user python function using this
methodology, but I ran into a little trouble. Here's some helpful
information (for folks other than Colin who already knows this), with
a couple of questions at the end.
This function will operate on a list of files. I would like to have
it either operate on file objects provided via a hotwire pipe or else
on filenames explicitly provided to the function. In addition, I'd
like to be able to pass an option to the function.
Here's what I have:
@builtin_user(input=InputStreamSchema(File, optional=True),
output=str,
undoable=True,
idempotent=True,
argspec=MultiArgSpec('path'),
options=[['-a', '--argument']])
def mybuiltin(context, files, options=[]):
if len(files) == 0 and not context.input:
raise ValueError(_("Must specify at least one file"))
fs = Filesystem.getInstance()
fpath = lambda arg: FilePath(arg, context.cwd)
flist = map(fpath, files)
if context.input:
flist.extend(imap(lambda f: f.path, context.input))
have_arg = '-a' in options
for afile in flist:
pass
I modelled this after builtins/rm.py, which seems to do roughly the
same thing although it still uses the old-style declarations.
Trying to use it ran into problems with argument parsing. It turns
out that it should be:
@builtin_user(input=InputStreamSchema(File, optional=True),
output=str,
undoable=True,
idempotent=True,
argspec=MultiArgSpec('path'),
options=[['-a', '--argument']])
def mybuiltin(context, *files):
if len(files) == 0 and not context.input:
raise ValueError(_("Must specify at least one file"))
fs = Filesystem.getInstance()
fpath = lambda arg: FilePath(arg, context.cwd)
flist = map(fpath, files)
if context.input:
flist.extend(imap(lambda f: f.path, context.input))
have_arg = '-a' in options
for afile in flist:
pass
Specifically, options are passed as a member of context and the only
parameter should be *files which (for MultiArgSpec) is a tuple of 0 or
more arguments.
The processing of pipe elements and/or explicit arguments seems like
it would be a common idiom for builtins. Could some more decorator
magic or
context manipulation (or even just a helper function) be done to
simplify this more?
Also, does the decorator builtin work with classes as well as
functions? Explicitly, if I wanted to make my builtin a subclass of
the FileOpBuiltin, how would I go about it using the decorator syntax?