Skip to content

Commit

Permalink
Create command (#92)
Browse files Browse the repository at this point in the history
Copies starter example to destination

Fixes #22
  • Loading branch information
moggers87 committed May 1, 2021
1 parent 3a33bd6 commit 55efa37
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ For example:
> content_path: content
> EOF
.. note::
Exhibition comes with a starter template for new sites. See ``exhibit create --help`` for more information.
You can now generate your first Exhibition website!:
.. code-block:: shell
Expand Down
23 changes: 22 additions & 1 deletion exhibition/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
"""

import logging
import pathlib
import shutil

import click

from . import __version__, config, utils
from exhibition import __version__, config, utils
import exhibition as exhib_module

logger = logging.getLogger("exhibition")

Expand Down Expand Up @@ -73,3 +76,21 @@ def serve(server, port):
thread.join()
except (KeyboardInterrupt, SystemExit):
httpd.shutdown()


@exhibition.command(short_help="Create a starter site")
@click.argument("destination", metavar="PATH")
@click.option("-f", "--force", is_flag=True, help="overwrite destination if it exists")
def create(destination, force):
"""
Generate a starter site with a basic configuration for website.
"""
starter = pathlib.Path(exhib_module.__path__[0], "data", "starter")

if force:
shutil.rmtree(destination, ignore_errors=True)

if pathlib.Path(destination).exists():
raise click.ClickException("Directory '{}' exists, aborting".format(destination))

shutil.copytree(starter, destination)
5 changes: 5 additions & 0 deletions exhibition/data/starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example Website

Hey there! Welcome to this very basic Exhibition static website.

To build, type `exhibit gen`.
6 changes: 6 additions & 0 deletions exhibition/data/starter/content/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
extends: page.j2
---
{% block content %}
Hello!
{% endblock %}
5 changes: 5 additions & 0 deletions exhibition/data/starter/site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
title: My Example Website
filter: exhibition.filters.jinja2
deploy_path: deploy
content_path: content
templates: templates
9 changes: 9 additions & 0 deletions exhibition/data/starter/templates/page.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>{{ node.meta.title|typogrify }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
</head>
<body>{% block content %}{% endblock %}</body>
<html>
56 changes: 56 additions & 0 deletions exhibition/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
#
##

from filecmp import dircmp
from tempfile import TemporaryDirectory
from unittest import TestCase, mock
import logging
import pathlib

from click.testing import CliRunner

from exhibition import command, config
import exhibition


class CommandTestCase(TestCase):
Expand Down Expand Up @@ -120,3 +124,55 @@ def test():
self.assertEqual(log_mock.addHandler.call_count, 2)
self.assertEqual(log_mock.setLevel.call_count, 2)
self.assertEqual(log_mock.setLevel.call_args, ((logging.DEBUG,), {}))


class CreateTestCase(TestCase):
def _dir_cmp(self, pathA, pathB):
dir_diff = dircmp(pathA, pathB)
return dir_diff.left_only + dir_diff.right_only + dir_diff.funny_files + dir_diff.diff_files

def test_create(self):
with TemporaryDirectory() as tmpDir:
path = pathlib.Path(tmpDir, "site")
runner = CliRunner()
result = runner.invoke(command.exhibition, ["create", str(path)])
self.assertEqual(result.exit_code, 0)
self.assertEqual(path.exists(), True)
self.assertEqual(
self._dir_cmp(path, pathlib.Path(exhibition.__path__[0], "data", "starter")),
[],
)

def test_existing_dir(self):
with TemporaryDirectory() as tmpDir:
path = pathlib.Path(tmpDir, "site")
path.mkdir()
runner = CliRunner()
result = runner.invoke(command.exhibition, ["create", str(path)])
self.assertEqual(result.exit_code, 1)
self.assertEqual(path.exists(), True)

def test_force_with_existing_dir(self):
with TemporaryDirectory() as tmpDir:
path = pathlib.Path(tmpDir, "site")
path.mkdir()
runner = CliRunner()
result = runner.invoke(command.exhibition, ["create", "--force", str(path)])
self.assertEqual(result.exit_code, 0)
self.assertEqual(path.exists(), True)
self.assertEqual(
self._dir_cmp(path, pathlib.Path(exhibition.__path__[0], "data", "starter")),
[],
)

def test_force_without_existing_dir(self):
with TemporaryDirectory() as tmpDir:
path = pathlib.Path(tmpDir, "site")
runner = CliRunner()
result = runner.invoke(command.exhibition, ["create", "--force", str(path)])
self.assertEqual(result.exit_code, 0)
self.assertEqual(path.exists(), True)
self.assertEqual(
self._dir_cmp(path, pathlib.Path(exhibition.__path__[0], "data", "starter")),
[],
)
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exclude =
max-complexity = 10

[isort]
line_length = 120
line_length = 100
from_first = true
use_parentheses = true
skip_glob =
Expand Down Expand Up @@ -65,6 +65,6 @@ commands = make html
extras = docs

[testenv:isort]
commands = isort --check-only .
commands = isort --check-only --diff .
deps = isort
skip_install = true

0 comments on commit 55efa37

Please sign in to comment.