-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from carte-data/flatten-command
Add flatten command
- Loading branch information
Showing
5 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import glob | ||
from typing import Tuple | ||
from jinja2 import Environment, PackageLoader, select_autoescape, Template | ||
from pathlib import Path | ||
|
||
from jinja2.loaders import FileSystemLoader | ||
|
||
import carte_cli.utils.frontmatter as frontmatter | ||
from carte_cli.model.carte_table_model import TableMetadata | ||
|
||
|
||
def _get_flattened_template(template_path: str): | ||
if template_path is None: | ||
env = Environment( | ||
loader=PackageLoader("carte_cli.utils", "templates"), | ||
autoescape=select_autoescape(["md"]), | ||
) | ||
template = env.get_template("dataset_flattened.md") | ||
|
||
else: | ||
env = Environment(loader=FileSystemLoader(searchpath="./")) | ||
template = env.get_template(template_path) | ||
|
||
return template | ||
|
||
|
||
def flatten(input_dir: str, output_dir: str, template_path: str) -> None: | ||
|
||
template = _get_flattened_template(template_path) | ||
|
||
file_paths = glob.glob(input_dir + "/*/*/*.md", recursive=True) | ||
output_paths = [ | ||
os.path.join(output_dir, file_path[(len(input_dir)+1) :]) | ||
for file_path in file_paths | ||
] | ||
|
||
for file_path, output_path in zip(file_paths, output_paths): | ||
metadata, content = frontmatter.parse(file_path) | ||
dataset = TableMetadata.from_frontmatter(metadata, content) | ||
flattened_metadata, flattened_content = flatten_dataset(dataset, template) | ||
Path("/".join(output_path.split("/")[:-1])).mkdir(parents=True, exist_ok=True) | ||
frontmatter.dump(output_path, flattened_metadata, flattened_content) | ||
|
||
print("Done!") | ||
|
||
|
||
def flatten_dataset(dataset: TableMetadata, template: Template) -> Tuple[dict, str]: | ||
metadata = {"title": dataset.name} | ||
content = template.render(dataset=dataset) | ||
return metadata, content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# {{ dataset.name }} {: .page-title } | ||
|
||
<a class="edit-link" href="/admin/#/collections/datasets/entries/{{dataset.connection}}/{{dataset.database}}/{{dataset.name}}"></a> | ||
|
||
#### Type: {: .attribute } | ||
`{{ dataset.table_type.value }}` | ||
|
||
<br> | ||
{: .attribute-break} | ||
|
||
#### Database: {: .attribute } | ||
`{{ dataset.database }}` | ||
|
||
<br> | ||
{: .attribute-break} | ||
|
||
#### Location: {: .attribute } | ||
`{{ dataset.location }}` | ||
|
||
<br> | ||
{: .attribute-break} | ||
|
||
## Description | ||
|
||
{% if dataset.description.strip() != '' %} | ||
{{ dataset.description.strip() }} | ||
{% else %} | ||
No description | ||
{: .no-description } | ||
{% endif %} | ||
|
||
## Columns | ||
|
||
{% for column in dataset.columns %} | ||
##### {{ column.name }} {: .column-name } | ||
|
||
`{{ column.column_type }}`{: .column-type } | ||
{% if column.description != none %} | ||
{{- column.description -}} | ||
{: .column-description } | ||
|
||
{% else %} | ||
No description | ||
{: .column-description .no-description } | ||
|
||
{% endif %} | ||
{% endfor %} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters