162
162
import argparse
163
163
import cPickle
164
164
import os
165
- import gc
166
165
import shutil
167
166
import sys
168
167
import time
169
168
170
169
import sc2reader
171
170
171
+
172
172
def run (args ):
173
173
#Reset wipes the destination clean so we can start over.
174
- if args .reset : reset (args )
174
+ if args .reset :
175
+ reset (args )
175
176
176
177
#Set up validates the destination and source directories.
177
178
#It also loads the previous state or creates one as necessary.
@@ -186,12 +187,12 @@ def run(args):
186
187
try :
187
188
#Read the file and expose useful aspects for renaming/filtering
188
189
replay = sc2reader .load_replay (path , load_level = 2 )
189
- except KeyboardInterrupt as e :
190
+ except KeyboardInterrupt :
190
191
raise
191
192
except :
192
193
#Failure to parse
193
194
file_name = os .path .basename (path )
194
- directory = make_directory (args ,('parse_error' ,))
195
+ directory = make_directory (args , ('parse_error' ,))
195
196
new_path = os .path .join (directory , file_name )
196
197
source_path = path [len (args .source ):]
197
198
args .log .write ("Error parsing replay: {0}" .format (source_path ))
@@ -204,7 +205,8 @@ def run(args):
204
205
aspects = generate_aspects (args , replay )
205
206
206
207
#Use the filter args to select files based on replay attributes
207
- if filter_out_replay (args , replay ): continue
208
+ if filter_out_replay (args , replay ):
209
+ continue
208
210
209
211
#Apply the aspects to the rename formatting.
210
212
#'/' is a special character for creation of subdirectories.
@@ -232,14 +234,16 @@ def run(args):
232
234
save_state (state , args )
233
235
234
236
#We only run once in batch mode!
235
- if args .mode == 'BATCH' : break
237
+ if args .mode == 'BATCH' :
238
+ break
236
239
237
240
#Since new replays come in fairly infrequently, reduce system load
238
241
#by sleeping for an acceptable response time before the next scan.
239
242
time .sleep (args .period )
240
243
241
244
args .log .write ('Batch Completed' )
242
245
246
+
243
247
def filter_out_replay (args , replay ):
244
248
player_names = set ([player .name for player in replay .players ])
245
249
filter_out_player = not set (args .filter_player ) & player_names
@@ -249,10 +253,12 @@ def filter_out_replay(args, replay):
249
253
else :
250
254
return not filter_out_player
251
255
256
+
252
257
# We need to create these compare functions at runtime because the ordering
253
258
# hinges on the --favored PLAYER options passed in from the command line.
254
259
def create_compare_funcs (args ):
255
260
favored_set = set (name .lower () for name in args .favored )
261
+
256
262
def player_compare (player1 , player2 ):
257
263
# Normalize the player names and generate our key metrics
258
264
player1_name = player1 .name .lower ()
@@ -303,6 +309,7 @@ def team_compare(team1, team2):
303
309
304
310
return team_compare , player_compare
305
311
312
+
306
313
def generate_aspects (args , replay ):
307
314
teams = sorted (replay .teams , args .team_compare )
308
315
matchups , team_strings = list (), list ()
@@ -314,14 +321,15 @@ def generate_aspects(args, replay):
314
321
team_strings .append (string )
315
322
316
323
return sc2reader .utils .AttributeDict (
317
- result = teams [0 ].result ,
318
- length = replay .length ,
319
- map = replay .map ,
320
- type = replay .type ,
321
- date = replay .date .strftime (args .date_format ),
322
- matchup = 'v' .join (matchups ),
323
- teams = ' vs ' .join (team_strings )
324
- )
324
+ result = teams [0 ].result ,
325
+ length = replay .length ,
326
+ map = replay .map ,
327
+ type = replay .type ,
328
+ date = replay .date .strftime (args .date_format ),
329
+ matchup = 'v' .join (matchups ),
330
+ teams = ' vs ' .join (team_strings )
331
+ )
332
+
325
333
326
334
def make_directory (args , path_parts ):
327
335
directory = args .dest
@@ -336,6 +344,7 @@ def make_directory(args, path_parts):
336
344
337
345
return directory
338
346
347
+
339
348
def scan (args , state ):
340
349
args .log .write ("SCANNING: {0}\n " .format (args .source ))
341
350
files = sc2reader .utils .get_files (
@@ -347,8 +356,10 @@ def scan(args, state):
347
356
followlinks = args .follow_links )
348
357
return filter (lambda f : os .path .getctime (f ) > state .last_sync , files )
349
358
350
- def exit (msg ,* args ,** kwargs ):
351
- sys .exit (msg .format (* args ,** kwargs )+ "\n \n Script Aborted." )
359
+
360
+ def exit (msg , * args , ** kwargs ):
361
+ sys .exit (msg .format (* args , ** kwargs )+ "\n \n Script Aborted." )
362
+
352
363
353
364
def reset (args ):
354
365
if not os .path .exists (args .dest ):
@@ -366,9 +377,10 @@ def reset(args):
366
377
else :
367
378
sys .exit ("Script Aborted" )
368
379
380
+
369
381
def setup (args ):
370
382
args .team_compare , args .player_compare = create_compare_funcs (args )
371
- args .action = sc2reader .utils .AttributeDict ( type = args .action , run = shutil .copy if args .action == 'COPY' else shutil .move )
383
+ args .action = sc2reader .utils .AttributeDict (type = args .action , run = shutil .copy if args .action == 'COPY' else shutil .move )
372
384
if not os .path .exists (args .source ):
373
385
msg = 'Source does not exist: {0}.\n \n Script Aborted.'
374
386
sys .exit (msg .format (args .source ))
@@ -384,7 +396,7 @@ def setup(args):
384
396
elif not os .path .isdir (args .dest ):
385
397
sys .exit ('Destination must be a directory.\n \n Script Aborted' )
386
398
387
- data_file = os .path .join (args .dest ,'sc2autosave.dat' )
399
+ data_file = os .path .join (args .dest , 'sc2autosave.dat' )
388
400
389
401
args .log .write ('Loading state from file: {0}\n ' .format (data_file ))
390
402
if os .path .isfile (data_file ) and not args .reset :
@@ -393,11 +405,12 @@ def setup(args):
393
405
else :
394
406
return sc2reader .utils .AttributeDict (last_sync = 0 )
395
407
408
+
396
409
def save_state (state , args ):
397
410
state .last_sync = time .time ()
398
- data_file = os .path .join (args .dest ,'sc2autosave.dat' )
411
+ data_file = os .path .join (args .dest , 'sc2autosave.dat' )
399
412
if not args .dryrun :
400
- with open (data_file ,'w' ) as file :
413
+ with open (data_file , 'w' ) as file :
401
414
cPickle .dump (state , file )
402
415
else :
403
416
args .log .write ('Writing state to file: {0}\n ' .format (data_file ))
@@ -418,11 +431,11 @@ def main():
418
431
419
432
general = parser .add_argument_group ('General Options' )
420
433
general .add_argument ('--mode' , dest = 'mode' ,
421
- type = str , choices = ['BATCH' ,'CYCLE' ], default = 'BATCH' ,
434
+ type = str , choices = ['BATCH' , 'CYCLE' ], default = 'BATCH' ,
422
435
help = 'The operating mode for the organizer' )
423
436
424
437
general .add_argument ('--action' , dest = 'action' ,
425
- choices = ['COPY' ,'MOVE' ], default = "COPY" , type = str ,
438
+ choices = ['COPY' , 'MOVE' ], default = "COPY" , type = str ,
426
439
help = 'Have the organizer move your files instead of copying' )
427
440
general .add_argument ('--period' ,
428
441
dest = 'period' , type = int , default = 0 ,
@@ -497,8 +510,8 @@ def main():
497
510
498
511
try :
499
512
run (parser .parse_args ())
500
- except KeyboardInterrupt as e :
513
+ except KeyboardInterrupt :
501
514
print "\n \n Script Interupted. Process Aborting"
502
515
503
516
if __name__ == '__main__' :
504
- main ()
517
+ main ()
0 commit comments