Skip to content

Commit

Permalink
Merge branch 'release-0.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
pignacio committed Apr 30, 2015
2 parents 0f98f51 + d8e59fb commit bef336c
Show file tree
Hide file tree
Showing 20 changed files with 531 additions and 282 deletions.
19 changes: 17 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
.. :changelog:
History
-------
#######

0.0.2 (2015-04-30)
------------------

* Simpler ``NagiosLogger`` interface. (backwards incompatible)

* Feature: ``Testcase.assertSoftCalledWith`` method for mock partial call
assertions.

* FIX: ``NagiosLogger`` was not restoring stdout.

* FIX: ``NagiosLogger`` choked when output contained empty lines.

* Feature: ``namedtuple_with_defaults`` uses a ``lambda`` instead of a
classmethod for mutable defaults. (backwards incompatible)

0.0.1 (2015-03-29)
----------------------
------------------

* Packaging
* Namedtuple with defaults
Expand Down
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ lint:
@if ! which pylint >/dev/null; then echo "pylint not installed.\nRun:\n pip install pylint" && false; fi
@if ! which pep8 >/dev/null; then echo "pep8 not installed.\nRun:\n pip install pep8" && false; fi
-pep8 pignacio_scripts tests
pylint pignacio_scripts tests
-find pignacio_scripts -name "*.py" -exec pylint {} +
-find tests -name "*.py" -exec pylint {} +

test: test-deps
test: test-deps clean-pyc
python setup.py nosetests

test-cover: test-deps
test-cover: test-deps clean-pyc
python setup.py nosetests --with-coverage --cover-package=pignacio_scripts

test-all: test-deps
test-all: test-deps clean-pyc
@if ! which tox >/dev/null; then echo "tox not installed.\nRun:\n pip install tox" && false; fi
tox

test-deps:
pip install -r test_requirements.txt

coverage: test-deps
coverage: test-deps clean-pyc
coverage run --source pignacio_scripts setup.py nosetests
make coverage-show

Expand Down Expand Up @@ -90,3 +91,6 @@ dist-deps:

install: clean
python setup.py install

yapf:
find pignacio_scripts -name "*.py" -exec yapf --diff {} +
15 changes: 13 additions & 2 deletions docs/nagios/nagios-logger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ creating and debugging good checks complex:
* stderr is not reported. So, if a naive python alarm fails unexpectedly, all
you get through the web interface is a warn status and "(null)" as the
script output.

* The first line of output is special, beacuse its shown in the service list
view. I found providing the correct information in this first line can help
a lot when it comes to finding whats wrong in a failing system. A simple
print or log statement anywhere can break this.

* Empty lines are replaced with a literal ``\n``.

:py:class:`~pignacio_scripts.nagios.logger.NagiosLogger` does a variety of
things to solve this problems, and help when writing checks, namely:

* stderr is redirected to classical stdout. If something writes there, you get
it on the service output.

* stdout is buffered, and rendered at the end of the service output. This
allows for print statements and logging in the middle of the check without
worrying about moving the first line.

* Empty lines are replaced with (non-empty) whitespace.

* logging is configured at INFO level by default.

* Unexpected exceptions are logged and the check exits in a graceful manner
with a UNK status.

* Provides an API to log errors, warnings and important stuff. This is used to
determine the check status (OK/WARN/CRIT), show the most relevant stuff in
the first line, and create a report between the first line and the buffered
stdout

* Remembers the exit code for each status for you ;)

