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

Typing #44

Merged
merged 2 commits into from
Aug 28, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/pre-merge-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ jobs:
pip install -r requirements.txt
- name: Run tests
run: pytest
- name: Run typecheck
run: |
mypy adr_viewer \
--install-types \
--non-interactive \
--ignore-missing-imports \
--pretty
26 changes: 14 additions & 12 deletions adr_viewer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List, Iterator, Optional, Dict

import glob
from jinja2.loaders import FileSystemLoader
import mistune
Expand All @@ -8,7 +10,7 @@
from bottle import Bottle, run


def extract_statuses_from_adr(page_object):
def extract_statuses_from_adr(page_object) -> Iterator[str]:
status_section = page_object.find('h2', text='Status')

if status_section and status_section.nextSibling:
Expand All @@ -25,20 +27,20 @@ def extract_statuses_from_adr(page_object):
continue


def parse_adr_to_config(path):
def parse_adr_to_config(path) -> Optional[Dict]:
adr_as_html = mistune.markdown(open(path).read())

soup = BeautifulSoup(adr_as_html, features='html.parser')

status = list(extract_statuses_from_adr(soup))
statuses = list(extract_statuses_from_adr(soup))

if any([line.startswith("Amended by") for line in status]):
if any([line.startswith("Amended by") for line in statuses]):
status = 'amended'
elif any([line.startswith("Accepted") for line in status]):
elif any([line.startswith("Accepted") for line in statuses]):
status = 'accepted'
elif any([line.startswith("Superseded by") for line in status]):
elif any([line.startswith("Superseded by") for line in statuses]):
status = 'superseded'
elif any([line.startswith("Proposed") or line.startswith("Pending") for line in status]):
elif any([line.startswith("Proposed") or line.startswith("Pending") for line in statuses]):
status = 'pending'
else:
status = 'unknown'
Expand All @@ -55,7 +57,7 @@ def parse_adr_to_config(path):
return None


def render_html(config, template_dir_override=None):
def render_html(config, template_dir_override=None) -> str:

env = Environment(
loader=PackageLoader('adr_viewer', 'templates') if template_dir_override is None else FileSystemLoader(template_dir_override),
Expand All @@ -67,20 +69,20 @@ def render_html(config, template_dir_override=None):
return template.render(config=config)


def get_adr_files(path):
def get_adr_files(path) -> List[str]:
files = glob.glob(path)
files.sort()
return files


def run_server(content, port):
def run_server(content, port) -> None:
print(f'Starting server at http://localhost:{port}/')
app = Bottle()
app.route('/', 'GET', lambda: content)
run(app, host='localhost', port=port, quiet=True)


def generate_content(path, template_dir_override=None, title=None):
def generate_content(path, template_dir_override=None, title=None) -> str:

files = get_adr_files("%s/*.md" % path)

Expand Down Expand Up @@ -110,7 +112,7 @@ def generate_content(path, template_dir_override=None, title=None):
@click.option('--serve', default=False, help='Serve content at http://localhost:8000/', is_flag=True)
@click.option('--port', default=8000, help='Change port for the server', show_default=True)
@click.option('--template-dir', default=None, help='Template directory.', show_default=True)
def main(adr_path, output, title, serve, port, template_dir):
def main(adr_path, output, title, serve, port, template_dir) -> None:
content = generate_content(adr_path, template_dir, title)

if serve:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mistune
bs4
jinja2
pytest
bottle
bottle
mypy