Skip to content

Commit

Permalink
style: Apply isort & black style
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Mar 18, 2024
1 parent bbb0b86 commit 7f8631d
Show file tree
Hide file tree
Showing 54 changed files with 2,179 additions and 1,704 deletions.
33 changes: 16 additions & 17 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# -- Project information -----------------------------------------------------

project = 'python-fluent'
project = "python-fluent"


# -- General configuration ---------------------------------------------------
Expand All @@ -26,49 +26,48 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
'sphinx.ext.autodoc',
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
"sphinx.ext.autodoc",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]


# Add src_dir/docs/_templates in a hook as we only have the src_dir then.
def setup(app):
app.connect('config-inited', add_templates)
app.connect("config-inited", add_templates)


def add_templates(app, config):
config.templates_path.insert(0, f'{app.srcdir}/_templates')
config.templates_path.insert(0, f"{app.srcdir}/_templates")


# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'nature'
html_theme = "nature"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_css_files = ['project-fluent.css']
html_js_files = ['versions.js']
html_static_path = ["_static"]
html_css_files = ["project-fluent.css"]
html_js_files = ["versions.js"]

html_sidebars = {
'**': ['globaltoc.html', 'versions.html', 'searchbox.html'],
}
html_theme_options = {
"**": ["globaltoc.html", "versions.html", "searchbox.html"],
}
html_theme_options = {}

# -- Extension configuration -------------------------------------------------

Expand All @@ -79,6 +78,6 @@ def add_templates(app, config):

# -- Options for autodoc extension --------------------------------------------
autodoc_mock_imports = [
'attr',
"attr",
]
autodoc_member_order = 'bysource'
autodoc_member_order = "bysource"
15 changes: 8 additions & 7 deletions fluent.docs/fluent/docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from pathlib import Path

from .build import DocBuilder


def finalize_builddir(repo_name):
'Bookkeeping on the docs build directory'
root = Path('_build') / repo_name
with open(root / '.nojekyll', 'w') as fh:
fh.write('')
"Bookkeeping on the docs build directory"
root = Path("_build") / repo_name
with open(root / ".nojekyll", "w") as fh:
fh.write("")


