Skip to content

Commit 609e1df

Browse files
authored
Merge pull request #209 from cclauss/fix_examples
Fix examples/sc2autosave.py my moving sc2reader.scripts.utils.Formatter
2 parents ba8b52e + 7b8dbf7 commit 609e1df

File tree

6 files changed

+80
-87
lines changed

6 files changed

+80
-87
lines changed

examples/sc2autosave.py

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,71 @@
159159
POST-Parse, how to do it?!?!?!?!
160160
"""
161161
import argparse
162-
import cPickle
163162
import os
163+
import pickle
164+
import re
164165
import shutil
165166
import sys
167+
import textwrap
166168
import time
167169

168170
import 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

176229
def run(args):
@@ -375,8 +428,7 @@ def reset(args):
375428
print(
376429
f"About to reset directory: {args.dest}\nAll 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

examples/sc2store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python
22

3-
import cPickle
43
import os
4+
import pickle
55
import shutil
6-
import sys
76
import sqlite3
7+
import sys
88
import time
99

1010
import sc2reader

sc2reader/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
"""
2-
sc2reader
3-
~~~~~~~~~~~
2+
sc2reader
3+
~~~~~~~~~~~
44
5-
A library for loading data from Starcraft II game resources.
5+
A library for loading data from Starcraft II game resources.
66
7-
SC2Factory methods called on the package will be delegated to the default
8-
SC2Factory. To default to a cached factory set one or more of the following
9-
variables in your environment:
7+
SC2Factory methods called on the package will be delegated to the default
8+
SC2Factory. To default to a cached factory set one or more of the following
9+
variables in your environment:
1010
11-
SC2READER_CACHE_DIR = '/absolute/path/to/existing/cache/directory/'
12-
SC2READER_CACHE_MAX_SIZE = MAXIMUM_CACHE_ENTRIES_TO_HOLD_IN_MEMORY
11+
SC2READER_CACHE_DIR = '/absolute/path/to/existing/cache/directory/'
12+
SC2READER_CACHE_MAX_SIZE = MAXIMUM_CACHE_ENTRIES_TO_HOLD_IN_MEMORY
1313
14-
You can also set the default factory via setFactory, useFileCache, useDictCache,
15-
or useDoubleCache functions.
14+
You can also set the default factory via setFactory, useFileCache, useDictCache,
15+
or useDoubleCache functions.
1616
17-
:copyright: (c) 2011 by Graylin Kim.
18-
:license: MIT, see LICENSE for more details.
17+
:copyright: (c) 2011 by Graylin Kim.
18+
:license: MIT, see LICENSE for more details.
1919
"""
2020

2121
__version__ = "1.8.0"

sc2reader/readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ def __init__(self):
18571857
25: (
18581858
None,
18591859
self.command_manager_reset_event,
1860-
), # Re-using this old number
1860+
), # Reusing this old number
18611861
61: (None, self.trigger_hotkey_pressed_event),
18621862
103: (CommandManagerStateEvent, self.command_manager_state_event),
18631863
104: (

sc2reader/scripts/utils.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

sc2reader/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import binascii
2-
import os
32
import json
3+
import os
44
from datetime import timedelta, datetime
55

6-
from sc2reader.log_utils import loggable
7-
from sc2reader.exceptions import MPQError
86
from sc2reader.constants import COLOR_CODES, COLOR_CODES_INV
7+
from sc2reader.exceptions import MPQError
8+
from sc2reader.log_utils import loggable
99

1010

1111
class DepotFile:

0 commit comments

Comments
 (0)