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

Enable style checking for entire code base #620

Merged
merged 9 commits into from
Jul 7, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 17 additions & 0 deletions .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ env:


jobs:
lint:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install ruff
run: pip install ruff
- name: Check code formatting
run: ruff format --diff
- name: Lint code base
run: ruff check


generate-wheels-matrix:
name: "Generate wheels matrix"
runs-on: "ubuntu-latest"
Expand Down Expand Up @@ -245,6 +261,7 @@ jobs:
permissions:
contents: write
needs:
- lint
- test-and-build
- make-sdist
- test-interfaces
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ htmlcov
# Ignore all compiled python files (e.g. from running the unit tests)
*.pyc
*.pyo
*.py{}
*.py-e

# Ignore potential directory created during install
Expand Down
123 changes: 63 additions & 60 deletions doc/apidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
# information.

__author__ = "Patrick Kunzmann"
__all__ = ["create_api_doc", "skip_non_methods"]
__all__ = ["create_api_doc", "skip_nonrelevant"]

from os.path import join, isdir
from os import listdir, makedirs
from importlib import import_module
import types
import json
import enum
from textwrap import dedent
import json
import types
from collections import OrderedDict

from importlib import import_module
from os import listdir, makedirs
from os.path import isdir, join
from textwrap import dedent

_INDENT = " " * 4

Expand All @@ -24,7 +23,6 @@
_pck_categories = json.load(file, object_pairs_hook=OrderedDict)



