diff --git a/examples/sc2autosave.py b/examples/sc2autosave.py index 3364b4a9..d2ae97c0 100755 --- a/examples/sc2autosave.py +++ b/examples/sc2autosave.py @@ -159,18 +159,71 @@ POST-Parse, how to do it?!?!?!?! """ import argparse -import cPickle import os +import pickle +import re import shutil import sys +import textwrap import time import sc2reader -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 + +class Formatter(argparse.RawTextHelpFormatter): + """FlexiFormatter which respects new line formatting and wraps the rest + + Example: + >>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter) + >>> parser.add_argument('a',help='''\ + ... This argument's help text will have this first long line\ + ... wrapped to fit the target window size so that your text\ + ... remains flexible. + ... + ... 1. This option list + ... 2. is still persisted + ... 3. and the option strings get wrapped like this\ + ... with an indent for readability. + ... + ... You must use backslashes at the end of lines to indicate that\ + ... you want the text to wrap instead of preserving the newline. + ... ''') + + Only the name of this class is considered a public API. All the methods + provided by the class are considered an implementation detail. + """ + + @classmethod + def new(cls, **options): + return lambda prog: Formatter(prog, **options) + + def _split_lines(self, text, width): + lines = list() + main_indent = len(re.match(r"( *)", text).group(1)) + # Wrap each line individually to allow for partial formatting + for line in text.splitlines(): + # Get this line's indent and figure out what indent to use + # if the line wraps. Account for lists of small variety. + indent = len(re.match(r"( *)", line).group(1)) + list_match = re.match(r"( *)(([*-+>]+|\w+\)|\w+\.) +)", line) + if list_match: + sub_indent = indent + len(list_match.group(2)) + else: + sub_indent = indent + + # Textwrap will do all the hard work for us + line = self._whitespace_matcher.sub(" ", line).strip() + new_lines = textwrap.wrap( + text=line, + width=width, + initial_indent=" " * (indent - main_indent), + subsequent_indent=" " * (sub_indent - main_indent), + ) + + # Blank lines get eaten by textwrap, put it back with [' '] + lines.extend(new_lines or [" "]) + + return lines def run(args): @@ -375,8 +428,7 @@ def reset(args): print( f"About to reset directory: {args.dest}\nAll files and subdirectories will be removed." ) - choice = raw_input("Proceed anyway? (y/n) ") - if choice.lower() == "y": + if input("Proceed anyway? (y/n) ").strip().lower() == "y": args.log.write(f"Removing old directory: {args.dest}\n") if not args.dryrun: print(args.dest) @@ -410,7 +462,7 @@ def setup(args): args.log.write(f"Loading state from file: {data_file}\n") if os.path.isfile(data_file) and not args.reset: with open(data_file) as file: - return cPickle.load(file) + return pickle.load(file) else: return sc2reader.utils.AttributeDict(last_sync=0) @@ -420,7 +472,7 @@ def save_state(state, args): data_file = os.path.join(args.dest, "sc2autosave.dat") if not args.dryrun: with open(data_file, "w") as file: - cPickle.dump(state, file) + pickle.dump(state, file) else: args.log.write(f"Writing state to file: {data_file}\n") @@ -429,7 +481,7 @@ def main(): parser = argparse.ArgumentParser( description="Automatically copy new replays to directory", fromfile_prefix_chars="@", - formatter_class=sc2reader.scripts.utils.Formatter.new(max_help_position=35), + formatter_class=Formatter.new(max_help_position=35), epilog="And that's all folks", ) diff --git a/examples/sc2store.py b/examples/sc2store.py index 77d0e71b..7f54a3fb 100755 --- a/examples/sc2store.py +++ b/examples/sc2store.py @@ -1,10 +1,10 @@ #!/usr/bin/env python -import cPickle import os +import pickle import shutil -import sys import sqlite3 +import sys import time import sc2reader diff --git a/sc2reader/__init__.py b/sc2reader/__init__.py index 415b270d..49b76657 100644 --- a/sc2reader/__init__.py +++ b/sc2reader/__init__.py @@ -1,21 +1,21 @@ """ - sc2reader - ~~~~~~~~~~~ +sc2reader +~~~~~~~~~~~ - A library for loading data from Starcraft II game resources. +A library for loading data from Starcraft II game resources. - SC2Factory methods called on the package will be delegated to the default - SC2Factory. To default to a cached factory set one or more of the following - variables in your environment: +SC2Factory methods called on the package will be delegated to the default +SC2Factory. To default to a cached factory set one or more of the following +variables in your environment: - SC2READER_CACHE_DIR = '/absolute/path/to/existing/cache/directory/' - SC2READER_CACHE_MAX_SIZE = MAXIMUM_CACHE_ENTRIES_TO_HOLD_IN_MEMORY + SC2READER_CACHE_DIR = '/absolute/path/to/existing/cache/directory/' + SC2READER_CACHE_MAX_SIZE = MAXIMUM_CACHE_ENTRIES_TO_HOLD_IN_MEMORY - You can also set the default factory via setFactory, useFileCache, useDictCache, - or useDoubleCache functions. +You can also set the default factory via setFactory, useFileCache, useDictCache, +or useDoubleCache functions. - :copyright: (c) 2011 by Graylin Kim. - :license: MIT, see LICENSE for more details. +:copyright: (c) 2011 by Graylin Kim. +:license: MIT, see LICENSE for more details. """ __version__ = "1.8.0" diff --git a/sc2reader/readers.py b/sc2reader/readers.py index c74c7c89..eab6e4c9 100644 --- a/sc2reader/readers.py +++ b/sc2reader/readers.py @@ -1857,7 +1857,7 @@ def __init__(self): 25: ( None, self.command_manager_reset_event, - ), # Re-using this old number + ), # Reusing this old number 61: (None, self.trigger_hotkey_pressed_event), 103: (CommandManagerStateEvent, self.command_manager_state_event), 104: ( diff --git a/sc2reader/scripts/utils.py b/sc2reader/scripts/utils.py deleted file mode 100644 index 1f87baa4..00000000 --- a/sc2reader/scripts/utils.py +++ /dev/null @@ -1,59 +0,0 @@ -import argparse -import re -import textwrap - - -class Formatter(argparse.RawTextHelpFormatter): - """FlexiFormatter which respects new line formatting and wraps the rest - - Example: - >>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter) - >>> parser.add_argument('a',help='''\ - ... This argument's help text will have this first long line\ - ... wrapped to fit the target window size so that your text\ - ... remains flexible. - ... - ... 1. This option list - ... 2. is still persisted - ... 3. and the option strings get wrapped like this\ - ... with an indent for readability. - ... - ... You must use backslashes at the end of lines to indicate that\ - ... you want the text to wrap instead of preserving the newline. - ... ''') - - Only the name of this class is considered a public API. All the methods - provided by the class are considered an implementation detail. - """ - - @classmethod - def new(cls, **options): - return lambda prog: Formatter(prog, **options) - - def _split_lines(self, text, width): - lines = list() - main_indent = len(re.match(r"( *)", text).group(1)) - # Wrap each line individually to allow for partial formatting - for line in text.splitlines(): - # Get this line's indent and figure out what indent to use - # if the line wraps. Account for lists of small variety. - indent = len(re.match(r"( *)", line).group(1)) - list_match = re.match(r"( *)(([*-+>]+|\w+\)|\w+\.) +)", line) - if list_match: - sub_indent = indent + len(list_match.group(2)) - else: - sub_indent = indent - - # Textwrap will do all the hard work for us - line = self._whitespace_matcher.sub(" ", line).strip() - new_lines = textwrap.wrap( - text=line, - width=width, - initial_indent=" " * (indent - main_indent), - subsequent_indent=" " * (sub_indent - main_indent), - ) - - # Blank lines get eaten by textwrap, put it back with [' '] - lines.extend(new_lines or [" "]) - - return lines diff --git a/sc2reader/utils.py b/sc2reader/utils.py index 7b638d3f..660f07bd 100644 --- a/sc2reader/utils.py +++ b/sc2reader/utils.py @@ -1,11 +1,11 @@ import binascii -import os import json +import os from datetime import timedelta, datetime -from sc2reader.log_utils import loggable -from sc2reader.exceptions import MPQError from sc2reader.constants import COLOR_CODES, COLOR_CODES_INV +from sc2reader.exceptions import MPQError +from sc2reader.log_utils import loggable class DepotFile: