Page MenuHomePhabricator

Add script autocompletion support to pwb.py
Open, LowestPublic

Description

Steps to reproduce

  1. Open Terminal in pywikibot folder
  2. Type python pwb.py h[Tab]

Expected behavior
h should be autocompleted (to harvest_template)

Current behavior
Nothing happens because scripts are located in scripts/ subfolder. But there still is a chance to help bash autocompletion with some library like argcomplete

Event Timeline

Xqt triaged this task as Lowest priority.Sep 10 2017, 11:27 AM
Xqt subscribed.

The problem might be that pwb expands the default scripts folder to other folders too which could contain scripts like "archive", "maintenance" or any private script path. This means all directory entries of all these paths must be collected and ordered first.

Do I have to make any dependency changes or something to use argcomplete library or I can directly submit the patch?

Do I have to make any dependency changes or something to use argcomplete library or I can directly submit the patch?

No, directly submit the patch to gerrit but noto the github clone. Don't use any external libraries except the pyhton standard libaries.

Do I have to make any dependency changes or something to use argcomplete library or I can directly submit the patch?

No, directly submit the patch to gerrit but noto the github clone. Don't use any external libraries except the pyhton standard libaries.

I have used a third party library called argcomplete to accomplish this task, as suggested in the task description.

There should be a way to avoid the library and mimick its code in Pywikibot without installing it as we don't want to add another dependency

Also argcomplete does need argparse, which we do not support

@Dvorapa I looked at Python native modules, especially readline & cmd, but none seems to be useful in our use case.

the basic is very simple: in pwb.py line 200 scripts contains all given scripts. global filename contains the given filename shortcut. You can then create a list of all matching scripts like

similar_scripts = [script for script in scripts if script.startswith(filename)]

Maybe the patches of T217195 should be merged first.

the basic is very simple: in pwb.py line 200 scripts contains all given scripts. global filename contains the given filename shortcut. You can then create a list of all matching scripts like

similar_scripts = [script for script in scripts if script.startswith(filename)]

Maybe the patches of T217195 should be merged first.

Does the filename contains partial text entered after python pwb.py, which we can use to find similar scripts when [Tab] is pressed.

Does the filename contains partial text entered after python pwb.py, which we can use to find similar scripts when [Tab] is pressed.

Sample: type python pwb.py h[Enter]

filename becomes "h". Since harvest_template.py is the only script starting with "h" it can start then.

It does not work with [Tab] because a command line must be delimited with [Enter] (on Windows)

It should work with tab key too in case of Linux (precisely tab + enter)

It should work with tab key too in case of Linux (precisely tab + enter)

As soon as [Enter] is pressed, the pwb.py will start the execution and there won't be any autocompletion of the script name.

We need some way to get the partial text entered after python pwb.py without using argparse and that partial text can be used for finding matching scripts.

We need some way to get the partial text entered after python pwb.py without using argparse and that partial text can be used for finding matching scripts.

Yes. See my sample above.

python pwb.py h[Enter] In this case, as harvest_template.py is the only script starting with "h", we can execute it, but what about the cases in which multiple scripts are present starting with the entered partial text. We can't execute any of them, as we don't know which one user wants. Further, the execution of pwb.py (because of [Enter]) will just display the MISSPELLED message. There is no autocompletion of script name.

Therefore patches of T217195 should be merged first. Implementing this task would much more easier.

Well, the two approaches should ideally work both simultaneously. In my point of view:

pwb.py h[Tab] -> autocomplete the command to pwb.py harvest_template and wait (so a user can write the arguments)
pwb.py a[Tab] -> show the possibilities (add_text & archivebot) and wait (so the user can pick one and write it with the arguments)
pwb.py h[Enter] -> autocomplete the command to pwb.py harvest_template and run it (with an easy way to interrupt)
pwb.py a[Enter] -> show the possibilities (add_text & archivebot) and ask, which one to run ([1-2]? : _)

pwb.py harves_temlate[Tab] -> fix the command to pwb.py harvest_template and wait (so a user can write the arguments)
pwb.py harves_temlate[Enter] -> fix the command to pwb.py harvest_template and run it (with an easy way to interrupt)

Yes this is already prepared with my patches without the tab function because it is not available on windows