jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] move generate_user_files.py to pywikibot/scripts/

Enable generate_user_files with site package

- move generate_user_files.py to pywikibot/scripts/
- Update parse_sections to resolve config.py path
- move _import_with_no_user_config from generate_family_file.py to
to pywikibot/scripts/__init__.py
- import _import_with_no_user_config from pywikibot.scripts
in generate_family_file.py
- update imports
- update tests scripts
- update doc

Bug: T107629
Change-Id: I7fd8aa95c7338e4028ab1ec8f7b42e82c2594e74
---
M .appveyor.yml
M .travis.yml
M CONTENT.rst
M docs/installation.rst
M docs/utilities/index.rst
M generate_family_file.py
M pwb.py
M pywikibot/CONTENT.rst
M pywikibot/scripts/__init__.py
R pywikibot/scripts/generate_user_files.py
M tests/generate_user_files_tests.py
M tox.ini
M user-config.py.sample
13 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/.appveyor.yml b/.appveyor.yml
index 42603e8..7fa65f7 100644
--- a/.appveyor.yml
+++ b/.appveyor.yml
@@ -73,7 +73,7 @@
- set PYTHONIOENCODING=utf8

- "mkdir %PYWIKIBOT_DIR%"
- - "python -Werror::UserWarning -m generate_user_files -dir:%PYWIKIBOT_DIR% -family:wikipedia -lang:en -v -debug -user:%PYWIKIBOT_USERNAME%"
+ - "python -Werror::UserWarning -m pwb generate_user_files -dir:%PYWIKIBOT_DIR% -family:wikipedia -lang:en -v -debug -user:%PYWIKIBOT_USERNAME%"
- ps: "[IO.File]::AppendAllText($env:PYWIKIBOT_USER_CONFIG, 'max_retries = 2; maximum_GET_length = 5000; transliteration_target = None;')"
- ps: "[IO.File]::AppendAllText($env:PYWIKIBOT_USER_CONFIG, 'usernames[''wikipedia''][''test''] = ''{0}'';' -f $env:PYWIKIBOT_USERNAME)"
- ps: "[IO.File]::AppendAllText($env:PYWIKIBOT_USER_CONFIG, 'usernames[''wikidata''][''test''] = ''{0}'';' -f $env:PYWIKIBOT_USERNAME)"
diff --git a/.travis.yml b/.travis.yml
index d16a73f..5283c71 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -75,7 +75,7 @@
python -m generate_family_file 'https://theportalwiki.com/wiki/Main_Page' 'portalwiki' 'y' 'y';
fi

- - python -W error::UserWarning -m generate_user_files -dir:~/.pywikibot/ -family:$FAMILY -lang:$LANGUAGE -v -user:"$PYWIKIBOT_USERNAME"
+ - python -W error::UserWarning -m pwb generate_user_files -dir:~/.pywikibot/ -family:$FAMILY -lang:$LANGUAGE -v -user:"$PYWIKIBOT_USERNAME"

- if [[ -n "$USER_PASSWORD" && -n "$PYWIKIBOT_USERNAME" ]]; then
printf "usernames['wikipedia']['en'] = '%q'\n" "$PYWIKIBOT_USERNAME" >> ~/.pywikibot/user-config.py ;
diff --git a/CONTENT.rst b/CONTENT.rst
index 230992c..7f42248 100644
--- a/CONTENT.rst
+++ b/CONTENT.rst
@@ -18,13 +18,11 @@
+---------------------------+-----------------------------------------------------------+
| generate_family_file.py | Creates a new family file |
+---------------------------+-----------------------------------------------------------+
- | generate_user_files.py | Creates user-config.py or user-fixes.py |
- +---------------------------+-----------------------------------------------------------+
| HISTORY.rst | PyPI version history file |
+---------------------------+-----------------------------------------------------------+
| LICENSE | Reference to the MIT license |
+---------------------------+-----------------------------------------------------------+
- | pwb.py | Wrapper script to use Pywikibot in 'directory' mode |
+ | pwb.py | Code entry wrapper script (directory mode only) |
+---------------------------+-----------------------------------------------------------+
| README.rst | Short info string used by Pywikibot Nightlies |
+---------------------------+-----------------------------------------------------------+
diff --git a/docs/installation.rst b/docs/installation.rst
index 0f1f34e..b943842 100644
--- a/docs/installation.rst
+++ b/docs/installation.rst
@@ -15,7 +15,7 @@

