jenkins-bot merged this change.

View Change

Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
[cleanup] move code parts from create_user_config to copy_sections

Partly detached from I68a04e9198dc179f06c6245f1c1aec518603bd34

- There is no warning anymore if no section or only few lines where found.
This part has been moved to test suite.

Change-Id: I86f06d4ec7e3a93cc217145ed659be2e8dc64d3b
---
M generate_user_files.py
M tests/generate_user_files_tests.py
2 files changed, 73 insertions(+), 43 deletions(-)

diff --git a/generate_user_files.py b/generate_user_files.py
index 57dda8d..bef3b80 100755
--- a/generate_user_files.py
+++ b/generate_user_files.py
@@ -15,7 +15,6 @@
import sys

from textwrap import fill
-from warnings import warn

from generate_family_file import _import_with_no_user_config

@@ -231,6 +230,42 @@
{botpasswords}"""


+def copy_sections():
+ """Take config sections and copying them to user-config.py.
+
+ config2.py will be in the pywikibot/ directory relative to this
+ generate_user_files script.
+
+ @return: config text of all sections.
+ @rtype: str
+ """
+ install = os.path.dirname(os.path.abspath(__file__))
+ with codecs.open(os.path.join(install, 'pywikibot', 'config2.py'),
+ 'r', 'utf-8') as config_f:
+ config_file = config_f.read()
+
+ result = re.findall(
+ '^(# ############# (?:'
+ 'LOGFILE|'
+ 'EXTERNAL SCRIPT PATH|'
+ 'INTERWIKI|'
+ 'SOLVE_DISAMBIGUATION|'
+ 'IMAGE RELATED|'
+ 'TABLE CONVERSION BOT|'
+ 'WEBLINK CHECKER|'
+ 'DATABASE|'
+ 'SEARCH ENGINE|'
+ 'COPYRIGHT|'
+ 'FURTHER'
+ ') SETTINGS .*)^(?=#####|# =====)',
+ config_file, re.MULTILINE | re.DOTALL)
+
+ if not result: # Something is wrong with the regex
+ return None
+
+ return '\n'.join(result)
+
+
def create_user_config(main_family, main_code, main_username, force=False):
"""
Create a user-config.py in base_dir.
@@ -292,49 +327,12 @@
"('{0}', BotPassword('{1}', '{2}'))".format(*botpassword)
for botpassword in botpasswords)

- config_text = ''
- config_content = SMALL_CONFIG
-
- try:
- # config2.py will be in the pywikibot/ directory relative to this
- # script (generate_user_files)
- install = os.path.dirname(os.path.abspath(__file__))
- with codecs.open(os.path.join(install, "pywikibot", "config2.py"),
- "r", "utf-8") as config_f:
- config_file = config_f.read()
-
- res = re.findall("^(# ############# (?:"
- "LOGFILE|"
- 'EXTERNAL SCRIPT PATH|'
- "INTERWIKI|"
- "SOLVE_DISAMBIGUATION|"
- "IMAGE RELATED|"
- "TABLE CONVERSION BOT|"
- "WEBLINK CHECKER|"
- "DATABASE|"
- "SEARCH ENGINE|"
- "COPYRIGHT|"
- "FURTHER"
- ") SETTINGS .*)^(?=#####|# =====)",
- config_file, re.MULTILINE | re.DOTALL)
-
- if not res:
- warn('Extended config extraction failed', UserWarning)
-
- config_text = '\n'.join(res)
- if len(config_text.splitlines()) < 350:
- warn('Extended config extraction too short: %d'
- % len(config_text.splitlines()),
- UserWarning)
-
+ config_text = copy_sections()
+ if config_text:
config_content = EXTENDED_CONFIG
- except Exception as e:
- # If the warning was explicitly enabled, raise
- if isinstance(e, UserWarning):
- raise
- pywikibot.output('Exception while creating extended user-config; '
- 'falling back to simple user-config.')
- pywikibot.exception()
+ else:
+ pywikibot.output('Creating a small variant of user-config.py')
+ config_content = SMALL_CONFIG

try:
# Finally save user-config.py
diff --git a/tests/generate_user_files_tests.py b/tests/generate_user_files_tests.py
index 743543a..bde7497 100644
--- a/tests/generate_user_files_tests.py
+++ b/tests/generate_user_files_tests.py
@@ -10,6 +10,7 @@
import re

from tests.aspects import unittest, TestCase
+from unittest import expectedFailure

import generate_user_files as guf

@@ -67,6 +68,37 @@
self.assertEqual(code, 'test')
self.assertEqual(user, 'bar')

+ @expectedFailure # T145371
+ def test_copy_sections_fail(self):
+ """Test copy_sections function for sections not in config text."""
+ config_text = guf.copy_sections()
+ for section in ('HTTP SETTINGS',
+ 'REPLICATION BOT SETTINGS',
+ ):
+ self.assertNotIn(section, config_text)
+
+ def test_copy_sections_not_found(self):
+ """Test copy_sections function for sections not in config text."""
+ config_text = guf.copy_sections()
+ for section in ('ACCOUNT SETTINGS',
+ 'OBSOLETE SETTINGS',
+ 'EXTERNAL EDITOR SETTINGS',
+ ):
+ self.assertNotIn(section, config_text)
+
+ def test_copy_sections_found(self):
+ """Test copy_sections function for sections found in config text."""
+ config_text = guf.copy_sections()
+ self.assertIsNotNone(config_text)
+ for section in ('LOGFILE SETTINGS',
+ 'EXTERNAL SCRIPT PATH SETTINGS',
+ 'INTERWIKI SETTINGS',
+ 'FURTHER SETTINGS',
+ ):
+ self.assertIn(section, config_text)
+ lines = config_text.splitlines()
+ self.assertGreater(len(lines), 350)
+

if __name__ == '__main__': # pragma: no cover
try:

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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I86f06d4ec7e3a93cc217145ed659be2e8dc64d3b
Gerrit-Change-Number: 455179
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot (75)