Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions examples/sc2autosave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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",
)

Expand Down
4 changes: 2 additions & 2 deletions examples/sc2store.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 12 additions & 12 deletions sc2reader/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion sc2reader/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down
59 changes: 0 additions & 59 deletions sc2reader/scripts/utils.py

This file was deleted.

6 changes: 3 additions & 3 deletions sc2reader/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down