#!/usr/libexec/platform-python -s
#  -*- coding: utf-8 -*-
# *****************************************************************************
# NICOS, the Networked Instrument Control System of the MLZ
# Copyright (c) 2009-2021 by the NICOS contributors (see AUTHORS)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Georg Brandl <georg.brandl@fz-juelich.de>
#
# *****************************************************************************

import argparse
import logging
import sys
from os import path

sys.path.insert(0, path.dirname(path.dirname(path.realpath(__file__))))

from nicos.clients.gui.panels.history import StandaloneHistoryApp
from nicos.core.sessions.simple import SingleDeviceSession
from nicos.utils.loggers import ColoredConsoleHandler, NicosLogger, initLoggers

usage = '''%(prog)s [-h] [-c CACHE] [-p PREFIX] [key ... ]

When some keys are given, they are opened as plots on startup.  For example:

    %(prog)s T_ccr5,T_ccr5.setpoint

A time specification can be given with a colon:

    %(prog)s T_ccr5,T_ccr5.setpoint:24h

A separate view plot is opened for each such argument.
'''

parser = argparse.ArgumentParser(usage=usage)
parser.add_argument('-c', '--cache', dest='cache', action='store',
                    help='CACHE server:port, user is asked if not given',
                    default='')
parser.add_argument('-p', '--prefix', dest='prefix', action='store',
                    help='PREFIX is the key prefix; it will be added in front '
                    'of the key, defaults to \'nicos/\'', default='nicos/')
parser.add_argument('keys', nargs=argparse.REMAINDER,
                    help='')

opts = parser.parse_args()

# Set up logging for the GUI instance.
initLoggers()
log = NicosLogger('gui')
log.parent = None
log.setLevel(logging.INFO)
log.addHandler(ColoredConsoleHandler())


# set up logging for unhandled exceptions in Qt callbacks
def log_unhandled(*exc_info):
    import traceback
    traceback.print_exception(*exc_info)
    log.exception('unhandled exception in QT callback', exc_info=exc_info)


sys.excepthook = log_unhandled


sys.exit(SingleDeviceSession.run('history', StandaloneHistoryApp,
                                 {'prefix': opts.prefix, 'cache': opts.cache,
                                  'views': opts.keys},
                                 pidfile=False))
