1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3#
4# (c) Copyright 2015 HP Development Company, L.P.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19#
20# Author: Amarnath Chitumalla, Suma Byrappa
21#
22
23__version__ = '1.0'
24__mod__ = 'hp-diagnose_plugin'
25__title__ = 'Diagnose Plugin Utility'
26__doc__ = "Diagnose HP Plugin. Installs plugins if absent"
27
28# Std Lib
29import sys
30import getopt
31import time
32import os.path
33import re
34import os
35
36
37# Local
38from base.g import *
39from base import utils, module
40
41try:
42    from importlib import import_module
43except ImportError as e:
44    log.debug(e)
45    from base.utils import dyn_import_mod as import_module
46
47
48def usage(typ='text'):
49    if typ == 'text':
50        utils.log_title(__title__, __version__)
51    utils.format_text(USAGE, typ, __title__, __mod__, __version__)
52    sys.exit(0)
53
54
55USAGE = [ (__doc__, "", "name", True),
56          ("Usage: %s [OPTIONS]" % __mod__, "", "summary", True),
57          utils.USAGE_OPTIONS,
58          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
59          utils.USAGE_HELP,
60          utils.USAGE_SPACE,
61          utils.USAGE_SEEALSO,
62          ("hp-plugin", "", "seealso", False),
63          ("hp-setup", "", "seealso", False),
64          ("hp-firmware", "", "seealso", False),
65        ]
66
67
68mod = module.Module(__mod__, __title__, __version__, __doc__, USAGE,
69                    (INTERACTIVE_MODE, GUI_MODE),
70                    (UI_TOOLKIT_QT3, UI_TOOLKIT_QT4, UI_TOOLKIT_QT5), True)
71
72opts, device_uri, printer_name, mode, ui_toolkit, loc = \
73    mod.parseStdOpts( handle_device_printer=False)
74
75plugin_path = None
76install_mode = PLUGIN_REQUIRED
77plugin_reason = PLUGIN_REASON_NONE
78
79if mode == GUI_MODE:
80    if ui_toolkit == 'qt3':
81        log.error("Unable to load Qt3. Please use Qt4")
82
83    else: #qt4
84        if not utils.canEnterGUIMode4():
85            log.error("%s requires GUI support . Is Qt4 installed?" % __mod__)
86            sys.exit(1)
87
88        # try:
89        #     from PyQt4.QtGui import QApplication, QMessageBox
90        #     from ui4.plugindiagnose import PluginDiagnose
91        #     from installer import pluginhandler
92        # except ImportError:
93        #     log.error("Unable to load Qt4 support. Is it installed?")
94        #     sys.exit(1)
95
96        QApplication, ui_package = utils.import_dialog(ui_toolkit)
97        ui = import_module(ui_package + ".plugindiagnose")
98        from installer import pluginhandler
99
100        app = QApplication(sys.argv)
101        pluginObj = pluginhandler.PluginHandle()
102        plugin_sts = pluginObj.getStatus()
103        if plugin_sts == PLUGIN_INSTALLED:
104            log.info("Device Plugin is already installed")
105            sys.exit(0)
106        elif plugin_sts == PLUGIN_NOT_INSTALLED:
107            dialog = ui.PluginDiagnose(None, install_mode, plugin_reason)
108        else:
109            dialog = ui.PluginDiagnose(None, install_mode, plugin_reason, True)
110
111        dialog.show()
112        try:
113            log.debug("Starting GUI loop...")
114            app.exec_()
115        except KeyboardInterrupt:
116            log.error("User exit")
117            sys.exit(0)
118else: #Interaction mode
119    log.error("Only Qt4 GUI mode is supported \n")
120    usage()
121
122log.info("")
123log.info("Done.")
124