define-things/define_things/cli/create.py

93 lines
No EOL
3.2 KiB
Python

from importlib.resources import files
from pathlib import Path
import click
import subprocess
from jinja2 import (
Environment,
FileSystemLoader,
select_autoescape,
)
from datetime import datetime
import shutil
def get_template_dir():
return files("define_things").joinpath("templates")
def create_jinja_env():
block_delims = ('{', '}')
statement_marker = '%'
comment_marker = '#'
return Environment(
loader=FileSystemLoader(get_template_dir()),
block_start_string=f'{block_delims[0]}{statement_marker}',
block_end_string=f'{statement_marker}{block_delims[1]}',
variable_start_string=f'{block_delims[0]}{block_delims[0]}',
variable_end_string=f'{block_delims[1]}{block_delims[1]}',
comment_start_string=f'{block_delims[0]}{comment_marker}',
comment_end_string=f'{comment_marker}{block_delims[1]}',
autoescape=select_autoescape(['html', 'htm', 'xml']),
)
def render_file(template_path, jinja_env, data, output_path):
tmpl = jinja_env.get_template(str(template_path))
rendered_rec = tmpl.render(**data)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(rendered_rec)
def get_recursive_filepaths(dir):
return [p for p in dir.rglob("*") if p.is_file()]
def create_project(
data,
):
"""Creates a project in the current working directory."""
# Setup jinja variables
jinja_env = create_jinja_env()
# create project dir
base_dir = Path.cwd() / data['project_name']
base_dir.mkdir(parents=True, exist_ok=True)
# Get template paths
template_dir = get_template_dir()
template_paths = get_recursive_filepaths(template_dir)
for p in template_paths:
# if it is a jinja template, render at same relative path
# if it is NOT a jinja template, copy to same relative path
relative_path = p.relative_to(template_dir)
filename = p.name
pathstr = str(p)
outpath = base_dir / str(relative_path).replace("schema_name", data['schema_name']).replace(".j2", "")
if filename.endswith('.j2'):
render_file(relative_path, jinja_env, data, outpath)
else:
outpath.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(p, outpath)
@click.command('create')
@click.option('--project-name', prompt='Project name', help='The name of the concepts project to be created (required)', type=str,)
@click.option('--schema-name', prompt='Schema name', help='The name of the initial schema that will be created from the ORINOCO template (required)', type=str,)
@click.option('--schema-title', prompt='Schema title', help='A short sentence describing the schema (defaults to empty string)', type=str, default='')
@click.option('--schema-id', prompt='Schema ID', help='URI serving as the persistent identifier of the initial schema (defaults to empty string)', type=str, default='')
def cli(
project_name: str,
schema_name: str,
schema_title: str = '',
schema_id: str = '',
):
schema_name_updated = schema_name.replace(" ", "-")
data = {
'project_name': project_name,
'schema_name': schema_name_updated,
'schema_title': schema_title,
'schema_id': schema_id,
}
create_project(data)