Skip to content

Commit

Permalink
chore(pre-commit): use ruff (#261)
Browse files Browse the repository at this point in the history
* build: align with upstream

* build: update pre-commit

* chore: run ruff

* chore: ignore blame on reformat

* ci: fix pip audit invocation
  • Loading branch information
blaggacao authored Aug 14, 2024
1 parent df7b115 commit e1f3521
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 140 deletions.
12 changes: 12 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Since version 2.23 (released in August 2019), git-blame has a feature
# to ignore or bypass certain commits.
#
# This file contains a list of commits that are not likely what you
# are looking for in a blame, such as mass reformatting or renaming.
# You can set this file as a default ignore file for blame by running
# the following command.
#
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs

# Apply ruff autoformatting
7d5ca3b70bf57cded4e43e5211cf064a71d120aa
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
- uses: actions/checkout@v3
- run: |
pip install pip-audit
pip-audit -r ${GITHUB_WORKSPACE}/requirements.txt
pip-audit --desc on .
34 changes: 8 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fail_fast: false

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
files: "wiki.*"
Expand All @@ -20,17 +20,15 @@ repos:
- id: check-yaml
- id: debug-statements

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.7
hooks:
- id: pyupgrade
args: ['--py310-plus']
- id: ruff
name: "Run ruff linter and apply fixes"
args: ["--fix"]

- repo: https://github.com/adityahase/black
rev: 9cb0a69f4d0030cdf687eddf314468b39ed54119
hooks:
- id: black
additional_dependencies: ['click==8.0.4']
- id: ruff-format
name: "Format Python code"

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
Expand All @@ -47,22 +45,6 @@ repos:
wiki/templates/includes/.*|
wiki/public/js/lib/.*
)$
- repo: https://github.com/timothycrosley/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
# W191 - Doesn't like indentation with tabs
# E501 - Doesn't like lines > 79 (some sql queries are ~140)
# E203 - Doesn't like space infront of ':' (isn't pep8 compliant)
# W503 - Doesn't like line break before binary operator (isn't pep8 compliant)
args: ['--ignore=W191,E501,E203,W503']
additional_dependencies: ['flake8-bugbear',]

ci:
autoupdate_schedule: weekly
skip: []
Expand Down
18 changes: 0 additions & 18 deletions MANIFEST.in

This file was deleted.

68 changes: 56 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
[tool.black]
line-length = 99

[tool.isort]
line_length = 99
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
indent = "\t"
profile = "black"
[project]
name = "wiki"
authors = [
{ name = "Frappe Technologies Pvt Ltd", email = "[email protected]"}
]
description = "Simple Wiki App"
requires-python = ">=3.10,<3.13"
readme = "README.md"
dynamic = ["version"]

[project.urls]
Homepage = "https://frappe.io/wiki"
Repository = "https://github.com/frappe/wiki.git"
"Bug Reports" = "https://github.com/frappe/wiki/issues"

[build-system]
requires = ["flit_core >=3.4,<4"]
build-backend = "flit_core.buildapi"


[tool.ruff]
line-length = 110
target-version = "py310"

[tool.ruff.lint]
select = [
"F",
"E",
"W",
"I",
"UP",
"B",
"RUF",
]
ignore = [
"B017", # assertRaises(Exception) - should be more specific
"B018", # useless expression, not assigned to anything
"B023", # function doesn't bind loop variable - will have last iteration's value
"B904", # raise inside except without from
"E101", # indentation contains mixed spaces and tabs
"E402", # module level import not at top of file
"E501", # line too long
"E741", # ambiguous variable name
"F401", # "unused" imports
"F403", # can't detect undefined names from * import
"F405", # can't detect undefined names from * import
"F722", # syntax error in forward type annotation
"W191", # indentation contains tabs
"RUF001", # string contains ambiguous unicode character
]
typing-modules = ["frappe.types.DF"]

[tool.ruff.format]
quote-style = "double"
indent-style = "tab"
docstring-code-format = true
Empty file removed requirements.txt
Empty file.
19 changes: 0 additions & 19 deletions setup.py

This file was deleted.

4 changes: 2 additions & 2 deletions wiki/public/js/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ window.Wiki = class Wiki {
let active_sidebar_item = $(".doc-sidebar .sidebar-item.active");
if (active_sidebar_item.length > 0) {
setTimeout(function () {
let position = active_sidebar_item.offset().top - $(".doc-sidebar").offset().top;
let position =
active_sidebar_item.offset().top - $(".doc-sidebar").offset().top;
$(".doc-sidebar .web-sidebar").get(0).scrollTo(0, position);
}, 50);
}
Expand Down Expand Up @@ -84,7 +85,6 @@ window.Wiki = class Wiki {
switchBanner();

$("body").toggleClass("dark");

});
}

