Opened 7 years ago

Last modified 6 years ago

#1380 assigned enhancement

Command to print Python equivalent of command-line input

Reported by: Tristan Croll Owned by: Tom Goddard
Priority: moderate Milestone:
Component: Command Line Version:
Keywords: Cc: Greg Couch
Blocked By: Blocking:
Notify when closed: Platform: all
Project: ChimeraX

Description

It can be quite a challenge at times to track down the Python functions/arguments needed to get the equivalent function to text entered at the command line - particularly for things like save and open where the actual functions are registered from bundles. Would be wonderful for third-party developers to have a command something like:

pySyntax save #1 relModel #1

that prints the equivalent Python snippet to the log.

Change History (4)

comment:1 by Tom Goddard, 7 years ago

Cc: Greg Couch added

That would be cool. And I think it is doable. It probably needs to show the code for parsing some options like atom specifiers too, so "show @CA" would return something like

from chimerax.core.commands import parse_atom_spec
atoms = parse_atom_spec('@CA')
from chimerax.std_commands import show
show(session, atoms)

comment:2 by Greg Couch, 7 years ago

I'm not so sure how doable this would be. Finding the function for a command is:

func = chimerax.core.commands.command_function("save")
print('%s.%s' % (func.__module__, func.__qualname__))

But that function location might not match the supported location given in the documentation. That is, the function location above is just a clue as to where the supported function is. And, there is no public API to get the registered argument types for the function (the CmdDesc instance). help(func) gives some information, but it may be incomplete (especially with the open and save commands where bundles can register additional keyword arguments for their file types).

Thinking about how to implement the parse_atom_spec function:

def parse_atom_spec(session, spec):
    from chimerax.core.commands import AtomSpecArg
    value, _, rest = AtomSpecArg.parse(spec, session)
    if rest:
        raise RuntimeError("extra characters at end of atom specification")
    return value

That could be generalized to function that wrapped annotations into simple parse function:

parse_atom_spec = as_parser(AtomSpecArg)
Last edited 7 years ago by Greg Couch (previous) (diff)

comment:3 by Greg Couch, 7 years ago

Added as_parser() as an experimental API.

comment:4 by Greg Couch, 6 years ago

Renamed as_parser to make_converter. And make it a "Supported API". An example of using it to get a color is:

from chimerax.commands import make_converter, ColorArg
color_convert = make_converter(ColorArg)
color = color_convert(session, "red")

where "red" could be replaced with any color specification. An AnnotationError (whose base classes are UserError and ValueError) is raised if the complete text can not be converted to the appropriate type.

Note: See TracTickets for help on using tickets.