159159POST-Parse, how to do it?!?!?!?!
160160"""
161161import argparse
162- import cPickle
163162import os
163+ import pickle
164+ import re
164165import shutil
165166import sys
167+ import textwrap
166168import time
167169
168170import sc2reader
169171
170- try :
171- raw_input # Python 2
172- except NameError :
173- raw_input = input # Python 3
172+
173+ class Formatter (argparse .RawTextHelpFormatter ):
174+ """FlexiFormatter which respects new line formatting and wraps the rest
175+
176+ Example:
177+ >>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter)
178+ >>> parser.add_argument('a',help='''\
179+ ... This argument's help text will have this first long line\
180+ ... wrapped to fit the target window size so that your text\
181+ ... remains flexible.
182+ ...
183+ ... 1. This option list
184+ ... 2. is still persisted
185+ ... 3. and the option strings get wrapped like this\
186+ ... with an indent for readability.
187+ ...
188+ ... You must use backslashes at the end of lines to indicate that\
189+ ... you want the text to wrap instead of preserving the newline.
190+ ... ''')
191+
192+ Only the name of this class is considered a public API. All the methods
193+ provided by the class are considered an implementation detail.
194+ """
195+
196+ @classmethod
197+ def new (cls , ** options ):
198+ return lambda prog : Formatter (prog , ** options )
199+
200+ def _split_lines (self , text , width ):
201+ lines = list ()
202+ main_indent = len (re .match (r"( *)" , text ).group (1 ))
203+ # Wrap each line individually to allow for partial formatting
204+ for line in text .splitlines ():
205+ # Get this line's indent and figure out what indent to use
206+ # if the line wraps. Account for lists of small variety.
207+ indent = len (re .match (r"( *)" , line ).group (1 ))
208+ list_match = re .match (r"( *)(([*-+>]+|\w+\)|\w+\.) +)" , line )
209+ if list_match :
210+ sub_indent = indent + len (list_match .group (2 ))
211+ else :
212+ sub_indent = indent
213+
214+ # Textwrap will do all the hard work for us
215+ line = self ._whitespace_matcher .sub (" " , line ).strip ()
216+ new_lines = textwrap .wrap (
217+ text = line ,
218+ width = width ,
219+ initial_indent = " " * (indent - main_indent ),
220+ subsequent_indent = " " * (sub_indent - main_indent ),
221+ )
222+
223+ # Blank lines get eaten by textwrap, put it back with [' ']
224+ lines .extend (new_lines or [" " ])
225+
226+ return lines
174227
175228
176229def run (args ):
@@ -375,8 +428,7 @@ def reset(args):
375428 print (
376429 f"About to reset directory: { args .dest } \n All files and subdirectories will be removed."
377430 )
378- choice = raw_input ("Proceed anyway? (y/n) " )
379- if choice .lower () == "y" :
431+ if input ("Proceed anyway? (y/n) " ).strip ().lower () == "y" :
380432 args .log .write (f"Removing old directory: { args .dest } \n " )
381433 if not args .dryrun :
382434 print (args .dest )
@@ -410,7 +462,7 @@ def setup(args):
410462 args .log .write (f"Loading state from file: { data_file } \n " )
411463 if os .path .isfile (data_file ) and not args .reset :
412464 with open (data_file ) as file :
413- return cPickle .load (file )
465+ return pickle .load (file )
414466 else :
415467 return sc2reader .utils .AttributeDict (last_sync = 0 )
416468
@@ -420,7 +472,7 @@ def save_state(state, args):
420472 data_file = os .path .join (args .dest , "sc2autosave.dat" )
421473 if not args .dryrun :
422474 with open (data_file , "w" ) as file :
423- cPickle .dump (state , file )
475+ pickle .dump (state , file )
424476 else :
425477 args .log .write (f"Writing state to file: { data_file } \n " )
426478
@@ -429,7 +481,7 @@ def main():
429481 parser = argparse .ArgumentParser (
430482 description = "Automatically copy new replays to directory" ,
431483 fromfile_prefix_chars = "@" ,
432- formatter_class = sc2reader . scripts . utils . Formatter .new (max_help_position = 35 ),
484+ formatter_class = Formatter .new (max_help_position = 35 ),
433485 epilog = "And that's all folks" ,
434486 )
435487
0 commit comments