# Path: pysc2/lib/stopwatch.py
# class Stat(object):
# class StopWatchContext(object):
# class TracingStopWatchContext(StopWatchContext):
# class FakeStopWatchContext(object):
# class StopWatch(object):
#   def __init__(self):
#   def reset(self):
#   def add(self, val):
#   def avg(self):
#   def dev(self):
#   def merge(self, other):
#   def build(summation, average, standard_deviation, minimum, maximum, number):
#   def parse(s):
#   def __str__(self):
#   def __init__(self, stopwatch, name):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __enter__(self):
#   def __exit__(self, *args, **kwargs):
#   def _log(self, s):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __init__(self, enabled=True, trace=False):
#   def disable(self):
#   def enable(self):
#   def trace(self):
#   def custom(self, factory):
#   def __call__(self, name):
#   def decorate(self, name_or_func):
#     def decorator(name, func):
#       def _stopwatch(*args, **kwargs):
#   def push(self, name):
#   def pop(self):
#   def cur_stack(self):
#   def clear(self):
#   def add(self, name, duration):
#   def __getitem__(self, name):
#   def times(self):
#   def merge(self, other):
#   def parse(s):
#   def str(self, threshold=0.1):
#   def __str__(self):


import contextlib
import itertools
import os
import socket
import sys
import time
import enum
import websocket
from absl import logging
from absl import flags
from pysc2.lib import stopwatch
from s2clientprotocol import sc2api_pb2 as sc_pb
    assert len(kwargs) == 1, "Must make a single request."
    name = list(kwargs.keys())[0]
    req = sc_pb.Request(**kwargs)
    req.id = next(self._count)
    try:
      res = self.send_req(req)
    except ConnectionError as e:
      raise ConnectionError("Error during %s: %s" % (name, e))
    if res.HasField("id") and res.id != req.id:
      raise ConnectionError(
          "Error during %s: Got a response with a different id" % name)
    return getattr(res, name)

  def _packet_str(self, packet):
    """Return a string form of this packet."""
    max_lines = FLAGS.sc2_verbose_protocol
    packet_str = str(packet).strip()
    if max_lines <= 0:
      return packet_str
    lines = packet_str.split("\n")
    line_count = len(lines)
    lines = [line[:MAX_WIDTH] for line in lines[:max_lines + 1]]
    if line_count > max_lines + 1:  # +1 to prefer the last line to skipped msg.
      lines[-1] = "***** %s lines skipped *****" % (line_count - max_lines)
    return "\n".join(lines)

  def _log(self, s, *args):
    r"""Log a string. It flushes but doesn't append \n, so do that yourself."""
    # TODO(tewalds): Should this be using logging.info instead? How to see them
    # outside of google infrastructure?

# Path: pysc2/lib/stopwatch.py
# class Stat(object):
# class StopWatchContext(object):
# class TracingStopWatchContext(StopWatchContext):
# class FakeStopWatchContext(object):
# class StopWatch(object):
#   def __init__(self):
#   def reset(self):
#   def add(self, val):
#   def avg(self):
#   def dev(self):
#   def merge(self, other):
#   def build(summation, average, standard_deviation, minimum, maximum, number):
#   def parse(s):
#   def __str__(self):
#   def __init__(self, stopwatch, name):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __enter__(self):
#   def __exit__(self, *args, **kwargs):
#   def _log(self, s):
#   def __enter__(self):
#   def __exit__(self, unused_exception_type, unused_exc_value, unused_traceback):
#   def __init__(self, enabled=True, trace=False):
#   def disable(self):
#   def enable(self):
#   def trace(self):
#   def custom(self, factory):
#   def __call__(self, name):
#   def decorate(self, name_or_func):
#     def decorator(name, func):
#       def _stopwatch(*args, **kwargs):
#   def push(self, name):
#   def pop(self):
#   def cur_stack(self):
#   def clear(self):
#   def add(self, name, duration):
#   def __getitem__(self, name):
#   def times(self):
#   def merge(self, other):
#   def parse(s):
#   def str(self, threshold=0.1):
#   def __str__(self):


# Path: pysc2/lib/protocol.py
import contextlib
import itertools
import os
import socket
import sys
import time
import enum
import websocket
from absl import logging
from absl import flags
from pysc2.lib import stopwatch
from s2clientprotocol import sc2api_pb2 as sc_pb

    assert len(kwargs) == 1, "Must make a single request."
    name = list(kwargs.keys())[0]
    req = sc_pb.Request(**kwargs)
    req.id = next(self._count)
    try:
      res = self.send_req(req)
    except ConnectionError as e:
      raise ConnectionError("Error during %s: %s" % (name, e))
    if res.HasField("id") and res.id != req.id:
      raise ConnectionError(
          "Error during %s: Got a response with a different id" % name)
    return getattr(res, name)

  def _packet_str(self, packet):
    """Return a string form of this packet."""
    max_lines = FLAGS.sc2_verbose_protocol
    packet_str = str(packet).strip()
    if max_lines <= 0:
      return packet_str
    lines = packet_str.split("\n")
    line_count = len(lines)
    lines = [line[:MAX_WIDTH] for line in lines[:max_lines + 1]]
    if line_count > max_lines + 1:  # +1 to prefer the last line to skipped msg.
      lines[-1] = "***** %s lines skipped *****" % (line_count - max_lines)
    return "\n".join(lines)

  def _log(self, s, *args):
    r"""Log a string. It flushes but doesn't append \n, so do that yourself."""
    # TODO(tewalds): Should this be using logging.info instead? How to see them
    # outside of google infrastructure?

    sys.stderr.write((s + "\n") % args)