Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install pre-commit with isort, black, and flake8. #7

Merged
merged 1 commit into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.isort]
profile = "black"
34 changes: 19 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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"],
)
59 changes: 33 additions & 26 deletions sphinx_gitstamp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "/":
Expand All @@ -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 == "":
Expand All @@ -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__,
}
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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