162162import argparse
163163import cPickle
164164import os
165- import gc
166165import shutil
167166import sys
168167import time
169168
170169import sc2reader
171170
171+
172172def run (args ):
173173 #Reset wipes the destination clean so we can start over.
174- if args .reset : reset (args )
174+ if args .reset :
175+ reset (args )
175176
176177 #Set up validates the destination and source directories.
177178 #It also loads the previous state or creates one as necessary.
@@ -186,12 +187,12 @@ def run(args):
186187 try :
187188 #Read the file and expose useful aspects for renaming/filtering
188189 replay = sc2reader .load_replay (path , load_level = 2 )
189- except KeyboardInterrupt as e :
190+ except KeyboardInterrupt :
190191 raise
191192 except :
192193 #Failure to parse
193194 file_name = os .path .basename (path )
194- directory = make_directory (args ,('parse_error' ,))
195+ directory = make_directory (args , ('parse_error' ,))
195196 new_path = os .path .join (directory , file_name )
196197 source_path = path [len (args .source ):]
197198 args .log .write ("Error parsing replay: {0}" .format (source_path ))
@@ -204,7 +205,8 @@ def run(args):
204205 aspects = generate_aspects (args , replay )
205206
206207 #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
208210
209211 #Apply the aspects to the rename formatting.
210212 #'/' is a special character for creation of subdirectories.
@@ -232,14 +234,16 @@ def run(args):
232234 save_state (state , args )
233235
234236 #We only run once in batch mode!
235- if args .mode == 'BATCH' : break
237+ if args .mode == 'BATCH' :
238+ break
236239
237240 #Since new replays come in fairly infrequently, reduce system load
238241 #by sleeping for an acceptable response time before the next scan.
239242 time .sleep (args .period )
240243
241244 args .log .write ('Batch Completed' )
242245
246+
243247def filter_out_replay (args , replay ):
244248 player_names = set ([player .name for player in replay .players ])
245249 filter_out_player = not set (args .filter_player ) & player_names
@@ -249,10 +253,12 @@ def filter_out_replay(args, replay):
249253 else :
250254 return not filter_out_player
251255
256+
252257# We need to create these compare functions at runtime because the ordering
253258# hinges on the --favored PLAYER options passed in from the command line.
254259def create_compare_funcs (args ):
255260 favored_set = set (name .lower () for name in args .favored )
261+
256262 def player_compare (player1 , player2 ):
257263 # Normalize the player names and generate our key metrics
258264 player1_name = player1 .name .lower ()
@@ -303,6 +309,7 @@ def team_compare(team1, team2):
303309
304310 return team_compare , player_compare
305311
312+
306313def generate_aspects (args , replay ):
307314 teams = sorted (replay .teams , args .team_compare )
308315 matchups , team_strings = list (), list ()
@@ -314,14 +321,15 @@ def generate_aspects(args, replay):
314321 team_strings .append (string )
315322
316323 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+
325333
326334def make_directory (args , path_parts ):
327335 directory = args .dest
@@ -336,6 +344,7 @@ def make_directory(args, path_parts):
336344
337345 return directory
338346
347+
339348def scan (args , state ):
340349 args .log .write ("SCANNING: {0}\n " .format (args .source ))
341350 files = sc2reader .utils .get_files (
@@ -347,8 +356,10 @@ def scan(args, state):
347356 followlinks = args .follow_links )
348357 return filter (lambda f : os .path .getctime (f ) > state .last_sync , files )
349358
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+
352363
353364def reset (args ):
354365 if not os .path .exists (args .dest ):
@@ -366,9 +377,10 @@ def reset(args):
366377 else :
367378 sys .exit ("Script Aborted" )
368379
380+
369381def setup (args ):
370382 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 )
372384 if not os .path .exists (args .source ):
373385 msg = 'Source does not exist: {0}.\n \n Script Aborted.'
374386 sys .exit (msg .format (args .source ))
@@ -384,7 +396,7 @@ def setup(args):
384396 elif not os .path .isdir (args .dest ):
385397 sys .exit ('Destination must be a directory.\n \n Script Aborted' )
386398
387- data_file = os .path .join (args .dest ,'sc2autosave.dat' )
399+ data_file = os .path .join (args .dest , 'sc2autosave.dat' )
388400
389401 args .log .write ('Loading state from file: {0}\n ' .format (data_file ))
390402 if os .path .isfile (data_file ) and not args .reset :
@@ -393,11 +405,12 @@ def setup(args):
393405 else :
394406 return sc2reader .utils .AttributeDict (last_sync = 0 )
395407
408+
396409def save_state (state , args ):
397410 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' )
399412 if not args .dryrun :
400- with open (data_file ,'w' ) as file :
413+ with open (data_file , 'w' ) as file :
401414 cPickle .dump (state , file )
402415 else :
403416 args .log .write ('Writing state to file: {0}\n ' .format (data_file ))
@@ -418,11 +431,11 @@ def main():
418431
419432 general = parser .add_argument_group ('General Options' )
420433 general .add_argument ('--mode' , dest = 'mode' ,
421- type = str , choices = ['BATCH' ,'CYCLE' ], default = 'BATCH' ,
434+ type = str , choices = ['BATCH' , 'CYCLE' ], default = 'BATCH' ,
422435 help = 'The operating mode for the organizer' )
423436
424437 general .add_argument ('--action' , dest = 'action' ,
425- choices = ['COPY' ,'MOVE' ], default = "COPY" , type = str ,
438+ choices = ['COPY' , 'MOVE' ], default = "COPY" , type = str ,
426439 help = 'Have the organizer move your files instead of copying' )
427440 general .add_argument ('--period' ,
428441 dest = 'period' , type = int , default = 0 ,
@@ -497,8 +510,8 @@ def main():
497510
498511 try :
499512 run (parser .parse_args ())
500- except KeyboardInterrupt as e :
513+ except KeyboardInterrupt :
501514 print "\n \n Script Interupted. Process Aborting"
502515
503516if __name__ == '__main__' :
504- main ()
517+ main ()
0 commit comments