Skip to content

Commit

Permalink
minor simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 6, 2024
1 parent c295305 commit c1ed5e6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 82 deletions.
48 changes: 13 additions & 35 deletions mediawiki/mediawiki.py
Expand Up @@ -185,10 +185,7 @@ def proxies(self) -> Optional[Dict]:
@proxies.setter
def proxies(self, proxies: Optional[Dict]):
"""Turn on, off, or set proxy use through the Requests library"""
if isinstance(proxies, dict):
self._proxies = proxies
else:
self._proxies = None
self._proxies = proxies if isinstance(proxies, dict) else None
self._reset_session()

@property
Expand Down Expand Up @@ -227,10 +224,7 @@ def timeout(self) -> float:
def timeout(self, timeout: float):
"""Set request timeout in seconds (or fractions of a second)"""

if timeout is None:
self._timeout = None # no timeout
return
self._timeout = float(timeout) # allow the exception to be raised
self._timeout = None if timeout is None else float(timeout)

@property
def verify_ssl(self) -> Union[bool, str]:
Expand All @@ -240,9 +234,7 @@ def verify_ssl(self) -> Union[bool, str]:
@verify_ssl.setter
def verify_ssl(self, verify_ssl: Union[bool, str]):
"""Set request verify SSL parameter; defaults to True if issue"""
self._verify_ssl = True
if isinstance(verify_ssl, (bool, str)):
self._verify_ssl = verify_ssl
self._verify_ssl = verify_ssl if isinstance(verify_ssl, (bool, str)) else True
self._reset_session()

@property
Expand Down Expand Up @@ -280,9 +272,7 @@ def category_prefix(self) -> str:
@category_prefix.setter
def category_prefix(self, prefix: str):
"""Set the category prefix correctly"""
if prefix[-1:] == ":":
prefix = prefix[:-1]
self._cat_prefix = prefix
self._cat_prefix = prefix[:-1] if prefix[-1:] == ":" else prefix

@property
def user_agent(self) -> str:
Expand Down Expand Up @@ -324,10 +314,9 @@ def refresh_interval(self) -> Optional[int]:
@refresh_interval.setter
def refresh_interval(self, refresh_interval: int):
"""Set the new cache refresh interval"""
if isinstance(refresh_interval, int) and refresh_interval > 0:
self._refresh_interval = refresh_interval
else:
self._refresh_interval = None
self._refresh_interval = (
refresh_interval if isinstance(refresh_interval, int) and refresh_interval > 0 else None
)

def login(self, username: str, password: str, strict: bool = True) -> bool:
"""Login as specified user
Expand Down Expand Up @@ -371,8 +360,7 @@ def login(self, username: str, password: str, strict: bool = True) -> bool:
self._is_logged_in = False
reason = res["login"]["reason"]
if strict:
msg = f"MediaWiki login failure: {reason}"
raise MediaWikiLoginError(msg)
raise MediaWikiLoginError(f"MediaWiki login failure: {reason}")
return False

# non-properties
Expand Down Expand Up @@ -482,9 +470,7 @@ def random(self, pages: int = 1) -> Union[str, List[str]]:
request = self.wiki_request(query_params)
titles = [page["title"] for page in request["query"]["random"]]

if len(titles) == 1:
return titles[0]
return titles
return titles[0] if len(titles) == 1 else titles

@memoize
def allpages(self, query: str = "", results: int = 10) -> List[str]:
Expand All @@ -506,8 +492,7 @@ def allpages(self, query: str = "", results: int = 10) -> List[str]:

self._check_error_response(request, query)

titles = [page["title"] for page in request["query"]["allpages"]]
return titles
return [page["title"] for page in request["query"]["allpages"]]

@memoize
def search(
Expand Down Expand Up @@ -546,9 +531,7 @@ def search(
search_results = [d["title"] for d in raw_results["query"]["search"]]

if suggestion:
sug = None
if raw_results["query"].get("searchinfo"):
sug = raw_results["query"]["searchinfo"]["suggestion"]
sug = raw_results["query"]["searchinfo"]["suggestion"] if raw_results["query"].get("searchinfo") else None
return search_results, sug
return search_results

Expand Down Expand Up @@ -665,10 +648,7 @@ def opensearch(self, query: str, results: int = 10, redirect: bool = True) -> Li

self._check_error_response(out, query)

res: List[Tuple[str, str, str]] = []
for i, item in enumerate(out[1]):
res.append((item, out[2][i], out[3][i]))
return res
return [(item, out[2][i], out[3][i]) for i, item in enumerate(out[1])]

@memoize
def prefixsearch(self, prefix: str, results: int = 10) -> List[str]:
Expand Down Expand Up @@ -790,9 +770,7 @@ def categorymembers(
search_params["cmlimit"] = results - returned_results
# end while loop

if subcategories:
return pages, subcats
return pages
return (pages, subcats) if subcategories else pages

def categorytree(self, category: str, depth: int = 5) -> Dict[str, Any]:
"""Generate the Category Tree for the given categories
Expand Down
71 changes: 34 additions & 37 deletions mediawiki/mediawikipage.py
Expand Up @@ -260,9 +260,11 @@ def images(self) -> List[str]:
"prop": "imageinfo", # this will be replaced by fileinfo
"iiprop": "url",
}
for page in self._continued_query(params):
if "imageinfo" in page and "url" in page["imageinfo"][0]:
self._images.append(page["imageinfo"][0]["url"])
self._images.extend(
page["imageinfo"][0]["url"]
for page in self._continued_query(params)
if "imageinfo" in page and "url" in page["imageinfo"][0]
)
self._images = sorted(self._images)
return self._images

