Draft: Share option definition between cli and magic.
There is some request in #324 to have the same options (when possible) between the magic and the CLI.
I'm looking into defining those options only once, and refactoring main/magic to not repeat the options handling.
I'm tryin to be the least invasive as possible.
I'm not yet looking for deep review, but mostly letting you know I'm working on this, and maybe cursory validation that the approach is ok (or not), or wether you prefer a more thourough refactor, or simply me redefining the options twice in the __main__ and the magic.
I also want to note that pyinsturment is currently using Otparse which is marked as deprecated since 3.2 in favor of argparse. I guess there might be a reason not to use Argparse.
I'm thinking we might want to have and explicit opt-in list instead of opt-out list, and that many of those options like load and co, only make sens with a line magic, not a cell magic.
This makes me realize looking at options that the IPython magic in consoel should likely default to --color --unicode
Hmm, I think I'd suggest a different approach... I think most of the options probably shouldnt be shared.
β Where does the β Behaviour should match
β implementation live? β between cmdline and ipython?
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββΌββββββββββββββββββββββββββ
β β
--version show program's version number and exit β N/A β X
-h, --help show this help message and exit β N/A β X Different impl
--load=FILENAME instead of running a script, load a profile session β N/A β X
from a pyisession file β β
--load-prev=IDENTIFIER β N/A β X
instead of running a script, load a previous profile β β
session as specified by an identifier β β
-m MODULE run library module as a script, like 'python -m β N/A β X
module' β β
-c PROGRAM program passed in as string, like 'python -c "..."' β N/A β X
--from-path (POSIX only) instead of the working directory, look β N/A β X
for scriptfile in the PATH environment variable β β
-o OUTFILE, --outfile=OUTFILE β main() β ? I think this confuses what
save to <outfile> β β the ipython magic does
-r RENDERER, --renderer=RENDERER β get_renderer_class() β X The ipython-native formats
how the report should be rendered. One of: 'text', β β are text and html, which
'html', 'json', 'speedscope', 'pstats', or python β β are supplied as mime-type
import path to a renderer class. Defaults to the β β alternatives, so I don't
appropriate format for the extension if OUTFILE is β β think this can be changed.
given, otherwise, defaults to 'text'. β β
-p RENDER_OPTION, --render-option=RENDER_OPTION β compute_render_options() β β
options to pass to the renderer, in the format β β
'flag_name' or 'option_name=option_value'. For β β
example, to set the option 'time', pass '-p β β
time=percent_of_total'. To pass multiple options, use β β
the -p option multiple times. You can set processor β β
options using dot-syntax, like '-p β β
processor_options.filter_threshold=0'. option_value is β β
parsed as a JSON value or a string. β β
-t, --timeline render as a timeline - preserve ordering and don't β compute_render_options() β β
condense repeated calls β β
--hide=EXPR glob-style pattern matching the file paths whose β compute_render_options() β β
frames to hide. Defaults to hiding non-application β β
code β β
--hide-regex=REGEX regex matching the file paths whose frames to hide. β compute_render_options() β β
Useful if --hide doesn't give enough control. β β
--show=EXPR glob-style pattern matching the file paths whose β compute_render_options() β β
frames to show, regardless of --hide or --hide-regex. β β
For example, use --show '*/<library>/*' to show frames β β
within a library that would otherwise be hidden. β β
--show-regex=REGEX regex matching the file paths whose frames to always β compute_render_options() β β
show. Useful if --show doesn't give enough control. β β
--show-all show everything β compute_render_options() β β
--unicode (text renderer only) force unicode text output β compute_render_options() β X ipython should
--no-unicode (text renderer only) force ascii text output β compute_render_options() β X set these to be
--color (text renderer only) force ansi color text output β compute_render_options() β X enabled
--no-color (text renderer only) force no color text output β compute_render_options() β X
-i INTERVAL, --interval=INTERVAL β main() β β
Minimum time, in seconds, between each stack sample. β β
Smaller values allow resolving shorter duration β β
function calls but conversely incur a greater runtime β β
and memory consumption overhead. For longer running β β
scripts, setting a larger interval can help control β β
the rate at which the memory required to store the β β
stack samples increases. β β
--use-timing-thread Use a separate thread to time the interval between β main() β β
stack samples. This can reduce the overhead of β β
sampling on some systems. β β
Other options needed
- async_mode
We could get a lot of milage from sharing the logic of compute_render_options. I suppose it would be nice to share some other properties of the options, but yes, unfortunately pyinstrument is still on optparse - I think the reason was that it was difficult to leave some options unparsed with argparse when I first wrote it over 10 years ago. Since pyinstrument is on optparse and ipython is subclassing argparse, I think it'd be unwise to try to make the options definitions polymorphic.
Ok, i'l see if I can find a better way to do that, and pull some shared logic into compute_render_options, maybe there is a way to have shared options defined as data somewhere.
Thanks for the feedback.
For what it is worth I was asked to look at #324 (for cross reference). I don't have much context, but I'm guessing that --load and -o might be used to someone to save/load profiles to see and avoid reaching for the CLI, so I can see how they could be useful in the magic.
For -r RENDERER, --renderer=RENDERER I indeed see minimal use, but maybe someone prefers that ascii version they want to copy/past in the notebook ? And for --(no-)color/--(no-)unicode, I agree thay should be on by default, but still maybe ability to disable them.
closing after #350