Skip to content

Commit d55755e

Browse files
committed
Add a @plugin decorator for plugins.
Plugins are really just callables that accept a replay as the first argument. This decorator allows you to curry configuration parameters for later calls to the plugin.
1 parent 287c2bf commit d55755e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

sc2reader/plugins/replay.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
from sc2reader import log_utils
88
from sc2reader.utils import Length
99
from sc2reader.events import SelectionEvent, HotkeyEvent, AddToHotkeyEvent, GetFromHotkeyEvent, SetToHotkeyEvent
10-
from sc2reader.plugins.utils import PlayerSelection, GameState, JSONDateEncoder
10+
from sc2reader.plugins.utils import PlayerSelection, GameState, JSONDateEncoder, plugin
1111

12+
@plugin
1213
def toJSON(replay, **user_options):
1314
options = dict(cls=JSONDateEncoder)
1415
options.update(user_options)
1516
return json.dumps(toDict(replay), **options)
1617

17-
18+
@plugin
1819
def toDict(replay):
1920
# Build observers into dictionary
2021
observers = list()
@@ -79,7 +80,7 @@ def toDict(replay):
7980
'observers': observers
8081
}
8182

82-
83+
@plugin
8384
def APMTracker(replay):
8485
efilter = lambda event: getattr(event,'is_player_action',False)
8586
for player in replay.players:
@@ -90,7 +91,7 @@ def APMTracker(replay):
9091
player.apm[event.second/60] += 1
9192
player.avg_apm = sum(player.apm.values())/float(len(player.apm.keys()))
9293

93-
94+
@plugin
9495
def SelectionTracker(replay):
9596
logger = log_utils.get_logger(SelectionTracker)
9697
efilter = lambda e: isinstance(e, SelectionEvent) or isinstance(e, HotkeyEvent)

sc2reader/plugins/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
from __future__ import absolute_import
22

33
import json
4+
from functools import wraps
45
from datetime import datetime
56
from sc2reader import log_utils
67

8+
from functools import wraps
9+
def plugin(func):
10+
@wraps(func)
11+
def wrapper(**options):
12+
@wraps(func)
13+
def call(*args, **kwargs):
14+
opt = kwargs.copy()
15+
opt.update(options)
16+
return func(*args, **opt)
17+
return call
18+
return wrapper
19+
720
class JSONDateEncoder(json.JSONEncoder):
821
def default(self, obj):
922
if isinstance(obj, datetime):

0 commit comments

Comments
 (0)