def create_api_doc(src_path, doc_path):
"""
Create *.rst files for API documentation.
Expand All @@ -40,11 +38,7 @@ def create_api_doc(src_path, doc_path):
# Create directory to store apidoc
if not isdir(doc_path):
makedirs(doc_path)
package_list = _create_package_doc(
"biotite",
join(src_path, "biotite"),
doc_path
)
package_list = _create_package_doc("biotite", join(src_path, "biotite"), doc_path)
_create_package_index(doc_path, package_list)


Expand All @@ -67,31 +61,35 @@ def _create_package_doc(pck, src_path, doc_path):
module = import_module(pck)
attr_list = dir(module)
# Classify attribute names into classes and functions
class_list = [attr for attr in attr_list
# Do not document private classes
if attr[0] != "_"
# Check if object is a class
and isinstance(getattr(module, attr), type)]
func_list = [attr for attr in attr_list
# Do not document private classes
if attr[0] != "_"
# All functions are callable...
and callable(getattr(module, attr))
# ...but classes are also callable
and attr not in class_list
]
class_list = [
attr
for attr in attr_list
# Do not document private classes
if attr[0] != "_"
# Check if object is a class
and isinstance(getattr(module, attr), type)
]
func_list = [
attr
for attr in attr_list
# Do not document private classes
if attr[0] != "_"
# All functions are callable...
and callable(getattr(module, attr))
# ...but classes are also callable
and attr not in class_list
]
# Create *.rst files
_create_package_page(doc_path, pck, class_list, func_list, sub_pck)
for class_name in class_list:
_create_class_page(doc_path, pck, class_name)
for function_name in func_list:
_create_function_page(doc_path, pck, function_name)

return([pck] + sub_pck)
return [pck] + sub_pck


def _create_package_page(doc_path, package_name,
classes, functions, subpackages):
def _create_package_page(doc_path, package_name, classes, functions, subpackages):
attributes = classes + functions

# Get categories for this package
Expand All @@ -114,7 +112,6 @@ def _create_package_page(doc_path, package_name,
misc_category_name = "Miscellaneous" if categories else "Content"
categories[misc_category_name] = misc_attributes


# String for categorized class and function enumeration
category_strings = []
for category, attrs in categories.items():
Expand All @@ -135,12 +132,11 @@ def _create_package_page(doc_path, package_name,
attributes_string = "\n".join(category_strings)

# String for subpackage enumeration
subpackages_string = "\n".join(
[_INDENT + pck for pck in subpackages]
)
subpackages_string = "\n".join([_INDENT + pck for pck in subpackages])

# Assemble page
file_content = dedent(f"""
file_content = (
dedent(f"""

``{package_name}``
{"=" * (len(package_name) + 4)}
Expand All @@ -150,16 +146,21 @@ def _create_package_page(doc_path, package_name,

.. currentmodule:: {package_name}

""") + attributes_string
""")
+ attributes_string
)
if len(subpackages) > 0:
file_content += dedent(f"""
file_content += (
dedent("""

Subpackages
-----------

.. autosummary::

""") + subpackages_string
""")
+ subpackages_string
)
with open(join(doc_path, f"{package_name}.rst"), "w") as f:
f.write(file_content)

Expand Down Expand Up @@ -201,18 +202,19 @@ def _create_function_page(doc_path, package_name, function_name):

def _create_package_index(doc_path, package_list):
# String for package enumeration
packages_string = "\n".join(
[_INDENT + pck for pck in sorted(package_list)]
)
packages_string = "\n".join([_INDENT + pck for pck in sorted(package_list)])

file_content = dedent(f"""
file_content = (
dedent("""
API Reference
=============

.. autosummary::
:toctree:

""") + packages_string
""")
+ packages_string
)
with open(join(doc_path, "index.rst"), "w") as f:
f.write(file_content)

Expand Down Expand Up @@ -249,20 +251,21 @@ def _is_relevant_type(obj):
# These are some special built-in Python methods
return False
return (
# Functions
type(obj) in [
types.FunctionType, types.BuiltinFunctionType, types.MethodType
]
) | (
# Functions from C-extensions
type(obj).__name__ in [
"cython_function_or_method",
"fused_cython_function"
]
) | (
# Enum instance
isinstance(obj, enum.Enum)
) | (
# Inner class
isinstance(obj, type)
)
(
# Functions
type(obj)
in [types.FunctionType, types.BuiltinFunctionType, types.MethodType]
)
| (
# Functions from C-extensions
type(obj).__name__ in ["cython_function_or_method", "fused_cython_function"]
)
| (
# Enum instance
isinstance(obj, enum.Enum)
)
| (
# Inner class
isinstance(obj, type)
)
)
34 changes: 17 additions & 17 deletions doc/bibliography.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
__author__ = "Patrick Kunzmann"

import warnings
from pybtex.richtext import Text, Tag, HRef
from pybtex.richtext import HRef, Tag, Text
from pybtex.style.formatting import BaseStyle


class IEEEStyle(BaseStyle):
def format_article(self, param):
entry = param["entry"]

try:
authors = []
for author in entry.persons["author"]:
Expand All @@ -28,7 +28,7 @@ def format_article(self, param):
text += " "
text += " ".join([s for s in author.last_names])
authors.append(Text(text + ", "))

title = ""
in_protected = False
for char in entry.fields["title"]:
Expand All @@ -46,34 +46,34 @@ def format_article(self, param):
else:
title += char.lower()
title = Text('"', title, '," ')

journal = Text(Tag("em", entry.fields["journal"]), ", ")

if "volume" in entry.fields:
volume = Text("vol. ", entry.fields["volume"], ", ")
else:
volume = Text()

if "pages" in entry.fields:
pages = Text("pp. ", entry.fields["pages"], ", ")
else:
pages = Text()

date = entry.fields["year"]
if "month" in entry.fields:
date = entry.fields["month"] + " " + date
date = Text(date, ". ")
if "doi" in entry.fields:
doi = Text("doi: ", HRef(
"https://doi.org/" + entry.fields["doi"],
entry.fields["doi"]
))

if "doi" in entry.fields:
doi = Text(
"doi: ",
HRef("https://doi.org/" + entry.fields["doi"], entry.fields["doi"]),
)
else:
doi = Text()

return Text(*authors, title, journal, volume, pages, date, doi)
except:

except Exception:
warnings.warn(f"Invalid BibTeX entry '{entry.key}'")
return Text(entry.key)
return Text(entry.key)
Loading
Loading