Skip to content

Commit 9abbf38

Browse files
committed
Cleans up the configuration internals for sc2reader. Slight interface change.
1 parent 6231b92 commit 9abbf38

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

sc2reader/__init__.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@
88

99
__version__ = "0.3.0"
1010

11-
#Library configuration and constants
12-
FULL = 1
13-
PARTIAL = 2
14-
CUSTOM = 3
15-
16-
FILES_FULL = ['replay.initData','replay.details','replay.attributes.events','replay.message.events','replay.game.events']
17-
FILES_PARTIAL = ['replay.initData','replay.details','replay.attributes.events','replay.message.events']
11+
FILES = {
12+
"FULL": [
13+
'replay.initData',
14+
'replay.details',
15+
'replay.attributes.events',
16+
'replay.message.events',
17+
'replay.game.events'
18+
],
19+
20+
"PARTIAL": [
21+
'replay.initData',
22+
'replay.details',
23+
'replay.attributes.events',
24+
'replay.message.events'
25+
],
26+
}
1827

19-
PROCESSORS_FULL = [
28+
PROCESSORS = {
29+
"FULL": [
2030
PeopleProcessor(),
2131
AttributeProcessor(),
2232
TeamsProcessor(),
@@ -25,15 +35,16 @@
2535
EventProcessor(),
2636
ApmProcessor(),
2737
ResultsProcessor()
28-
]
38+
],
2939

30-
PROCESSORS_PARTIAL = [
40+
"PARTIAL": [
3141
PeopleProcessor(),
3242
AttributeProcessor(),
3343
TeamsProcessor(),
3444
MessageProcessor(),
3545
RecorderProcessor(),
36-
]
46+
],
47+
}
3748

3849
class ReaderMap(object):
3950
def __getitem__(self,key):
@@ -97,28 +108,21 @@ def read_header(file):
97108
return data[1],data[3]
98109

99110
class SC2Reader(object):
100-
def __init__(self, parse=FULL, directory="", processors=[], debug=False, files=None):
101-
#Check that arguments are consistent with expectations up front
102-
#Easier to debug issues this way
103-
if parse == FULL:
104-
files = FILES_FULL
105-
processors = PROCESSORS_FULL + processors
106-
elif parse == PARTIAL:
107-
files = FILES_PARTIAL
108-
processors = PROCESSORS_PARTIAL + processors
109-
elif parse == CUSTOM:
110-
if not files:
111-
raise ValueError("Custom parsing requires specification the files arguments")
112-
else:
113-
raise ValueError("parse must be either FULL, PARTIAL, or CUSTOM")
111+
def __init__(self, parse="FULL", directory="", processors=[], debug=False, files=None):
112+
#Sanitize the parse level
113+
parse = parse.upper()
114+
if parse not in ("FULL","PARTIAL","CUSTOM"):
115+
raise ValueError("Unrecognized parse argument `%s`" % parse)
114116

115-
#Update the class configuration
117+
#get our defaults and save preferences
118+
files = FILES.get(parse,files)
119+
processors = PROCESSORS.get(parse,processors)
116120
self.__dict__.update(locals())
117121

118122
def read(self, location):
119-
#account for the directory option
120-
if self.directory: location = os.path.join(self.directory,location)
121-
123+
#Sanitize the location provided (accounting for directory option)
124+
if self.directory:
125+
location = os.path.join(self.directory,location)
122126
if not os.path.exists(location):
123127
raise ValueError("Location must exist")
124128

0 commit comments

Comments
 (0)