This sample is shipped with the repository but is not available with
the site-package. For more settings use :mod:`generate_user_files` script
-(not available with site-package) or refer :mod:`pywikibot.config`.
+or refer :mod:`pywikibot.config`.

.. note::
Please also see the documentation at :manpage:`Installation`
diff --git a/docs/utilities/index.rst b/docs/utilities/index.rst
index 9965397..5e61800 100644
--- a/docs/utilities/index.rst
+++ b/docs/utilities/index.rst
@@ -19,4 +19,4 @@
generate\_user\_files script
----------------------------

-.. automodule:: generate_user_files
+.. automodule:: pywikibot.scripts.generate_user_files
diff --git a/generate_family_file.py b/generate_family_file.py
index 330a725..f3125f8 100755
--- a/generate_family_file.py
+++ b/generate_family_file.py
@@ -29,7 +29,6 @@
import string
import sys

-from os import environ, getenv
from typing import Optional
from urllib.parse import urlparse

@@ -63,6 +62,7 @@
pass `Y` or `y` to disable certificate validaton `N` or `n`
to keep it enabled.
"""
+ from pywikibot.scripts import _import_with_no_user_config
# from pywikibot.site_detect import MWSite
# when required but disable user-config checks
# so the family can be created first,
@@ -275,19 +275,6 @@
"""


-def _import_with_no_user_config(*import_args):
- """Return __import__(*import_args) without loading user-config.py."""
- orig_no_user_config = getenv('PYWIKIBOT_NO_USER_CONFIG')
- environ['PYWIKIBOT_NO_USER_CONFIG'] = '2'
- result = __import__(*import_args)
- # Reset this flag
- if not orig_no_user_config:
- del environ['PYWIKIBOT_NO_USER_CONFIG']
- else:
- environ['PYWIKIBOT_NO_USER_CONFIG'] = orig_no_user_config
- return result
-
-
def main():
"""Process command line arguments and generate a family file."""
if len(sys.argv) > 1 and sys.argv[1] == '-help':
diff --git a/pwb.py b/pwb.py
index 1f6741d..6c44f9c 100755
--- a/pwb.py
+++ b/pwb.py
@@ -285,7 +285,8 @@
or filename == 'version.py'):
print("NOTE: 'user-config.py' was not found!")
print('Please follow the prompts to create it:')
- run_python_file(os.path.join(_pwb_dir, 'generate_user_files.py'), [])
+ run_python_file(os.path.join(
+ _pwb_dir, 'pywikibot', 'scripts', 'generate_user_files.py'), [])
# because we have loaded pywikibot without user-config.py loaded,
# we need to re-start the entire process. Ask the user to do so.
print('Now, you have to re-execute the command to start your script.')
diff --git a/pywikibot/CONTENT.rst b/pywikibot/CONTENT.rst
index deb5c3e..468259a 100644
--- a/pywikibot/CONTENT.rst
+++ b/pywikibot/CONTENT.rst
@@ -115,7 +115,9 @@
+----------------------------+------------------------------------------------------+
| scripts | Framework helper scripts and entry points |
+============================+======================================================+
- | pwb.py | code entry wrapper script (site-package only) |
+ | generate_user_files.py | Creates user-config.py or user-fixes.py |
+ +----------------------------+------------------------------------------------------+
+ | pwb.py | Code entry wrapper script (site-package only) |
+----------------------------+------------------------------------------------------+
| version.py | Outputs Pywikibot's revision number, Python's |
| | version and OS used. |
diff --git a/pywikibot/scripts/__init__.py b/pywikibot/scripts/__init__.py
index 5a69b88..4836696 100644
--- a/pywikibot/scripts/__init__.py
+++ b/pywikibot/scripts/__init__.py
@@ -4,3 +4,22 @@

