Skip to content

Commit

Permalink
Add mypy and embrace all black formating defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
humrochagf committed Jan 14, 2021
1 parent 5051d25 commit 9fd2390
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 88
4 changes: 0 additions & 4 deletions .isort.cfg

This file was deleted.

11 changes: 7 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

######################################

ROOTDIR := $(PWD)
PACKAGE := "revelation"
PACKAGE := "revelation"

######################################

.PHONY: typecheck
typecheck: # type check code
poetry run mypy .

.PHONY: lint
lint: # lint code
poetry run flake8 .
Expand All @@ -21,8 +24,8 @@ cover: # coverage tests

.PHONY: format
format:
poetry run isort $(ROOTDIR)
poetry run black -l 79 .
poetry run isort .
poetry run black .

.PHONY: clean
clean: # remove temporary files and artifacts
Expand Down
34 changes: 33 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ coveralls = "^2.2.0"
pytest = "^6.2.1"
pytest-mock = "^3.5.0"
pytest-cov = "^2.10.1"
mypy = "^0.790"

[tool.isort]
known_first_party = "revelation"
profile = "black"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
4 changes: 1 addition & 3 deletions revelation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def __init__(
self.config = Config(config)
self.presentation = presentation

shared_data = {
"/static": os.path.join(os.path.dirname(__file__), "static")
}
shared_data = {"/static": os.path.join(os.path.dirname(__file__), "static")}

shared_data.update(self.parse_shared_data(media))
shared_data.update(self.parse_shared_data(theme))
Expand Down
48 changes: 20 additions & 28 deletions revelation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import glob
import os
import shutil
from typing import Any, Dict, Optional

import typer
from typer import Option
from typer.main import get_command
from werkzeug.serving import run_simple

import revelation
Expand Down Expand Up @@ -89,9 +91,9 @@ def mkpresentation(presentation: str):
def mkstatic(
ctx: typer.Context,
presentation: str,
config: str = Option(None, "--config", "-c", help="Custom config file"),
media: str = Option(None, "--media", "-m", help="Custom media folder"),
theme: str = Option(None, "--theme", "-t", help="Custom theme folder"),
config: Optional[str] = Option(None, "--config", "-c", help="Custom config file"),
media: Optional[str] = Option(None, "--media", "-m", help="Custom media folder"),
theme: Optional[str] = Option(None, "--theme", "-t", help="Custom theme folder"),
output_folder: str = Option(
"output",
"--output-folder",
Expand All @@ -110,7 +112,7 @@ def mkstatic(
"-r",
help="Overwrite the output folder if exists",
),
style: str = Option(
style: Optional[str] = Option(
None,
"--style-override-file",
"-s",
Expand All @@ -124,9 +126,7 @@ def mkstatic(
echo("Reveal.js not found, running installation...")

# Change after fix on https://github.com/tiangolo/typer/issues/102
ctx.invoke(
typer.main.get_command(cli).get_command(ctx, "installreveal")
)
ctx.invoke(get_command(cli).get_command(ctx, "installreveal")) # type: ignore

output_folder = os.path.realpath(output_folder)

Expand All @@ -153,10 +153,7 @@ def mkstatic(
if force:
shutil.rmtree(output_folder)
else:
error(
f"'{output_folder}' already exists, "
"use --force to override it."
)
error(f"'{output_folder}' already exists, " "use --force to override it.")

raise typer.Abort()

Expand All @@ -167,9 +164,7 @@ def mkstatic(

# if has override style copy
if style:
shutil.copy(
style, os.path.join(output_folder, os.path.basename(style))
)
shutil.copy(style, os.path.join(output_folder, os.path.basename(style)))

shutil.copytree(REVEALJS_FOLDER, os.path.join(staticfolder, "revealjs"))

Expand Down Expand Up @@ -218,9 +213,7 @@ def mkstatic(
output_file = os.path.join(output_folder, output_file)

with open(output_file, "wb") as f:
f.write(
app.dispatch_request(None).get_data(as_text=True).encode("utf-8")
)
f.write(app.dispatch_request(None).get_data(as_text=True).encode("utf-8"))

echo(f"Static presentation generated in {os.path.realpath(output_folder)}")

Expand All @@ -229,11 +222,11 @@ def mkstatic(
def start(
ctx: typer.Context,
presentation: str,
port: str = Option(4000, "--port", "-p", help="Presentation server port"),
config: str = Option(None, "--config", "-c", help="Custom config file"),
media: str = Option(None, "--media", "-m", help="Custom media folder"),
theme: str = Option(None, "--theme", "-t", help="Custom theme folder"),
style: str = Option(
port: int = Option(4000, "--port", "-p", help="Presentation server port"),
config: Optional[str] = Option(None, "--config", "-c", help="Custom config file"),
media: Optional[str] = Option(None, "--media", "-m", help="Custom media folder"),
theme: Optional[str] = Option(None, "--theme", "-t", help="Custom theme folder"),
style: Optional[str] = Option(
None,
"--style-override-file",
"-s",
Expand All @@ -253,9 +246,7 @@ def start(
echo("Reveal.js not found, running installation...")

# Change after fix on https://github.com/tiangolo/typer/issues/102
ctx.invoke(
typer.main.get_command(cli).get_command(ctx, "installreveal")
)
ctx.invoke(get_command(cli).get_command(ctx, "installreveal")) # type: ignore

# Check for presentation file
if os.path.isfile(presentation):
Expand Down Expand Up @@ -304,7 +295,7 @@ def start(
# instantiating revelation app
app = Revelation(presentation, config, media, theme, style)

server_args = {
server_args: Dict[str, Any] = {
"hostname": "localhost",
"port": port,
"application": app,
Expand All @@ -315,7 +306,8 @@ def start(
server_args["use_debugger"] = True
server_args["use_reloader"] = True
server_args["reloader_type"] = "watchdog"
server_args["extra_files"] = glob.glob(os.path.join(path, "*.md"))
server_args["extra_files"] += glob.glob(os.path.join(path, "*.css"))
server_args["extra_files"] = glob.glob(os.path.join(path, "*.md")) + glob.glob(
os.path.join(path, "*.css")
)

run_simple(**server_args)
12 changes: 3 additions & 9 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@ def test_parse_shared_data(presentation: Presentation, revelation: Revelation):


def test_load_slides(presentation: Presentation, revelation: Revelation):
presentation.file.write_text(
"# Pag1\n---\n# Pag2.1\n---~\n# Page2.2", "utf8"
)
presentation.file.write_text("# Pag1\n---\n# Pag2.1\n---~\n# Page2.2", "utf8")

slides = revelation.load_slides(str(presentation.file), "---", "---~")

assert slides == [["# Pag1\n"], ["\n# Pag2.1\n", "\n# Page2.2"]]


def test_load_slides_non_normalized(
presentation: Presentation, revelation: Revelation
):
def test_load_slides_non_normalized(presentation: Presentation, revelation: Revelation):
presentation.file.write_text("# Pag1\r---\r\n# Pag2", "utf8")

slides = revelation.load_slides(str(presentation.file), "---", "---~")

assert slides == [["# Pag1\n"], ["\n# Pag2"]]


def test_load_slides_non_ascii(
presentation: Presentation, revelation: Revelation
):
def test_load_slides_non_ascii(presentation: Presentation, revelation: Revelation):
presentation.file.write_text("# こんにちは\n---\n# 乾杯", "utf8")

slides = revelation.load_slides(str(presentation.file), "---", "---~")
Expand Down
28 changes: 13 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ def test_mkstatic_custom_media(presentation: Presentation):
custom_media_file = output_dir / "media" / presentation_media_file.name

runner = CliRunner()
result = runner.invoke(cli, [
"mkstatic",
str(presentation.file),
"-o",
str(output_dir),
"-m",
str(presentation_media),
])
result = runner.invoke(
cli,
[
"mkstatic",
str(presentation.file),
"-o",
str(output_dir),
"-m",
str(presentation_media),
],
)

assert result.exit_code == 0
assert output_dir.is_dir()
Expand Down Expand Up @@ -98,10 +101,7 @@ def test_mkstatic_without_config(presentation: Presentation):
)

assert result.exit_code == 0
assert (
"Configuration file not detected, running with defaults."
in result.output
)
assert "Configuration file not detected, running with defaults." in result.output


def test_mkstatic_with_theme(presentation: Presentation):
Expand Down Expand Up @@ -254,9 +254,7 @@ def test_start_style_not_file(presentation: Presentation):
style_dir.mkdir()

runner = CliRunner()
result = runner.invoke(
cli, ["start", str(presentation.file), "-s", str(style_dir)]
)
result = runner.invoke(cli, ["start", str(presentation.file), "-s", str(style_dir)])

assert result.exit_code == 1
assert result.output == (
Expand Down
8 changes: 2 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def test_extract_file_zipfile(
):
src_files = sorted((f.name for f in presentation.root.iterdir()))

extracted_dir = Path(
extract_file(presentation_zip, str(presentation.parent))
)
extracted_dir = Path(extract_file(presentation_zip, str(presentation.parent)))

extracted_files = sorted((f.name for f in extracted_dir.iterdir()))

Expand All @@ -50,9 +48,7 @@ def test_extract_file_tarfile(
):
src_files = sorted((f.name for f in presentation.root.iterdir()))

extracted_dir = Path(
extract_file(presentation_tar, str(presentation.parent))
)
extracted_dir = Path(extract_file(presentation_tar, str(presentation.parent)))

extracted_files = sorted((f.name for f in extracted_dir.iterdir()))

Expand Down

0 comments on commit 9fd2390

Please sign in to comment.