summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2016-11-05 12:13:27 +0100
committernicolargo <nicolas@nicolargo.com>2016-11-05 12:13:27 +0100
commit65121f7796c1577863cf59b96e46c75fef544e83 (patch)
tree4554fbf1083af9af975ae26c1f291c2de12e4c81
parent5521197a6e2b60e1496266841ebf8ff50067d047 (diff)
Refactor the main() function
-rw-r--r--glances/__init__.py190
1 files changed, 113 insertions, 77 deletions
diff --git a/glances/__init__.py b/glances/__init__.py
index 2c16d18b..deb38679 100644
--- a/glances/__init__.py
+++ b/glances/__init__.py
@@ -96,112 +96,148 @@ def end():
sys.exit(0)
-def main():
- """Main entry point for Glances.
+def start_standalone(config, args):
+ """Start the standalone mode"""
+ logger.info("Start standalone mode")
- Select the mode (standalone, client or server)
- Run it...
- """
- # Log Glances and PSutil version
- logger.info('Start Glances {}'.format(__version__))
- logger.info('{} {} and PSutil {} detected'.format(
- platform.python_implementation(),
- platform.python_version(),
- psutil_version))
+ # Share global var
+ global standalone
+
+ # Import the Glances standalone module
+ from glances.standalone import GlancesStandalone
+
+ # Init the standalone mode
+ standalone = GlancesStandalone(config=config, args=args)
+
+ # Start the standalone (CLI) loop
+ standalone.serve_forever()
+
+
+def start_clientbrowser(config, args):
+ """Start the browser client mode"""
+ logger.info("Start client mode (browser)")
# Share global var
- global core, standalone, client, server, webserver
+ global client
- # Create the Glances main instance
- core = GlancesMain()
+ # Import the Glances client browser module
+ from glances.client_browser import GlancesClientBrowser
- # Catch the CTRL-C signal
- signal.signal(signal.SIGINT, __signal_handler)
+ # Init the client
+ client = GlancesClientBrowser(config=config, args=args)
- # Glances can be ran in standalone, client or server mode
- if core.is_standalone() and not WINDOWS:
- logger.info("Start standalone mode")
+ # Start the client loop
+ client.serve_forever()
- # Import the Glances standalone module
- from glances.standalone import GlancesStandalone
+ # Shutdown the client
+ client.end()
- # Init the standalone mode
- standalone = GlancesStandalone(config=core.get_config(),
- args=core.get_args())
- # Start the standalone (CLI) loop
- standalone.serve_forever()
+def start_client(config, args):
+ """Start the client mode"""
+ logger.info("Start client mode")
- elif core.is_client() and not WINDOWS:
- if core.is_client_browser():
- logger.info("Start client mode (browser)")
+ # Share global var
+ global client
- # Import the Glances client browser module
- from glances.client_browser import GlancesClientBrowser
+ # Import the Glances client browser module
+ from glances.client import GlancesClient
- # Init the client
- client = GlancesClientBrowser(config=core.get_config(),
- args=core.get_args())
+ # Init the client
+ client = GlancesClient(config=config, args=args)
- else:
- logger.info("Start client mode")
+ # Test if client and server are in the same major version
+ if not client.login():
+ logger.critical("The server version is not compatible with the client")
+ sys.exit(2)
- # Import the Glances client module
- from glances.client import GlancesClient
+ # Start the client loop
+ client.serve_forever()
- # Init the client
- client = GlancesClient(config=core.get_config(),
- args=core.get_args())
+ # Shutdown the client
+ client.end()
- # Test if client and server are in the same major version
- if not client.login():
- logger.critical("The server version is not compatible with the client")
- sys.exit(2)
- # Start the client loop
- client.serve_forever()
+def start_server(config, args):
+ """Start the server mode"""
+ logger.info("Start server mode")
- # Shutdown the client
- client.end()
+ # Share global var
+ global server
- elif core.is_server():
- logger.info("Start server mode")
+ # Import the Glances server module
+ from glances.server import GlancesServer
- # Import the Glances server module
- from glances.server import GlancesServer
+ server = GlancesServer(cached_time=args.cached_time,
+ config=config,
+ args=args)
+ print('Glances server is running on {}:{}'.format(args.bind_address, args.port))
- args = core.get_args()
+ # Set the server login/password (if -P/--password tag)
+ if args.password != "":
+ server.add_user(args.username, args.password)
- server = GlancesServer(cached_time=args.cached_time,
- config=core.get_config(),
- args=args)
- print('Glances server is running on {}:{}'.format(args.bind_address, args.port))
- # Set the server login/password (if -P/--password tag)
- if args.password != "":
- server.add_user(args.username, args.password)
+ # Start the server loop
+ server.serve_forever()
- # Start the server loop
- server.serve_forever()
+ # Shutdown the server?
+ server.server_close()
- # Shutdown the server?
- server.server_close()
- elif core.is_webserver() or (core.is_standalone() and WINDOWS):
- logger.info("Start web server mode")
+def start_webserver(config, args):
+ """Start the Web server mode"""
+ logger.info("Start web server mode")
+
+ # Share global var
+ global webserver
- # Import the Glances web server module
- from glances.webserver import GlancesWebServer
+ # Import the Glances web server module
+ from glances.webserver import GlancesWebServer
- args = core.get_args()
+ # Init the web server mode
+ webserver = GlancesWebServer(config=config, args=args)
+ # Start the web server loop
+ webserver.serve_forever()
+
+
+def main():
+ """Main entry point for Glances.
+
+ Select the mode (standalone, client or server)
+ Run it...
+ """
+ # Log Glances and PSutil version
+ logger.info('Start Glances {}'.format(__version__))
+ logger.info('{} {} and PSutil {} detected'.format(
+ platform.python_implementation(),
+ platform.python_version(),
+ psutil_version))
+
+ # Share global var
+ global core
+
+ # Create the Glances main instance
+ core = GlancesMain()
+ config = core.get_config()
+ args = core.get_args()
+
+ # Catch the CTRL-C signal
+ signal.signal(signal.SIGINT, __signal_handler)
+
+ # Glances can be ran in standalone, client or server mode
+ if core.is_standalone() and not WINDOWS:
+ start_standalone(config=config, args=args)
+ elif core.is_client() and not WINDOWS:
+ if core.is_client_browser():
+ start_clientbrowser(config=config, args=args)
+ else:
+ start_client(config=config, args=args)
+ elif core.is_server():
+ start_server(config=config, args=args)
+ elif core.is_webserver() or (core.is_standalone() and WINDOWS):
# Web server mode replace the standalone mode on Windows OS
# In this case, try to start the web browser mode automaticaly
if core.is_standalone() and WINDOWS:
args.open_web_browser = True
-
- # Init the web server mode
- webserver = GlancesWebServer(config=core.get_config(),
- args=args)
-
- # Start the web server loop
- webserver.serve_forever()
+ start_webserver(config=config, args=args)