I'm trying to get pywikibot set up for the first time. It's on a Miraheze site and I'm getting this error when I try to log in:
-----------
WARNING: Non-JSON response received from server comprehensibleinputwiki:en for url
https://comprehensibleinput.miraheze.org/w/api.php
The server may be down.
Status code: 200
The text message is:
MediaWiki API help - Comprehensible Input Wiki
document.documentElement.className="client-js";RLCONF={"wgBreakFrames":true,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"7a406c0d0d1c2121406e444b","wgCSPNonce":false,"wgCanonicalNamespace":"Special","wgCanonicalSpecialPageName":"ApiHelp","wgNamespaceNumber":-1,"wgPageName":"Special:ApiHelp","wgTitle":"ApiHelp","wgCurRevisionId":0,"wgRevisionId":0,"wgArticleId":0,"wgIsArticle":false,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"Special:ApiHelp","wgRelevantArticleId":0,"wgIsProbablyEditable":false,"wgRelevantPageIsProbablyEditable":false,"wgVisualEditor":{"pageLanguageCode":"en","pageLanguageDir":"ltr","pageVariantFallbacks":"en"},"wgNoticeProject":"all",
...
------------
So it seems that it can access the page, but is not parsing it correctly for some reason. Anyone know what the issue is?
I wrote a script which requires admin rights. Someone told he couldn't use
it because Pywikibot does not supoort 2FA login. Is that correct?
--
Bináris
I've been beating my head against the wall trying to figure out how to build a Mock of APISite. If I do the obvious:
site = mocker.MagicMock(spec=APISite)
I end up with:
> AttributeError: Mock object has no attribute 'encodings'
Yet, if I hand-build a Site object, sure enough it does:
>>> import pywikibot
>>> site = pywikibot.Site("en", "wikipedia")
>>> site.encodings()
('utf-8', 'iso-8859-1')
>>>
To make a long story short, eventually I found this bit of magic in _basesite.py:
> def __getattr__(self, attr):
> """Delegate undefined methods calls to the Family object."""
> if hasattr(self.__class__, attr):
> return getattr(self.__class__, attr)
> try:
> method = getattr(self.family, attr)
> if not callable(method):
> raise AttributeError
> f = functools.partial(method, self.code)
> if hasattr(method, '__doc__'):
> f.__doc__ = method.__doc__
> return f
> except AttributeError:
> raise AttributeError("{} instance has no attribute '{}'"
> .format(self.__class__.__name__, attr))
WTF? I mean, I see what it's doing, but why go to this level of obfuscation? It's basically reimplementing class inheritance manually (and in a way which is totally beyond the ability of Mock to understand).
Where are the logins stored? In compat they had an obvious directory. Now I
searched it a lot, but did not find.
Whenever I run login.py, the previous account is thrown out, so I cannot
comfortably use two accounts. Is it on purpose?
--
Bináris
I've got:
platform darwin -- Python 3.9.13, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/roy/dev/dyk-tools, configfile: src/pages/pytest.ini
plugins: mock-3.10.0, socket-0.5.1
When I run the following:
def test_ip(mocker):
mock_IPv4Address = mocker.patch('ipaddress.IPv4Address', autospec=True)
ip = mock_IPv4Address('1.2.3.4')
assert mock_IPv4Address.xxyyzz
def test_site(mocker):
mock_Site = mocker.patch('pywikibot.Site', autospec=True)
site = mock_Site('en', 'wikipedia')
assert site.xxyyzz
test_ip() fails, as I expect it to, with AttributeError: Mock object has no attribute 'xxyyzz'. But, test_site() passes. WTF? Why does it not also fail with the same error?
python pwb.py -nolog -user:Bináris userpageprotect.py
python pwb.py -nolog -user:"Bináris" userpageprotect.py
Neither asks for my password when the script wants to protect a page. (The
script is own development.)
Instead I get:
pywikibot.exceptions.UserRightsError: User "None" does not have required
user right "protect"
CRITICAL: Exiting due to uncaught exception <class
'pywikibot.exceptions.UserRightsError'>
Do I do soemthing wrong?
I liked the compat solution with my sysop user in user-config much better.
:-(
--
Bináris
I'm diving into my first serious PWB application. I'm using it as a library to replace mwclient.
I'm not sure how to approach testing. I'd really like something which is isolated from making actual API calls (i.e. hermetic unit tests). Does there exist a mechanism to do that?
What I've been doing so far is using unittest.mock to create mock Page objects, but that's not wondeful. unitest.mock is always a little clunky to use, but more than that, it's too easy to create mocks that don't accurately emulate the real PWB behaviors, so your tests aren't really testing what they should be. The other problem is if you don't mock every possible code path to the backend API, you can end up hitting a real server by accident during your tests. Which we be not a good thing.
How do people handle this?