An example showing this features and the basic usage:
Expand All @@ -43,9 +53,10 @@ An example showing this features and the basic usage:
size = get_queue_size()
NagiosLogger.important('Queue size: {}'.format(size))
if size > 1000:
NagiosLogger.error('Queue size is bigger than 1000', 'QueueSize')
NagiosLogger.error('Queue size is bigger than 1000')
if size > 200:
NagiosLogger.warning('Queue size is bigger than 200', 'QueueSize')
NagiosLogger.warning('Queue size is bigger than 200')
return "This will be shown in the first line if there were no errors"
if __name__ == '__main__':
Expand Down
13 changes: 5 additions & 8 deletions docs/namedtuple/namedtuple-with-defaults.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ Construction fails if one of the fields without default is missing:
[..]
ValueError: Missing argument for namedtuple: 'a'
If you need a mutable default value (``[]``, ``{}``, etc.), you can override
the ``_get_defaults`` classmethod.
If you need a mutable default value (``[]``, ``{}``, etc.), you can pass a
function with no arguments that returns the defaults.
.. code:: python
>>> _MyTuple = namedtuple_with_defaults('MyTuple', ['value', 'error_list'])
>>> MyTuple = namedtuple_with_defaults(
'MyTuple', ['value', 'error_list'],
defaults=lambda: {'error_list': []})
>>>
>>> class MyTuple(_MyTuple):
... @classmethod
... def _get_defaults(cls):
... return {'error_list': []}
...
>>> MyTuple(value=3)
MyTuple(value=3, error_list=[])
19 changes: 17 additions & 2 deletions docs/testing/testcase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ If you need capturing stdout for all tests in a `TestCase`, you can:
print "Hello world!"
self.assertEqual(self.stdout.getvalue(), 'Hello world!\n')
There's a function for checking partial calls to a ``mock`` :

.. code:: python
def test_partial_calls(self):
mock = Mock()
mock(1, kwarg=2)
self.assertSoftCalledWith(mock, 1) # OK
self.assertSoftCalledWith(mock, kwarg=2) # OK
self.assertSoftCalledWith(mock, 1, kwarg=2) # OK
self.assertSoftCalledWith(mock, 2) # FAIL
self.assertSoftCalledWith(mock, kwarg=1) # FAIL
Lastly, a little helper function for checking sizes:

.. code:: python
def test_some_size(self):
self.assertSize([1, 2, 3], 3)
def test_some_size(self):
self.assertSize([1, 2, 3], 3)
2 changes: 1 addition & 1 deletion pignacio_scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

__author__ = 'Ignacio Rossi'
__email__ = 'rossi.ignacio@gmail.com '
__version__ = '0.0.1'
__version__ = '0.0.2'
10 changes: 4 additions & 6 deletions pignacio_scripts/makefile_generator/src/generate_cpp_makefile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from __future__ import unicode_literals, print_function
from argparse import ArgumentParser, ArgumentTypeError
import collections
Expand All @@ -8,7 +7,6 @@
import re
import yaml


INCLUDE_RE = r'\s*#include\s+"([^"]*)"'
HEADER_EXTENSIONS = [".h", ".hpp"]
SOURCE_EXTENSIONS = [".c", ".cpp"]
Expand All @@ -34,10 +32,9 @@ def __init__(self, config):
self.compiler = self._get_field("compiler", description="Compiler")
self.cargs = self._get_field("compiler_args", default="")
self.source_dir = os.path.normpath(
self._get_field("source_dir", description="Sources dir")
)
self.program = self._get_field("program",
description="Program name")
self._get_field("source_dir",
description="Sources dir"))
self.program = self._get_field("program", description="Program name")
self.compiled_subdir = self._get_field("compiled_subdir",
default=".compiled")

Expand Down Expand Up @@ -135,6 +132,7 @@ def _get_object_filename(source_path, config):
def _print_makefile(config, fout):
def _print(message=""):
print(message, file=fout)

dependencies = _walk_source_dir(config.source_dir)
_print("# Automatically generated makefile. DO NOT EDIT BY HAND")
_print("# Generated on {}".format(datetime.datetime.now()))
Expand Down
1 change: 0 additions & 1 deletion pignacio_scripts/nagios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@

from .logger import NagiosLogger


__all__ = ['NagiosLogger']
Loading

0 comments on commit bef336c

Please sign in to comment.