159
159
POST-Parse, how to do it?!?!?!?!
160
160
"""
161
161
import argparse
162
- import cPickle
163
162
import os
163
+ import pickle
164
+ import re
164
165
import shutil
165
166
import sys
167
+ import textwrap
166
168
import time
167
169
168
170
import sc2reader
169
171
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
174
227
175
228
176
229
def run (args ):
@@ -375,8 +428,7 @@ def reset(args):
375
428
print (
376
429
f"About to reset directory: { args .dest } \n All files and subdirectories will be removed."
377
430
)
378
- choice = raw_input ("Proceed anyway? (y/n) " )
379
- if choice .lower () == "y" :
431
+ if input ("Proceed anyway? (y/n) " ).strip ().lower () == "y" :
380
432
args .log .write (f"Removing old directory: { args .dest } \n " )
381
433
if not args .dryrun :
382
434
print (args .dest )
@@ -410,7 +462,7 @@ def setup(args):
410
462
args .log .write (f"Loading state from file: { data_file } \n " )
411
463
if os .path .isfile (data_file ) and not args .reset :
412
464
with open (data_file ) as file :
413
- return cPickle .load (file )
465
+ return pickle .load (file )
414
466
else :
415
467
return sc2reader .utils .AttributeDict (last_sync = 0 )
416
468
@@ -420,7 +472,7 @@ def save_state(state, args):
420
472
data_file = os .path .join (args .dest , "sc2autosave.dat" )
421
473
if not args .dryrun :
422
474
with open (data_file , "w" ) as file :
423
- cPickle .dump (state , file )
475
+ pickle .dump (state , file )
424
476
else :
425
477
args .log .write (f"Writing state to file: { data_file } \n " )
426
478
@@ -429,7 +481,7 @@ def main():
429
481
parser = argparse .ArgumentParser (
430
482
description = "Automatically copy new replays to directory" ,
431
483
fromfile_prefix_chars = "@" ,
432
- formatter_class = sc2reader . scripts . utils . Formatter .new (max_help_position = 35 ),
484
+ formatter_class = Formatter .new (max_help_position = 35 ),
433
485
epilog = "And that's all folks" ,
434
486
)
435
487
0 commit comments