Skip to content

Commit

Permalink
Merge pull request #58 from mdrachuk/clean-mime-types-human-era
Browse files Browse the repository at this point in the history
Clean/mime-types/human era
  • Loading branch information
mdrachuk committed Jun 29, 2020
2 parents d8e5b5c + 01a75cc commit ff4dc05
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lightweight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def blog_posts(source):
logger = logging.getLogger('lw')
logger.setLevel(logging.INFO)

__version__ = '1.0.0.dev51'
__version__ = '1.0.0.dev52'
16 changes: 15 additions & 1 deletion lightweight/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def dev(host: str, port: int) -> Site:
from logging import getLogger
from os import getcwd
from pathlib import Path
from shutil import rmtree
from typing import Callable, Any

from .errors import InvalidCommand, InvalidSiteCliUsage
Expand Down Expand Up @@ -73,12 +74,13 @@ def _argument_parser(self):

def _add_commands(self, subparsers):
self._add_build_cli(subparsers)
self._add_clean_cli(subparsers)
self._add_server_cli(subparsers)

def _add_build_cli(self, subparsers):
p = subparsers.add_parser(name='build', description='Generate site to the out directory')
p.add_argument('--out', type=str, default=self.default_out,
help='output directory for generation results.Defaults to cwd / "out"')
help='output directory for generation results. Defaults to cwd / "out"')
p.add_argument('--host', type=str, default=None, help=f'defaults to "{self.default_host}"')
p.add_argument('--port', type=int, default=None, help=f'defaults to "{self.default_port}"')
p.add_argument('--url', type=str, default=None,
Expand All @@ -99,6 +101,18 @@ def _run_build(self, args: Any):
logger.info(f' Starting building "{url}"')
self.build(url).generate(args.out)

def _add_clean_cli(self, subparsers):
p = subparsers.add_parser(name='clean', description='Remove the out directory')
p.add_argument('--out', type=str, default=self.default_out,
help='output directory for generation results. Defaults to cwd / "out"')
add_log_arguments(p)
p.set_defaults(func=self._run_clean)

def _run_clean(self, args: Any):
out = Path(args.out).absolute()
logger.info(f'Removing files at {str(out)}')
rmtree(out)

def _add_server_cli(self, subparsers):
p = subparsers.add_parser(name='serve',
description='Lightweight development server for static files')
Expand Down
1 change: 1 addition & 0 deletions lightweight/lw.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def custom_jinja_tags():
original_tags = (jinja_env.block_start_string, jinja_env.block_end_string,
jinja_env.variable_start_string, jinja_env.variable_end_string,
jinja_env.comment_start_string, jinja_env.comment_end_string)
jinja_env.filters['human_era'] = lambda year: 10000 + year # https://www.youtube.com/watch?v=czgOWmtGVGs
jinja_env.block_start_string = '{?'
jinja_env.block_end_string = '?}'
jinja_env.variable_start_string = '{!'
Expand Down
2 changes: 1 addition & 1 deletion lightweight/project-template/_templates_/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<footer>
<p>
© {! ctx.generated.year !}
© {! ctx.generated.year | human_era !}
|
made with <a href="https://lightweight.site" class="lw">Lightweight</a>
</p>
Expand Down
15 changes: 12 additions & 3 deletions lightweight/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class MimeType(Enum):
"""Mime-type of the file written to response Content-Type."""
html = 'text/html'
css = 'text/css'
image = 'image'
jpeg = 'image/jpeg'
png = 'image/png'
gif = 'image/gif'
svg = 'image/svg+xml'
plaintext = 'text/plain'

@classmethod
Expand All @@ -58,8 +61,14 @@ def of(cls, path: Path) -> MimeType:
return cls.html
if path.suffix == '.css':
return cls.css
if path.suffix == '.png' or path.suffix == '.jpg' or path.suffix == '.jpeg' or path.suffix == '.gif':
return cls.image
if path.suffix == '.jpg' or path.suffix == '.jpeg':
return cls.jpeg
if path.suffix == '.png':
return cls.png
if path.suffix == '.gif':
return cls.gif
if path.suffix == '.svg':
return cls.svg
return cls.plaintext


Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ def test_invalid_command_shows_help(self, capsys):
run_site_cli("test_cli.py")
assert "usage: test_cli.py" in capsys.readouterr().out

def test_clean(self, tmp_path: Path):
with directory(tmp_path):
out = tmp_path / 'out'
out.mkdir(parents=True)
(out / 'file').touch()
run_site_cli("test_cli.py clean")
assert not out.exists()

def test_specific_clean(self, tmp_path: Path):
with directory(tmp_path):
out = tmp_path / 'generated'
out.mkdir(parents=True)
(out / 'file').touch()
run_site_cli("test_cli.py clean --out generated")
assert not out.exists()

def test_build(self, mock_start_server, tmp_path: Path):
with directory(tmp_path):
index = tmp_path / 'index'
Expand Down

0 comments on commit ff4dc05

Please sign in to comment.