def build_root(repo_name):
'''Build the top-level documentation.
"""Build the top-level documentation.
See :py:mod:`.build` on building sub-projects.
'''
with DocBuilder(repo_name, '.') as builder:
"""
with DocBuilder(repo_name, ".") as builder:
builder.build()
113 changes: 57 additions & 56 deletions fluent.docs/fluent/docs/build.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from collections import defaultdict
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
from collections import defaultdict
from pathlib import Path

from .tags import get_tag_infos


def build(repo_name, projects, releases_after=None):
'''Build documentation for projects.
"""Build documentation for projects.
Build the given projects in _build/repo_name.
Create versioned documentations for tags after ``releases_after``,
if given.
'''
"""
tagged_versions = []
if releases_after:
tagged_versions = [
tag for tag in get_tag_infos(releases_after)
if tag.project in projects
tag for tag in get_tag_infos(releases_after) if tag.project in projects
]
# List of versions we have for each project
versions_4_project = defaultdict(list)
Expand All @@ -28,54 +28,45 @@ def build(repo_name, projects, releases_after=None):
for project in projects:
if project in versions_4_project:
last_vers[project] = versions_4_project[project][0]
versions_4_project[project][:0] = ['dev', 'stable']
versions_4_project[project][:0] = ["dev", "stable"]
else:
# No releases yet, just dev
last_vers[project] = 'dev'
versions_4_project[project].append('dev')
last_vers[project] = "dev"
versions_4_project[project].append("dev")
# Build current dev version for each project
for project in projects:
src_dir = project
builder = ProjectBuilder(
repo_name, src_dir, project, versions_4_project[project], 'dev'
repo_name, src_dir, project, versions_4_project[project], "dev"
)
with builder:
builder.build()
# Create redirect page from project to stable release or dev
index = Path(f'_build/{repo_name}/{project}/index.html')
target = 'stable' if last_vers[project] != 'dev' else 'dev'
index.write_text(
f'<meta http-equiv="refresh" content="0; URL={target}/">\n'
)
index = Path(f"_build/{repo_name}/{project}/index.html")
target = "stable" if last_vers[project] != "dev" else "dev"
index.write_text(f'<meta http-equiv="refresh" content="0; URL={target}/">\n')
worktree = None
for tag in tagged_versions:
if worktree is None:
worktree = tempfile.mkdtemp()
subprocess.run([
'git',
'worktree', 'add',
'--detach',
worktree
])
subprocess.run([
'git',
'checkout',
f'{tag.project}@{tag.version}'
], cwd=worktree)
subprocess.run(["git", "worktree", "add", "--detach", worktree])
subprocess.run(
["git", "checkout", f"{tag.project}@{tag.version}"], cwd=worktree
)
with ProjectBuilder(
repo_name,
os.path.join(worktree, tag.project),
tag.project,
versions_4_project[tag.project],
tag.version
tag.version,
) as builder:
builder.build()
if worktree is not None:
shutil.rmtree(worktree)
for project in projects:
if last_vers[project] == 'dev':
if last_vers[project] == "dev":
continue
stable = Path(f'_build/{repo_name}/{project}/stable')
stable = Path(f"_build/{repo_name}/{project}/stable")
if stable.is_symlink():
stable.unlink()
if stable.exists():
Expand All @@ -84,8 +75,7 @@ def build(repo_name, projects, releases_after=None):


class DocBuilder:
'''Builder for the top-level documentation.
'''
"""Builder for the top-level documentation."""

def __init__(self, repo_name, src_dir):
self.repo_name = repo_name
Expand All @@ -103,22 +93,31 @@ def build(self):
subprocess.check_call(cmd, env=env)

def command(self):
return self.cmd_prefix + self.cmd_opts + [
f'{self.src_dir}/docs',
self.dest_dir,
]
return (
self.cmd_prefix
+ self.cmd_opts
+ [
f"{self.src_dir}/docs",
self.dest_dir,
]
)

def environ(self):
return os.environ.copy()

@property
def cmd_prefix(self):
return [
'sphinx-build',
'-c', 'docs',
'-a', '-E', '-W',
'-A', 'root_url=/' + self.repo_name,
'-d', self.doc_tree,
"sphinx-build",
"-c",
"docs",
"-a",
"-E",
"-W",
"-A",
"root_url=/" + self.repo_name,
"-d",
self.doc_tree,
]

@property
Expand All @@ -127,16 +126,16 @@ def cmd_opts(self):

@property
def dest_dir(self):
return f'_build/{self.repo_name}'
return f"_build/{self.repo_name}"

@property
def doc_tree(self):
return '_build/doctrees'
return "_build/doctrees"


class ProjectBuilder(DocBuilder):
'''Builder for individual projects, with project name and version.
'''
"""Builder for individual projects, with project name and version."""

def __init__(self, repo_name, src_dir, project_name, versions, version):
super().__init__(repo_name, src_dir)
self.project_name = project_name
Expand All @@ -146,9 +145,9 @@ def __init__(self, repo_name, src_dir, project_name, versions, version):
def __exit__(self, exc_type, exc_val, exc_tb):
# Remove static theme files from project static, they're
# used from the top-level _static.
for staticfile in Path(self.dest_dir).glob('_static/*'):
for staticfile in Path(self.dest_dir).glob("_static/*"):
# The options are project-specific.
if staticfile.name != 'documentation_options.js':
if staticfile.name != "documentation_options.js":
staticfile.unlink()
return False

Expand All @@ -158,36 +157,38 @@ def build(self):

def environ(self):
env = super().environ()
env['PYTHONPATH'] = self.src_dir
env["PYTHONPATH"] = self.src_dir
return env

@property
def cmd_opts(self):
opts = [
'-D', 'project=' + self.project_name,
"-D",
"project=" + self.project_name,
]
if self.version != 'dev':
if self.version != "dev":
opts += [
'-D', f'release={self.version}',
"-D",
f"release={self.version}",
]
return opts

@property
def dest_dir(self):
return f'_build/{self.repo_name}/{self.project_name}/{self.version}'
return f"_build/{self.repo_name}/{self.project_name}/{self.version}"

def create_versions_doc(self):
target_path = Path(self.src_dir) / 'docs' / '_templates'
target_path = Path(self.src_dir) / "docs" / "_templates"
target_path.mkdir(exist_ok=True)
target_path = target_path / 'versions.html'
links = ' '.join(
target_path = target_path / "versions.html"
links = " ".join(
f'<a href="/{self.repo_name}/{self.project_name}/{v}">{v}</a>'
for v in self.versions
)
content = f'''<h3>Versions</h3>
content = f"""<h3>Versions</h3>
<p id="versions">
<span class="version">{self.version}</span>
<span class="links">{links}</span>
</p>
'''
"""
target_path.write_text(content)
32 changes: 16 additions & 16 deletions fluent.docs/fluent/docs/tags.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
from datetime import date
import subprocess
from datetime import date


def get_tag_infos(cut_off_date):
'''Get fluent.* tags newer than cut_off_date.
"""Get fluent.* tags newer than cut_off_date.
TagInfo objects are ordered by committer date, newest first.
'''
"""
taglines = subprocess.run(
[
'git', 'tag', '--list', 'fluent.*',
'--sort=-committerdate',
'--format=%(refname:lstrip=2) %(committerdate:short)',
"git",
"tag",
"--list",
"fluent.*",
"--sort=-committerdate",
"--format=%(refname:lstrip=2) %(committerdate:short)",
],
encoding='utf-8',
encoding="utf-8",
stdout=subprocess.PIPE,
check=True
check=True,
).stdout.splitlines()
return [
ti for ti in (TagInfo(line) for line in taglines)
if ti.date > cut_off_date
]
return [ti for ti in (TagInfo(line) for line in taglines) if ti.date > cut_off_date]


class TagInfo:
def __init__(self, tagline):
tag, date_string = tagline.split(' ')
self.project, self.version = tag.split('@')
tag, date_string = tagline.split(" ")
self.project, self.version = tag.split("@")
self.date = date.fromisoformat(date_string)

@property
def tag(self):
return f'{self.project}@{self.version}'
return f"{self.project}@{self.version}"

def __repr__(self):
return f'{self.tag} ({self.date})'
return f"{self.tag} ({self.date})"
Loading

0 comments on commit 7f8631d

Please sign in to comment.