Skip to content

Commit

Permalink
drastically improved pylint score (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudonym117 authored Oct 29, 2021
1 parent 8087ff6 commit e027c6f
Show file tree
Hide file tree
Showing 34 changed files with 459 additions and 123 deletions.
13 changes: 13 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[MESSAGES CONTROL]
disable = C0330, C0326

[format]
max-line-length = 88
class-naming-style = PascalCase
module-naming-style = PascalCase

[similarities]
ignore-comments=yes
ignore-docstrings=yes
ignore-imports=yes
ignore-signatures=yes
13 changes: 9 additions & 4 deletions src/riotwatcher/Handlers/DeprecationHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from . import RequestHandler

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


class DeprecationHandler(RequestHandler):
Expand All @@ -30,10 +30,15 @@ def after_request(
return response
key = f"{endpoint_name}.{method_name}"
if key not in self._warned:
# technically race condition here, but worst case is we double log a warning
# technically race condition here, but worst case is we double log
self._warned.add(key)
log.warning(
"Method %s has been deprecated by Riot! It will no longer work after %s",
LOG.warning(
" ".join(
[
"Method %s has been deprecated by Riot!",
"It will no longer work after %s",
]
),
key,
deprecation,
)
Expand Down
2 changes: 1 addition & 1 deletion src/riotwatcher/Handlers/DeserializerAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class DeserializerAdapter(RequestHandler):
def __init__(self, deserializer: Deserializer):
super().__init__()
self._deserializer = deserializer

def after_request(
Expand All @@ -21,5 +22,4 @@ def after_request(
return self._deserializer.deserialize(endpoint_name, method_name, response.text)

def after_static_request(self, url: str, response: Response) -> Any:
# TODO: add params to get endpoint_name and method_name
return self._deserializer.deserialize(None, None, response.text)
16 changes: 11 additions & 5 deletions src/riotwatcher/Handlers/RateLimit/BasicRateLimiter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import datetime
import logging

from typing import Dict, Optional
from typing import Dict, Optional, Tuple

from ...RateLimiter import RateLimiter

from . import ApplicationRateLimiter, MethodRateLimiter, OopsRateLimiter
from . import (
ApplicationRateLimiter,
MethodRateLimiter,
OopsRateLimiter,
InternalLimiter,
)

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


class BasicRateLimiter(RateLimiter):
Expand All @@ -16,7 +21,7 @@ class BasicRateLimiter(RateLimiter):
def __init__(self):
super().__init__()

self._limiters = (
self._limiters: Tuple[InternalLimiter, InternalLimiter, InternalLimiter] = (
BasicRateLimiter.__application_rate_limiter,
MethodRateLimiter(),
OopsRateLimiter(),
Expand All @@ -41,12 +46,13 @@ def wait_until(
if wait_until[0] is not None and wait_until[0] > datetime.datetime.now():
to_wait = wait_until[0] - datetime.datetime.now()

log.debug(
LOG.debug(
"waiting for %s seconds due to %s limit...",
to_wait.total_seconds(),
wait_until[1],
)
return wait_until[0]
return None

def record_response(
self,
Expand Down
21 changes: 14 additions & 7 deletions src/riotwatcher/Handlers/RateLimit/HeaderBasedLimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

from typing import Dict, List, Optional

from .InternalLimiter import InternalLimiter
from .Limits import LimitCollection, RawLimit

log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)


class HeaderBasedLimiter:
class HeaderBasedLimiter(InternalLimiter):
def __init__(self, limit_header: str, count_header: str, friendly_name: str = None):
super().__init__()
self._limit_header = limit_header
self._count_header = count_header
self._friendly_name = friendly_name
Expand Down Expand Up @@ -73,7 +75,7 @@ def _extract_headers(self, headers: Dict[str, str]) -> Optional[List[RawLimit]]:
return None

if len(limits) != len(counts):
log.warning(
LOG.warning(
'header "%s" and "%s" have different sizes!',
self._limit_header,
self._count_header,
Expand All @@ -83,10 +85,15 @@ def _extract_headers(self, headers: Dict[str, str]) -> Optional[List[RawLimit]]:

for limit in combined_limits:
if limit[0][1] != limit[1][1]:
log.warning(
'seems that limits for headers "%s" and "%s" did not match up correctly! '
+ 'There may be issues in rate limiting. Headers were: "%s", "%s"'
+ 'Limits from "%s" will be used.',
LOG.warning(
" ".join(
[
'seems that limits for headers "%s" and "%s" did not match',
"up correctly! There may be issues in rate limiting.",
'Headers were: "%s", "%s".',
'Limits from "%s" will be used.',
]
),
self._limit_header,
self._count_header,
headers.get(self._limit_header),
Expand Down
23 changes: 23 additions & 0 deletions src/riotwatcher/Handlers/RateLimit/InternalLimiter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import abc
import datetime

from typing import Dict


class InternalLimiter(abc.ABC):
@abc.abstractmethod
def wait_until(
self, region: str, endpoint_name: str, method_name: str
) -> datetime.datetime:
pass

@abc.abstractmethod
def update_limiter(
self,
region: str,
endpoint_name: str,
method_name: str,
status: int,
headers: Dict[str, str],
):
pass
13 changes: 9 additions & 4 deletions src/riotwatcher/Handlers/RateLimit/Limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Iterable, Optional


log = logging.getLogger(__name__)
LOG = logging.getLogger(__name__)

RawLimit = namedtuple("RawLimit", ["count", "limit", "time"])

Expand Down Expand Up @@ -72,14 +72,19 @@ def set_raw_limit(self, raw_limit: RawLimit):
and self._raw_limit.limit == 0
and self._raw_limit.count == 0
):
log.warning(
"overwriting time limit, previously %s, now %s. This may cause rate limitting issues.",
LOG.warning(
" ".join(
[
"overwriting time limit, previously %s, now %s.",
"This may cause rate limitting issues.",
]
),
self._raw_limit.time,
raw_limit.time,
)

if self._raw_limit.limit != raw_limit.limit:
log.info(
LOG.info(
"rate limit changed from %s/%ss to %s/%ss",
self._raw_limit.limit,
self._raw_limit.time,
Expand Down
11 changes: 7 additions & 4 deletions src/riotwatcher/Handlers/RateLimit/OopsRateLimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

from typing import Dict

log = logging.getLogger(__name__)
from .InternalLimiter import InternalLimiter

LOG = logging.getLogger(__name__)

class OopsRateLimiter:

class OopsRateLimiter(InternalLimiter):
def __init__(self):
super().__init__()
self._friendly_name = "429_Limit"
self._retry_at = None

Expand Down Expand Up @@ -35,7 +38,7 @@ def update_limiter(
limit_type = headers.get("X-Rate-Limit-Type")

if retry_after is not None:
log.info(
LOG.info(
'hit 429 from "%s" limit! retrying after %s seconds',
limit_type,
retry_after,
Expand All @@ -44,6 +47,6 @@ def update_limiter(
seconds=int(retry_after)
)
else:
log.info(
LOG.info(
'hit 429 from "%s" limit! no retry after header...', limit_type
)
1 change: 1 addition & 0 deletions src/riotwatcher/Handlers/RateLimit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .InternalLimiter import InternalLimiter
from .HeaderBasedLimiter import HeaderBasedLimiter
from .ApplicationRateLimiter import ApplicationRateLimiter
from .Limits import Limit, LimitCollection, RawLimit
Expand Down
Loading

0 comments on commit e027c6f

Please sign in to comment.