Framework utility scripts reference#
Folder which holds framework scripts.
Added in version 7.0.
Removed in version 9.4: preload_sites script, previously added in release 6.0
(T226157), was removed (T348925).
- pywikibot.scripts._import_with_no_user_config(*import_args)[source]#
Return
__import__(*import_args)without loading user config.Added in version 3.0.
Changed in version 7.0: moved to pywikibot.scripts
pwb wrapper script#
Wrapper script to invoke pywikibot-based scripts
This wrapper script invokes script by its name in this search order:
Scripts listed in
user_script_pathslist inside your user config settings file (usuallyuser-config.py) in the given order. Refer External Script Path Settings.User scripts residing in
scripts/userscripts(directory mode only).Scripts residing in
scriptsfolder (directory mode only).Maintenance scripts residing in
scripts/maintenance(directory mode only).Site-package scripts (site-package only)
Framework scripts residing in
pywikibot/scripts.
This wrapper script is able to invoke scripts even if the script name is misspelled. In directory mode it also checks package dependencies.
Run scripts with pywikibot in directory mode using:
python pwb.py <pwb options> <name_of_script> <options>
or run scripts with pywikibot installed as a site package using:
pwb <pwb options> <name_of_script> <options>
This wrapper script uses the package directory to store all user files, will fix up search paths so the package does not need to be installed, etc.
Currently, <pwb options> are Global options. This can be used
for tests to set the default site (see T216825):
python pwb.py -lang:de bot_tests -v
See also
pwb entry point
Changed in version 7.0: pwb wrapper was added to the Python site package lib.
Changed in version 7.7: pwb wrapper is able to set PYWIKIBOT_TEST_... environment variables,
see Environment variables.
Changed in version 8.0: renamed to wrapper.py.
Changed in version 9.4: enable external scripts via entry points.
- pywikibot.scripts.wrapper.check_modules(script=None)[source]#
Check whether mandatory modules are present.
This also checks Python version when importing dependencies from setup.py
- Parameters:
script (str | None) – The script name to be checked for dependencies
- Returns:
True if all dependencies are installed
- Raises:
RuntimeError – wrong Python version found in setup.py
- Return type:
bool
- pywikibot.scripts.wrapper.check_pwb_versions(package)[source]#
Validate package version and scripts version.
- Rules:
Pywikibot version must not be older than scrips version
Scripts version must not be older than previous Pywikibot version due to deprecation policy
- Parameters:
package (str)
- Return type:
None
- pywikibot.scripts.wrapper.execute()[source]#
Parse arguments, extract filename and run the script.
Added in version 7.0: renamed from
main()- Return type:
bool
- pywikibot.scripts.wrapper.find_alternates(filename, script_paths)[source]#
Search for similar filenames in the given script paths.
- pywikibot.scripts.wrapper.find_filename(filename)[source]#
Search for the filename in the given script paths.
Changed in version 7.0: Search users_scripts_paths in config.base_dir
Changed in version 9.0: Add config.base_dir to search path
Changed in version 9.4: Search in entry point paths
- pywikibot.scripts.wrapper.handle_args(_, *args)[source]#
Handle args and get filename.
Changed in version 7.7: Catch
PYWIKIBOT_TEST_...environment variables.- Returns:
filename, script args, local pwb args, environment variables
- Parameters:
args (str)
- Return type:
tuple[str, list[str], list[str], list[str]]
- pywikibot.scripts.wrapper.main()[source]#
Script entry point. Print doc if necessary.
Changed in version 7.0: previous implementation was renamed to
execute()- Return type:
None
- pywikibot.scripts.wrapper.run()[source]#
Site package entry point. Print doc if necessary.
Added in version 7.0.
- Return type:
None
- pywikibot.scripts.wrapper.run_python_file(filename, args, package=None)[source]#
Run a python file as if it were the main program on the command line.
Changed in version 7.7: Set and restore
PYWIKIBOT_TEST_...environment variables.- Parameters:
filename (str) – The path to the file to execute, it need not be a .py file.
args (list[str]) – is the argument list to present as sys.argv, as strings.
package (Optional[module]) – The package of the script. Used for checks.
- Return type:
None
generate_family_file script#
This script generates a family file from a given URL
This script must be invoked with the pwb wrapper script/code entry point.
Usage:
pwb generate_family_file.py [<url>] [<name>] [<dointerwiki>] [<verify>]
Parameters are optional. They must be given consecutively but may be omitted if there is no successor parameter. The parameters are:
<url>: an url from where the family settings are loaded
<name>: the family name without "_family.py" tail.
<dointerwiki>: predefined answer (y|s|n) to add multiple site codes
<verify>: disable certificate validaton `(y|n)
Example:
pwb generate_family_file.py https://www.mywiki.bogus/wiki/Main_Page mywiki
This will create the file mywiki_family.py in families folder of your base directory.
Changed in version 7.0: moved to pywikibot.scripts folder; create family files in families folder of your base directory instead of pywikibot/families.
Changed in version 8.1: [s]trict can be given for <dointerwiki> parameter to ensure that sites are from the given domain.
Changed in version 8.4: If the url scheme is missing, https will be used.
- class pywikibot.scripts.generate_family_file.FamilyFileGenerator(url=None, name=None, dointerwiki=None, verify=None)[source]#
Bases:
objectFamily file creator object.
Parameters are optional. If missing the script asks for the values.
- Parameters:
url (str | None) – an url from where the family settings are loaded
name (str | None) – the family name without “_family.py” tail.
dointerwiki (str | None) – Predefined answer to add multiple site codes. Pass
Yoryfor yes,Sorsfor strict which only includes site of the same domain (usually for Wikimedia sites),Nornfor no andEoreif you want to edit the collection of sites.verify (str | None) – If a certificate verification fails, you may pass
Yoryto disable certificate validatonNornto keep it enabled.
- getlangs(w)[source]#
Determine site code of a family.
Changed in version 8.1: with [e]dit the interwiki list can be given delimited by space or comma or both. With [s]trict only sites with the same domain are collected. A [h]elp answer was added to show more information about possible answers.
- Return type:
None
generate_user_files script#
Script to create user-config.py. Other file names are not supported
Changed in version 7.0: moved to pywikibot.scripts folder.
Changed in version 8.0: let user the choice which section to be copied. Also EXTERNAL EDITOR SETTINGS section can be copied.
Changed in version 10.7.1: Default PASS_BASENAME was changed from user-password.py to
user-password.cfg
- pywikibot.scripts.generate_user_files.ask_for_dir_change(force)[source]#
Ask whether the base directory is has to be changed.
Only give option for directory change if user-config.py or user- password already exists in the directory. This will repeat if user- config.py also exists in the requested directory.
- Parameters:
force (bool) – Skip asking for directory change
- Returns:
whether user file or password file exists already
- Return type:
tuple[bool, bool]
- pywikibot.scripts.generate_user_files.copy_sections(force=False, default='n')[source]#
Take config sections and copy them to user-config.py.
Changed in version 8.0: force and default options were added.
- Parameters:
force (bool) – Copy all sections if force is True
default (str) – Default answer for input_sections. Should be ‘a’ for all or ‘n’ for none to copy.
- Returns:
config text of all selected sections.
- Return type:
str | None
- pywikibot.scripts.generate_user_files.create_user_config(main_family, main_code, main_username, force=False)[source]#
Create a user-config.py in base_dir.
Create a user-password.py if necessary.
- Parameters:
main_family (str)
main_code (str)
main_username (str)
force (bool)
- Return type:
None
- pywikibot.scripts.generate_user_files.file_exists(filename)[source]#
Return whether the file exists and print a message if it exists.
- Return type:
bool
- pywikibot.scripts.generate_user_files.get_site_and_lang(default_family='wikipedia', default_lang='en', default_username=None, force=False)[source]#
Ask the user for the family, site code and username.
- Parameters:
default_family (str | None) – The default family which should be chosen.
default_lang (str | None) – The default site code which should be chosen, if the family supports it.
default_username (str | None) – The default username which should be chosen.
force (bool)
- Returns:
The family, site code and username
- Return type:
tuple[str, str, str]
- pywikibot.scripts.generate_user_files.input_sections(variant, sections, skip=None, force=False, default='n')[source]#
Ask for settings to copy.
Added in version 8.0.
- Parameters:
variant (str) – Variant of the setting section. Either ‘framework’ or ‘scripts’
sections (list[_ConfigSection]) – A sections list previously read from the config file
skip (Callable | None) – a filter function
force (bool) – Force input if True
default (str)
- Return type:
list[str]
- pywikibot.scripts.generate_user_files.main(*args)[source]#
Process command line arguments and generate user-config.
If args is an empty list, sys.argv is used.
- Parameters:
args (str) – command line arguments
- Return type:
None
login script#
Script to log the bot in to a wiki account
Suggestion is to make a special account to use for bot use only. Make sure this bot account is well known on your home wiki before using.
The following parameters are supported:
- -all
Try to log in on all sites where a username is defined in user config file (user-config.py).
- -cookies
Only login from cookies file, don’t ask for password
- -logout
Log out of the current site. Combine with
-allto log out of all sites, or with Global options-family,-langor-siteto log out of a specific site.- -oauth
Generate OAuth authentication information.
Note
Need to copy OAuth tokens to your user config file manually. -logout is not compatible with -oauth.
- -autocreate
Auto-create an account using unified login when necessary.
Note
the global account must exist already before using this.
- -async
Run the bot in parallel tasks, only useful together with
-alloption
Hint
Use Global options -code, -family or -site
to determine the site to login/logout.
If not given as parameter, the script will ask for your username and password (password entry will be hidden), log in to your home wiki using this combination, and store the resulting cookies (containing your password hash, so keep it secured!) in a file in the data subdirectory.
All scripts in this library will be looking for this cookie file and will use the login information if it is present.
To log out, throw away the *.lwp file that is created in the data
subdirectory.
Changed in version 7.4: moved to pywikibot.scripts folder.
Changed in version 7.7: -async option was added.
Changed in version 10.2: wildcard site codes in usernames dict are supported.
Changed in version 10.3: the -cookies option
shell script#
Spawns an interactive Python shell and imports the pywikibot library
To exit the shell, type ctrl-D (Linux) or ctrl-Z (Windows)
or use the exit() function.
The following local option is supported:
- -noimport
Do not import the pywikibot library. All other arguments are ignored in this case.
Usage:
python pwb.py shell [args]
Changed in version 7.0: moved to pywikibot.scripts
version script#
Script to determine the Pywikibot version (tag, revision and date)
The following option is supported:
- -nouser
do not print usernames; otherwise they are printed for each registered family
Changed in version 7.0: version script was moved to the framework scripts folder.
Added in version 9.1.2: the -nouser option was added.
Changed in version 10.6: The User-Agent string is now printed for the default site. To print
it for another site, call the pwb wrapper with the global option,
e.g.:
pwb -site:wikipedia:test version
Note
The shown UA reflects the default config settings. It might differ
if a user-agent is passed via the headers parameter to
comms.http.request(), comms.http.fetch() or to
comms.eventstreams.EventStreams. It can also differ if
comms.http.fetch() is used with use_fake_user_agent set to
True or to a custom UA string, or if
fake_user_agent_exceptions is defined in the config file.