99from collections import defaultdict
1010
1111class SC2Reader (object ):
12+ """
13+ The primary interface to the sc2reader library. Acts as a configurable
14+ factory for :class:`Replay` objects. Maintains a set of registered readers,
15+ datapacks, and listeners with filterfuncs that allow the factory to apply
16+ a replay specific context to each replay as it loads.
17+
18+ #TODO: Include some examples here...
19+
20+ See the specific functions below for details.
21+
22+ :param True register_defaults: Automatically registers default readers
23+ and datapacks. Only disable if you know what you are doing.
24+
25+ :param True load_events: Enables parsing of game events. If you are do
26+ not need this information setting to false will reduce replay load
27+ time.
28+
29+ :param True autoplay: Enables auto playing of replays after loading game
30+ events. Playing events triggers enables registered listeners to add
31+ new data features to replays. Option ignored if load_events is false.
32+
33+ :param False load_map: Triggers downloading and parsing of map files
34+ associated with replays as they are loaded. When false, only the map
35+ url and name are available.
36+
37+ :param False verbose: Causes many steps in the replay loading process
38+ to produce more verbose output.
39+
40+ :param string directory: Specifies a base directory to prepend to all paths
41+ before attempting to load the replay.
42+
43+ :param -1 depth: Indicates the maximum search depth when loading replays
44+ from directories. -1 indicates no limit, 0 turns recursion off.
45+
46+ :param list exclude: A list of directory names (not paths) to exclude when
47+ performing recursive searches while loading replays from directories.
48+
49+ :param False followlinks: Enables symlink following when recursing through
50+ directories to load replay files.
51+
52+ """
1253
1354 default_options = dict (
1455 # General use
@@ -39,22 +80,58 @@ def __init__(self, **options):
3980
4081 self .configure (** options )
4182
83+ def configure (self , ** new_options ):
84+ """
85+ Update the factory settings with the specified overrides.
86+
87+ See :class:`SC2Reader` for a list of available options.
88+
89+ :param new_options: Option values to override current factory settings.
90+ """
91+ self .options .update (new_options )
92+
93+ def reset (self ):
94+ """
95+ Resets the current factory to default settings and removes all
96+ registered readers, datapacks, and listeners.
97+ """
98+ self .options = utils .AttributeDict (self .default_options )
99+ self .registered_readers = defaultdict (list )
100+ self .registered_datapacks = list ()
101+ self .registered_listeners = defaultdict (list )
102+
103+
104+ def load_replays (self , collection , options = None , ** new_options ):
105+ """
106+ Loads the specified collection of replays using the current factory
107+ settings with specified overrides.
42108
43- def load_replays (self , replay_collection , options = None , ** new_options ):
109+ :param collection: Either a directory path or a mixed collection of
110+ directories, file paths, and open file objects.
111+
112+ :param None options: When options are passed directly into the options
113+ parameter the current factory settings are ignored and only the
114+ specified options are used during replay load.
115+
116+ :param new_options: Options values to override current factory settings
117+ for the collection of replays to be loaded.
118+
119+ :rtype: generator(:class:`Replay`)
120+ """
44121 options = options or utils .merged_dict (self .options , new_options )
45122
46123 # Get the directory and hide it from nested calls
47124 directory = options .get ('directory' ,'' )
48125 if 'directory' in options : del options ['directory' ]
49126
50- if isinstance (replay_collection , basestring ):
51- full_path = os .path .join (directory , replay_collection )
127+ if isinstance (collection , basestring ):
128+ full_path = os .path .join (directory , collection )
52129 for replay_path in utils .get_replay_files (full_path , ** options ):
53130 with open (replay_path ) as replay_file :
54131 yield self .load_replay (replay_file , options = options )
55132
56133 else :
57- for replay_file in replay_collection :
134+ for replay_file in collection :
58135 if isinstance (replay_file , basestring ):
59136 full_path = os .path .join (directory , replay_file )
60137 if os .path .isdir (full_path ):
@@ -67,6 +144,21 @@ def load_replays(self, replay_collection, options=None, **new_options):
67144 yield self .load_replay (replay_file , options = options )
68145
69146 def load_replay (self , replay_file , options = None , ** new_options ):
147+ """
148+ Loads the specified replay using current factory settings with the
149+ specified overrides.
150+
151+ :param replay: An open file object or a path to a single file.
152+
153+ :param None options: When options are passed directly into the options
154+ parameter the current factory settings are ignored and only the
155+ specified options are used during replay load.
156+
157+ :param new_options: Options values to override current factory settings
158+ while loading this replay.
159+
160+ :rtype: :class:`Replay`
161+ """
70162 options = options or utils .merged_dict (self .options , new_options )
71163 load_events = options .get ('load_events' ,True )
72164 autoplay = options .get ('autoplay' ,True )
@@ -127,24 +219,81 @@ def get_listeners(self, replay):
127219
128220
129221 def register_listener (self , events , listener , filterfunc = lambda r : True ):
222+ """
223+ Allows you to specify event listeners for adding new features to the
224+ :class:`Replay` objects on :meth:`~Replay.play`. sc2reader comes with a
225+ small collection of :class:`Listener` classes that you can apply to your
226+ replays as needed.
227+
228+ Events are sent to listeners in registration order as they come up. By
229+ specifying a parent class you can register a listener to a set of events
230+ at once instead of listing them out individually. See the tutorials for
231+ more information.
232+
233+ :param events: A list of event classes you want sent to this listener.
234+ Registration to a single event can be done by specifying a single
235+ event class instead of a list. An isinstance() check is used so
236+ you can catch sets of classes at once by supplying a parent class.
237+
238+ :param listener: The :class:`Listener` object you want events sent to.
239+
240+ :param filterfunc: A function that accepts a partially loaded
241+ :class:`Replay` object as an argument and returns true if the
242+ reader should be used on this replay.
243+ """
130244 try :
131245 for event in events :
132246 self .registered_listeners [event ].append ((filterfunc , listener ))
133247 except TypeError :
134248 self .registered_listeners [event ].append ((filterfunc , listener ))
135249
136250 def register_reader (self , data_file , reader , filterfunc = lambda r : True ):
251+ """
252+ Allows you to specify your own reader for use when reading the data
253+ files packed into the .SC2Replay archives. Datapacks are checked for
254+ use with the supplied filterfunc in reverse registration order to give
255+ user registered datapacks preference over factory default datapacks.
256+
257+ Don't use this unless you know what you are doing.
258+
259+ :param data_file: The full file name that you would like this reader to
260+ parse.
261+
262+ :param reader: The :class:`Reader` object you wish to use to read the
263+ data file.
264+
265+ :param filterfunc: A function that accepts a partially loaded
266+ :class:`Replay` object as an argument and returns true if the
267+ reader should be used on this replay.
268+ """
137269 self .registered_readers [data_file ].insert (0 ,(filterfunc , reader ))
138270
139271 def register_datapack (self , datapack , filterfunc = lambda r : True ):
272+ """
273+ Allows you to specify your own datapacks for use when loading replays.
274+ Datapacks are checked for use with the supplied filterfunc in reverse
275+ registration order to give user registered datapacks preference over
276+ factory default datapacks.
277+
278+ This is how you would add mappings for your favorite custom map.
279+
280+ :param datapack: A :class:`BaseData` object to use for mapping unit
281+ types and ability codes to their corresponding classes.
282+
283+ :param filterfunc: A function that accepts a partially loaded
284+ :class:`Replay` object as an argument and returns true if the
285+ datapack should be used on this replay.
286+ """
140287 self .registered_datapacks .insert (0 ,(filterfunc , datapack ))
141288
142289
143290 def register_defaults (self ):
291+ """Registers all factory default objects."""
144292 self .register_default_readers ()
145293 self .register_default_datapacks ()
146294
147295 def register_default_readers (self ):
296+ """Registers factory default readers."""
148297 self .register_reader ('replay.details' , readers .DetailsReader_Base ())
149298 self .register_reader ('replay.initData' , readers .InitDataReader_Base ())
150299 self .register_reader ('replay.message.events' , readers .MessageEventsReader_Base ())
@@ -156,21 +305,12 @@ def register_default_readers(self):
156305 self .register_reader ('replay.game.events' , readers .GameEventsReader_19595 (), lambda r : 19595 <= r .build )
157306
158307 def register_default_datapacks (self ):
308+ """Registers factory default datapacks."""
159309 self .register_datapack (data .Data_16561 , lambda r : 16561 <= r .build < 17326 )
160310 self .register_datapack (data .Data_17326 , lambda r : 17326 <= r .build < 18317 )
161311 self .register_datapack (data .Data_18317 , lambda r : 18317 <= r .build < 19595 )
162312 self .register_datapack (data .Data_19595 , lambda r : 19595 <= r .build )
163313
164-
165- def configure (self , ** new_options ):
166- self .options .update (new_options )
167-
168- def reset (self ):
169- self .options = utils .AttributeDict (self .default_options )
170- self .registered_readers = defaultdict (list )
171- self .registered_datapacks = list ()
172- self .registered_listeners = defaultdict (list )
173-
174314__defaultSC2Reader = SC2Reader ()
175315
176316register_datapack = __defaultSC2Reader .register_datapack
0 commit comments