Skip to content

Commit

Permalink
Enh: Pylint (Review of PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Durieux committed Jul 19, 2015
1 parent f175c10 commit 06bced5
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 53 deletions.
6 changes: 5 additions & 1 deletion alignak/bin/_deprecated_VERSION.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class DeprecatedAlignakBin(CustomModule):
@property
def VERSION(self):
"""Any code importing, or using, `VERSION` from alignak.bin will have
this deprecation warning emitted, *if* deprecation warnings are enabled."""
this deprecation warning emitted, *if* deprecation warnings are enabled.
:return: version number
:rtype: str
"""
warnings.warn(
'`alignak.bin.VERSION` is deprecated version attribute'
' and will be removed in a future release.\n'
Expand Down
154 changes: 112 additions & 42 deletions alignak/bin/alignak
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ except ImportError:
from alignak.bin import VERSION

from alignak.log import logger, cprint, nagFormatter
"""TODO: cprint can't be load from alignak.log"""
from alignak.objects.config import Config


logger.setLevel('WARNING')
logger.handlers[0].setFormatter(nagFormatter)


# Handle some signals
def sig_handler(signalnum, handle):
""" Handle some signals """
Expand Down Expand Up @@ -116,13 +118,28 @@ else:
# This will allow to add comments to the generated configuration file, once
# by section.
class ConfigParserWithComments(ConfigParser.RawConfigParser):

def add_comment(self, section, comment):
"""
Add comment in section
:param section:
:type section:
:param comment:
:type comment: str
:return: None
"""
if not comment.startswith('#'):
comment = '#' + comment
self.set(section, ';%s' % (comment,), None)

def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
"""Write an .ini-format representation of the configuration state.
:param fp: file processing
:type fp: file
:return: None
"""
if self._defaults:
fp.write("[%s]\n" % ConfigParser.DEFAULTSECT)
for (key, value) in self._defaults.items():
Expand All @@ -135,6 +152,16 @@ class ConfigParserWithComments(ConfigParser.RawConfigParser):
fp.write("\n")

def _write_item(self, fp, key, value):
"""Write item
:param fp: file processing
:type fp: file
:param key:
:type key:
:param value: None of string value
:type value: None | str
:return: None
"""
if key.startswith(';') and value is None:
fp.write("%s\n" % (key[1:],))
else:
Expand Down Expand Up @@ -175,9 +202,14 @@ class CLICommander(object):

self.completion_matches = []


def load_cli_mods(self, opts):
# Main list of keywords for the first parameter
"""
Main list of keywords for the first parameter
:param opts: options
:type opts:
:return: None
"""
self.keywords = {}
if 'paths' not in self.config or 'cli' not in self.config.get('paths', []):
# We are dign the init, so bail out
Expand Down Expand Up @@ -244,8 +276,12 @@ class CLICommander(object):

logger.debug('We load the keywords %s' % self.keywords)


def loop(self):
"""
Loop
:return: None
"""
if not self.init_done:
logger.error('CLI loading not done: missing configuration data. Please run --init')
return
Expand Down Expand Up @@ -285,14 +321,25 @@ class CLICommander(object):
logger.debug("WANT TO CALL WITH ARGS %s" % args)
self.one_loop(args)


def execute_shell(self, line):
"""
Execute shell
:param line: command line
:type line: str
:return: None
"""
output = os.popen(line).read()
print output


# Execute a function based on the command line
def one_loop(self, command_args):
"""
Execute a function based on the command line
:param command_args: arguments list
:type command_args: list
:return: None
"""
if not self.init_done:
logger.error('CLI loading not done: missing configuration data. Please run --init')
return
Expand Down Expand Up @@ -337,11 +384,19 @@ class CLICommander(object):
str(cmd_opts) + str(type(cmd_opts)) + str(dir(cmd_opts)))
f(*cmd_args, **cmd_opts.__dict__)


# Complete is a bit strange in readline.
# It will call it as it do not answser None, by increasing the
# state int for each call. So don't loop forever!
def complete(self, text, state):
"""
Complete is a bit strange in readline.
It will call it as it do not answer None, by increasing the
state int for each call. So don't loop forever!
:param text:
:type text:
:param state:
:type state:
:return: list of complete commands
:rtype: list | None
"""
# print "STATE?", text, state

# New completion call
Expand Down Expand Up @@ -372,43 +427,58 @@ class CLICommander(object):
return response





def write_ini_file(cfg, CLI, opts):
new_cfg = ConfigParserWithComments()
modify = False
if not cfg:
cfg = ConfigParser.ConfigParser()
"""
Write ini file
:param cfg: ConfigParser object
:type cfg: object
:param CLI:
:type CLI: object
:param opts: options
:type opts: object
:return: None
"""
new_cfg = ConfigParserWithComments()
modify = False
if not cfg:
cfg = ConfigParser.ConfigParser()