.. versionadded:: 7.0
"""
+from os import environ, getenv
+
+
+def _import_with_no_user_config(*import_args: str):
+ """Return __import__(*import_args) without loading user-config.py.
+
+ .. versionadded:: 3.0
+ .. versionchanged:: 7.0
+ moved to pywikibot.scripts
+ """
+ orig_no_user_config = getenv('PYWIKIBOT_NO_USER_CONFIG')
+ environ['PYWIKIBOT_NO_USER_CONFIG'] = '2'
+ result = __import__(*import_args)
+ # Reset this flag
+ if not orig_no_user_config:
+ del environ['PYWIKIBOT_NO_USER_CONFIG']
+ else:
+ environ['PYWIKIBOT_NO_USER_CONFIG'] = orig_no_user_config
+ return result
diff --git a/generate_user_files.py b/pywikibot/scripts/generate_user_files.py
similarity index 95%
rename from generate_user_files.py
rename to pywikibot/scripts/generate_user_files.py
index 745de1e..1a364c2 100755
--- a/generate_user_files.py
+++ b/pywikibot/scripts/generate_user_files.py
@@ -1,5 +1,9 @@
#!/usr/bin/python
-"""Script to create user-config.py."""
+"""Script to create user-config.py.
+
+.. versionchanged:: 7.0
+ moved to pywikibot.scripts folder
+"""
#
# (C) Pywikibot team, 2010-2021
#
@@ -9,14 +13,16 @@
import os
import re
import sys
+
from collections import namedtuple
+from pathlib import Path
from textwrap import fill
from typing import Optional

-from generate_family_file import _import_with_no_user_config
+from pywikibot.scripts import _import_with_no_user_config

-
-if sys.version_info[:2] >= (3, 9):
+PYTHON_VERSION = sys.version_info[:2]
+if PYTHON_VERSION >= (3, 9):
Tuple = tuple
else:
from typing import Tuple
@@ -42,9 +48,6 @@
except AttributeError:
console_encoding = None

-# the directory in which generate_user_files.py is located
-pywikibot_dir = sys.path[0]
-
if console_encoding is None or sys.platform == 'cygwin':
console_encoding = 'iso-8859-1'

@@ -217,21 +220,22 @@
{botpasswords}"""


-def parse_sections():
+def parse_sections() -> list:
"""Parse sections from config.py file.

- config.py will be in the pywikibot/ directory relative to this
- generate_user_files script.
+ config.py will be in the pywikibot/ directory whereas
+ generate_user_files script is in pywikibot/scripts.

:return: a list of ConfigSection named tuples.
- :rtype: list
"""
data = []
ConfigSection = namedtuple('ConfigSection', 'head, info, section')

- install = os.path.dirname(os.path.abspath(__file__))
- with codecs.open(os.path.join(install, 'pywikibot', 'config.py'),
- 'r', 'utf-8') as config_f:
+ config_path = Path(__file__).resolve().parents[1].joinpath('config.py')
+ if PYTHON_VERSION < (3, 6):
+ config_path = str(config_path)
+
+ with codecs.open(config_path, 'r', 'utf-8') as config_f:
config_file = config_f.read()

result = re.findall(
diff --git a/tests/generate_user_files_tests.py b/tests/generate_user_files_tests.py
index b24af84..2824de5 100644
--- a/tests/generate_user_files_tests.py
+++ b/tests/generate_user_files_tests.py
@@ -7,7 +7,8 @@
import re
import unittest

-import generate_user_files as guf
+from pywikibot.scripts import generate_user_files as guf
+
from tests.aspects import TestCase


diff --git a/tox.ini b/tox.ini
index 47069fb..f109f51 100644
--- a/tox.ini
+++ b/tox.ini
@@ -10,7 +10,7 @@

[params]
doctest_skip = --ignore-files=(eventstreams|gui|mysql)\.py
-generate_user_files = -W error::UserWarning -m generate_user_files -family:wikipedia -lang:test -v
+generate_user_files = -W error::UserWarning -m pwb generate_user_files -family:wikipedia -lang:test -v

[testenv]
basepython =
diff --git a/user-config.py.sample b/user-config.py.sample
index 2889796..cf80b2c 100644
--- a/user-config.py.sample
+++ b/user-config.py.sample
@@ -1,5 +1,6 @@
-# This is a sample file. You should use generate_user_files.py
-# to create your user-config.py file.
+# This is a sample file. You should use generate_user_files script
+# to create your user-config.py file:
+# pwb generate_user_files

mylang = 'en'
family = 'wikipedia'

To view, visit change 740534. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I7fd8aa95c7338e4028ab1ec8f7b42e82c2594e74
Gerrit-Change-Number: 740534
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: AbdealiJK <abdealikothari@gmail.com>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged