#!/usr/bin/python

#------------------------------------------------------------------------------
# Copyright 2008-2012 Istituto Nazionale di Fisica Nucleare (INFN)
# 
# Licensed under the EUPL, Version 1.1 only (the "Licence").
# You may not use this work except in compliance with the Licence. 
# You may obtain a copy of the Licence at:
# 
# http://joinup.ec.europa.eu/system/files/EN/EUPL%20v.1.1%20-%20Licence.pdf
# 
# Unless required by applicable law or agreed to in 
# writing, software distributed under the Licence is 
# distributed on an "AS IS" basis, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. 
# See the Licence for the specific language governing 
# permissions and limitations under the Licence.
#------------------------------------------------------------------------------

import os
import sys
import getopt
from wnodes.cli.providers import infoproviders
from wnodes.cli.utils import utilities
 
class SizeImagesInfo:
    '''A command-line program to show size image information.'''
    def __init__(self):
        self.verbose = False

    def __usage(self):
        print ("Usage: size_images_info [OPTION]\n"+
            "Request to show information about size images supported\n\n"+
            "OPTION\n"+
            "  --verbose                show verbose information. The\n"+
            "                           default of which is %s.\n"
            % self.verbose +
            "  -h, --help               display this help and exit.\n"+
            "  -v, --version            output version information and exit.\n")

    def __parse(self):
        try:
            opts, args = getopt.getopt(sys.argv[1:], \
                "hv", ["help", "verbose", "version"])
        except getopt.GetoptError, err:
            print str(err) 
            self.__usage()
            sys.exit(2)

        for key, value in opts:
            if key in ("-h", "--help"):
                self.__usage()
                sys.exit()
            elif key == "--verbose":
                self.verbose = True
            elif key in ("-v", "--version"):
                self.version = __import__('cli').get_release()
                sys.exit()
            else:
                raise errors.OptionError("unhandled option")

    def __print_size_images_info(self):
        size_images_info = infoproviders.get_json_file_information()

        for provider, value in size_images_info.iteritems():
            print ("%s Resource Provider:\n"
                % provider +
                "TAG          CORES  MEMORY      DISK")
            max_length,longest_key = utilities.get_longest_string(value.keys())

            for size, new_value in value.iteritems():
                print ("%s%s%s      %s%s%s"
                    % (size, utilities.add_empty_space(len(size), max_length),
                    new_value['cpu'], new_value['memory'],
                    utilities.add_empty_space(len(new_value['memory']), 8),
                    new_value['disk']))

            print "\n"

        
    def doWork(self):
        self.__parse()
        self.__print_size_images_info()

if __name__ == '__main__':
    try:
        a = SizeImagesInfo()
        a.doWork()
    except KeyboardInterrupt:
        print '\n\nExecution n!'
        sys.exit(1)
