Skip to content

Commit

Permalink
Use black formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgeiter committed Dec 27, 2021
1 parent e7be5fc commit fcb37dd
Show file tree
Hide file tree
Showing 27 changed files with 3,460 additions and 3,800 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ on:
- master

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Install poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install
- name: Check formatting
run: poetry run black --check .
test:
name: Test
runs-on: ${{ matrix.os }}
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The summary of a commit should be concise and worded in an imperative mood.

#### Code Style

Make sure your code follows [PEP-8](https://www.python.org/dev/peps/pep-0008/) and keeps things consistent with the rest of the code.
This project uses [Black][black] for code formatting.

#### Tests

Expand All @@ -47,3 +47,4 @@ If it makes sense, writing tests for your PRs is always appreciated and will hel
[Python 3]: https://www.python.org/
[poetry]: https://python-poetry.org/
[git-commit-message]: https://chris.beams.io/posts/git-commit/
[black]: https://black.readthedocs.io/
133 changes: 76 additions & 57 deletions mkdocs_awesome_pages_plugin/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def __init__(self, item: str, context: str):


class MetaNavItem:

def __init__(self, value: str, title: Optional[str] = None):
self.value = value
self.title = title
Expand All @@ -38,18 +37,18 @@ def from_yaml(item: Union[str, dict], context: str):
if isinstance(value, str) and isinstance(title, str):
return MetaNavItem(value, title)

raise TypeError('Invalid nav item format {type} [{context}]'.format(type=item, context=context))
raise TypeError("Invalid nav item format {type} [{context}]".format(type=item, context=context))


class RestType(Enum):
GLOB = 'glob'
REGEX = 'regex'
ALL = 'all'
GLOB = "glob"
REGEX = "regex"
ALL = "all"


class MetaNavRestItem(MetaNavItem):

_REGEX = r'^\.{3}\s*(?:\|\s*(flat)\s*)?\s*(?:\|\s*(?:(regex|glob)=)?(.*))?'
_REGEX = r"^\.{3}\s*(?:\|\s*(flat)\s*)?\s*(?:\|\s*(?:(regex|glob)=)?(.*))?"

def __init__(self, value: str):
super().__init__(value)
Expand Down Expand Up @@ -100,26 +99,35 @@ def __len__(self):

class Meta:

TITLE_ATTRIBUTE = 'title'
NAV_ATTRIBUTE = 'nav'
ARRANGE_ATTRIBUTE = 'arrange'
ARRANGE_REST_TOKEN = '...'
COLLAPSE_ATTRIBUTE = 'collapse'
COLLAPSE_SINGLE_PAGES_ATTRIBUTE = 'collapse_single_pages'
HIDE_ATTRIBUTE = 'hide'
ORDER_ATTRIBUTE = 'order'

ORDER_ASC = 'asc'
ORDER_DESC = 'desc'

def __init__(self, *, title: Optional[str] = None, arrange: Optional[List[str]] = None,
nav: Optional[List[MetaNavItem]] = None, path: Optional[str] = None, collapse: bool = None,
collapse_single_pages: bool = None, hide: bool = None, order: Optional[str] = None):
TITLE_ATTRIBUTE = "title"
NAV_ATTRIBUTE = "nav"
ARRANGE_ATTRIBUTE = "arrange"
ARRANGE_REST_TOKEN = "..."
COLLAPSE_ATTRIBUTE = "collapse"
COLLAPSE_SINGLE_PAGES_ATTRIBUTE = "collapse_single_pages"
HIDE_ATTRIBUTE = "hide"
ORDER_ATTRIBUTE = "order"

ORDER_ASC = "asc"
ORDER_DESC = "desc"

def __init__(
self,
*,
title: Optional[str] = None,
arrange: Optional[List[str]] = None,
nav: Optional[List[MetaNavItem]] = None,
path: Optional[str] = None,
collapse: bool = None,
collapse_single_pages: bool = None,
hide: bool = None,
order: Optional[str] = None
):

if nav is None and arrange is not None:
nav = [MetaNavItem.from_yaml(value, path) for value in arrange]
if MetaNavRestItem('...') not in nav:
nav.append(MetaNavRestItem('...'))
if MetaNavRestItem("...") not in nav:
nav.append(MetaNavRestItem("..."))

self.title = title
self.nav = nav
Expand All @@ -130,7 +138,7 @@ def __init__(self, *, title: Optional[str] = None, arrange: Optional[List[str]]
self.order = order

@staticmethod
def try_load_from(path: Optional[str]) -> 'Meta':
def try_load_from(path: Optional[str]) -> "Meta":
if path is None:
return Meta()
try:
Expand All @@ -139,8 +147,8 @@ def try_load_from(path: Optional[str]) -> 'Meta':
return Meta(path=path)

@staticmethod
def load_from(path: str) -> 'Meta':
with open(path, encoding='utf-8') as file:
def load_from(path: str) -> "Meta":
with open(path, encoding="utf-8") as file:
contents = yaml.safe_load(file) or {}
title = contents.get(Meta.TITLE_ATTRIBUTE)
arrange = contents.get(Meta.ARRANGE_ATTRIBUTE)
Expand All @@ -153,29 +161,30 @@ def load_from(path: str) -> 'Meta':
if title is not None:
if not isinstance(title, str):
raise TypeError(
'Expected "{attribute}" attribute to be a string - got {type} [{context}]'
.format(attribute=Meta.TITLE_ATTRIBUTE,
type=type(title),
context=path)
'Expected "{attribute}" attribute to be a string - got {type} [{context}]'.format(
attribute=Meta.TITLE_ATTRIBUTE,
type=type(title),
context=path,
)
)
if arrange is not None:
if not isinstance(arrange, list) or not all(isinstance(s, str) for s in arrange):
raise TypeError(
'Expected "{attribute}" attribute to be a list of strings - got {type} [{context}]'
.format(attribute=Meta.ARRANGE_ATTRIBUTE,
type=type(arrange),
context=path)
'Expected "{attribute}" attribute to be a list of strings - got {type} [{context}]'.format(
attribute=Meta.ARRANGE_ATTRIBUTE,
type=type(arrange),
context=path,
)
)
if arrange.count(Meta.ARRANGE_REST_TOKEN) > 1:
raise DuplicateRestItemError('...', path)
raise DuplicateRestItemError("...", path)

if nav is not None:
if not isinstance(nav, list):
raise TypeError(
'Expected "{attribute}" attribute to be a list - got {type} [{context}]'
.format(attribute=Meta.NAV_ATTRIBUTE,
type=type(nav),
context=path)
'Expected "{attribute}" attribute to be a list - got {type} [{context}]'.format(
attribute=Meta.NAV_ATTRIBUTE, type=type(nav), context=path
)
)

nav = [MetaNavItem.from_yaml(item, path) for item in nav]
Expand All @@ -189,35 +198,45 @@ def load_from(path: str) -> 'Meta':
if collapse is not None:
if not isinstance(collapse, bool):
raise TypeError(
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'
.format(attribute=Meta.COLLAPSE_ATTRIBUTE,
type=type(collapse),
context=path)
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'.format(
attribute=Meta.COLLAPSE_ATTRIBUTE,
type=type(collapse),
context=path,
)
)
if collapse_single_pages is not None:
if not isinstance(collapse_single_pages, bool):
raise TypeError(
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'
.format(attribute=Meta.COLLAPSE_SINGLE_PAGES_ATTRIBUTE,
type=type(collapse_single_pages),
context=path)
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'.format(
attribute=Meta.COLLAPSE_SINGLE_PAGES_ATTRIBUTE,
type=type(collapse_single_pages),
context=path,
)
)
if hide is not None:
if not isinstance(hide, bool):
raise TypeError(
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'
.format(attribute=Meta.COLLAPSE_ATTRIBUTE,
type=type(hide),
context=path)
'Expected "{attribute}" attribute to be a boolean - got {type} [{context}]'.format(
attribute=Meta.COLLAPSE_ATTRIBUTE,
type=type(hide),
context=path,
)
)
if order is not None:
if order != Meta.ORDER_ASC and order != Meta.ORDER_DESC:
raise TypeError(
'Expected "{attribute}" attribute to be either "desc" or "asc" - got "{order}" [{context}]'
.format(attribute=Meta.ORDER_ATTRIBUTE,
order=order,
context=path)
'Expected "{attribute}" attribute to be either "desc" or "asc" - got "{order}" [{context}]'.format(
attribute=Meta.ORDER_ATTRIBUTE, order=order, context=path
)
)

