# Path: apiary/tools/childprocess.py
# class ChildProcess(Process):
#     def __init__(self, *args, **kwargs):
#         super(ChildProcess, self).__init__(*args, **kwargs)
#         self.daemon = True
# 
#     def run(self):
#         # Make sure child processes don't catch ^C or eat input.
#         os.close(0)
#         signal(SIGINT, SIG_IGN)
# 
#         self.run_child_process()
# 
# Path: apiary/tools/debug.py
# def debug(tmpl, *args):
#     if _DEBUG:
#         print '[DEBUG] ' + (tmpl % args)
# 
# def traced_func(f):
#     name = f.__name__
# 
#     def wrapped(*a, **kw):
#         return trace_call(name, f, *a, **kw)
# 
#     return wrapped
# 
# def traced_method(m):
#     def wrapped(self, *a, **kw):
#         name = self.__class__.__name__ + '.' + m.__name__
#         return trace_call(name, m, self, *a, **kw)
#     return wrapped
# 
# Path: apiary/tools/stats.py
# class Tally(IntegerStatistic):
#     def __init__(self):
#         super(IntegerStatistic,self).__init__()
# 
#         self._grand_total = 0
#         self.reset()
# 
#     def add(self):
#         self._total += 1
# 
#     def calculate(self):
#         self._grand_total += self._total
# 
#         stats = OrderedDict()
# 
#         stats["Current"] = self._total
#         stats["Total"] = self._grand_total
#         stats["Rate"] = self._total / (time.time() - self._start_time)
# 
#         return stats
# 
#     def reset(self):
#         self._total = 0
#         self._start_time = time.time()
# 
# class Level(IntegerStatistic):
#     def __init__(self):
#         super(Level,self).__init__()
# 
#         self._level = 0
#         self._levels = [0]
# 
#     def add(self, direction):
#         if direction == "+":
#             self._level += 1
#         elif direction == "-":
#             self._level -= 1
# 
#         self._levels.append(self._level)
# 
#     def reset(self):
#         self._levels = []
# 
#     def calculate(self):
#         stats = OrderedDict()
# 
#         stats['Current'] = self._level
# 
#         if self._levels:
#             add_series_stats(stats, self._levels)
# 
#         return stats
# 
# class Series(FloatStatistic):
#     def __init__(self):
#         super(Series, self).__init__()
# 
#         self._values = []
# 
#     def add(self, value):
#         self._values.append(value)
# 
#     def reset(self):
#         self._values = []
# 
#     def calculate(self):
#         stats = OrderedDict()
# 
#         if self._values:
#             stats['Current'] = self._values[-1]
#             add_series_stats(stats, self._values)
# 
#         return stats
# 
# Path: apiary/tools/table.py
# def format_table(rows):
#     if not rows:
#         return
# 
#     num_cols = max(len(row) for row in rows)
# 
#     # make all rows as long as the longest
#     rows = [row + [(ALIGN_LEFT, "")] * (num_cols - len(row)) for row in rows]
# 
#     # calculate max column widths
#     widths = [max(len(r[i][1]) for r in rows) for i in xrange(num_cols)]
# 
#     output = ""
#     for row in rows:
#         for i, (align, text) in enumerate(row):
#             if i > 0:
#                 output += " "
# 
#             output += "{0:{1}{2}}".format(text, align, widths[i])
#         output += "\n"
# 
#     return output
# 
# ALIGN_LEFT = "<"
# 
# ALIGN_CENTER = "^"
# 
# ALIGN_RIGHT = ">"


import optparse
import os
import re
import random
import socket
import sys
import tempfile
import cPickle
import MySQLdb
import time
import warnings
from datetime import datetime
from threading import Thread
from multiprocessing import Value, Queue
from multiprocessing.queues import Empty
from collections import defaultdict
from itertools import chain
from apiary.tools.childprocess import ChildProcess
from apiary.tools.debug import debug, traced_func, traced_method
from apiary.tools.stats import Tally, Level, Series
from apiary.tools.table import format_table, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT
        print "=" * len(timestamp)

        table = []

        for name, stat in (sorted(self._tallies.items()) +
                           sorted(self._levels.iteritems()) +
                           sorted(self._series.iteritems())):
            report = stat.report()

            if report:
                row = [(ALIGN_RIGHT, "%s: " % name)]
                row.extend(report)
                table.append(row)

        print format_table(table) or "",

    def run_child_process(self):
        while True:
            try:
                done = self.worker_status(self._queue.get(timeout=1))
            except Empty:
                done = False

            if done or time.time() - self._last_report > self._options.stats_interval:
                self.report()

            if done:
                break



