#!/usr/bin/env python
#
# Copyright (C) 2014 
#   Brian Caswell <bmc@lungetech.com>
#   Narf Industries <info@narfindustries.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import string
import sys
import os

from os.path import join,dirname,abspath
from generator.actions import Actions
from random import choice,randint
from struct import *

lehdr = "PTTC"
behdr = "CTTP"
INTERNALERR = "If anyone asks, you're my debugger.\x00"
SUBMITOK = "Oh, sure. Let the robot do all the work.\x00" 
DELETEOK = "Hopes: deleted.\x00"
NOTIMPLEMENTED = "You want me to do *two* things?!\x00"

INFO = 0x4347C000
WARN = 0x4347C001
ERROR = 0x4347C002

def random_alpha(a, b):
    return ''.join(choice(string.letters) for _ in range(randint(a, b)))

def chunks(l, n):
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

class CTTP(Actions):

    def start(self):
        self.files = {}

    def _retrieve(self, ver, code):
        le = True
        hdr = lehdr if le else behdr