Replace getters/setters with properties

...because it's the Pythonic way to do it.
This commit is contained in:
Alessio Sergi 2015-02-26 18:26:26 +01:00
parent 0f083d215a
commit 929b832b74

View File

@ -53,8 +53,8 @@ class GlancesClient(object):
self.args = args self.args = args
self.config = config self.config = config
# Client mode: # Default client mode
self.set_mode() self._client_mode = 'glances'
# Return to browser or exit # Return to browser or exit
self.return_to_browser = return_to_browser self.return_to_browser = return_to_browser
@ -84,21 +84,19 @@ class GlancesClient(object):
else: else:
logger.error(msg) logger.error(msg)
def set_mode(self, mode='glances'): @property
def client_mode(self):
"""Get the client mode."""
return self._client_mode
@client_mode.setter
def client_mode(self, mode):
"""Set the client mode. """Set the client mode.
- 'glances' = Glances server (default) - 'glances' = Glances server (default)
- 'snmp' = SNMP (fallback) - 'snmp' = SNMP (fallback)
""" """
self.mode = mode self._client_mode = mode
def get_mode(self):
"""Get the client mode.
- 'glances' = Glances server (default)
- 'snmp' = SNMP (fallback)
"""
return self.mode
def login(self): def login(self):
"""Logon to the server.""" """Logon to the server."""
@ -106,15 +104,14 @@ class GlancesClient(object):
if not self.args.snmp_force: if not self.args.snmp_force:
# First of all, trying to connect to a Glances server # First of all, trying to connect to a Glances server
self.set_mode('glances')
client_version = None client_version = None
try: try:
client_version = self.client.init() client_version = self.client.init()
except socket.error as err: except socket.error as err:
# Fallback to SNMP # Fallback to SNMP
logger.error("Connection to Glances server failed (%s)" % err) self.client_mode = 'snmp'
self.set_mode('snmp') logger.error("Connection to Glances server failed: {0}".format(err))
fallbackmsg = _("Trying fallback to SNMP...") fallbackmsg = _("No Glances server found. Trying fallback to SNMP...")
if not self.return_to_browser: if not self.return_to_browser:
print(fallbackmsg) print(fallbackmsg)
else: else:
@ -128,22 +125,25 @@ class GlancesClient(object):
self.log_and_exit(msg) self.log_and_exit(msg)
return False return False
if self.get_mode() == 'glances' and version.split('.')[0] == client_version.split('.')[0]: if self.client_mode == 'glances':
# Check that both client and server are in the same major version
if version.split('.')[0] == client_version.split('.')[0]:
# Init stats # Init stats
self.stats = GlancesStatsClient(config=self.config, args=self.args) self.stats = GlancesStatsClient(config=self.config, args=self.args)
self.stats.set_plugins(json.loads(self.client.getAllPlugins())) self.stats.set_plugins(json.loads(self.client.getAllPlugins()))
logger.debug( logger.debug("Client version: {0} / Server version: {1}".format(version, client_version))
"Client version: %s / Server version: %s" % (version, client_version)) else:
elif self.get_mode() == 'glances': self.log_and_exit("Client and server not compatible: \
self.log_and_exit("Client and server not compatible: Client version: %s / Server version: %s" % (version, client_version)) Client version: {0} / Server version: {1}".format(version, client_version))
return False return False
else: else:
self.set_mode('snmp') self.client_mode = 'snmp'
if self.get_mode() == 'snmp': # SNMP mode
if self.client_mode == 'snmp':
logger.info("Trying to grab stats by SNMP...") logger.info("Trying to grab stats by SNMP...")
# Fallback to SNMP if needed
from glances.core.glances_stats import GlancesStatsClientSNMP from glances.core.glances_stats import GlancesStatsClientSNMP
# Init stats # Init stats
@ -166,13 +166,13 @@ class GlancesClient(object):
def update(self): def update(self):
"""Update stats from Glances/SNMP server.""" """Update stats from Glances/SNMP server."""
if self.get_mode() == 'glances': if self.client_mode == 'glances':
return self.update_glances() return self.update_glances()
elif self.get_mode() == 'snmp': elif self.client_mode == 'snmp':
return self.update_snmp() return self.update_snmp()
else: else:
self.end() self.end()
logger.critical("Unknown server mode: {0}".format(self.get_mode())) logger.critical("Unknown server mode: {0}".format(self.client_mode))
sys.exit(2) sys.exit(2)
def update_glances(self): def update_glances(self):
@ -231,7 +231,7 @@ class GlancesClient(object):
# Export stats using export modules # Export stats using export modules
self.stats.export(self.stats) self.stats.export(self.stats)
return self.get_mode() return self.client_mode
def end(self): def end(self):
"""End of the client session.""" """End of the client session."""