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
76from 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+
1313class 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
6469def 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 )
0 commit comments