From 42c130defd4304fed399c45c57c9e1e819f8ea62 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 19 Dec 2022 23:23:15 -0800 Subject: [PATCH 1/6] Improve logging --- sphinx_gitstamp/__init__.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index 9969b3c..1c56483 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -13,9 +13,12 @@ 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 +61,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( + "Can't fetch git history for {:s}.rst.".format(fullpagename), + 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( + "{:s}: Can't parse datestamp () {:s} ) for gitstamp, output won't have last updated" + "time.".format(pagename, updated), + type="gitstamp", + subtype="datestamp", ) pass @@ -92,10 +98,16 @@ def what_build_am_i(app): 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.error( + "gitstamp extension enabled, but no git repository found. No git datestamps will be" + "generated.", + type="gitstamp", + subtype="repo", ) else: app.connect("html-page-context", page_context_handler) From c2c5ce7a79d2a73fcfe2d76b1a17de72bf8125c7 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Mon, 19 Dec 2022 23:29:07 -0800 Subject: [PATCH 2/6] Improve logging --- tests/test_simple.py | 1 + tox.ini | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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() diff --git a/tox.ini b/tox.ini index c66c778..81f1d9f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3{7,8,9}-sphinx{2,3,4,5,last} +envlist = py3{7,8,9}-sphinx{2,3,4,5,last}, flake8 [testenv] deps = From 50b3dbd1cba0ba9d5e5eefa16866829b5012ded0 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Tue, 20 Dec 2022 22:03:31 -0800 Subject: [PATCH 3/6] Improve warning text --- sphinx_gitstamp/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index 1c56483..b0ad129 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -62,15 +62,15 @@ def page_context_handler(app, pagename, templatename, context, doctree): except git.exc.GitCommandError: # File doesn't exist or something else went wrong. logger.warning( - "Can't fetch git history for {:s}.rst.".format(fullpagename), + "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format(fullpagename), type="gitstamp", subtype="file", ) except ValueError: # Datestamp can't be parsed. logger.warning( - "{:s}: Can't parse datestamp () {:s} ) for gitstamp, output won't have last updated" - "time.".format(pagename, updated), + "sphinx-gitstamp: Can't parse datestamp ({:s}) for gitstamp, output on {:s} won't have last updated" + "time.".format(updated, pagename), type="gitstamp", subtype="datestamp", ) @@ -103,8 +103,8 @@ def what_build_am_i(app): type="gitstamp", subtype="info", ) - logger.error( - "gitstamp extension enabled, but no git repository found. No git datestamps will be" + logger.warning( + "sphinx-gitstamp: No git repository found. No git datestamps will be" "generated.", type="gitstamp", subtype="repo", From d9bfa186c468cba439e96531a1edd90d02e9bfdd Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Tue, 20 Dec 2022 22:11:29 -0800 Subject: [PATCH 4/6] Clean up wording --- sphinx_gitstamp/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index b0ad129..7d54a63 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -62,14 +62,16 @@ def page_context_handler(app, pagename, templatename, context, doctree): except git.exc.GitCommandError: # File doesn't exist or something else went wrong. logger.warning( - "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format(fullpagename), + "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format( + fullpagename + ), type="gitstamp", subtype="file", ) except ValueError: # Datestamp can't be parsed. logger.warning( - "sphinx-gitstamp: Can't parse datestamp ({:s}) for gitstamp, output on {:s} won't have last updated" + "sphinx-gitstamp: Can't parse datestamp ({:s}), output on {:s} won't have last updated" "time.".format(updated, pagename), type="gitstamp", subtype="datestamp", From b09aa5905a7d91c2779b49e2701ef578d78fb177 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 21 Dec 2022 18:42:36 -0800 Subject: [PATCH 5/6] More logging cleanup --- sphinx_gitstamp/__init__.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index 7d54a63..a6e10a5 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -12,7 +12,6 @@ import os import sys -from sphinx import errors from sphinx.util.logging import getLogger __version__ = "0.4.0" @@ -62,9 +61,7 @@ def page_context_handler(app, pagename, templatename, context, doctree): except git.exc.GitCommandError: # File doesn't exist or something else went wrong. logger.warning( - "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format( - fullpagename - ), + "sphinx-gitstamp: Can't fetch git history for {:s}.rst.".format(pagename), type="gitstamp", subtype="file", ) @@ -87,14 +84,13 @@ 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 From a0134de31421b90dc3cb141efc02dfb7955447d4 Mon Sep 17 00:00:00 2001 From: Jared Dillard Date: Wed, 21 Dec 2022 18:43:29 -0800 Subject: [PATCH 6/6] remove flake8 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 81f1d9f..c66c778 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py3{7,8,9}-sphinx{2,3,4,5,last}, flake8 +envlist = py3{7,8,9}-sphinx{2,3,4,5,last} [testenv] deps =