# Import data from the loaded configuration
for section in cfg.sections():
if not new_cfg.has_section(section):
new_cfg.add_section(section)
for (k, v) in cfg.items(section):
new_cfg.set(section, k, v)

for (s, d) in CLI.ini_defaults.iteritems():
comment = d.get('comment', '')
values = d.get('values', [])
if not cfg.has_section(s) and not new_cfg.has_section(s):
print "Creating ini section", s
new_cfg.add_section(s)
# Import data from the loaded configuration
for section in cfg.sections():
if not new_cfg.has_section(section):
new_cfg.add_section(section)
for (k, v) in cfg.items(section):
new_cfg.set(section, k, v)

for (s, d) in CLI.ini_defaults.iteritems():
comment = d.get('comment', '')
values = d.get('values', [])
if not cfg.has_section(s) and not new_cfg.has_section(s):
print "Creating ini section", s
new_cfg.add_section(s)
modify = True
if comment:
new_cfg.add_comment(s, comment)
for (k, v) in values:
if not cfg.has_option(s, k):
new_cfg.set(s, k, v)
modify = True
if comment:
new_cfg.add_comment(s, comment)
for (k, v) in values:
if not cfg.has_option(s, k):
new_cfg.set(s, k, v)
modify = True
# print new_cfg.items(s)
if modify:
print "Saving the new configuration file", opts.iniconfig
with open(opts.iniconfig, 'wb') as configfile:
new_cfg.write(configfile)
# print new_cfg.items(s)
if modify:
print "Saving the new configuration file", opts.iniconfig
with open(opts.iniconfig, 'wb') as configfile:
new_cfg.write(configfile)


def main(custom_args=None):
"""
Main
:param custom_args: custom arguments
:type custom_args: None | list
:return: None
"""
parser = optparse.OptionParser(
'',
version="%prog " + VERSION,
Expand Down
5 changes: 3 additions & 2 deletions alignak/bin/alignak_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

'''
"""
This class is an interface for the Broker
The broker listens to the Arbiter for the configuration sent through
the given port as first argument.
Expand All @@ -53,7 +53,7 @@
listening the arbiter (one a timeout)
In case the arbiter has a new conf to send, the broker forget its old
schedulers (and their associated broks) and take the new ones instead.
'''
"""

import os
import sys
Expand All @@ -63,6 +63,7 @@
from alignak.daemons.brokerdaemon import Broker
from alignak import __version__


def main():
"""Parse args and run main daemon function
Expand Down
4 changes: 2 additions & 2 deletions alignak/bin/alignak_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

'''
"""
This class is the application that launches checks
The poller listens to the Arbiter for the configuration sent through
the given port as first argument.
Expand All @@ -52,7 +52,7 @@
listening the arbiter (one a timeout)
In case the arbiter has a new conf to send, the poller forget its old
schedulers (and the associated checks) and take the new ones instead.
'''
"""

import sys
import os
Expand Down
4 changes: 2 additions & 2 deletions alignak/bin/alignak_reactionner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

'''
"""
This class is an application that launches actions like
notifications or event handlers
The reactionner listens to the Arbiter for the configuration sent through
Expand All @@ -53,7 +53,7 @@
on listening the arbiter (one a timeout)
In case the arbiter has a new conf to send, the reactionner forget its old
schedulers (and the associated actions) and take the new ones instead.
'''
"""

import sys
import os
Expand Down
4 changes: 2 additions & 2 deletions alignak/bin/alignak_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.

'''
"""
This class is an interface for the Receiver
The receiver listens to the Arbiter for the configuration sent through
the given port as first argument.
Expand All @@ -51,7 +51,7 @@
listening the arbiter (one a timeout)
In case the arbiter has a new conf to send, the receiver forget its old
schedulers (and their associated broks) and take the new ones instead.
'''
"""

import os
import sys
Expand Down
4 changes: 2 additions & 2 deletions alignak/bin/alignak_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
# If the implementation is easy to explain, it may be a good idea.
# Namespaces are one honking great idea -- let's do more of those!

'''
"""
This class is the application in charge of scheduling
The scheduler listens to the Arbiter for the configuration sent through
the given port as first argument.
Expand All @@ -84,7 +84,7 @@
listening the arbiter (one a timeout)
In case the arbiter has a new conf to send, the scheduler is stopped
and a new one is created.
'''
"""
import os
import sys
import optparse
Expand Down

0 comments on commit 06bced5

Please sign in to comment.