#!/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.utils import load_config
from wnodes.cli.utils import get_config
from wnodes.cli.utils import checks 
from wnodes.cli.utils import list_images_tags
 
class ListImagesTags:
    '''A command-line program to list images tags.'''
    def __init__(self):
        self.verbose = False
        self.conf = (False,'wnodes-cli.cfg')

    def __usage(self):
        print ("Usage: wnodes_list_images_tags [OPTION]\n"+
            "Request to show information about images stored in the WNoDeS\n"+
            "repository\n\n"+
            "OPTION\n"+
            "  -c, --conf=CONF_FILE     specifies a customized conf filename.\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:], \
                "hvc:", ["help", "verbose", "version", "conf="])
        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 in ("-c", "--conf"):
                self.conf = (True, value)
            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 doWork(self):
        self.__parse()

        if not self.conf[0]:
            config_file = \
                get_config.ConfigurationFileLocation(\
                    file_name = self.conf[1]).get_configuration_file()
        else:
            config_file = \
                get_config.ConfigurationFileLocation(\
                    file_name = self.conf[1]).get_custom_configuration_file()

        load_user_data = load_config.LoadUserConfig(\
            config_file = config_file).load_user_data()

        obj = list_images_tags.ListTags(load_user_data)
        tags = obj.get_tags()

        if len(tags[1][1]) > 0:
            list_images_tags.print_list_header()
            list_images_tags.print_image_tag_details(tags[1][1])
        else:
            print 'No Images Tags available'

if __name__ == '__main__':
    try:
        a = ListImagesTags()
        a.doWork()
    except get_config.NoFileFound, err:
        print '\n\nExecution: ', err
    except get_config.NoPathFound, err:
        print '\n\nExecution: ', err
    except load_config.WrongConfigurationFile, err:
        print '\n\nExecution: ', err
    except load_config.WrongConfigurationSettings, err:
        print '\n\nExecution: ', err
    except checks.NoCmdFound, err:
        print '\n\nExecution: ', err
    except list_images_tags.ConnectionError, err:
        print '\n\nExecution: ', err
    except KeyboardInterrupt:
        print '\n\nExecution n!'
        sys.exit(1)
    except:
        sys.exit(1)