# Path: apiary/tools/childprocess.py
# class ChildProcess(Process):
#     def __init__(self, *args, **kwargs):
#         super(ChildProcess, self).__init__(*args, **kwargs)
#         self.daemon = True
# 
#     def run(self):
#         # Make sure child processes don't catch ^C or eat input.
#         os.close(0)
#         signal(SIGINT, SIG_IGN)
# 
#         self.run_child_process()
# 
# Path: apiary/tools/debug.py
# def debug(tmpl, *args):
#     if _DEBUG:
#         print '[DEBUG] ' + (tmpl % args)
# 
# def traced_func(f):
#     name = f.__name__
# 
#     def wrapped(*a, **kw):
#         return trace_call(name, f, *a, **kw)
# 
#     return wrapped
# 
# def traced_method(m):
#     def wrapped(self, *a, **kw):
#         name = self.__class__.__name__ + '.' + m.__name__
#         return trace_call(name, m, self, *a, **kw)
#     return wrapped
# 
# Path: apiary/tools/stats.py
# class Tally(IntegerStatistic):
#     def __init__(self):
#         super(IntegerStatistic,self).__init__()
# 
#         self._grand_total = 0
#         self.reset()
# 
#     def add(self):
#         self._total += 1
# 
#     def calculate(self):
#         self._grand_total += self._total
# 
#         stats = OrderedDict()
# 
#         stats["Current"] = self._total
#         stats["Total"] = self._grand_total
#         stats["Rate"] = self._total / (time.time() - self._start_time)
# 
#         return stats
# 
#     def reset(self):
#         self._total = 0
#         self._start_time = time.time()
# 
# class Level(IntegerStatistic):
#     def __init__(self):
#         super(Level,self).__init__()
# 
#         self._level = 0
#         self._levels = [0]
# 
#     def add(self, direction):
#         if direction == "+":
#             self._level += 1
#         elif direction == "-":
#             self._level -= 1
# 
#         self._levels.append(self._level)
# 
#     def reset(self):
#         self._levels = []
# 
#     def calculate(self):
#         stats = OrderedDict()
# 
#         stats['Current'] = self._level
# 
#         if self._levels:
#             add_series_stats(stats, self._levels)
# 
#         return stats
# 
# class Series(FloatStatistic):
#     def __init__(self):
#         super(Series, self).__init__()
# 
#         self._values = []
# 
#     def add(self, value):
#         self._values.append(value)
# 
#     def reset(self):
#         self._values = []
# 
#     def calculate(self):
#         stats = OrderedDict()
# 
#         if self._values:
#             stats['Current'] = self._values[-1]
#             add_series_stats(stats, self._values)
# 
#         return stats
# 
# Path: apiary/tools/table.py
# def format_table(rows):
#     if not rows:
#         return
# 
#     num_cols = max(len(row) for row in rows)
# 
#     # make all rows as long as the longest
#     rows = [row + [(ALIGN_LEFT, "")] * (num_cols - len(row)) for row in rows]
# 
#     # calculate max column widths
#     widths = [max(len(r[i][1]) for r in rows) for i in xrange(num_cols)]
# 
#     output = ""
#     for row in rows:
#         for i, (align, text) in enumerate(row):
#             if i > 0:
#                 output += " "
# 
#             output += "{0:{1}{2}}".format(text, align, widths[i])
#         output += "\n"
# 
#     return output
# 
# ALIGN_LEFT = "<"
# 
# ALIGN_CENTER = "^"
# 
# ALIGN_RIGHT = ">"


# Path: apiary/base.py
import optparse
import os
import re
import random
import socket
import sys
import tempfile
import cPickle
import MySQLdb
import time
import warnings
from datetime import datetime
from threading import Thread
from multiprocessing import Value, Queue
from multiprocessing.queues import Empty
from collections import defaultdict
from itertools import chain
from apiary.tools.childprocess import ChildProcess
from apiary.tools.debug import debug, traced_func, traced_method
from apiary.tools.stats import Tally, Level, Series
from apiary.tools.table import format_table, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT

        print "=" * len(timestamp)

        table = []

        for name, stat in (sorted(self._tallies.items()) +
                           sorted(self._levels.iteritems()) +
                           sorted(self._series.iteritems())):
            report = stat.report()

            if report:
                row = [(ALIGN_RIGHT, "%s: " % name)]
                row.extend(report)
                table.append(row)

        print format_table(table) or "",

    def run_child_process(self):
        while True:
            try:
                done = self.worker_status(self._queue.get(timeout=1))
            except Empty:
                done = False

            if done or time.time() - self._last_report > self._options.stats_interval:
                self.report()

            if done:
                break



class QueenBee(ChildProcess):