#!/usr/bin/python3

"""
Python Bugzilla Interface

Simple command-line interface to bugzilla to allow:
 - searching
 - getting bug info
 - saving attachments

Requirements
------------
 - Python 3.3 or later

Classes
-------
 - BugzillaProxy - Server proxy for communication with Bugzilla

"""

import argparse
import configparser
import sys

from bugz.argparsers import make_parser
from bugz.configfile import get_config
from bugz.connection import Connection
from bugz.errhandling import BugzError
from bugz.log import log_error, log_info


def main():
	ArgParser = argparse.ArgumentParser(
		argument_default=argparse.SUPPRESS,
		epilog='use -h after a sub-command for sub-command specific help')
	make_parser(ArgParser)
	args = ArgParser.parse_args()

	ConfigParser = configparser.ConfigParser(default_section='default')
	get_config(ConfigParser, getattr(args, 'config_file', None))

	conn = Connection(args, ConfigParser)

	if not hasattr(args, 'func'):
		ArgParser.print_usage()
		return 1

	try:
		args.func(conn)
		return 0
	except BugzError as e:
		log_error(e)
		return 1
	except RuntimeError as e:
		log_error(e)
		return 1
	except KeyboardInterrupt:
		log_info('Stopped due to keyboard interrupt')
		return 1
	except:
		raise

if __name__ == "__main__":
	sys.exit(main())