Expand All @@ -284,8 +286,7 @@ def logos(self) -> List[str]:
info = self._soup.find("table", {"class": "infobox"})
if info is not None and isinstance(info, Tag):
children = info.find_all("a", class_="image")
for child in children:
self._logos.append("https:" + child.img["src"])
self._logos.extend("https:" + child.img["src"] for child in children)
return self._logos

@property
Expand Down Expand Up @@ -460,15 +461,14 @@ def summarize(self, sentences: int = 0, chars: int = 0) -> str:
Precedence for parameters: sentences then chars; if both are 0 then the entire first section is returned"""
query_params: Dict[str, Any] = {"prop": "extracts", "explaintext": "", "titles": self.title}
if sentences:
query_params["exsentences"] = 10 if sentences > 10 else sentences
query_params["exsentences"] = min(sentences, 10)
elif chars:
query_params["exchars"] = 1 if chars < 1 else chars
query_params["exchars"] = max(chars, 1)
else:
query_params["exintro"] = ""

request = self.mediawiki.wiki_request(query_params)
summary = request["query"]["pages"][self.pageid].get("extract")
return summary
return request["query"]["pages"][self.pageid].get("extract")

@property
def sections(self) -> List[str]:
Expand Down Expand Up @@ -586,9 +586,7 @@ def parse_section_links(self, section_title: str) -> Optional[List[Tuple[str, st
id_tag = headline.get("id")
break

if id_tag is not None:
return self._parse_section_links(id_tag)
return None
return self._parse_section_links(id_tag) if id_tag is not None else None

# Protected Methods
def __load(self, redirect: bool = True, preload: bool = False):
Expand Down Expand Up @@ -664,31 +662,31 @@ def _raise_disambiguation_error(self, page: Dict, pageid: int):

def _handle_redirect(self, redirect: bool, preload: bool, query: Dict, page: Dict[str, Any]):
"""handle redirect"""
if redirect:
redirects = query["redirects"][0]

if "normalized" in query:
normalized = query["normalized"][0]
if normalized["from"] != self.title:
raise MediaWikiException(ODD_ERROR_MESSAGE)
from_title = normalized["to"]
else:
if not getattr(self, "title", None):
self.title = redirects["from"]
delattr(self, "pageid")
from_title = self.title
if redirects["from"] != from_title:
raise MediaWikiException(ODD_ERROR_MESSAGE)
if not redirect:
raise RedirectError(getattr(self, "title", page["title"]))

# change the title and reload the whole object
self.__init__( # type: ignore
self.mediawiki,
title=redirects["to"],
redirect=redirect,
preload=preload,
)
redirects = query["redirects"][0]

if "normalized" in query:
normalized = query["normalized"][0]
if normalized["from"] != self.title:
raise MediaWikiException(ODD_ERROR_MESSAGE)
from_title = normalized["to"]
else:
raise RedirectError(getattr(self, "title", page["title"]))
if not getattr(self, "title", None):
self.title = redirects["from"]
delattr(self, "pageid")
from_title = self.title
if redirects["from"] != from_title:
raise MediaWikiException(ODD_ERROR_MESSAGE)

# change the title and reload the whole object
self.__init__( # type: ignore
self.mediawiki,
title=redirects["to"],
redirect=redirect,
preload=preload,
)

def _continued_query(self, query_params: Dict[str, Any], key: str = "pages") -> Iterator[Dict[Any, Any]]:
"""Based on
Expand Down Expand Up @@ -761,8 +759,7 @@ def _parse_section_links(self, id_tag: Optional[str]) -> List[Tuple[str, str]]:
if node.name == "a":
all_links.append(self.__parse_link_info(node))
else:
for link in node.find_all("a"):
all_links.append(self.__parse_link_info(link))
all_links.extend(self.__parse_link_info(link) for link in node.find_all("a"))
return all_links

def __parse_link_info(self, link: Tag) -> Tuple[str, str]:
Expand Down
5 changes: 1 addition & 4 deletions mediawiki/utilities.py
Expand Up @@ -77,7 +77,4 @@ def is_relative_url(url: str) -> Optional[bool]:
"""simple method to determine if a url is relative or absolute"""
if url.startswith("#"):
return None
if url.find("://") > 0 or url.startswith("//"):
# either 'http(s)://...' or '//cdn...' and therefore absolute
return False
return True
return url.find("://") <= 0 and not url.startswith("//")
12 changes: 6 additions & 6 deletions tests/utilities.py
Expand Up @@ -32,11 +32,11 @@ def walk(next_node, depth):

if len(next_node["sub-categories"].keys()) == 0:
return next_node["depth"]
else:
for key in next_node["sub-categories"].keys():
path_depth = walk(next_node["sub-categories"][key], depth)
if path_depth and path_depth > depth:
depth = path_depth
return depth

for key in next_node["sub-categories"].keys():
path_depth = walk(next_node["sub-categories"][key], depth)
if path_depth and path_depth > depth:
depth = path_depth
return depth

return walk(node, 0)

0 comments on commit c1ed5e6

Please sign in to comment.