Skip to content

Commit

Permalink
Drop usage of black & flake8 in favor of ruff - Refs #1321
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Balder Bach <benjamin@overtag.dk>
  • Loading branch information
oscarmcm and benjaoming committed Dec 4, 2023
1 parent 3660265 commit 626f517
Show file tree
Hide file tree
Showing 94 changed files with 1,299 additions and 642 deletions.
21 changes: 6 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: '5.0.4' # pick a git hash / tag to point to
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
hooks:
- id: flake8
args: ["--max-line-length=213", "--extend-ignore=E203", "--max-complexity=10"]
exclude: "^(.*/migrations/|testproject/testproject/settings/)"
- id: ruff
language_version: python3
- id: ruff-format
language_version: python3
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
Expand All @@ -23,13 +24,3 @@ repos:
src/wiki/static/wiki/bootstrap/js/.*|
.*\.map
)$
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
9 changes: 7 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ def process_docstring(app, what, name, obj, options, lines):
% (field.attname, type(field).__name__, to)
)
else:
lines.append(":type %s: %s" % (field.attname, type(field).__name__))
lines.append(
":type %s: %s" % (field.attname, type(field).__name__)
)

return lines


extlinks = {
"url-issue": ("https://github.com/django-wiki/django-wiki/issues/%s", "#%s"),
"url-issue": (
"https://github.com/django-wiki/django-wiki/issues/%s",
"#%s",
),
}


Expand Down
75 changes: 23 additions & 52 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ dependencies = [
# several extensions have imported internal APIs, so be very
# careful here.
"Markdown>=3.4,<3.6",
"pymdown-extensions",
# Set of extensions for Python Markdown.
"pymdown-extensions>=10.5,<10.6",
]
dynamic = ["version"]

