Skip to content

Commit 0f7097d

Browse files
committed
Finish doc generation and add tests
1 parent e37ae28 commit 0f7097d

File tree

12 files changed

+212
-24
lines changed

12 files changed

+212
-24
lines changed

cloudquery/sdk/docs/generator.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,43 @@ def _generate_markdown(self, directory: str):
9898
f.write(formatted_all_tables)
9999

100100
for table in self._tables:
101-
table_template = env.from_string(TABLE)
102-
table_md = table_template.render(table=table)
103-
formatted_table_md = self._format_markdown(table_md)
104-
with open(os.path.join(directory, table.name + ".md"), "w") as f:
105-
f.write(formatted_table_md)
101+
self._render_table(directory, env, table)
102+
103+
def _render_table(self, directory: str, env: jinja2.Environment, table: Table):
104+
table_template = env.from_string(TABLE)
105+
table_md = table_template.render(table=table)
106+
formatted_table_md = self._format_markdown(table_md)
107+
with open(os.path.join(directory, table.name + ".md"), "w") as f:
108+
f.write(formatted_table_md)
109+
for relation in table.relations:
110+
self._render_table(directory, env, relation)
106111

107112
def _all_tables_entry(self, table: Table):
108113
env = jinja2.Environment()
109114
env.globals['indent_to_depth'] = self._indent_to_depth
110115
env.globals['all_tables_entry'] = self._all_tables_entry
116+
env.globals['indent_table_to_depth'] = self._indent_table_to_depth
111117
entry_template = env.from_string(ALL_TABLES_ENTRY)
112118
return entry_template.render(table=table)
113119

114120
@staticmethod
115-
def _indent_to_depth(text: str, depth: int):
121+
def _indent_table_to_depth(table: Table) -> str:
122+
s = ""
123+
t = table
124+
while t.parent is not None:
125+
s += " "
126+
t = t.parent
127+
return s
128+
129+
@staticmethod
130+
def _indent_to_depth(text: str, depth: int) -> str:
116131
indentation = depth * 4 # You can adjust the number of spaces as needed
117132
lines = text.split('\n')
118133
indented_lines = [(' ' * indentation) + line for line in lines]
119134
return '\n'.join(indented_lines)
120135

121136
@staticmethod
122-
def _format_markdown(text: str):
137+
def _format_markdown(text: str) -> str:
123138
re_match_newlines = re.compile(r'\n{3,}')
124139
re_match_headers = re.compile(r'(#{1,6}.+)\n+')
125140

