Skip to content

Commit

Permalink
Added core docs template
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jun 1, 2016
1 parent 165e269 commit f940dfe
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
12 changes: 8 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_version():
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinxcontrib.plantuml',
# 'sphinx.ext.intersphinx',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
]

Expand Down Expand Up @@ -110,6 +110,10 @@ def which(name, flags=os.X_OK):
# install it
subprocess.call(["fc-cache", "-vf", fontdir])

napoleon_use_ivar = True

autoclass_content = "both"

autodoc_member_order = 'bysource'

# Add any paths that contain templates here, relative to this directory.
Expand All @@ -136,9 +140,9 @@ def which(name, flags=os.X_OK):
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# intersphinx_mapping = {
# 'python': ('http://python.readthedocs.org/en/v2.7.2/', None),
#}
intersphinx_mapping = {
'python': ('http://docs.python.org/2.7/', None),
}

# -- Options for HTML output ----------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions docs/dev/core.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Core API
========

.. module:: malcolm.core

Many classes in Malcolm use the same baseclass that implements some logging
methods:

.. autoclass:: Loggable
:members:

Add more classes here...
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The main documentation for the site is organised into a few sections:

dev/tags
dev/types
dev/core
dev/contributing
dev/release_notes

1 change: 1 addition & 0 deletions malcolm/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from loggable import Loggable
29 changes: 25 additions & 4 deletions malcolm/core/loggable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Loggable(object):
"""Utility class that provides a named logger for a class instance"""

def __init__(self, logger_name):
"""
Args:
logger_name (str): Name of the logger to appear in log messages
"""
super(Loggable, self).__init__()
# The name that we will pass to the logger
self._logger_name = None
Expand All @@ -16,10 +20,27 @@ def set_logger_name(self, logger_name):
"""Change the name of the logger that log_* should call
Args:
logger_name (str): Name of the logger
logger_name (str): Name of the logger to appear in log messages
"""
self._logger_name = logger_name
self._logger = logging.getLogger(self._logger_name)
# set self.log_debug = self._logger.debug etc.
for n in "debug warning info error exception".split():
setattr(self, "log_%s" % n, getattr(self._logger, n))

def log_debug(self, msg, *args, **kwargs):
"""Call :meth:`logging.Logger.debug`"""
self._logger.debug(msg, *args, **kwargs)

def log_info(self, msg, *args, **kwargs):
"""Call :meth:`logging.Logger.info`"""
self._logger.info(msg, *args, **kwargs)

def log_warning(self, msg, *args, **kwargs):
"""Call :meth:`logging.Logger.warning`"""
self._logger.warning(msg, *args, **kwargs)

def log_error(self, msg, *args, **kwargs):
"""Call :meth:`logging.Logger.error`"""
self._logger.error(msg, *args, **kwargs)

def log_exception(self, msg, *args, **kwargs):
"""Call :meth:`logging.Logger.exception`"""
self._logger.exception(msg, *args, **kwargs)
8 changes: 5 additions & 3 deletions tests/test_core/test_loggable.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def test_init(self, mock_logging):
mock_logging.getLogger.assert_called_once_with("foo")

@patch("malcolm.core.loggable.logging")
def test_debug_calls_logger_function(self, mock_logging):
def test_calls_logger_function(self, mock_logging):
l = Loggable("bar")
l.log_debug("hello")
l._logger.debug.assert_called_once_with("hello")
for n in "debug info warning error exception".split():
m = getattr(l, "log_%s" % n)
m("hello", n)
getattr(l._logger, n).assert_called_once_with("hello", n)

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit f940dfe

Please sign in to comment.