return Meta(title=title, arrange=arrange, nav=nav, path=path, collapse=collapse,
collapse_single_pages=collapse_single_pages, hide=hide, order=order)
return Meta(
title=title,
arrange=arrange,
nav=nav,
path=path,
collapse=collapse,
collapse_single_pages=collapse_single_pages,
hide=hide,
order=order,
)
43 changes: 26 additions & 17 deletions mkdocs_awesome_pages_plugin/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
from pathlib import Path
from typing import List, Optional, Union, Set

from mkdocs.structure.nav import Navigation as MkDocsNavigation, Section, Link, \
_add_parent_links, _add_previous_and_next_links
from mkdocs.structure.nav import (
Navigation as MkDocsNavigation,
Section,
Link,
_add_parent_links,
_add_previous_and_next_links,
)
from mkdocs.structure.pages import Page

from .meta import Meta, MetaNavRestItem, RestItemList
Expand All @@ -21,22 +26,25 @@ def __init__(self, entry: str, context: str):
class TitleInRootHasNoEffect(Warning):
def __init__(self, filename: str):
super().__init__(
'Using the "title" attribute in the {filename} file of the doc root has no effect'
.format(filename=filename)
'Using the "title" attribute in the {filename} file of the doc root has no effect'.format(filename=filename)
)


