<commit_before>import io
import os
import shlex
import sys
import tempfile
import unittest

from pycoin.cmds import ku, msg, tx


DEFAULT_ENV = {
    "PYCOIN_BTC_PROVIDERS": "blockr.io blockchain.info blockexplorer.com",
    "PATH": os.environ.get("PATH")
}


TOOL_LOOKUP = {
    "tx" : (tx.create_parser(), tx.tx),
    "ku" : (ku.create_parser(), ku.ku),
    "msg" : (msg.create_parser(), msg.msg)
}



class ToolTest(unittest.TestCase):

    def invoke_tool(self, args):
        tool_name = args[0]
        parser, main = TOOL_LOOKUP[tool_name]
        return main(parser.parse_args(args[1:]), parser)

    def launch_tool(self, cmd_line=None, args=None, env=None):
        if args is None:
            args = shlex.split(cmd_line)

        new_environ = dict(env or {})
        new_environ.update(DEFAULT_ENV)

        # capture io
        new_stdout = io.StringIO()
        old_stdout = sys.stdout
        old_environ = os.environ

        os.environ = new_environ
        sys.stdout = new_stdout

        self.invoke_tool(args)

        sys.stdout = old_stdout
        os.environ = old_environ
        output = new_stdout.getvalue()
        return output

    def set_cache_dir(self):
        temp_dir = tempfile.mkdtemp()
        return temp_dir
<commit_msg>Get tests working with Python 2.7.<commit_after>import os
import shlex
import sys
import tempfile
import unittest

try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

from pycoin.cmds import ku, msg, tx


DEFAULT_ENV = {
    "PYCOIN_BTC_PROVIDERS": "blockr.io blockchain.info blockexplorer.com",
    "PATH": os.environ.get("PATH")
}


TOOL_LOOKUP = {
    "tx" : (tx.create_parser(), tx.tx),
    "ku" : (ku.create_parser(), ku.ku),
    "msg" : (msg.create_parser(), msg.msg)
}



class ToolTest(unittest.TestCase):

    def invoke_tool(self, args):
        tool_name = args[0]
        parser, main = TOOL_LOOKUP[tool_name]
        return main(parser.parse_args(args[1:]), parser)

    def launch_tool(self, cmd_line=None, args=None, env=None):
        if args is None:
            args = shlex.split(cmd_line)

        new_environ = dict(env or {})
        new_environ.update(DEFAULT_ENV)

        # capture io
        new_stdout = StringIO()
        old_stdout = sys.stdout
        old_environ = os.environ

        os.environ = new_environ
        sys.stdout = new_stdout

        self.invoke_tool(args)

        sys.stdout = old_stdout
        os.environ = old_environ
        output = new_stdout.getvalue()
        return output

    def set_cache_dir(self):
        temp_dir = tempfile.mkdtemp()
        return temp_dir

{'fraction_non_alphanumeric': 0.10704761904761904, 'fraction_numerical': 0.002285714285714286, 'mean_word_length': 2.6021947873799727, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2813199', 'n_tokens_mistral': 893, 'n_tokens_neox': 877, 'n_words': 194}