|
| 1 | +# This program is free software; you can redistribute it and/or modify |
| 2 | +# it under the terms of the GNU General Public License as published by |
| 3 | +# the Free Software Foundation; either version 2 of the License, or |
| 4 | +# (at your option) any later version. |
| 5 | +# |
| 6 | +# This program is distributed in the hope that it will be useful, |
| 7 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | +# |
| 10 | +# See LICENSE for more details. |
| 11 | +# |
| 12 | +# Copyright: Red Hat Inc. 2015 |
| 13 | +# Author: Cleber Rosa <[email protected]> |
| 14 | + |
| 15 | +""" |
| 16 | +This is the main entry point for the rest client cli application |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +import sys |
| 21 | +import types |
| 22 | +import importlib |
| 23 | +import functools |
| 24 | + |
| 25 | +from avocado import settings |
| 26 | +from avocado.core import output |
| 27 | +from avocado.core import exit_codes |
| 28 | +from avocado.restclient import connection |
| 29 | +from avocado.restclient.cli import parser |
| 30 | + |
| 31 | +__all__ = ['App'] |
| 32 | + |
| 33 | + |
| 34 | +class App(object): |
| 35 | + |
| 36 | + """ |
| 37 | + Base class for CLI application |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self): |
| 41 | + """ |
| 42 | + Initializes a new app instance. |
| 43 | +
|
| 44 | + This class is intended both to be used by the stock client application |
| 45 | + and also to be reused by custom applications. If you want, say, to |
| 46 | + limit the amount of command line actions and its arguments, you can |
| 47 | + simply supply another argument parser class to this constructor. Of |
| 48 | + course another way to customize it is to inherit from this and modify |
| 49 | + its members at will. |
| 50 | + """ |
| 51 | + self.connection = None |
| 52 | + self.parser = parser.Parser() |
| 53 | + self.parser.add_arguments_on_all_modules() |
| 54 | + self.view = output.View() |
| 55 | + |
| 56 | + def initialize_connection(self): |
| 57 | + """ |
| 58 | + Initialize the connection instance |
| 59 | + """ |
| 60 | + try: |
| 61 | + self.connection = connection.Connection( |
| 62 | + hostname=self.args.hostname, |
| 63 | + port=self.args.port, |
| 64 | + username=self.args.username, |
| 65 | + password=self.args.password) |
| 66 | + except connection.InvalidConnectionError: |
| 67 | + self.view.notify(event="error", |
| 68 | + msg="Error: could not connect to the server") |
| 69 | + sys.exit(exit_codes.AVOCADO_JOB_FAIL) |
| 70 | + except connection.InvalidServerVersionError: |
| 71 | + self.view.notify(event="error", |
| 72 | + msg=("REST server version is higher than " |
| 73 | + "than this client can support.")) |
| 74 | + self.view.notify(event="error", |
| 75 | + msg=("Please use a more recent version " |
| 76 | + "of the REST client application.")) |
| 77 | + sys.exit(exit_codes.AVOCADO_JOB_FAIL) |
| 78 | + |
| 79 | + def dispatch_action(self): |
| 80 | + """ |
| 81 | + Calls the actions that was specified via command line arguments. |
| 82 | +
|
| 83 | + This involves loading the relevant module file. |
| 84 | + """ |
| 85 | + module_name = "%s.%s" % ('avocado.restclient.cli.actions', |
| 86 | + self.args.top_level_action) |
| 87 | + |
| 88 | + try: |
| 89 | + module = importlib.import_module(module_name) |
| 90 | + except ImportError: |
| 91 | + return |
| 92 | + |
| 93 | + # Filter out the attributes out of the loaded module that look |
| 94 | + # like command line actions, based on type and 'is_action' attribute |
| 95 | + module_actions = {} |
| 96 | + for attribute_name in module.__dict__: |
| 97 | + attribute = module.__dict__[attribute_name] |
| 98 | + if (isinstance(attribute, types.FunctionType) and |
| 99 | + hasattr(attribute, 'is_action')): |
| 100 | + if attribute.is_action: |
| 101 | + module_actions[attribute_name] = attribute |
| 102 | + |
| 103 | + chosen_action = None |
| 104 | + for action in module_actions.keys(): |
| 105 | + if getattr(self.args, action, False): |
| 106 | + chosen_action = action |
| 107 | + break |
| 108 | + |
| 109 | + kallable = module_actions.get(chosen_action, None) |
| 110 | + if kallable is not None: |
| 111 | + self.initialize_connection() |
| 112 | + return kallable(self) |
| 113 | + else: |
| 114 | + self.view.notify(event="error", |
| 115 | + msg="Action specified is not implemented") |
| 116 | + |
| 117 | + def run(self): |
| 118 | + """ |
| 119 | + Main entry point for application |
| 120 | + """ |
| 121 | + action_result = None |
| 122 | + try: |
| 123 | + self.args = self.parser.parse_args() |
| 124 | + action_result = self.dispatch_action() |
| 125 | + except KeyboardInterrupt: |
| 126 | + print 'Interrupted' |
| 127 | + |
| 128 | + if isinstance(action_result, int): |
| 129 | + sys.exit(action_result) |
| 130 | + elif isinstance(action_result, bool): |
| 131 | + if action_result is True: |
| 132 | + sys.exit(0) |
| 133 | + else: |
| 134 | + sys.exit(1) |
0 commit comments