class HideInRootHasNoEffect(Warning):
def __init__(self, filename: str):
super().__init__(
'Using the "hide" attribute in the {filename} file of the doc root has no effect'
.format(filename=filename)
'Using the "hide" attribute in the {filename} file of the doc root has no effect'.format(filename=filename)
)


class AwesomeNavigation:

def __init__(self, items: List[NavigationItem], options: Options, docs_dir: str, explicit_sections: Set[Section]):
def __init__(
self,
items: List[NavigationItem],
options: Options,
docs_dir: str,
explicit_sections: Set[Section],
):
self.options = options
self.explicit_sections = explicit_sections

Expand All @@ -48,11 +56,7 @@ def __init__(self, items: List[NavigationItem], options: Options, docs_dir: str,
if self.meta.root.hide is not None:
warnings.warn(HideInRootHasNoEffect(self.options.filename))

self.items = self._process_children(
items,
self.options.collapse_single_pages,
self.meta.root
)
self.items = self._process_children(items, self.options.collapse_single_pages, self.meta.root)

def _process_children(self, children: List[NavigationItem], collapse: bool, meta: Meta) -> List[NavigationItem]:
self._order(children, meta)
Expand All @@ -73,7 +77,7 @@ def _order(self, items: List[NavigationItem], meta: Meta):
if meta.order is not None:
items.sort(
key=lambda i: basename(self._get_item_path(i)),
reverse=meta.order == Meta.ORDER_DESC
reverse=meta.order == Meta.ORDER_DESC,
)

def _nav(self, items: List[NavigationItem], meta: Meta) -> List[NavigationItem]:
Expand Down Expand Up @@ -119,7 +123,7 @@ def _nav(self, items: List[NavigationItem], meta: Meta) -> List[NavigationItem]:

for index, item in enumerate(result):
if isinstance(item, MetaNavRestItem):
result[index:index + 1] = rest[item]
result[index : index + 1] = rest[item]

return result

Expand Down Expand Up @@ -172,8 +176,13 @@ def to_mkdocs(self) -> MkDocsNavigation:


class NavigationMeta:

def __init__(self, items: List[NavigationItem], options: Options, docs_dir: str, explicit_sections: Set[Section]):
def __init__(
self,
items: List[NavigationItem],
options: Options,
docs_dir: str,
explicit_sections: Set[Section],
):
self.options = options
self.sections = {}
self.docs_dir = docs_dir
Expand Down

0 comments on commit fcb37dd

Please sign in to comment.