Expand Down
29 changes: 10 additions & 19 deletions wiki/wiki/doctype/migrate_to_wiki/migrate_to_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def migrate_dir(self, root, dirs, files):
wiki_sidebar = frappe.new_doc("Wiki Sidebar")
parent_wiki_sidebar = f"{self.documentation_route}{os.sep}{root[root.find(self.docs_directory) + len(self.docs_directory) + 1: ]}".replace(
"//", "/"
).strip(
"/"
)
).strip("/")
wiki_sidebar_dict = {
"route": f"{parent_wiki_sidebar}{os.sep}{directory}".replace("//", "/"),
"title": directory.capitalize(),
Expand All @@ -101,7 +99,6 @@ def migrate_dir(self, root, dirs, files):
wiki_sidebar_item.save()

def migrate_file(self, root, file, files):

if file == "index.md" and "contents.md" in files:
return
heading_index = -1
Expand All @@ -116,14 +113,10 @@ def migrate_file(self, root, file, files):

parent = f"{self.documentation_route}{os.sep}{root[root.find(self.docs_directory) + len(self.docs_directory) + 1: ]}".replace(
"//", "/"
).strip(
"/"
)
).strip("/")
route = f"{parent}{os.sep}{file[:-3]}"

title = (
lines[heading_index].strip("#").strip(" ") if heading_index != -1 else route.split(os.sep)[-1]
)
title = lines[heading_index].strip("#").strip(" ") if heading_index != -1 else route.split(os.sep)[-1]
content = "".join(lines[heading_index + 1 :]) if heading_index != -1 else "".join(lines)
if "shifted to landing page" in content:
return
Expand All @@ -134,7 +127,6 @@ def migrate_file(self, root, file, files):
content = content.replace(self.docs_directory.strip("w"), f"/{self.documentation_route}")

if file.endswith("index.md") or file.endswith("contents.md"):

try:
with open(f"{root}{os.sep}index.txt") as f:
lines = f.readlines()
Expand Down Expand Up @@ -221,11 +213,8 @@ def create_files(self):
folder = f"{folder}{os.sep}{directory}"

for root, dirs, files in self.assets_tree_generator:

for directory in dirs:
fold = (
f"{folder}{os.sep}{root[root.find(self.assets_directory) + len(self.assets_directory) + 1:]}"
)
fold = f"{folder}{os.sep}{root[root.find(self.assets_directory) + len(self.assets_directory) + 1:]}"
fold = fold.replace(f"{os.sep}{os.sep}", os.sep)
if fold.endswith(f"{os.sep}"):
fold = fold[:-1]
Expand Down Expand Up @@ -257,7 +246,9 @@ def create_files(self):
with open(f"{root}{os.sep}{file}", "rb") as f:
content_hash = get_content_hash(f.read())
except OSError:
frappe.msgprint(frappe._("File {0} does not exist").format(f"{root}{os.sep}{file}"))
frappe.msgprint(
frappe._("File {0} does not exist").format(f"{root}{os.sep}{file}")
)
raise

new_file_name = get_file_name(file, content_hash[-6:])
Expand Down Expand Up @@ -299,6 +290,6 @@ def create_files(self):
orig_file_url = f"{self.assets_prepend}{os.sep}{root[root.find(self.assets_directory) + len(self.assets_directory) + 1:] }{os.sep}{file}"

self.docs_change_dict[orig_file_url] = file_url
self.docs_change_dict[
orig_file_url.replace("{{docs_base_url}}", self.docs_base_url)
] = file_url
self.docs_change_dict[orig_file_url.replace("{{docs_base_url}}", self.docs_base_url)] = (
file_url
)
11 changes: 4 additions & 7 deletions wiki/wiki/doctype/wiki_page/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

_redisearch_available = False
try:
from redis.commands.search.query import Query # noqa: F401
from redis.commands.search.query import Query

_redisearch_available = True
except ImportError:
Expand All @@ -39,7 +39,7 @@ def search(query, path, space):

return {"docs": result, "search_engine": "frappe_web_search"}

from redis.commands.search.query import Query # noqa: F811
from redis.commands.search.query import Query
from redis.exceptions import ResponseError

# if redisearch enabled use redisearch
Expand All @@ -58,8 +58,7 @@ def search(query, path, space):
names = list(set(names))

