|
1 | | -from click import command, argument, echo |
| 1 | +import os |
2 | 2 |
|
3 | | -from cloudinary_cli.defaults import TEMPLATE_EXTS |
4 | | -from cloudinary_cli.utils.utils import load_template |
| 3 | +from click import command, argument, echo, option |
| 4 | + |
| 5 | +from cloudinary_cli.defaults import TEMPLATE_EXTS, TEMPLATE_FOLDER |
| 6 | +from cloudinary_cli.utils.utils import load_template, print_help_and_exit |
5 | 7 |
|
6 | 8 |
|
7 | 9 | @command("make", short_help="Return template code for implementing the specified Cloudinary widget.", |
8 | 10 | help="""\b |
9 | 11 | Return template code for implementing the specified Cloudinary widget. |
10 | | -e.g. cld make product_gallery |
| 12 | +e.g. cld make media library widget |
| 13 | + cld make python find all empty folders |
11 | 14 | """) |
12 | 15 | @argument("template", nargs=-1) |
13 | | -def make(template): |
14 | | - language = "html" |
| 16 | +@option("-ll", "--list-languages", is_flag=True, help="List available languages.") |
| 17 | +@option("-lt", "--list-templates", is_flag=True, help="List available templates.") |
| 18 | +def make(template, list_languages, list_templates): |
| 19 | + if not any([template, list_languages, list_templates]): |
| 20 | + print_help_and_exit() |
| 21 | + |
| 22 | + if list_languages: |
| 23 | + echo("Available languages") |
| 24 | + with os.scandir(TEMPLATE_FOLDER) as languages: |
| 25 | + for tpl_language in languages: |
| 26 | + if tpl_language.is_dir(): |
| 27 | + echo(tpl_language.name) |
| 28 | + return True |
| 29 | + |
| 30 | + language, template = _handle_language_and_template(template) |
| 31 | + |
| 32 | + if list_templates: |
| 33 | + echo(f"Available templates for language: {language}") |
| 34 | + with os.scandir(os.path.join(TEMPLATE_FOLDER, language)) as templates: |
| 35 | + for template_file in templates: |
| 36 | + if template_file.is_file(): |
| 37 | + echo(template_file.name.replace("_", " ")) |
| 38 | + return True |
| 39 | + |
| 40 | + echo(load_template(language, '_'.join(template))) |
| 41 | + |
| 42 | + |
| 43 | +def _handle_language_and_template(language_and_template): |
| 44 | + language = "html" # default language, in case not specified |
| 45 | + |
| 46 | + if not language_and_template: |
| 47 | + return language, language_and_template |
| 48 | + |
| 49 | + template = list(language_and_template) |
15 | 50 | if template[-1] in TEMPLATE_EXTS.keys(): |
16 | | - language = template[-1] |
17 | | - template = template[:-1] |
| 51 | + language = template.pop() |
18 | 52 | elif template[0] in TEMPLATE_EXTS.keys(): |
19 | | - language = template[0] |
20 | | - template = template[1:] |
| 53 | + language = template.pop(0) |
21 | 54 |
|
22 | | - echo(load_template(language, '_'.join(template))) |
| 55 | + return language, template |
0 commit comments