diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index 9969b3c..a6e10a5 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -12,10 +12,12 @@ import os import sys -from sphinx import errors +from sphinx.util.logging import getLogger __version__ = "0.4.0" +logger = getLogger(__name__) + # Gets the datestamp of the latest commit on the given file # Converts the datestamp into something more readable # Skips files whose datestamp we can't parse. @@ -58,15 +60,18 @@ def page_context_handler(app, pagename, templatename, context, doctree): ) except git.exc.GitCommandError: # File doesn't exist or something else went wrong. - raise errors.ExtensionError( - "Can't fetch git history for %s.rst." % fullpagename + logger.warning( + "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format(pagename), + type="gitstamp", + subtype="file", ) except ValueError: # Datestamp can't be parsed. - app.info( - "%s: Can't parse datestamp () %s ) for gitstamp, output \ - won't have last updated time." - % (pagename, updated) + logger.warning( + "sphinx-gitstamp: Can't parse datestamp ({:s}), output on {:s} won't have last updated" + "time.".format(updated, pagename), + type="gitstamp", + subtype="datestamp", ) pass @@ -79,23 +84,28 @@ def what_build_am_i(app): try: import git - except ImportError as e: - raise errors.ExtensionError( - f"""Unable to import gitpython. \ -Required to generate html. You may need to run: pip install gitpython. - -The error was: {e} -""" + except ImportError: + logger.warning( + "sphinx-gitstamp: gitpython not found. Please ensure it is installed.", + type="gitstamp", + subtype="dependency", ) + return try: global g g = git.Git(".") except BaseException: - app.info(sys.exc_info()[0]) - app.warn( - "gitstamp extension enabled, but no git repository found. No \ - git datestamps will be generated." + logger.info( + sys.exc_info()[0], + type="gitstamp", + subtype="info", + ) + logger.warning( + "sphinx-gitstamp: No git repository found. No git datestamps will be" + "generated.", + type="gitstamp", + subtype="repo", ) else: app.connect("html-page-context", page_context_handler) diff --git a/tests/test_simple.py b/tests/test_simple.py index bccdd28..0d54d4d 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -7,5 +7,6 @@ confoverrides={"gitstamp_fmt": "%b %d, %Y"}, ) def test_simple(app, status, warning): + app.warningiserror = True app.build() assert "bar.html" in app.outdir.listdir()