Utility scripts

Folder which holds framework scripts.

When uploading pywikibot to pypi the pwb.py (wrapper script) and pywikibot i18n package are copied here.

New in version 7.0.

pywikibot.scripts._import_with_no_user_config(*import_args)[source]

Return __import__(*import_args) without loading user-config.py.

New 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:

  1. Scripts listed in user_script_paths list inside your user-config.py settings file in the given order. Refer External Script Path Settings.

  2. User scripts residing in scripts/userscripts (directory mode only).

  3. Scripts residing in scripts folder (directory mode only).

  4. Maintenance scripts residing in scripts/maintenance (directory mode only).

  5. 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

Changed in version 7.0: pwb wrapper was added to the Python site package lib

pwb.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 or None) – The script name to be checked for dependencies

Returns

True if all dependencies are installed

Return type

bool

Raises

RuntimeError – wrong Python version found in setup.py

pwb.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

pwb.execute()[source]

Parse arguments, extract filename and run the script.

New in version 7.0: renamed from main()

pwb.find_alternates(filename, script_paths)[source]

Search for similar filenames in the given script paths.

pwb.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

pwb.handle_args(pwb_py, *args)[source]

Handle args and get filename.

Returns

filename, script args, local args for pwb.py

Return type

tuple

pwb.main()[source]

Script entry point. Print doc if necessary.

Changed in version 7.0: previous implementation was renamed to execute()

pwb.run()[source]

Site package entry point. Print doc if necessary.

New in version 7.0.

pwb.run_python_file(filename, args, package=None)[source]

Run a python file as if it were the main program on the command line.

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.

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|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.

class pywikibot.scripts.generate_family_file.FamilyFileGenerator(url=None, name=None, dointerwiki=None, verify=None)[source]

Bases: object

Family file creator object.

Parameters are optional. If not given the script asks for the values.

Parameters
  • url (Optional[str]) – an url from where the family settings are loaded

  • name (Optional[str]) – the family name without “_family.py” tail.

  • dointerwiki (Optional[str]) – Predefined answer to add multiple site codes. Pass Y or y for yes N or n for no and E or e if you want to edit the collection of sites.

  • verify (Optional[str]) – If a certificate verification failes, you may pass Y or y to disable certificate validaton N or n to keep it enabled.

Return type

None

get_params()[source]

Ask for parameters if necessary.

Return type

bool

get_wiki()[source]

Get wiki from base_url.

getapis()[source]

Load other site pages.

Return type

None

getlangs(w)[source]

Determine site code of a family.

Return type

None

run()[source]

Main method, generate family file.

Return type

None

writefile(verify)[source]

Write the family file.

Return type

None

pywikibot.scripts.generate_family_file.main()[source]

Process command line arguments and generate a family file.

Return type

None

generate_user_files script

Script to create user-config.py.

Changed in version 7.0: moved to pywikibot.scripts folder

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.change_base_dir()[source]

Create a new user directory.

pywikibot.scripts.generate_user_files.copy_sections()[source]

Take config sections and copy them to user-config.py.

Returns

config text of all selected sections.

Return type

str

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

force (bool) –

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 (Optional[str]) – The default family which should be chosen.

  • default_lang (Optional[str]) – The default site code which should be chosen, if the family supports it.

  • default_username (Optional[str]) – 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.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

pywikibot.scripts.generate_user_files.parse_sections()[source]

Parse sections from config.py file.

config.py will be in the pywikibot/ directory whereas generate_user_files script is in pywikibot/scripts.

Returns

a list of ConfigSection named tuples.

Return type

list

pywikibot.scripts.generate_user_files.save_botpasswords(botpasswords, _fncpass)[source]

Write botpasswords to file.

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:

-family:FF   Log in to the LL language of the FF family.
-lang:LL     Example: -family:wiktionary -lang:fr will log you in at
             fr.wiktionary.org.

-site:FF:LL  Log in to the LL language of the FF family

-all         Try to log in on all sites where a username is defined in
             user-config.py.

-logout      Log out of the current site. Combine with -all to log out of
             all sites, or with -family and -lang to log out of a specific
             site.

-oauth       Generate OAuth authentication information.
             NOTE: Need to copy OAuth tokens to your user-config.py
             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.

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

pywikibot.scripts.login.main(*args)[source]

Process command line arguments and invoke bot.

If args is an empty list, sys.argv is used.

Parameters

args (str) – command line arguments

Return type

None

preload_sites script

Script that preloads site and user info for all sites of given family.

The following parameters are supported:

-worker:<num> The number of parallel tasks to be run. Default is the

number of processors on the machine

Usage:

python pwb.py preload_sites [{<family>}] [-worker:{<num>}]

To force preloading, change the global expiry value to 0:

python pwb.py -API_config_expiry:0 preload_sites [{<family>}]

Changed in version 7.4: script was moved to the framework scripts folder.

pywikibot.scripts.preload_sites.families_list = ['wikibooks', 'wikinews', 'wikipedia', 'wikiquote', 'wikisource', 'wikiversity', 'wikivoyage', 'wiktionary']

supported families by this script

pywikibot.scripts.preload_sites.preload_families(families, worker)[source]

Preload all sites of all given family files.

Changed in version 7.3: Default of worker is calculated like for Python 3.8 but preserves at least one worker for each element in families_list for better performance.

Parameters
  • families (Union[List[str], Set[str]]) –

  • worker (Optional[int]) –

Return type

None

pywikibot.scripts.preload_sites.preload_family(family, executor)[source]

Preload all sites of a single family file.

Parameters
  • family (str) –

  • executor (concurrent.futures.thread.ThreadPoolExecutor) –

Return type

None

shell script

Spawns an interactive Python shell and imports the pywikibot library.

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

pywikibot.scripts.shell.main(*args)[source]

Script entry point.

Parameters

args (str) –

Return type

None

version script

Script to determine the Pywikibot version (tag, revision and date).

Changed in version 7.0: version script was moved to the framework scripts folder

class pywikibot.scripts.version.DummyModule[source]

Bases: object

Fake module instance.

pywikibot.scripts.version.main(*args)[source]

Print pywikibot version and important settings.

Parameters

args (str) –

Return type

None