cloudquery/sdk/docs/markdown_templates.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ALL_TABLES = """# Source Plugin: {{ plugin_name }}
22
## Tables
3-
{% for table in tables %}
3+
{%- for table in tables %}
44
{{ all_tables_entry(table) }}
5-
{% endfor %}"""
5+
{%- endfor %}"""
66

7-
ALL_TABLES_ENTRY = """{{ "" | indent(4) }}- [{{ table.name }}]({{ table.name }}.md){% if table.is_incremental %} (Incremental){% endif %}
8-
{% for rel in table.relations %}
9-
{{ all_tables_entry(rel) | indent(4) }}
10-
{% endfor %}"""
7+
ALL_TABLES_ENTRY = """{{ indent_table_to_depth(table) }}- [{{ table.name }}]({{ table.name }}.md){% if table.is_incremental %} (Incremental){% endif %}
8+
{%- for rel in table.relations %}
9+
{{ all_tables_entry(rel) }}
10+
{%- endfor %}"""
1111

1212
TABLE = """# Table: {{ table.name }}
1313
@@ -21,21 +21,20 @@
2121
The primary key for this table is **{{ table.primary_keys[0] }}**.
2222
{% else %}
2323
The composite primary key for this table is (
24-
{% for index, pk in table.primary_keys %}
25-
{{ " ," if index else "" }} **{{ pk }}**
26-
{% endfor %}
24+
{%- for pk in table.primary_keys %}
25+
{{- ", " if loop.index > 1 else "" }}**{{ pk }}**
26+
{%- endfor -%}
2727
).
2828
{% endif %}
2929
3030
{% if table.is_incremental %}
3131
It supports incremental syncs{% set ik_length = table.incremental_keys | length %}
32-
{% if ik_length == 1 %} based on the **{{ table.incremental_keys[0] }}** column{% else %}
33-
based on the (
34-
{% for index, pk in table.incremental_keys %}
35-
{{ " ," if index else "" }} **{{ pk }}**
36-
{% endfor %}
32+
{%- if ik_length == 1 %} based on the **{{ table.incremental_keys[0] }}** column{% else %} based on the (
33+
{%- for ik in table.incremental_keys %}
34+
{{- ", " if loop.index > 1 else "" }}**{{ ik }}**
35+
{%- endfor -%}
3736
) columns{% endif %}.
38-
{% endif %}
37+
{%- endif %}
3938
4039
{% if table.relations or table.parent %}
4140
## Relations

cloudquery/sdk/schema/table.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import List
24

35
import pyarrow as pa
@@ -8,13 +10,14 @@
810

911
class Table:
1012
def __init__(self, name: str, columns: List[Column], title: str = "", description: str = "",
11-
relations=None, is_incremental: bool = False) -> None:
13+
parent: Table = None, relations: List[Table] = None, is_incremental: bool = False) -> None:
1214
self.name = name
1315
self.columns = columns
1416
self.title = title
1517
self.description = description
1618
if relations is None:
1719
relations = []
20+
self.parent = parent
1821
self.relations = relations
1922
self.is_incremental = is_incremental
2023

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ grpcio-tools==1.56.2
33
plugin-pb-python==0.0.11
44
protobuf==4.23.4
55
pyarrow==12.0.1
6-
pytest==7.4.0
6+
pytest==7.4.0
7+
Jinja2~=3.1.2

tests/docs/__init__.py

Whitespace-only changes.

tests/docs/snapshots/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Source Plugin: test_plugin
2+
3+
## Tables
4+
5+
- [test_table](test_table.md)
6+
- [test_table_composite_pk](test_table_composite_pk.md) (Incremental)
7+
- [test_table_relations](test_table_relations.md) (Incremental)
8+
- [test_table_child](test_table_child.md)
9+
- [test_table_grandchild](test_table_grandchild.md)

tests/docs/snapshots/test_table.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Table: test_table
2+
3+
This table shows data for Test Table.
4+
5+
The primary key for this table is **string**.
6+
7+
## Columns
8+
9+
| Name | Type |
10+
| ------------- | ------------- |
11+
|string (PK)|`string`|
12+
|int32|`int32`|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Table: test_table_child
2+
3+
This table shows data for Child Table.
4+
5+
The primary key for this table is **pk1**.
6+
7+
## Relations
8+
9+
This table depends on [test_table_relations](test_table_relations.md).
10+
11+
The following tables depend on test_table_child:
12+
13+
- [test_table_grandchild](test_table_grandchild.md)
14+
15+
## Columns
16+
17+
| Name | Type |
18+
| ------------- | ------------- |
19+
|pk1 (PK)|`string`|
20+
|fk1|`string`|
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Table: test_table_composite_pk
2+
3+
This table shows data for Composite PKs.
4+
5+
The composite primary key for this table is (**pk1**, **pk2**).
6+
7+
It supports incremental syncs based on the (**pk1**, **pk2**) columns.
8+
9+
## Columns
10+
11+
| Name | Type |
12+
| ------------- | ------------- |
13+
|pk1 (PK) (Incremental Key)|`string`|
14+
|pk2 (PK) (Incremental Key)|`string`|
15+
|int32|`int32`|
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Table: test_table_grandchild
2+
3+
This table shows data for Grandchild Table.
4+
5+
The primary key for this table is **pk1**.
6+
7+
## Relations
8+
9+
This table depends on [test_table_child](test_table_child.md).
10+
11+
## Columns
12+
13+
| Name | Type |
14+
| ------------- | ------------- |
15+
|pk1 (PK)|`string`|

0 commit comments

Comments
 (0)