Skip to content

Commit b3c1b78

Browse files
committed
Move the argparse formatter to the scripts/utils.py module.
1 parent 59ffd9a commit b3c1b78

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

sc2reader/scripts/sc2autosave.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def main():
407407
parser = argparse.ArgumentParser(
408408
description='Automatically copy new replays to directory',
409409
fromfile_prefix_chars='@',
410-
formatter_class=sc2reader.utils.Formatter.new(max_help_position=35),
410+
formatter_class=sc2reader.scripts.utils.Formatter.new(max_help_position=35),
411411
epilog="And that's all folks")
412412

413413
required = parser.add_argument_group('Required Arguments')

sc2reader/scripts/utils.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import argparse
2+
3+
class Formatter(argparse.RawTextHelpFormatter):
4+
"""FlexiFormatter which respects new line formatting and wraps the rest
5+
6+
Example:
7+
>>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter)
8+
>>> parser.add_argument('a',help='''\
9+
... This argument's help text will have this first long line\
10+
... wrapped to fit the target window size so that your text\
11+
... remains flexible.
12+
...
13+
... 1. This option list
14+
... 2. is still persisted
15+
... 3. and the option strings get wrapped like this\
16+
... with an indent for readability.
17+
...
18+
... You must use backslashes at the end of lines to indicate that\
19+
... you want the text to wrap instead of preserving the newline.
20+
... ''')
21+
22+
Only the name of this class is considered a public API. All the methods
23+
provided by the class are considered an implementation detail.
24+
"""
25+
26+
@classmethod
27+
def new(cls, **options):
28+
return lambda prog: Formatter(prog, **options)
29+
30+
def _split_lines(self, text, width):
31+
lines = list()
32+
main_indent = len(re.match(r'( *)',text).group(1))
33+
# Wrap each line individually to allow for partial formatting
34+
for line in text.splitlines():
35+
36+
# Get this line's indent and figure out what indent to use
37+
# if the line wraps. Account for lists of small variety.
38+
indent = len(re.match(r'( *)',line).group(1))
39+
list_match = re.match(r'( *)(([*-+>]+|\w+\)|\w+\.) +)',line)
40+
if(list_match):
41+
sub_indent = indent + len(list_match.group(2))
42+
else:
43+
sub_indent = indent
44+
45+
# Textwrap will do all the hard work for us
46+
line = self._whitespace_matcher.sub(' ', line).strip()
47+
new_lines = textwrap.wrap(
48+
text=line,
49+
width=width,
50+
initial_indent=' '*(indent-main_indent),
51+
subsequent_indent=' '*(sub_indent-main_indent),
52+
)
53+
54+
# Blank lines get eaten by textwrap, put it back with [' ']
55+
lines.extend(new_lines or [' '])
56+
57+
return lines

sc2reader/utils.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import absolute_import
22

3-
import argparse
43
import cStringIO
54
import fnmatch
65
import os
@@ -693,60 +692,3 @@ def __str__(self):
693692
return "{0:0>2}.{1:0>2}.{2:0>2}".format(self.hours,self.mins,self.secs)
694693
else:
695694
return "{0:0>2}.{1:0>2}".format(self.mins,self.secs)
696-
697-
698-
class Formatter(argparse.RawTextHelpFormatter):
699-
"""FlexiFormatter which respects new line formatting and wraps the rest
700-
701-
Example:
702-
>>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter)
703-
>>> parser.add_argument('a',help='''\
704-
... This argument's help text will have this first long line\
705-
... wrapped to fit the target window size so that your text\
706-
... remains flexible.
707-
...
708-
... 1. This option list
709-
... 2. is still persisted
710-
... 3. and the option strings get wrapped like this\
711-
... with an indent for readability.
712-
...
713-
... You must use backslashes at the end of lines to indicate that\
714-
... you want the text to wrap instead of preserving the newline.
715-
... ''')
716-
717-
Only the name of this class is considered a public API. All the methods
718-
provided by the class are considered an implementation detail.
719-
"""
720-
721-
@classmethod
722-
def new(cls, **options):
723-
return lambda prog: Formatter(prog, **options)
724-
725-
def _split_lines(self, text, width):
726-
lines = list()
727-
main_indent = len(re.match(r'( *)',text).group(1))
728-
# Wrap each line individually to allow for partial formatting
729-
for line in text.splitlines():
730-
731-
# Get this line's indent and figure out what indent to use
732-
# if the line wraps. Account for lists of small variety.
733-
indent = len(re.match(r'( *)',line).group(1))
734-
list_match = re.match(r'( *)(([*-+>]+|\w+\)|\w+\.) +)',line)
735-
if(list_match):
736-
sub_indent = indent + len(list_match.group(2))
737-
else:
738-
sub_indent = indent
739-
740-
# Textwrap will do all the hard work for us
741-
line = self._whitespace_matcher.sub(' ', line).strip()
742-
new_lines = textwrap.wrap(
743-
text=line,
744-
width=width,
745-
initial_indent=' '*(indent-main_indent),
746-
subsequent_indent=' '*(sub_indent-main_indent),
747-
)
748-
749-
# Blank lines get eaten by textwrap, put it back with [' ']
750-
lines.extend(new_lines or [' '])
751-
752-
return lines

0 commit comments

Comments
 (0)