Skip to content

Commit 7c36d04

Browse files
authored
Merge pull request #17 from mathsman5133/events
Merge Events branch
2 parents 53d1082 + 83a67d6 commit 7c36d04

File tree

18 files changed

+3480
-1531
lines changed

18 files changed

+3480
-1531
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Quick Example
3636
import coc
3737
import asyncio
3838
39-
client = coc.Client('email', 'password')
39+
client = coc.login('email', 'password')
4040
4141
async def get_some_player(tag):
4242
player = await client.get_player(tag)

coc/__init__.py

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = '0.1.3'
3+
__version__ = '0.2.0'
44

5-
from .client import Client
6-
from .dataclasses import (
5+
from .clans import (
76
Clan,
8-
BasicClan,
97
SearchClan,
8+
BasicClan,
109
WarClan,
11-
Player,
12-
BasicPlayer,
13-
WarMember,
14-
SearchPlayer,
15-
BaseWar,
16-
WarLog,
17-
CurrentWar,
18-
Achievement,
19-
Troop,
20-
Hero,
21-
Spell,
22-
WarAttack,
23-
Location,
24-
League,
25-
LeagueRankedPlayer,
26-
Season,
27-
LegendStatistics,
28-
Badge,
29-
Timestamp,
30-
LeaguePlayer,
31-
LeagueClan,
32-
LeagueGroup,
33-
LeagueWar,
34-
LeagueWarLogEntry
10+
LeagueClan
11+
)
12+
from .client import Client, EventsClient, login
13+
from .enums import (
14+
CacheType,
15+
HOME_TROOP_ORDER,
16+
BUILDER_TROOPS_ORDER,
17+
SPELL_ORDER,
18+
HERO_ORDER,
19+
SIEGE_MACHINE_ORDER
3520
)
3621
from .errors import (
3722
ClashOfClansException,
@@ -40,10 +25,47 @@
4025
InvalidArgument,
4126
InvalidCredentials,
4227
Forbidden,
43-
Maitenance
28+
Maitenance,
29+
GatewayError
4430
)
4531
from .http import HTTPClient
32+
from .iterators import (
33+
ClanIterator,
34+
PlayerIterator,
35+
ClanWarIterator,
36+
LeagueWarIterator,
37+
CurrentWarIterator
38+
)
39+
from .miscmodels import (
40+
Achievement,
41+
Badge,
42+
EqualityComparable,
43+
Hero,
44+
League,
45+
LegendStatistics,
46+
Location,
47+
Spell,
48+
Troop,
49+
Timestamp,
4650

47-
from .enums import (
48-
CacheType
4951
)
52+
from .players import (
53+
Player,
54+
BasicPlayer,
55+
SearchPlayer,
56+
LeaguePlayer,
57+
LeagueRankedPlayer,
58+
WarMember
59+
)
60+
from .wars import (
61+
BaseWar,
62+
WarLog,
63+
ClanWar,
64+
WarAttack,
65+
LeagueGroup,
66+
LeagueWar,
67+
LeagueWarLogEntry
68+
)
69+
70+
71+

coc/cache.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,31 @@ async def new_coro():
4848
return new_coro()
4949

5050

51-
def _wrap_store_coro(cache, key, coro):
51+
def _wrap_store_coro(cache, key, coro, update_cache):
5252
async def fctn():
5353
value = await coro
54-
cache[key] = value
54+
if update_cache:
55+
cache[key] = value
56+
5557
return value
5658
return fctn()
5759

5860

59-
def _try_wrap_store_coro(cache, key, data):
61+
def _try_wrap_store_coro(cache, key, data, update_cache):
6062
if inspect.isawaitable(data):
61-
return _wrap_store_coro(cache, key, data)
63+
return _wrap_store_coro(cache, key, data, update_cache)
6264
if inspect.isasyncgen(data):
6365
return data
6466

65-
cache[key] = data
67+
if update_cache:
68+
cache[key] = data
69+
6670
return data
6771

6872

6973
class LRU(OrderedDict):
74+
__slots__ = ('max_size', 'ttl')
75+
7076
def __init__(self, max_size, ttl):
7177
self.max_size = max_size
7278
self.ttl = ttl
@@ -76,7 +82,12 @@ def __getitem__(self, key):
7682
self.check_expiry()
7783

7884
value = super().__getitem__(key)
79-
self.move_to_end(key)
85+
86+
try:
87+
self.move_to_end(key)
88+
except KeyError:
89+
pass
90+
8091
return value[1]
8192

8293
def __setitem__(self, key, value):
@@ -105,12 +116,20 @@ def check_max_size(self):
105116

106117

107118
class Cache:
119+
__slots__ = ('cache', 'ttl', 'max_size', 'fully_populated',
120+
'_is_clan', '_is_player', '_is_war', '_is_static')
121+
108122
def __init__(self, max_size=128, ttl=None):
109123
self.cache = LRU(max_size, ttl)
110124
self.ttl = self.cache.ttl
111125
self.max_size = self.cache.max_size
112126
self.fully_populated = False
113127

128+
self._is_clan = False
129+
self._is_player = False
130+
self._is_war = False
131+
self._is_static = False
132+
114133
def __call__(self, *args, **kwargs):
115134
self.cache.check_expiry()
116135

@@ -121,6 +140,7 @@ def wrapper(*args, **kwargs):
121140
key = find_key(args, kwargs)
122141
cache = kwargs.pop('cache', False)
123142
fetch = kwargs.pop('fetch', True)
143+
update_cache = kwargs.pop('update_cache', True)
124144

125145
if not key:
126146
return func(*args, **kwargs)
@@ -130,7 +150,7 @@ def wrapper(*args, **kwargs):
130150
else:
131151
if fetch:
132152
data = func(*args, **kwargs)
133-
return _try_wrap_store_coro(self.cache, key, data)
153+
return _try_wrap_store_coro(self.cache, key, data, update_cache)
134154

135155
else:
136156
return None
@@ -141,7 +161,7 @@ def wrapper(*args, **kwargs):
141161
else:
142162
return None
143163

144-
return _try_wrap_store_coro(self.cache, key, data)
164+
return _try_wrap_store_coro(self.cache, key, data, update_cache)
145165

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

183203
return self.get_all_values()[:limit]
204+
205+
def events_cache(self):
206+
def deco(func):
207+
@wraps(func)
208+
def wrapper(*args, **kwargs):
209+
event_name = args[1]
210+
if event_name.endswith('batch_updates'):
211+
return func(*args, **kwargs)
212+
213+
event_args = [n for n in args[1:]]
214+
event_args.extend(kwargs.values())
215+
216+
key = f'{event_name}.{time.monotonic()}'
217+
218+
self.add(key, event_args)
219+
220+
return func(*args, **kwargs)
221+
return wrapper
222+
return deco
223+
224+
225+

0 commit comments

Comments
 (0)