Skip to content

Commit

Permalink
updates for pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 10, 2024
1 parent 536c838 commit 86c4473
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .pylintrc
Expand Up @@ -151,7 +151,9 @@ disable=raw-checker-failed,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead
use-symbolic-message-instead,
too-many-arguments,
protected-access,

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down Expand Up @@ -598,7 +600,7 @@ variable-naming-style=snake_case
[EXCEPTIONS]

# Exceptions that will emit a warning when caught.
overgeneral-exceptions=builtins.BaseException, builtins.Exception
overgeneral-exceptions=mediawiki.exceptions.BaseException,builtins.Exception


[LOGGING]
Expand Down
65 changes: 58 additions & 7 deletions mediawiki/configuraton.py
@@ -1,4 +1,5 @@
from dataclasses import dataclass, field
"""Configuration module"""
from dataclasses import asdict, dataclass, field
from datetime import datetime, timedelta
from typing import Dict, Optional, Union

Expand All @@ -8,6 +9,8 @@

@dataclass
class Configuration:
"""Configuration class"""

_lang: str = field(default="en", init=False, repr=False)
_api_url: str = field(default="https://en.wikipedia.org/w/api.php", init=False, repr=False)
_category_prefix: str = field(default="Category", init=False, repr=False)
Expand Down Expand Up @@ -84,32 +87,43 @@ def __init__(
self.timeout = timeout

def __repr__(self):
"""repr"""
keys = [
x.replace("_", "", 1)
for x in sorted(self.__dataclass_fields__.keys())
for x in sorted(asdict(self).keys())
if x not in ["_login", "_rate_limit_last_call", "_clear_memoized", "_reset_session"]
]
full = [f"{x}={self.__getattribute__(x)}" for x in keys]
return f"Configuration({', '.join(full)})"

@property
def lang(self) -> str:
"""str: The API URL language, if possible this will update the API URL
Note:
Use correct language titles with the updated API URL
Note:
Some API URLs do not encode language; unable to update if this is the case"""
return self._lang

@lang.setter
def lang(self, lang: str):
t_lang = lang.lower()
if self._lang == t_lang:
def lang(self, language: str):
"""Set the language to use; attempts to change the API URL"""
if self._lang == language.lower():
return
url = self._api_url
tmp = url.replace(f"/{self._lang}.", f"/{t_lang}.")
tmp = url.replace(f"/{self._lang}.", f"/{language.lower()}.")

self.api_url = tmp
self._lang = t_lang
self._lang = language.lower()
self._clear_memoized - True

@property
def api_url(self) -> str:
"""str: API URL of the MediaWiki site
Note:
Not settable; See :py:func:`mediawiki.MediaWiki.set_api_url`"""
return self._api_url

@api_url.setter
Expand All @@ -122,103 +136,140 @@ def api_url(self, api_url: str):

@property
def category_prefix(self) -> str:
"""str: The category prefix to use when using category based functions
Note:
Use the correct category name for the language selected"""
return self._category_prefix

@category_prefix.setter
def category_prefix(self, category_prefix: str):
"""Set the category prefix correctly"""
self._category_prefix = category_prefix[:-1] if category_prefix[-1:] == ":" else category_prefix

@property
def user_agent(self) -> str:
"""str: User agent string
Note:
If using in as part of another project, this should be changed"""
return self._user_agent

@user_agent.setter
def user_agent(self, user_agent: str):
"""Set the new user agent string
Note:
Will need to re-log into the MediaWiki if user agent string is changed"""
self._user_agent = user_agent

@property
def proxies(self) -> Optional[Dict]:
"""dict: Turn on, off, or set proxy use with the Requests library"""
return self._proxies

@proxies.setter
def proxies(self, proxies: Optional[Dict]):
"""Turn on, off, or set proxy use through the Requests library"""
self._proxies = proxies if isinstance(proxies, dict) else None

# reset session
self._reset_session = True

@property
def verify_ssl(self) -> Union[bool, str]:
"""bool | str: Verify SSL when using requests or path to cert file"""
return self._verify_ssl

@verify_ssl.setter
def verify_ssl(self, verify_ssl: Union[bool, str, None]):
"""Set request verify SSL parameter; defaults to True if issue"""
self._verify_ssl = verify_ssl if isinstance(verify_ssl, (bool, str)) else True

# reset session
self._reset_session = True

@property
def rate_limit(self) -> bool:
"""bool: Turn on or off Rate Limiting"""
return self._rate_limit

@rate_limit.setter
def rate_limit(self, rate_limit: bool):
"""Turn on or off rate limiting"""
self._rate_limit = bool(rate_limit)
self._rate_limit_last_call = None
self._clear_memoized = True

@property
def rate_limit_min_wait(self) -> timedelta:
"""timedelta: Time to wait between calls
Note:
Only used if rate_limit is **True**"""
return self._rate_limit_min_wait

@rate_limit_min_wait.setter
def rate_limit_min_wait(self, min_wait: timedelta):
"""Set minimum wait to use for rate limiting"""
self._rate_limit_min_wait = min_wait
self._rate_limit_last_call = None

@property
def username(self) -> Optional[str]:
"""str | None: Username to use to log into the mediawiki site"""
return self._username

@username.setter
def username(self, username: Optional[str]):
"""set the username, if needed, to log into the mediawiki site"""
self._username = username
if self.username and self.password:
self._login = True

@property
def password(self) -> Optional[str]:
"""str | None: Password to use to log into the mediawiki site"""
return self._password

@password.setter
def password(self, password: Optional[str]):
"""set the password, if needed, to log into the mediawiki site"""
self._password = password
if self.username and self.password:
self._login = True

@property
def refresh_interval(self) -> Optional[int]:
"""int | None: The interval at which the memoize cache is to be refresh"""
return self._refresh_interval

@refresh_interval.setter
def refresh_interval(self, refresh_interval: Optional[int]):
"Set the new cache refresh interval" ""
self._refresh_interval = (
refresh_interval if isinstance(refresh_interval, int) and refresh_interval > 0 else None
)

@property
def use_cache(self) -> bool:
"""bool: Whether caching should be used; on (**True**) or off (**False**)"""
return self._use_cache

@use_cache.setter
def use_cache(self, use_cache: bool):
"""toggle using the cache or not"""
self._use_cache = bool(use_cache)

@property
def timeout(self) -> Optional[float]:
"""float: Response timeout for API requests
Note:
Use **None** for no response timeout"""
return self._timeout

@timeout.setter
def timeout(self, timeout: Optional[float]):
"""Set request timeout in seconds (or fractions of a second)"""
self._timeout = None if timeout is None else float(timeout)

0 comments on commit 86c4473

Please sign in to comment.