Page MenuHomePhabricator

Use path in Python script calling
Closed, InvalidPublic

Description

Sorry for the title, i usually do suck in title thinking.

Currently in "Pywikibot" folder I have some 1000 scripts. Highly unstructured and so... If I need something sometimes it's hard to find needed script.

I was thinking about folder "projects" in Pywikibot folder, where could go project folders.

From cmd I would call script from project "wikidatafix" (which is in folder "projects"):

pwb.py wikidatafix fix_evething_in_wikidata.py

I chose wikidatafix as first argument for pwb.py a) for performance reasons (i may have 1000 projects, in each project 100 python files) b) i may want to have fix_evething_in_wikidata.py file in every project :)

Even if it would be bad idea for general use, maybe somebody could write some lines of python and show how to change pwb.py file (i guess)?

Event Timeline

user_config.py
# ############# EXTERNAL SCRIPT PATH SETTING ##############
# set your own script path to lookup for your script files.
# your private script path must be located inside the
# framework folder, subfolders must be delimited by '.'.
# every folder must contain an (empty) __init__.py file.
#
# The search order is
# 1. user_script_paths in the given order
# 2. scripts
# 3. scripts/maintenance
# 4. scripts/archive
#
# sample:
# user_script_paths = ['scripts.myscripts']
user_script_paths = []

So if you keep your script names unique and register any folder in this config, I think you will find this useful.

You can pass the full path of the file to pwb.py, so python pwb.py projects/wikidatafix/fix_everything_in_wikidata.py (or pwb.py projects\wikidatafix\fix_everything_in_wikidata.py on Windows) should work.

So if you keep your script names unique and register any folder in this config, I think you will find this useful.

Yes, I know about user_script_paths. Nice thing, but not exactly solution to this "problem".
a) as I said, I could have same-titled scripts in each folder, that I want to use in cmd. For example, I have import script folders for each thing (website), and I name them 'import.py'
b) not so very big issue, but performance. As I already said, I may have 1000 projects. Each time I run the script, it could have to go trough all those 1000 folders till it finds rights project.
c) maintaining user_script_paths if I have many projects would be hell :)

You can pass the full path of the file to pwb.py, so python pwb.py projects/wikidatafix/fix_everything_in_wikidata.py (or pwb.py projects\wikidatafix\fix_everything_in_wikidata.py on Windows) should work.

Your solution is quite long :) I may have wikidatafix folder somewhere deeper in path. Yes, I know, what I said in description, but I could have it like that. Writing it everytime (not a simple copy-paste)... Also hell :)

You also can have several pywikibot repositories each for a different project. Then cd to the wanted repo and run any script in it with pwb yourscript. You can shortcut the cd command via batch script.

And btw valhallasw’s proposal is not very different from your request:

Instead of
pwb.py wikidatafix fix_evething_in_wikidata.py
Just write
pwb.py wikidatafix/fix_evething_in_wikidata.py

and organize your folders accordingly. In case the command line is to long, use a shortcut via shell command.

Next solution if you have the same scripts for different project: use global options like -family and -lang for it (or implement your own option to select a project variant).

Or make a script with your path selection logic, that generate the needed command string "python pwb.py projects/wikidatafix/fix_everything_in_wikidata.py" and execute it.

There is -dir option to setup the base directory. or you can add the diectory to the script if invoking it. Please feel free to reopen this task if it does not work with the current Pywikibot release.