[project.optional-dependencies]
docs = [
"Django>=3.2",
"bleach>=6,<7",
"sphinx>=6,<8",
"sphinx_rtd_theme==2.0.0",
]
Expand Down Expand Up @@ -95,20 +94,28 @@ exclude = [

[tool.hatch.envs.default]
dependencies = [
"black>=22.3.0,<22.11",
"ddt",
"ruff==0.1.3",
"ddt==1.6.0",
"django-functest>=1.2,<1.6",
"flake8>=3.7,<5.1",
"pre-commit",
"pre-commit==3.5.0",
"pytest-cov",
"pytest-django",
"pytest-pythonpath",
"pytest>=6.2.5,<7.3",
]

[tool.hatch.envs.default.scripts]
# Ignore this entrypoint for now, as it's not fully implemented
lint = "flake8 --max-line-length=213 --extend-ignore=E203 --max-complexity=10 --exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg,*/*migrations,testproject src/wiki tests/"
lint = [
"ruff check src/wiki tests/ {args}",
"pre-commit install -f --install-hooks",
"pre-commit run --all-files --show-diff-on-failure",
]
format = "ruff format src/wiki tests/ {args}"
clean = [
"hatch run clean-build",
"hatch run clean-pyc",
"rm -fr htmlcov/",
]
clean-build = [
"rm -fr build",
"rm -fr dist",
Expand All @@ -131,32 +138,12 @@ matrix.django.dependencies = [
]

[tool.hatch.envs.test]
dependencies = [
"ddt",
"django-functest>=1.2,<1.6",
"pytest-cov",
"pytest-django",
"pytest-pythonpath",
"pytest>=6.2.5,<7.3",
"black>=22.3.0,<22.11",
"flake8>=3.7,<5.1",
"pre-commit",
]
matrix-name-format = "dj{value}"

[tool.hatch.envs.test.scripts]
all = [
"sh -c 'testproject/manage.py makemigrations --check'",
"pytest --cov=wiki tests/ {args}",
]
lint = [
"flake8 --max-line-length=213 --extend-ignore=E203 --max-complexity=10 --exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.eggs,*.egg,*/*migrations,testproject src/wiki tests/",
"pre-commit install -f --install-hooks",
"pre-commit run --all-files --show-diff-on-failure",
]
clean = [
"rm -fr .tox/",
"rm -fr htmlcov/",
"pytest tests/ --cov=wiki {args}",
]

[[tool.hatch.envs.test.matrix]]
Expand Down Expand Up @@ -187,8 +174,6 @@ pull = [

[tool.hatch.envs.docs]
dependencies = [
"Django>=3.2",
"bleach>=6,<7",
"sphinx>=6,<8",
"sphinx_rtd_theme==2.0.0",
]
Expand Down Expand Up @@ -289,27 +274,13 @@ exclude_lines = [
"if TYPE_CHECKING:",
]

[tool.black]
line-length = 88
include = '\.pyi?$'
exclude = '''
[tool.ruff]
exclude = ["env", "_build", "CVS", "__pycache__", ".eggs", "*.egg", "*/*migrations", "testproject"]
extend-ignore = ["E203"]
line-length = 79

(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| env
| _build
| buck-out
| build
| dist
)/
)
'''
[tool.ruff.mccabe]
max-complexity = 10

[tool.pytest.ini_options]
django_find_project = false
Expand Down
6 changes: 3 additions & 3 deletions src/wiki/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
revisions = models.ArticleRevision.objects.select_related("article").filter(
article=self.instance
)
revisions = models.ArticleRevision.objects.select_related(
"article"
).filter(article=self.instance)
self.fields["current_revision"].queryset = revisions
else:
self.fields[
Expand Down
4 changes: 3 additions & 1 deletion src/wiki/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def ready(self):
checks.check_for_obsolete_installed_apps,
checks.Tags.obsolete_installed_apps,
)
register(checks.check_for_context_processors, checks.Tags.context_processors)
register(
checks.check_for_context_processors, checks.Tags.context_processors
)
register(
checks.check_for_fields_in_custom_user_model,
checks.Tags.fields_in_custom_user_model,
Expand Down
32 changes: 24 additions & 8 deletions src/wiki/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@

#: The editor class to use -- maybe a 3rd party or your own...? You can always
#: extend the built-in editor and customize it!
EDITOR = getattr(django_settings, "WIKI_EDITOR", "wiki.editors.markitup.MarkItUp")
EDITOR = getattr(
django_settings, "WIKI_EDITOR", "wiki.editors.markitup.MarkItUp"
)

#: Whether to use Bleach or not. It's not recommended to turn this off unless
#: you know what you're doing and you don't want to use the other options.
MARKDOWN_SANITIZE_HTML = getattr(django_settings, "WIKI_MARKDOWN_SANITIZE_HTML", True)
MARKDOWN_SANITIZE_HTML = getattr(
django_settings, "WIKI_MARKDOWN_SANITIZE_HTML", True
)

#: Arguments for the Markdown instance, as a dictionary. The "extensions" key
#: should be a list of extra extensions to use besides the built-in django-wiki
Expand Down Expand Up @@ -52,7 +56,9 @@
"markdown.extensions.abbr",
"markdown.extensions.sane_lists",
],
"extension_configs": {"wiki.plugins.macros.mdx.toc": {"title": _("Contents")}},
"extension_configs": {
"wiki.plugins.macros.mdx.toc": {"title": _("Contents")}
},
}
MARKDOWN_KWARGS.update(getattr(django_settings, "WIKI_MARKDOWN_KWARGS", {}))

Expand Down Expand Up @@ -108,7 +114,9 @@

#: Allowed inline styles in Markdown article contents, default is no styles
#: (empty list).
MARKDOWN_HTML_STYLES = getattr(django_settings, "WIKI_MARKDOWN_HTML_STYLES", [])
MARKDOWN_HTML_STYLES = getattr(
django_settings, "WIKI_MARKDOWN_HTML_STYLES", []
)

_project_defined_attrs = getattr(
django_settings, "WIKI_MARKDOWN_HTML_ATTRIBUTE_WHITELIST", False
Expand Down Expand Up @@ -187,7 +195,9 @@

#: A function returning True/False if a user has permission to change
#: read/write access for groups and others.
CAN_CHANGE_PERMISSIONS = getattr(django_settings, "WIKI_CAN_CHANGE_PERMISSIONS", None)
CAN_CHANGE_PERMISSIONS = getattr(
django_settings, "WIKI_CAN_CHANGE_PERMISSIONS", None
)

#: Specifies if a user has access to soft deletion of articles.
CAN_DELETE = getattr(django_settings, "WIKI_CAN_DELETE", None)
Expand All @@ -209,7 +219,9 @@

#: Globally enable create access for anonymous users.
#: Defaults to ``ANONYMOUS_WRITE``.
ANONYMOUS_CREATE = getattr(django_settings, "WIKI_ANONYMOUS_CREATE", ANONYMOUS_WRITE)
ANONYMOUS_CREATE = getattr(
django_settings, "WIKI_ANONYMOUS_CREATE", ANONYMOUS_WRITE
)

#: Default setting to allow anonymous users upload access. Used in
#: plugins.attachments and plugins.images, and can be overwritten in
Expand Down Expand Up @@ -269,7 +281,9 @@
REVISIONS_PER_HOUR = getattr(django_settings, "WIKI_REVISIONS_PER_HOUR", 60)

#: Maximum allowed revisions per minute for any given user or IP.
REVISIONS_PER_MINUTES = getattr(django_settings, "WIKI_REVISIONS_PER_MINUTES", 5)
REVISIONS_PER_MINUTES = getattr(
django_settings, "WIKI_REVISIONS_PER_MINUTES", 5
)

#: Maximum allowed revisions per hour for any anonymous user and any IP.
REVISIONS_PER_HOUR_ANONYMOUS = getattr(
Expand All @@ -292,7 +306,9 @@
###########

#: Default Django storage backend to use for images, attachments etc.
STORAGE_BACKEND = getattr(django_settings, "WIKI_STORAGE_BACKEND", default_storage)
STORAGE_BACKEND = getattr(
django_settings, "WIKI_STORAGE_BACKEND", default_storage
)

#: Use django-sendfile for sending out files? Otherwise the whole file is
#: first read into memory and than send with a mime type based on the file.
Expand Down
8 changes: 6 additions & 2 deletions src/wiki/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def send_file(request, filepath, last_modified=None, filename=None):
if settings.USE_SENDFILE:
response = django_sendfile_response(request, filepath)
else:
response = HttpResponse(open(fullpath, "rb").read(), content_type=mimetype)
response = HttpResponse(
open(fullpath, "rb").read(), content_type=mimetype
)

if not last_modified:
response["Last-Modified"] = http_date(statobj.st_mtime)
Expand All @@ -46,7 +48,9 @@ def send_file(request, filepath, last_modified=None, filename=None):
if filename:
filename_escaped = filepath_to_uri(filename)
if "pdf" in mimetype.lower():
response["Content-Disposition"] = "inline; filename=%s" % filename_escaped
response["Content-Disposition"] = (
"inline; filename=%s" % filename_escaped
)
else:
response["Content-Disposition"] = (
"attachment; filename=%s" % filename_escaped
Expand Down
11 changes: 9 additions & 2 deletions src/wiki/core/markdown/mdx/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def run(self, lines):
m.group("code"), self.config, self.md.tab_length, lang=lang
)
placeholder = self.md.htmlStash.store(html)
text = "%s\n%s\n%s" % (text[: m.start()], placeholder, text[m.end() :])
text = "%s\n%s\n%s" % (
text[: m.start()],
placeholder,
text[m.end() :],
)
else:
break
return text.split("\n")
Expand Down Expand Up @@ -135,7 +139,10 @@ def extendMarkdown(self, md):
hiliter.config = self.getConfigs()

add_to_registry(
md.preprocessors, "fenced_code_block", hiliter, ">normalize_whitespace"
md.preprocessors,
"fenced_code_block",
hiliter,
">normalize_whitespace",
)

md.registerExtension(self)
Expand Down
5 changes: 3 additions & 2 deletions src/wiki/core/markdown/mdx/previewlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class PreviewLinksExtension(markdown.Extension):
"""Markdown Extension that sets all anchor targets to _blank when in preview mode"""

def extendMarkdown(self, md):

add_to_registry(md.treeprocessors, "previewlinks", PreviewLinksTree(md), "_end")
add_to_registry(
md.treeprocessors, "previewlinks", PreviewLinksTree(md), "_end"
)


class PreviewLinksTree(Treeprocessor):
Expand Down
6 changes: 4 additions & 2 deletions src/wiki/core/markdown/mdx/responsivetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class ResponsiveTableExtension(markdown.Extension):
"""Wraps all tables with Bootstrap's table-responsive class"""

def extendMarkdown(self, md):

add_to_registry(
md.treeprocessors, "responsivetable", ResponsiveTableTree(md), "_end"
md.treeprocessors,
"responsivetable",
ResponsiveTableTree(md),
"_end",
)


Expand Down
3 changes: 2 additions & 1 deletion src/wiki/core/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def page(self, number):
def page_range(self):
left = max(self.last_accessed_page_number - self.side_pages, 2)
right = min(
self.last_accessed_page_number + self.side_pages + 1, self.num_pages
self.last_accessed_page_number + self.side_pages + 1,
self.num_pages,
)
pages = []
if self.num_pages > 0:
Expand Down
11 changes: 9 additions & 2 deletions src/wiki/core/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def can_read(article, user):
if user == article.owner:
return True
if article.group_read:
if article.group and user.groups.filter(id=article.group.id).exists():
if (
article.group
and user.groups.filter(id=article.group.id).exists()
):
return True
if article.can_moderate(user):
return True
Expand All @@ -53,7 +56,11 @@ def can_write(article, user):
if user == article.owner:
return True
if article.group_write:
if article.group and user and user.groups.filter(id=article.group.id).exists():
if (
article.group
and user
and user.groups.filter(id=article.group.id).exists()
):
return True
if article.can_moderate(user):
return True
Expand Down

0 comments on commit 626f517

Please sign in to comment.