jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1226297?usp=email )

Change subject: IMPR: Use environment variables is usernames aren't within 
config.py
......................................................................

IMPR: Use environment variables is usernames aren't within config.py

If no username is given for a site in user4-config.py use the
environment variables 'PYWIKIBOT_USERNAME' usually used by tests or
'PWB_USERNAME' used on toolforge instead.

Bug: T414201
Change-Id: Ib08ac40ba8da795b48e59b57f21b58d12bddfd9f
---
M pywikibot/comms/http.py
1 file changed, 10 insertions(+), 4 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 3686317..a972e1a 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -33,6 +33,7 @@

 import atexit
 import codecs
+import os
 import re
 import sys
 import threading
@@ -176,8 +177,15 @@
     - replaces spaces (' ') with '_'
     - encodes the username as 'utf-8' and if the username is not ASCII
     - URL encodes the username if it is not ASCII, or contains '%'
+
+    .. versionchanged:: 11.0
+       If *username* is not given, get it from environment variables
+       'PYWIKIBOT_USERNAME' usually used by tests or 'PWB_USERNAME' used
+       on toolforge.
     """
     if not username:
+        username = os.getenv('PYWIKIBOT_USERNAME') or os.getenv('PWB_USERNAME')
+    if not username:
         return ''

     username = username.replace(' ', '_')  # Avoid spaces or %20.
@@ -215,13 +223,11 @@
     if config.user_agent_description:
         script_comments.append(config.user_agent_description)

-    username = ''
+    username = user_agent_username(site.username() if site else None)
     if site:
         script_comments.append(str(site))

-        # TODO: there are several ways of identifying a user, and username
-        # is not the best for a HTTP header if the username isn't ASCII.
-        if site.username():
+        if username:
             username = user_agent_username(site.username())
             script_comments.append('User:' + username)


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1226297?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib08ac40ba8da795b48e59b57f21b58d12bddfd9f
Gerrit-Change-Number: 1226297
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Fabfur <[email protected]>
Gerrit-Reviewer: JAn Dudík <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to