Skip to content

Commit

Permalink
Merge pull request #17 from mathsman5133/events
Browse files Browse the repository at this point in the history
Merge Events branch
  • Loading branch information
mathsman5133 authored Jun 17, 2019
2 parents 53d1082 + 83a67d6 commit 7c36d04
Show file tree
Hide file tree
Showing 18 changed files with 3,480 additions and 1,531 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Quick Example
import coc
import asyncio
client = coc.Client('email', 'password')
client = coc.login('email', 'password')
async def get_some_player(tag):
player = await client.get_player(tag)
Expand Down
84 changes: 53 additions & 31 deletions coc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
# -*- coding: utf-8 -*-

__version__ = '0.1.3'
__version__ = '0.2.0'

from .client import Client
from .dataclasses import (
from .clans import (
Clan,
BasicClan,
SearchClan,
BasicClan,
WarClan,
Player,
BasicPlayer,
WarMember,
SearchPlayer,
BaseWar,
WarLog,
CurrentWar,
Achievement,
Troop,
Hero,
Spell,
WarAttack,
Location,
League,
LeagueRankedPlayer,
Season,
LegendStatistics,
Badge,
Timestamp,
LeaguePlayer,
LeagueClan,
LeagueGroup,
LeagueWar,
LeagueWarLogEntry
LeagueClan
)
from .client import Client, EventsClient, login
from .enums import (
CacheType,
HOME_TROOP_ORDER,
BUILDER_TROOPS_ORDER,
SPELL_ORDER,
HERO_ORDER,
SIEGE_MACHINE_ORDER
)
from .errors import (
ClashOfClansException,
Expand All @@ -40,10 +25,47 @@
InvalidArgument,
InvalidCredentials,
Forbidden,
Maitenance
Maitenance,
GatewayError
)
from .http import HTTPClient
from .iterators import (
ClanIterator,
PlayerIterator,
ClanWarIterator,
LeagueWarIterator,
CurrentWarIterator
)
from .miscmodels import (
Achievement,
Badge,
EqualityComparable,
Hero,
League,
LegendStatistics,
Location,
Spell,
Troop,
Timestamp,

from .enums import (
CacheType
)
from .players import (
Player,
BasicPlayer,
SearchPlayer,
LeaguePlayer,
LeagueRankedPlayer,
WarMember
)
from .wars import (
BaseWar,
WarLog,
ClanWar,
WarAttack,
LeagueGroup,
LeagueWar,
LeagueWarLogEntry
)



58 changes: 50 additions & 8 deletions coc/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,31 @@ async def new_coro():
return new_coro()


def _wrap_store_coro(cache, key, coro):
def _wrap_store_coro(cache, key, coro, update_cache):
async def fctn():
value = await coro
cache[key] = value
if update_cache:
cache[key] = value

return value
return fctn()


def _try_wrap_store_coro(cache, key, data):
def _try_wrap_store_coro(cache, key, data, update_cache):
if inspect.isawaitable(data):
return _wrap_store_coro(cache, key, data)
return _wrap_store_coro(cache, key, data, update_cache)
if inspect.isasyncgen(data):
return data

cache[key] = data
if update_cache:
cache[key] = data

return data


class LRU(OrderedDict):
__slots__ = ('max_size', 'ttl')

def __init__(self, max_size, ttl):
self.max_size = max_size
self.ttl = ttl
Expand All @@ -76,7 +82,12 @@ def __getitem__(self, key):
self.check_expiry()

value = super().__getitem__(key)
self.move_to_end(key)

try:
self.move_to_end(key)
except KeyError:
pass

return value[1]

def __setitem__(self, key, value):
Expand Down Expand Up @@ -105,12 +116,20 @@ def check_max_size(self):


class Cache:
__slots__ = ('cache', 'ttl', 'max_size', 'fully_populated',
'_is_clan', '_is_player', '_is_war', '_is_static')

def __init__(self, max_size=128, ttl=None):
self.cache = LRU(max_size, ttl)
self.ttl = self.cache.ttl
self.max_size = self.cache.max_size
self.fully_populated = False

self._is_clan = False
self._is_player = False
self._is_war = False
self._is_static = False

def __call__(self, *args, **kwargs):
self.cache.check_expiry()

Expand All @@ -121,6 +140,7 @@ def wrapper(*args, **kwargs):
key = find_key(args, kwargs)
cache = kwargs.pop('cache', False)
fetch = kwargs.pop('fetch', True)
update_cache = kwargs.pop('update_cache', True)

if not key:
return func(*args, **kwargs)
Expand All @@ -130,7 +150,7 @@ def wrapper(*args, **kwargs):
else:
if fetch:
data = func(*args, **kwargs)
return _try_wrap_store_coro(self.cache, key, data)
return _try_wrap_store_coro(self.cache, key, data, update_cache)

else:
return None
Expand All @@ -141,7 +161,7 @@ def wrapper(*args, **kwargs):
else:
return None

return _try_wrap_store_coro(self.cache, key, data)
return _try_wrap_store_coro(self.cache, key, data, update_cache)

else:
log.debug('Using cached object with KEY: %s and VALUE: %s', key, data)
Expand Down Expand Up @@ -181,3 +201,25 @@ def get_limit(self, limit: int=None):
return self.get_all_values()

return self.get_all_values()[:limit]

def events_cache(self):
def deco(func):
@wraps(func)
def wrapper(*args, **kwargs):
event_name = args[1]
if event_name.endswith('batch_updates'):
return func(*args, **kwargs)

event_args = [n for n in args[1:]]
event_args.extend(kwargs.values())

key = f'{event_name}.{time.monotonic()}'

self.add(key, event_args)

return func(*args, **kwargs)
return wrapper
return deco



Loading

0 comments on commit 7c36d04

Please sign in to comment.