Skip to content

Commit

Permalink
i hear you like ruff (#251)
Browse files Browse the repository at this point in the history
* i hear you like ruff

* fix script

* more python

* Update rebuild_all_rtd.py
  • Loading branch information
nabobalis committed Apr 12, 2024
1 parent 7b96e53 commit 9495882
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 100 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ jobs:
website:
resource_class: large
docker:
- image: cimg/python:3.11
- image: cimg/python:3.12
steps:
- checkout
- run: *apt-install
- run: *tox
- run: python -m tox -e website -- -j 4
- run: python -m tox -e website -- -j 8
- store_artifacts:
path: .tmp/website/sunpy.org/_build/html
sunpy:
resource_class: large
docker:
- image: cimg/python:3.11
- image: cimg/python:3.12
steps:
- checkout
- run: *apt-install
- run: *tox
# Needed to install mplcairo
- run: sudo apt install libcairo2-dev pkg-config
# the large resource_class has 4 CPUs but auto detection does not work
- run: python -m tox -e sunpy_docs -- -j 4
- run: python -m tox -e sunpy_docs -- -j 8
# Remove some parts of the build to speed up upload
- run: rm -r .tmp/sunpy_docs/sunpy/docs/_build/html/.doctrees
- run: rm -r .tmp/sunpy_docs/sunpy/docs/_build/html/_downloads
Expand Down
15 changes: 4 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ ci:
autoupdate_schedule: "quarterly"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.3.5"
rev: "v0.3.7"
hooks:
- id: ruff
args: ["--fix"]
args: ["--fix", "--unsafe-fixes"]
- id: ruff-format
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
Expand All @@ -22,21 +23,13 @@ repos:
hooks:
- id: docformatter
args: ["--make-summary-multi-line", "--pre-summary-newline", "-ri"]
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [css, scss, javascript, rst, json, yaml, toml, markdown]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-ast
- id: check-case-conflict
Expand Down
24 changes: 11 additions & 13 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
Configuration file for the Sphinx documentation builder.
"""

import datetime
import os
import sys
import datetime
from pathlib import Path

from sphinx_gallery.sorting import ExampleTitleSortKey, ExplicitOrder

from sunpy_sphinx_theme import SVG_ICON

project = "Test Package"
author = "The Test Package Community"
copyright = f"{datetime.datetime.now().year}, {author}"
# Add the test package to the path so we can import it for automodapi
sys.path.append(Path().absolute().as_posix())
# This serves as a check
import test_package # NOQA: F401

project = "sunpy-sphinx-theme test docs"
author = "The SunPy Community"
copyright = f"{datetime.datetime.now(datetime.UTC).year}, {author}" # NOQA: A001
extensions = [
"sphinx_automodapi.automodapi",
"sphinx_automodapi.smart_resolver",
Expand All @@ -32,11 +36,6 @@
"sphinx.ext.todo",
"sphinx.ext.viewcode",
]

# Add the test package to the path so we can import it for automodapi
sys.path.append(Path(".").absolute().as_posix())
import test_package # NOQA - Test package must be on the path

exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
source_suffix = ".rst"
master_doc = "index"
Expand Down Expand Up @@ -68,7 +67,6 @@
"reproject": ("https://reproject.readthedocs.io/en/stable/", None),
}
html_theme = "sunpy"

html_theme_options = {
"footer_links": [
("Google", "https://google.com", 3),
Expand All @@ -90,14 +88,14 @@
]
sphinx_gallery_conf = {
"filename_pattern": "^((?!skip_).)*$",
"examples_dirs": os.path.join("..", "examples"),
"examples_dirs": str(Path("..") / Path("examples")),
"subsection_order": ExplicitOrder(
[
"../examples/section",
]
),
"within_subsection_order": ExampleTitleSortKey,
"gallery_dirs": os.path.join("generated", "gallery"),
"gallery_dirs": str(Path("generated") / Path("gallery")),
"default_thumb_file": SVG_ICON,
"abort_on_example_error": False,
"plot_gallery": "True",
Expand Down
4 changes: 2 additions & 2 deletions docs/test_package/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .animals import * # NOQA: F401, F403
from .timerange import * # NOQA: F401, F403
from .animals import * # noqa: F403
from .timerange import * # noqa: F403
3 changes: 2 additions & 1 deletion docs/test_package/animals.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def says(self, sound=None):
* `A URL. <www.sunpy.org>`__
"""
if self.sound is None and sound is None:
raise NotImplementedError("Silent Animals are not supported!")
msg = "Silent Animals are not supported!"
raise NotImplementedError(msg)

out_sound = self.sound if sound is None else sound
print(self.says_str.format(name=self.name, sound=out_sound))
Expand Down
35 changes: 17 additions & 18 deletions docs/test_package/timerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class TimeRange:
Examples
--------
>>> from sunpy.time import TimeRange
>>> time_range = TimeRange('2010/03/04 00:10', '2010/03/04 00:20')
>>> time_range = TimeRange(('2010/03/04 00:10', '2010/03/04 00:20'))
>>> time_range = TimeRange("2010/03/04 00:10", "2010/03/04 00:20")
>>> time_range = TimeRange(("2010/03/04 00:10", "2010/03/04 00:20"))
>>> import astropy.units as u
>>> time_range = TimeRange('2010/03/04 00:10', 400 * u.s)
>>> TimeRange('2010/03/04 00:10', 400 * u.day)
>>> time_range = TimeRange("2010/03/04 00:10", 400 * u.s)
>>> TimeRange("2010/03/04 00:10", 400 * u.day)
<sunpy.time.timerange.TimeRange object at ...>
Start: 2010-03-04 00:10:00
End: 2011-04-08 00:10:00
Expand All @@ -56,7 +56,7 @@ class TimeRange:
<BLANKLINE>
"""

def __init__(self, a, b=None, format=None):
def __init__(self, a, b=None, format=None): # NOQA: A002
# If a is a TimeRange object, copy attributes to new instance.
self._t1 = None
self._t2 = None
Expand All @@ -69,9 +69,9 @@ def __init__(self, a, b=None, format=None):
if b is None:
x = parse_time(a[0], format=format)
if len(a) != 2:
raise ValueError("If b is None a must have two elements")
else:
y = a[1]
msg = "If b is None a must have two elements"
raise ValueError(msg)
y = a[1]
else:
x = parse_time(a, format=format)
y = b
Expand Down Expand Up @@ -248,7 +248,7 @@ def __repr__(self):
fully_qualified_name = f"{self.__class__.__module__}.{self.__class__.__name__}"

return (
" <{} object at {}>".format(fully_qualified_name, hex(id(self)))
f" <{fully_qualified_name} object at {hex(id(self))}>"
+ "\n Start:".ljust(12)
+ t1
+ "\n End:".ljust(12)
Expand Down Expand Up @@ -286,7 +286,8 @@ def split(self, n=2):
A list of equally sized `sunpy.time.TimeRange` between the start and end times.
"""
if n <= 0:
raise ValueError("n must be greater than or equal to 1")
msg = "n must be greater than or equal to 1"
raise ValueError(msg)
subsections = []
previous_time = self.start
next_time = None
Expand Down Expand Up @@ -319,8 +320,8 @@ def window(self, cadence, window):
--------
>>> import astropy.units as u
>>> from sunpy.time import TimeRange
>>> time_range = TimeRange('2010/03/04 00:10', '2010/03/04 01:20')
>>> time_range.window(60*60*u.s, window=12*u.s) # doctest: +SKIP
>>> time_range = TimeRange("2010/03/04 00:10", "2010/03/04 01:20")
>>> time_range.window(60 * 60 * u.s, window=12 * u.s) # doctest: +SKIP
[ <sunpy.time.timerange.TimeRange object at 0x7f0214bfc208>
Start: 2010-03-04 00:10:00
End: 2010-03-04 00:10:12
Expand Down Expand Up @@ -403,11 +404,9 @@ def get_dates(self):
"""
Return all partial days contained within the time range.
"""
dates = []
dates = [
return [
parse_time(self.start.strftime("%Y-%m-%d")) + TimeDelta(i * u.day) for i in range(int(self.days.value) + 1)
]
return dates

@add_common_docstring(**_variables_for_parse_time_docstring())
def __contains__(self, time):
Expand All @@ -430,9 +429,9 @@ def __contains__(self, time):
Examples
--------
>>> from sunpy.time import TimeRange
>>> time1 = '2014/5/5 12:11'
>>> time2 = '2012/5/5 12:11'
>>> time_range = TimeRange('2014/05/04 13:54', '2018/02/03 12:12')
>>> time1 = "2014/5/5 12:11"
>>> time2 = "2012/5/5 12:11"
>>> time_range = TimeRange("2014/05/04 13:54", "2018/02/03 12:12")
>>> time1 in time_range
True
>>> time2 in time_range
Expand Down
30 changes: 0 additions & 30 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,6 @@ include-package-data = true

[tool.setuptools_scm]

[tool.black]
line-length = 120
target-version = ["py39"]
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.tox
| \.venv
| _build
| build
| dist
| \.history
)/
'''

[ tool.gilesbot ]
[ tool.gilesbot.circleci_artifacts.giles ]
enabled = false
Expand All @@ -77,16 +60,3 @@ exclude = '''
url = "sunpy/docs/_build/html/index.html"
message = "sunpy docs preview"
report_on_fail = true

[tool.isort]
profile = "black"
balanced_wrapping = true
default_section = "THIRDPARTY"
include_trailing_comma = true
known_first_party = "sunpy_sphinx_theme"
length_sort = false
length_sort_sections = "stdlib"
line_length = 110
multi_line_output = 3
no_lines_before = "LOCALFOLDER"
sections = "STDLIB, THIRDPARTY, FIRSTPARTY, LOCALFOLDER"
15 changes: 8 additions & 7 deletions rebuild_all_rtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import requests

BASE_PROJECT = "sunpy"
ORG_PROJECT = "sunpy"
BASE_URL = "https://readthedocs.org/api/v3/projects/"
TOKEN = os.environ["RTD_AUTH_TOKEN"]
headers = {"Authorization": f"Token {TOKEN}"}
HEADERS = {"Authorization": f"Token {os.environ['RTD_AUTH_TOKEN']}"}
# RTD removes the full stop from the project slug
WEBSITE_PROJECT = "sunpyorg"


def get_active_versions(project):
r = requests.get(f"{BASE_URL}{project}/versions", headers=headers, params={"active": True})
r = requests.get(f"{BASE_URL}{project}/versions", headers=HEADERS, params={"active": True})
if not r.ok:
print(f"Failed to get versions for {project}: {r}")
return []
Expand All @@ -28,7 +29,7 @@ def get_active_versions(project):


def get_all_subprojects(base_project):
r = requests.get(f"{BASE_URL}{base_project}/subprojects", headers=headers)
r = requests.get(f"{BASE_URL}{base_project}/subprojects", headers=HEADERS)
r = r.json()
results = r["results"]
return [res["child"]["slug"] for res in results]
Expand All @@ -37,12 +38,12 @@ def get_all_subprojects(base_project):
def rebuild_all_versions_for_project(project):
slugs = get_active_versions(project)
for slug in slugs:
r = requests.post(f"{BASE_URL}{project}/versions/{slug}/builds/", headers=headers)
r = requests.post(f"{BASE_URL}{project}/versions/{slug}/builds/", headers=HEADERS)
if r.status_code != 202:
print(f"{slug} failed to build with: {r}")


if __name__ == "__main__":
projects = [BASE_PROJECT] + get_all_subprojects(BASE_PROJECT)
projects = [ORG_PROJECT, *get_all_subprojects(ORG_PROJECT), WEBSITE_PROJECT]
for project in projects:
rebuild_all_versions_for_project(project)

0 comments on commit 9495882

Please sign in to comment.