-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.py
More file actions
400 lines (367 loc) · 11.5 KB
/
cli.py
File metadata and controls
400 lines (367 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import datetime
import json
from pathlib import Path
import click
from dateutil import parser as dateparser
from dateutil.parser import ParserError
from render_engine import Collection
from rich.console import Console
from render_engine_cli.event import ServerEventHandler
from render_engine_cli.utils import (
create_collection_entry,
display_filtered_templates,
get_available_themes,
get_editor,
get_site,
get_site_content_paths,
handle_content_file,
remove_output_folder,
split_args,
split_module_site,
validate_collection,
validate_file_name_or_slug,
validate_module_site,
)
try:
# Get the RE version for display. If it's not set it means we're working locally.
from render_engine.__version__ import version as re_version
except ImportError:
re_version = "development"
try:
# Get the CLI version for display. If it's not set it means we're working locally.
from render_engine_cli.__version__ import version as cli_version
except ImportError:
cli_version = "development"
def get_version_string() -> str:
return f"""
Render Engine CLI version {cli_version}
Render Engine version {re_version}""".strip()
@click.group()
@click.version_option(message=get_version_string())
def app(): ...
@app.command
@click.option(
"-e",
"--extra-context",
help="Extra context to pass to the cookiecutter template. This must be a JSON string",
default=None,
type=click.STRING,
)
@click.option(
"-t",
"--template",
help="Template tcd ..o use for creating a new site.",
default="https://github.com/render-engine/cookiecutter-render-engine-site",
type=click.STRING,
)
@click.option(
"--no-input",
is_flag=True,
default=False,
help="Do not prompt for parameters.",
)
@click.option(
"-o",
"--output-dir",
type=click.Path(
path_type=Path,
exists=False,
),
default=Path("./"),
)
@click.option(
"-c",
"--config-file",
type=click.Path(
path_type=Path,
exists=True,
readable=True,
),
default=None,
)
def init(template: str, extra_context: str, no_input: bool, output_dir: Path, config_file: Path):
"""
Create a new site configuration. You can provide extra_context to the cookiecutter template.
Also, any argument that cookiecutter accepts can be passed to this command.
The template can be a local path or a git repository.
"""
# Check if cookiecutter is installed
try:
from cookiecutter.main import cookiecutter
except ImportError:
click.secho(
"You need to install cookiecutter to use this command. Run `pip install cookiecutter` to install it.",
err=True,
fg="red",
)
raise click.Exit(0)
extra_context = json.loads(extra_context) if extra_context else None
cookiecutter(
template=template,
extra_context=extra_context,
output_dir=output_dir,
config_file=config_file,
no_input=no_input,
)
@app.command
@click.option(
"--module-site",
type=click.STRING,
# help="The module (python file) and site (the Site object) for your site in the format module:site",
callback=validate_module_site,
)
@click.option(
"-c",
"--clean",
help="Clean the output folder prior to building.",
is_flag=True,
default=False,
)
def build(module_site: str, clean: bool):
print("Running the build...")
module, site_name = split_module_site(module_site)
site = get_site(module, site_name)
if clean:
remove_output_folder(Path(site.output_path))
site.render()
@app.command
@click.option(
"--module-site",
type=click.STRING,
# help="The module (python file) and site (the Site object) for your site in the format module:site",
callback=validate_module_site,
)
@click.option(
"-c",
"--clean",
help="Clean the output folder prior to building.",
is_flag=True,
default=False,
)
@click.option(
"-r",
"--reload",
help="Reload on changes to the site.",
is_flag=True,
default=False,
)
@click.option("-p", "--port", type=click.IntRange(0, 65534), help="Port to serve on", default=8000.0)
def serve(module_site: str, clean: bool, reload: bool, port: int):
"""
Create an HTTP server to serve the site at `localhost`.
!!! warning
this is only for development purposes and should not be used in production.
Params:
module_site: Python module and initialize Site class
reload: Use to reload server on file change
build: flag to build the site prior to serving the app
directory: Directory to serve. If `module_site` is provided, this will be the `output_path` of the site.
port: Port to serve on
"""
if not module_site:
raise click.exceptions.BadParameter("You need to specify module:site")
module, site_name = split_module_site(module_site)
site = get_site(module, site_name)
if clean:
remove_output_folder(Path(site.output_path))
site.render()
server_address = ("127.0.0.1", port)
handler = ServerEventHandler(
import_path=module,
server_address=server_address,
dirs_to_watch=get_site_content_paths(site) if reload else None,
site=site_name,
output_path=site.output_path,
patterns=None,
ignore_patterns=[r".*output\\*.+$", r"\.\\\..+$", r".*__.*$"],
)
with handler:
pass
@app.command
@click.option(
"--module-site",
type=click.STRING,
# help="The module (python file) and site (the Site object) for your site in the format module:site",
callback=validate_module_site,
)
@click.option(
"--collection",
type=click.STRING,
help="The name of the collection to add the entry to.",
callback=validate_collection,
)
@click.option(
"--content",
default=None,
help="The content to include in the page. Either this or --content-file may be specified but not both.",
type=click.STRING,
)
@click.option(
"--content-file",
type=click.STRING,
callback=handle_content_file,
default=None,
help="Path to a file containing the desired content. Using 'stdin' will ask you to enter the content in "
"the terminal. Either this or `--content` may be provided but not both",
)
@click.option(
"-t",
"--title",
type=click.STRING,
help="Title for the new entry.",
default=None,
)
@click.option(
"-s",
"--slug",
type=click.STRING,
help="Slug for the new page.",
callback=validate_file_name_or_slug,
)
@click.option(
"-d",
"--include-date",
is_flag=True,
default=False,
help="Include today's date in the metadata for your entry.",
)
@click.option(
"-a",
"--args",
multiple=True,
type=click.STRING,
help="key value attrs to include in your entry use the format `--args key=value` or `--args key:value`",
)
@click.option(
"-e",
"--editor",
default="default",
type=click.STRING,
callback=get_editor,
help="Select the editor to use. If not set the default editor (as set by the EDITOR environment variable) "
"will be used. If 'none' is set no editor will be launched.",
)
@click.option(
"-f",
"--filename",
type=click.STRING,
help="The filename in which to save the path. Will be saved in the collection's `content_path` [REQUIRED]",
callback=validate_file_name_or_slug,
)
def new_entry(
module_site: str,
collection: str,
content: str,
content_file: str,
title: str,
slug: str,
include_date: bool,
args: list[str],
editor: str,
filename: str,
):
"""Creates a new collection entry based on the parser. Entries are added to the Collections content_path"""
parsed_args = split_args(args) if args else {}
# There is an issue with including `title` in the context to the parser that causes an exception. We can fix
# this by popping it out of the arguments here and using regex to push it back in later.
_title = parsed_args.pop("title", None)
# Prefer the title keyword from the one provided in `--args` in case someone does both.
title = title or _title
if slug:
# If `slug` is provided as a keyword add it to the `parsed_args` to be included in the rendering.
# Prefer the keyword to what is passed via `--args`
parsed_args["slug"] = slug
# Verify that we have a valid date should it be supplied or requested
if date := parsed_args.pop("date", None):
try:
date = dateparser.parse(date)
except ParserError:
raise ValueError(f"Invalid date: {repr(date)}.") from None
elif include_date:
date = datetime.datetime.today()
if date:
parsed_args["date"] = date
module, site_name = split_module_site(module_site)
site = get_site(module, site_name)
_collection: Collection
if not (
_collection := next(
coll for coll in site.route_list.values() if type(coll).__name__.lower() == collection.lower()
)
):
raise click.exceptions.BadParameter(f"Unknown collection: {collection}")
filepath = Path(_collection.content_path).joinpath(filename)
if filepath.exists():
if not click.confirm(
f"File {filename} exists are {_collection.content_path} - do you wish to overwrite that file?"
):
click.secho("Aborting new entry.", fg="yellow")
return
if content and content_file:
raise TypeError("Both content and content_file provided. At most one may be provided.")
if content_file:
content = content_file
if title:
parsed_args["title"] = title
try:
entry = create_collection_entry(
editor=editor,
filepath=filepath,
content=content or "",
collection=_collection,
**parsed_args,
)
except ValueError as e:
raise click.BadParameter from e
click.echo(entry)
@app.command
@click.option(
"--module-site",
type=click.STRING,
# help="The module (python file) and site (the Site object) for your site in the format module:site",
callback=validate_module_site,
)
@click.option(
"-t",
"--theme-name",
type=click.STRING,
help="Theme to search templates in.",
default="",
)
@click.option(
"-f",
"--filter-value",
type=click.STRING,
help="Filter templates based on names.",
default="",
)
def templates(module_site: str, theme_name: str, filter_value: str):
"""
CLI for listing available theme templates.
Params:
module_site: Python module and initialize Site class
theme_name: Optional. Specifies the theme to list templates from.
filter_value: Optional. Filters templates based on provided names.
"""
module, site_name = split_module_site(module_site)
site = get_site(module, site_name)
console = Console()
if theme_name:
available_themes = get_available_themes(console, site, theme_name)
if available_themes:
display_filtered_templates(
f"[bold green]Available templates for {theme_name} [bold green]",
available_themes,
filter_value,
)
else:
console.print("[red]No theme name specified. Listing all installed themes and their templates[red]")
for theme_prefix, theme_loader in site.theme_manager.prefix.items():
templates_list = theme_loader.list_templates()
display_filtered_templates(
f"[bold green]Showing templates for {theme_prefix}[bold green]",
templates_list,
filter_value,
)
if __name__ == "__main__":
app()