Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page.Page fails due to TypeError: 'dict_keys' object is not subscriptable #10

Open
lcoustillac opened this issue Nov 6, 2023 · 0 comments

Comments

@lcoustillac
Copy link

Hi, I am still trying to migrate a python 2.7 script to python3, using wikitools3, as in issues #8 and #9. in fact I now using the local branch I made that seems to fix my problem in #9.

This time my script is also attempting to access a page from my wiki :

#!/usr/bin/env python
""" quick and dirty script aimed at debugging wikitools3 """
""" see issues https://github.com/mediawiki-client-tools/wikitools3/issues """

import os
from wikitools3 import wiki, page

# now I am using my company's wiki api
api_url = os.environ.get('WIKI_URL')
username = os.environ.get('WIKI_USER')
password = os.environ.get('WIKI_PASSWORD')
print("api_url: " + str(api_url) + " username: " + username)
if not password:
    print("Empty variable: WIKI_PASSWORD")
    sys.exit(1)
site = wiki.Wiki(api_url, username, password, True)
# This "Test title" page does not exist but I get the same result with existing titles
p = page.Page(site, "Test title")

It fails like this:

Traceback (most recent call last):
  File "./test_wikitools.py", line 17, in <module>
    p = page.Page(site, "test title")
  File "/workdir/wikitools3/page.py", line 132, in __init__
    self.setPageInfo()
  File "/workdir/wikitools3/page.py", line 169, in setPageInfo
    self.pageid = response["query"]["pages"].keys()[0]
TypeError: 'dict_keys' object is not subscriptable

I suggest the following patch for page.py line 169:

         req = api.APIRequest(self.site, params)
         response = req.query(False)
-        self.pageid = response["query"]["pages"].keys()[0]
+        self.pageid = int(list(response["query"]["pages"].keys())[0])
         if self.pageid > 0:
             self.exists = True

The call to list() solves the TypeError: 'dict_keys' object is not subscriptable, and the call to int() averts a TypeError: '>' not supported between instances of 'str' and 'int' at line 170.

lcoustillac added a commit to lcoustillac/wikitools3 that referenced this issue Nov 6, 2023
lcoustillac added a commit to lcoustillac/wikitools3 that referenced this issue Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant