# Path: chirp/common/timestamp.py
# _TIMESTAMP_FORMAT = "%Y%m%d-%H%M%S"
# _PRETTY_FORMAT = "%c"
# PROCESS_START_TIME = time.time()  # time of importing this file
# _MIN_REASONABLE_TIMESTAMP = parse_human_readable("20081130-000000")
# _MAX_REASONABLE_TIMESTAMP = parse_human_readable("20380119-000000")
# _SECONDS_PER_MINUTE = 60
# _SECONDS_PER_HOUR = 60 * _SECONDS_PER_MINUTE
# _SECONDS_PER_DAY = 24 * _SECONDS_PER_HOUR
# def now():
# def now_ms():
# def process_start_time_ms():
# def process_age_ms():
# def get_human_readable(ts=None):
# def get_human_readable_ms(ms_ts):
# def get_pretty(ts=None):
# def get_pretty_ms(ms_ts):
# def parse_human_readable(human_readable_str):
# def is_valid(ts):
# def get_human_readable_duration(duration_s):
#     def _pluralize(n, unit):
# def get_human_readable_duration_ms(duration_ms):
# 
# Path: chirp/stream/message.py
# CONNECTED = "Connected"        # New connection initiated.
# RESPONSE = "Response"          # HTTP response received.
# BLOCK = "Block"                # A chunk of raw data.
# FRAME = "Frame"                # An MP3 frame.
# ERROR = "Error"                # An error.
# CONNECT_TIMEOUT_ERROR = "Connect Timeout Error"
# CONNECT_ERROR = "Connect Error"
# REQUEST_ERROR = "Request Error"
# READ_TIMEOUT_ERROR = "Read Timeout Error"
# READ_ERROR = "Read Error"
# MISSING_HEADERS_ERROR = "Missing HTTP Headers Error"
# MALFORMED_HEADERS_ERROR = "Malformed HTTP Headers Error"
# BAD_REDIRECT_ERROR = "Bad Redirect Error"
# END_OF_STREAM_ERROR = "End of Stream Error"
# class Message(object):
# class MessageSource(object):
# class MessageConsumer(looper.Looper):
# class MessageTee(MessageConsumer):
#     def payload_sha1(self):
#     def payload_hex_sha1(self):
#     def __str__(self):
#     def set_error(self, error_type, err):
#     def is_error(self):
#     def is_end_of_stream(self):
#     def __init__(self):
#     def _add_message(self, msg):
#     def _add_stop_message(self):
#     def get_next_message(self, timeout=None):
#     def get_all_messages(self):
#     def wait_until_empty(self):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _loop_once(self):
#     def __init__(self, input_src, num_outputs):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/mp3_writer.py
# _ARCHIVE_VOLUME = 0xff
# class MP3Writer(object):
#     def __init__(self, prefix, start_ms=None):
#     def _get_id3(self):
#     def _get_title(self):
#     def write(self, msg):
#     def close(self):


import errno
import os
import sys
import time
from chirp.common import timestamp
from chirp.stream import message
from chirp.stream import mp3_writer

class Archiver(message.MessageConsumer):

    ROOT_DIR = "/tmp/test-archives"
    FILE_PREFIX = "chirp"

    def __init__(self, src):
        message.MessageConsumer.__init__(self, src)
        self._writer = None
        self._last_hour = None
        self.rollover_count = 0

    def _dirname(self, ts_ms):
        time_tuple = time.localtime(int(ts_ms/1000))
        return os.path.join(self.ROOT_DIR,
                            time.strftime("%Y/%m/%d/", time_tuple))

    def _open(self, ts_ms):
        # Try to create the directory for the archive file.
        dirname = self._dirname(ts_ms)
        try:
            os.makedirs(dirname)
        except OSError, ex:
            if ex.errno != errno.EEXIST:
                raise ex
        prefix = os.path.join(dirname, self.FILE_PREFIX)
        return mp3_writer.MP3Writer(prefix, ts_ms)

    def _process_message(self, msg):
        # Skip any non-FRAME messages.

# Path: chirp/common/timestamp.py
# _TIMESTAMP_FORMAT = "%Y%m%d-%H%M%S"
# _PRETTY_FORMAT = "%c"
# PROCESS_START_TIME = time.time()  # time of importing this file
# _MIN_REASONABLE_TIMESTAMP = parse_human_readable("20081130-000000")
# _MAX_REASONABLE_TIMESTAMP = parse_human_readable("20380119-000000")
# _SECONDS_PER_MINUTE = 60
# _SECONDS_PER_HOUR = 60 * _SECONDS_PER_MINUTE
# _SECONDS_PER_DAY = 24 * _SECONDS_PER_HOUR
# def now():
# def now_ms():
# def process_start_time_ms():
# def process_age_ms():
# def get_human_readable(ts=None):
# def get_human_readable_ms(ms_ts):
# def get_pretty(ts=None):
# def get_pretty_ms(ms_ts):
# def parse_human_readable(human_readable_str):
# def is_valid(ts):
# def get_human_readable_duration(duration_s):
#     def _pluralize(n, unit):
# def get_human_readable_duration_ms(duration_ms):
# 
# Path: chirp/stream/message.py
# CONNECTED = "Connected"        # New connection initiated.
# RESPONSE = "Response"          # HTTP response received.
# BLOCK = "Block"                # A chunk of raw data.
# FRAME = "Frame"                # An MP3 frame.
# ERROR = "Error"                # An error.
# CONNECT_TIMEOUT_ERROR = "Connect Timeout Error"
# CONNECT_ERROR = "Connect Error"
# REQUEST_ERROR = "Request Error"
# READ_TIMEOUT_ERROR = "Read Timeout Error"
# READ_ERROR = "Read Error"
# MISSING_HEADERS_ERROR = "Missing HTTP Headers Error"
# MALFORMED_HEADERS_ERROR = "Malformed HTTP Headers Error"
# BAD_REDIRECT_ERROR = "Bad Redirect Error"
# END_OF_STREAM_ERROR = "End of Stream Error"
# class Message(object):
# class MessageSource(object):
# class MessageConsumer(looper.Looper):
# class MessageTee(MessageConsumer):
#     def payload_sha1(self):
#     def payload_hex_sha1(self):
#     def __str__(self):
#     def set_error(self, error_type, err):
#     def is_error(self):
#     def is_end_of_stream(self):
#     def __init__(self):
#     def _add_message(self, msg):
#     def _add_stop_message(self):
#     def get_next_message(self, timeout=None):
#     def get_all_messages(self):
#     def wait_until_empty(self):
#     def __init__(self, src):
#     def _process_message(self, msg):
#     def _loop_once(self):
#     def __init__(self, input_src, num_outputs):
#     def _process_message(self, msg):
# 
# Path: chirp/stream/mp3_writer.py
# _ARCHIVE_VOLUME = 0xff
# class MP3Writer(object):
#     def __init__(self, prefix, start_ms=None):
#     def _get_id3(self):
#     def _get_title(self):
#     def write(self, msg):
#     def close(self):


# Path: chirp/stream/archiver.py
import errno
import os
import sys
import time
from chirp.common import timestamp
from chirp.stream import message
from chirp.stream import mp3_writer


class Archiver(message.MessageConsumer):

    ROOT_DIR = "/tmp/test-archives"
    FILE_PREFIX = "chirp"

    def __init__(self, src):
        message.MessageConsumer.__init__(self, src)
        self._writer = None
        self._last_hour = None
        self.rollover_count = 0

    def _dirname(self, ts_ms):
        time_tuple = time.localtime(int(ts_ms/1000))
        return os.path.join(self.ROOT_DIR,
                            time.strftime("%Y/%m/%d/", time_tuple))

    def _open(self, ts_ms):
        # Try to create the directory for the archive file.
        dirname = self._dirname(ts_ms)
        try:
            os.makedirs(dirname)
        except OSError, ex:
            if ex.errno != errno.EEXIST:
                raise ex
        prefix = os.path.join(dirname, self.FILE_PREFIX)
        return mp3_writer.MP3Writer(prefix, ts_ms)

    def _process_message(self, msg):
        # Skip any non-FRAME messages.

        if msg.message_type != message.FRAME: