#!/usr/bin/python

# ck_setup.py - checks the veyepar setup - reports what features are ready.

from process import process

from main.models import Show, Location, Client

from django.conf import settings

import pw

import rax_uploader
import archive_uploader
import steve.richardapi

import os
import xml.etree.ElementTree
import requests

# from the blender build scripts
# https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

def p_print(text):
    print(text)
    return 

def p_okg(text):
    print(bcolors.OKGREEN + text +bcolors.ENDC)
    return 

def p_warn(text):
    print(bcolors.WARNING + text +bcolors.ENDC)
    return 

def p_fail(text):
    print(bcolors.FAIL + text +bcolors.ENDC)
    return 

class ck_setup(process):

    client=None
    show=None

    def ck_pw(self,
            service,
            client_id_field=None,
            cred_keys=[]):

        try: 
            creds = getattr(pw, service)
        except AttributeError as e:
            # 'module' object has no attribute 'foo'
            p_fail('pw.py does not have: "{}"'.format(service))
            return False

        keys = creds.keys()
        print "keys for service {}: {}".format( service, keys )

        key = getattr(self.client, client_id_field, None)

        # import code; code.interact(local=locals())

        print 'checking client.{} & pw.py for "{}" in: "{}={{..."'.format(
                client_id_field,key,service)

        if not key:
            p_warn('client.{} is blank'.format(client_id_field))
            return False
        elif key in keys:
            p_okg('key "{}" found in "{}" keys.'.format(key,service))
        else:
            p_warn('key "{}" not found in "{}" keys.'.format(key,service))
            raise AttributeError

        secrets = creds[key]
        # try not to display secret values
        print('names of secrets in pw.py {}:{}'.format( 
            key, secrets.keys() ))
        print('checking for existance of {}'.format(cred_keys))
        for cred_key in cred_keys:
            if cred_key not in secrets:
                p_warn('"{}" NOT found.'.format(cred_key))

        return secrets

    
    def ck_client(self):
        try:
            client_slug = self.options.client
        except AttributeError as e:
            p_fail("No client set in config file or command line.")
            raise e
        p_okg("client_slug: {}".format(client_slug))

        try:
            self.client = Client.objects.get(slug=client_slug)
        except Client.DoesNotExist as e:
            p_fail("client slug not found in db.")
            raise e

        return 

    def ck_show(self):
        try:
            show_slug = self.options.show
        except AttributeError as e:
            p_fail("No show set in config file or command line.")
            raise e
        p_okg("show_slug: {}".format(show_slug))

        try:
            self.show = Show.objects.get(slug=show_slug)
        except Show.DoesNotExist as e:
            p_fail( "show slug not found in db." )
            raise e

        return 

    def ck_dir(self):
        if os.path.exists(self.show_dir):
            print("~/Videos/showdir exits: {}".format(self.show_dir))
        else:
            # print(bcolors.FAIL + "~/Videos/showdir not created yet.  run mk_dirs.py"+bcolors.ENDC)
            p_fail("~/Videos/showdir not created yet.  run mk_dirs.py")


    def ck_title(self):