Skip to content

Commit 251d151

Browse files
committed
Restructure sc2reader options and option handling
Store options into an AttributeDict that gets a copy passed into each replay object on instanciation. This will allow options to affect the replay parsing and processing at runtime.
1 parent b5746c8 commit 251d151

File tree

3 files changed

+62
-70
lines changed

3 files changed

+62
-70
lines changed

sc2reader/__init__.py

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
1-
import os
1+
import os, copy
22

3-
import copy
3+
import mpyq
44

5-
from config import FULL, PARTIAL, CUSTOM, FILES, PROCESSORS, READERS
6-
from mpyq import MPQArchive
5+
import config
76
from objects import Replay
8-
from utils import ReplayBuffer
7+
from utils import ReplayBuffer, AttributeDict
98

109
__version__ = "0.3.0"
1110
__author__ = "Graylin Kim <[email protected]>"
1211

12+
1313
class SC2Reader(object):
14-
''' Class level interface to sc2reader.
15-
16-
<<usage documentation here>>
17-
'''
18-
19-
def __init__(self, parse_type="FULL", directory="", processors=[], debug=False, files=[], verbose=False, copy=copy.copy):
20-
try:
21-
#Update and save the reader configuration
22-
parse_type = parse_type.upper()
23-
files = FILES.get(parse_type, files)
24-
processors = PROCESSORS.get(parse_type, processors)
25-
self.__dict__.update(locals())
26-
except KeyError:
27-
raise ValueError("Unrecognized parse_type `%s`" % parse_type)
14+
def __init__(self, **options):
15+
#Set Defaults before configuring with user options
16+
self.options = AttributeDict(
17+
directory="",
18+
processors=[],
19+
debug=False,
20+
verbose=False,
21+
parse_events=True)
22+
self.configure(**options)
2823

2924
def read(self, location):
30-
if self.directory:
31-
location = os.path.join(self.directory,location)
25+
if self.options.directory:
26+
location = os.path.join(self.options.directory,location)
3227

33-
if self.verbose: print "Reading: %s" % location
28+
if self.options.verbose: print "Reading: %s" % location
3429

3530
if os.path.isdir(location):
3631
#SC2Reader::read each subfile/directory and combine the lists
@@ -40,32 +35,36 @@ def read(self, location):
4035

4136
else:
4237
with open(location) as replay_file:
43-
replay = Replay(self.copy(self),replay_file)
44-
archive = MPQArchive(location,listfile=False)
38+
replay = Replay(replay_file,**self.options.copy())
39+
archive = mpyq.MPQArchive(location,listfile=False)
4540

4641
for file in self.files:
4742
buffer = ReplayBuffer(archive.read_file(file))
4843
READERS[replay.build][file].read(buffer,replay)
4944

50-
for process in self.processors:
45+
#Handle user processors after internal processors
46+
for process in self.processors+self.options.processors:
5147
replay = process(replay)
5248

5349
return replay
5450

5551
def configure(self,**options):
56-
self.__dict__.update(options)
52+
self.options.update(options)
5753

54+
#Update system configuration
55+
myconfig = config.full if self.options.parse_events else config.partial
56+
self.files = myconfig.files
57+
self.processors = myconfig.processors
5858

59-
''' The package level interface is just a lightweight wrapper around a default
60-
SC2Reader class. See the documentation above for usage details '''
6159

60+
'''Package Level Interface'''
6261
__defaultSC2Reader = SC2Reader()
6362

63+
#Allow options on the package level read for one off reads.
64+
def read(location, **options):
65+
reader = SC2Reader(**options) if options else __defaultSC2Reader
66+
return reader.read(location)
67+
68+
#Allow package level configuration for lazy people
6469
def configure(**options):
6570
__defaultSC2Reader.configure(**options)
66-
67-
def read(location, **options):
68-
if options:
69-
return SC2Reader(**options).read(location)
70-
else:
71-
return __defaultSC2Reader.read(location)

sc2reader/config.py

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1+
from utils import AttributeDict
2+
13
from processors import *
24
from readers import *
35

4-
FULL = "FULL"
5-
PARTIAL = "PARTIAL"
6-
CUSTOM = "CUSTOM"
7-
8-
FILES = {
9-
"FULL": [
10-
'replay.initData',
11-
'replay.details',
12-
'replay.attributes.events',
13-
'replay.message.events',
14-
'replay.game.events'
15-
],
16-
17-
"PARTIAL": [
18-
'replay.initData',
19-
'replay.details',
20-
'replay.attributes.events',
21-
'replay.message.events'
22-
],
23-
}
24-
25-
PROCESSORS = {
26-
"FULL": [
27-
PeopleProcessor,
28-
AttributeProcessor,
29-
TeamsProcessor,
30-
MessageProcessor,
31-
RecorderProcessor,
32-
EventProcessor,
33-
ApmProcessor,
34-
ResultsProcessor,
35-
],
6+
full = AttributeDict(
7+
files = [
8+
'replay.initData',
9+
'replay.details',
10+
'replay.attributes.events',
11+
'replay.message.events',
12+
'replay.game.events'],
13+
processors = [
14+
PeopleProcessor,
15+
AttributeProcessor,
16+
TeamsProcessor,
17+
MessageProcessor,
18+
RecorderProcessor,
19+
EventProcessor,
20+
ApmProcessor,
21+
ResultsProcessor],
22+
)
3623

37-
"PARTIAL": [
24+
partial = AttributeDict(
25+
files = [
26+
'replay.initData',
27+
'replay.details',
28+
'replay.attributes.events',
29+
'replay.message.events'],
30+
processors = [
3831
PeopleProcessor,
3932
AttributeProcessor,
4033
TeamsProcessor,
4134
MessageProcessor,
4235
RecorderProcessor,
4336
],
44-
}
37+
)
4538

4639
class ReaderMap(dict):
4740
def __init__(self):

sc2reader/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def __str__(self):
1717

1818
class Replay(object):
1919

20-
def __init__(self, reader, replay_file):
20+
def __init__(self, replay_file, **options):
2121
#Useful references
22-
self.reader = reader
22+
self.opt = options
2323
self.filename = replay_file.name
2424

2525
#header information

0 commit comments

Comments
 (0)