Skip to content

Commit

Permalink
Merge pull request #22 from davidomarf/new
Browse files Browse the repository at this point in the history
Implement `ginpar new` to create new sketches with boilerplate code
  • Loading branch information
davidomarf committed Oct 24, 2019
2 parents af7f1db + 921034f commit 5bd3fb5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
49 changes: 48 additions & 1 deletion ginpar/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,30 @@
ginpar new
"""
import os
import yaml

import click
from jinja2 import Environment, FileSystemLoader

from ginpar.utils.files import create_folder, create_file
from ginpar.utils.echo import echo, error, success

## TODO: Move read_config into a shared library inside utils
def read_config(path):
"""Create a dictionary out of the YAML file received
Paremeters
----------
path : str
Path of the YAML file.
"""
with open(path, "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return config


def new(sketch):
Expand All @@ -31,4 +54,28 @@ def new(sketch):
sketch : str
Name of the sketch to create
"""
click.secho("You're in new", fg="blue")

_SITE = "config.yaml"
site = read_config(_SITE)

path = os.path.join(site["content_path"], sketch)

_TEMPLATES_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "templates", "sketch"
)
_jinja_env = Environment(loader=FileSystemLoader(_TEMPLATES_DIR), trim_blocks=True)

if os.path.isdir(path):
error(f"Failure.")
echo(f"{path} already exists.")
raise click.Abort()

create_folder(path)

sketch_template = _jinja_env.get_template("sketch.js")
data_template = _jinja_env.get_template("data.yaml")

create_file(os.path.join(path, "sketch.js"), sketch_template.render())
create_file(os.path.join(path, "data.yaml"), data_template.render())

echo(f"\nYour new sketch {path} is ready.\n")
7 changes: 7 additions & 0 deletions ginpar/templates/sketch/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- var: DIMENSIONS
attrs:
type: dimensions
value:
- 2048
- 2048
23 changes: 23 additions & 0 deletions ginpar/templates/sketch/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* You don't need to initialize your variables in here. However, if you decide
* to do it, you **must** use `var` instead of `let` or `const`.
*/

var DIMENSIONS = [400, 2048]

/**
* Standard function of p5js
*/
function setup() {
createCanvas(DIMENSIONS[0], DIMENSIONS[1]).parent("artwork-container");

// Call draw() only once
noLoop();
}

/**
* Standard function of p5js
*/
function draw() {
// Set a background color
background(225);
}
41 changes: 37 additions & 4 deletions tests/test_new.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import os

from click.testing import CliRunner
from ginpar import cli

def test_new():
runner = CliRunner()
result = runner.invoke(cli, ["new", 'hey'])
assert result.exit_code == 0
assert "You're in new" in result.output
"""New works properly on the default scenario.
Default scenario is when the directory of the new sketch doesn't
exist in the source_path..
"""
runner = CliRunner()
with runner.isolated_filesystem():
## Initialize a new project and cd it
runner.invoke(cli, ["init", "-q"])
os.chdir("my-site")

result = runner.invoke(cli, ["new", "test"])

data_path = os.path.join("sketches", "test", "data.yaml")
sketch_path = os.path.join("sketches", "test", "sketch.js")
assert result.exit_code == 0
assert os.path.isfile(data_path)
assert os.path.isfile(sketch_path)


def test_new_existent():
"""New works properly when the sketch already exists
"""
runner = CliRunner()
with runner.isolated_filesystem():
## Initialize a new project and cd it
runner.invoke(cli, ["init", "-q"])
os.chdir("my-site")

## Create test
runner.invoke(cli, ["new", "test"])

result = runner.invoke(cli, ["new", "test"])
assert result.exit_code == 1

0 comments on commit 5bd3fb5

Please sign in to comment.