data_by_name = {
d.name: d
for d in frappe.db.get_all("Wiki Page", fields=["name"], filters={"name": ["in", names]})
d.name: d for d in frappe.db.get_all("Wiki Page", fields=["name"], filters={"name": ["in", names]})
}

docs = []
Expand Down Expand Up @@ -148,9 +147,7 @@ def remove_index_for_records(records, space):


def update_index(doc):
record = frappe._dict(
{"name": doc.name, "title": doc.title, "content": doc.content, "route": doc.route}
)
record = frappe._dict({"name": doc.name, "title": doc.title, "content": doc.content, "route": doc.route})
space = get_space_route(doc.route)

create_index_for_records([record], space)
Expand Down
5 changes: 1 addition & 4 deletions wiki/wiki/doctype/wiki_page/test_wiki_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def tearDown(self):
self.wiki_page.delete()

def test_wiki_page_lifecycle(self):

self.assertEqual(
frappe.db.get_value("Wiki Page", {"route": "wiki/page"}, "name"), self.wiki_page.name
)
Expand Down Expand Up @@ -77,7 +76,5 @@ def test_wiki_page_deletion(self):
patches = frappe.get_all("Wiki Page Patch", {"wiki_page": self.wiki_page.name}, pluck="name")
self.assertEqual(patches, [])

sidebar_items = frappe.get_all(
"Wiki Group Item", {"wiki_page": self.wiki_page.name}, pluck="name"
)
sidebar_items = frappe.get_all("Wiki Group Item", {"wiki_page": self.wiki_page.name}, pluck="name")
self.assertEqual(sidebar_items, [])
14 changes: 3 additions & 11 deletions wiki/wiki/doctype/wiki_page/wiki_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def on_update(self):
update_index(self)

def on_trash(self):

frappe.db.sql("DELETE FROM `tabWiki Page Revision Item` WHERE wiki_page = %s", self.name)

frappe.db.sql(
Expand Down Expand Up @@ -185,9 +184,7 @@ def get_space_route(self):
if space := frappe.get_value("Wiki Group Item", {"wiki_page": self.name}, "parent"):
return frappe.get_value("Wiki Space", space, "route")
else:
frappe.throw(
"Wiki Page doesn't have a Wiki Space associated with it. Please add them via Desk."
)
frappe.throw("Wiki Page doesn't have a Wiki Space associated with it. Please add them via Desk.")

def calculate_toc_html(self, html):
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -238,9 +235,7 @@ def get_context(self, context):
context.edit_wiki_page = frappe.form_dict.get("editWiki")
context.new_wiki_page = frappe.form_dict.get("newWiki")
context.last_revision = self.get_last_revision()
context.number_of_revisions = frappe.db.count(
"Wiki Page Revision Item", {"wiki_page": self.name}
)
context.number_of_revisions = frappe.db.count("Wiki Page Revision Item", {"wiki_page": self.name})
# TODO: group all context values
context.hide_on_sidebar = frappe.get_value(
"Wiki Group Item", {"wiki_page": self.name}, "hide_on_sidebar"
Expand Down Expand Up @@ -353,7 +348,6 @@ def get_last_revision(self):
return frappe.get_doc("Wiki Page Revision", last_revision)

def clone(self, original_space, new_space):

# used in after_insert of Wiki Page to resist create of Wiki Page Revision
frappe.local.in_clone = True

Expand Down Expand Up @@ -439,9 +433,7 @@ def _save_file(match):
mtype = headers.split(";")[0]
filename = get_random_filename(content_type=mtype)

_file = frappe.get_doc(
{"doctype": "File", "file_name": filename, "content": content, "decode": True}
)
_file = frappe.get_doc({"doctype": "File", "file_name": filename, "content": content, "decode": True})
_file.save(ignore_permissions=True)
file_url = _file.file_url
file_ids["name"] += [_file.name]
Expand Down
4 changes: 1 addition & 3 deletions wiki/wiki/doctype/wiki_page/wiki_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class WikiPageRenderer(DocumentPage):
def can_render(self):
if wiki_space_name := frappe.get_value("Wiki Space", {"route": self.path}):
wiki_space = frappe.get_doc("Wiki Space", wiki_space_name)
topmost_wiki_route = frappe.get_value(
"Wiki Page", wiki_space.wiki_sidebars[0].wiki_page, "route"
)
topmost_wiki_route = frappe.get_value("Wiki Page", wiki_space.wiki_sidebars[0].wiki_page, "route")
frappe.redirect(f"/{quote(topmost_wiki_route)}")
return self.search_in_doctypes_with_web_view()

Expand Down
Loading

0 comments on commit e1f3521

Please sign in to comment.