diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a459da1 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +# Install pre-commit hooks via +# pre-commit install + +repos: + + - repo: https://github.com/PyCQA/isort + rev: 5.10.1 + hooks: + - id: isort + + - repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + + - repo: https://github.com/PyCQA/flake8 + rev: 6.0.0 + hooks: + - id: flake8 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5d7bf33 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,2 @@ +[tool.isort] +profile = "black" diff --git a/setup.py b/setup.py index cdc1e66..59729fc 100644 --- a/setup.py +++ b/setup.py @@ -1,24 +1,28 @@ -from setuptools import setup import os + +from setuptools import setup + import sphinx_gitstamp -long_description = open('README.rst' if os.path.exists('README.rst') else 'README.md').read() +long_description = open( + "README.rst" if os.path.exists("README.rst") else "README.md" +).read() setup( - name='sphinx-gitstamp', - description='git timestamp generator for Sphinx', + name="sphinx-gitstamp", + description="git timestamp generator for Sphinx", long_description=long_description, classifiers=[ - 'License :: OSI Approved :: BSD License', - 'Topic :: Documentation :: Sphinx', - 'Programming Language :: Python :: 3', - 'Framework :: Sphinx :: Extension', - ], + "License :: OSI Approved :: BSD License", + "Topic :: Documentation :: Sphinx", + "Programming Language :: Python :: 3", + "Framework :: Sphinx :: Extension", + ], version=sphinx_gitstamp.__version__, - author='Jared Dillard', - author_email='jared.dillard@gmail.com', - install_requires=['six', 'sphinx >= 1.2', 'gitpython'], + author="Jared Dillard", + author_email="jared.dillard@gmail.com", + install_requires=["six", "sphinx >= 1.2", "gitpython"], url="https://github.com/jdillard/sphinx-gitstamp", - license='MIT', - packages=['sphinx_gitstamp'], - ) + license="MIT", + packages=["sphinx_gitstamp"], +) diff --git a/sphinx_gitstamp/__init__.py b/sphinx_gitstamp/__init__.py index cb23e77..9969b3c 100644 --- a/sphinx_gitstamp/__init__.py +++ b/sphinx_gitstamp/__init__.py @@ -8,12 +8,13 @@ # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. +import datetime import os import sys -import datetime + from sphinx import errors -__version__ = '0.4.0' +__version__ = "0.4.0" # Gets the datestamp of the latest commit on the given file # Converts the datestamp into something more readable @@ -24,12 +25,13 @@ def page_context_handler(app, pagename, templatename, context, doctree): import git + global g if g is None: # We have already errored about this pass fullpagename = pagename - docsrc = '' + docsrc = "" try: docsrc = app.confdir + "/" if docsrc != "/": @@ -42,7 +44,7 @@ def page_context_handler(app, pagename, templatename, context, doctree): return try: - updated = g.log('--pretty=format:%ai', '-n 1', "%s.rst" % fullpagename) + updated = g.log("--pretty=format:%ai", "-n 1", "%s.rst" % fullpagename) updated = updated[:10] if updated == "": @@ -51,57 +53,62 @@ def page_context_handler(app, pagename, templatename, context, doctree): # that involves getting the source/output pair into the extension. return - context['gitstamp'] = datetime.datetime.strptime( - updated, - "%Y-%m-%d" - ).strftime( - app.config.gitstamp_fmt - ) + context["gitstamp"] = datetime.datetime.strptime(updated, "%Y-%m-%d").strftime( + app.config.gitstamp_fmt + ) 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) + raise errors.ExtensionError( + "Can't fetch git history for %s.rst." % fullpagename + ) 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)) + app.info( + "%s: Can't parse datestamp () %s ) for gitstamp, output \ + won't have last updated time." + % (pagename, updated) + ) pass # Only add the page context handler if we're generating html def what_build_am_i(app): global g - if (app.builder.format != 'html'): + if app.builder.format != "html": return try: import git except ImportError as e: - raise errors.ExtensionError(f"""Unable to import gitpython. \ + raise errors.ExtensionError( + f"""Unable to import gitpython. \ Required to generate html. You may need to run: pip install gitpython. The error was: {e} -""") +""" + ) try: global g - g = git.Git('.') + 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.") + app.warn( + "gitstamp extension enabled, but no git repository found. No \ + git datestamps will be generated." + ) else: - app.connect('html-page-context', page_context_handler) + app.connect("html-page-context", page_context_handler) # We can't immediately add a page context handler: we need to wait until we # know what the build output format is. def setup(app): - app.add_config_value('gitstamp_fmt', "%b %d, %Y", 'html') - app.connect('builder-inited', what_build_am_i) + app.add_config_value("gitstamp_fmt", "%b %d, %Y", "html") + app.connect("builder-inited", what_build_am_i) return { - 'parallel_read_safe': True, - 'parallel_write_safe': True, - 'version': __version__, + "parallel_read_safe": True, + "parallel_write_safe": True, + "version": __version__, } diff --git a/tox.ini b/tox.ini index 7f6fae0..94ff1a0 100644 --- a/tox.ini +++ b/tox.ini @@ -10,3 +10,7 @@ deps = sphinxtip: git+https://github.com/sphinx-doc/sphinx.git#egg=Sphinx-dev commands = pycodestyle sphinx_gitstamp/ + +[flake8] +max-line-length = 100 +extend-ignore = E203