Skip to content

Commit c55b3f1

Browse files
authored
Merge pull request #50 from brain-bican/docs
jinja documentation fixes
2 parents 214cee8 + 0d3d5d7 commit c55b3f1

File tree

4 files changed

+102
-17
lines changed

4 files changed

+102
-17
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="tdta",
11-
version="0.1.0.dev20",
11+
version="0.1.0.dev21",
1212
description="The aim of this project is to provide taxonomy development tools custom actions.",
1313
long_description=README,
1414
long_description_content_type="text/markdown",

src/tdta/documentation.py

+57-15
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,92 @@
11
import os
2-
from pathlib import Path
2+
import shutil
33

4+
from pathlib import Path
45
from jinja2 import Template
56
from urllib.parse import urlparse
67

78
from tdta.tdt_export import db_to_cas
89
from tdta.utils import read_project_config
10+
from tdta.command_line_utils import runcmd
11+
from tdta.version_control import git_update_local
912

1013
ANNOTATIONS_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/annotation_template.md")
1114
TAXONOMY_TEMPLATE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../resources/taxonomy_template.md")
1215

1316

14-
def generate_documentation(sqlite_db: str, output_folder: str, project_config=None):
17+
def generate_documentation(sqlite_db: str, output_folder: str, project_config=None, git_push=True):
1518
"""
1619
Generate markdown documentation for a CAS database.
1720
Parameters:
1821
sqlite_db: Path to the CAS database.
1922
output_folder: Path to the output documentation folder.
2023
project_config: Project configuration.
2124
"""
22-
if not os.path.exists(output_folder):
23-
os.makedirs(output_folder)
24-
25+
project_folder = Path(output_folder).parent.absolute()
26+
index_file = os.path.join(output_folder, "index.md")
2527
cell_sets_folder = os.path.join(output_folder, "cell_sets")
26-
if not os.path.exists(cell_sets_folder):
27-
os.makedirs(cell_sets_folder)
28+
29+
clear_docs_folder(cell_sets_folder, index_file, output_folder)
2830

2931
cas_obj = db_to_cas(sqlite_db)
3032
cas = cas_obj.to_dict()
3133
if project_config is None:
32-
project_config = read_project_config(Path(output_folder).parent.absolute())
34+
project_config = read_project_config(project_folder)
3335
cas = transform_cas(cas, project_config)
3436

37+
generate_annotation_docs(cas, cell_sets_folder)
38+
generate_taxonomy_doc(cas, index_file, output_folder)
39+
40+
if git_push:
41+
runcmd("cd {dir} && git add --all {docs_folder}".format(dir=project_folder,
42+
docs_folder=os.path.relpath(output_folder, project_folder)))
43+
git_update_local(project_folder.absolute().as_posix(), "Update project documentation")
44+
runcmd("cd {dir} && git push".format(dir=project_folder))
45+
print("Taxonomy documentation sent to GitHub.")
46+
print("Github action is triggered to publish the documentation on the website. Please check the status of the action.")
47+
48+
49+
def generate_taxonomy_doc(cas, index_file, output_folder):
50+
"""
51+
Generate the taxonomy documentation (index.md).
52+
Parameters:
53+
cas: CAS object
54+
index_file: Path to the index file
55+
output_folder: Path to the output folder
56+
"""
57+
taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE)
58+
rendered_file = taxonomy_template.render(cas=cas)
59+
with open(index_file, "w") as fh:
60+
fh.write(rendered_file)
61+
print("Taxonomy documentation generated at {out_dir}".format(out_dir=output_folder))
62+
63+
64+
def generate_annotation_docs(cas, cell_sets_folder):
65+
"""
66+
Generate markdown documentation for each cell set in the CAS.
67+
Parameters:
68+
cas: CAS object
69+
cell_sets_folder: Path to the cell sets folder
70+
"""
3571
annotation_template = read_jinja_template(ANNOTATIONS_TEMPLATE)
36-
cell_sets_folder = os.path.join(output_folder, "cell_sets")
3772
for annotation in cas["annotations"]:
3873
rendered_file = annotation_template.render(annotation=annotation, metadata=cas)
3974
annotation_file_name = annotation["cell_set_accession"].replace(":", "_")
40-
41-
with open(os.path.join(cell_sets_folder, annotation_file_name + ".md"), "w") as fh:
75+
with open(os.path.join(cell_sets_folder, annotation_file_name + ".md"), "w") as fh:
4276
fh.write(rendered_file)
4377

44-
taxonomy_template = read_jinja_template(TAXONOMY_TEMPLATE)
45-
rendered_file = taxonomy_template.render(cas=cas)
46-
with open(os.path.join(output_folder, "index.md"), "w") as fh:
47-
fh.write(rendered_file)
78+
79+
def clear_docs_folder(cell_sets_folder, index_file, output_folder):
80+
"""
81+
Deletes the existing docs folder content.
82+
"""
83+
if not os.path.exists(output_folder):
84+
os.makedirs(output_folder)
85+
else:
86+
shutil.rmtree(cell_sets_folder, ignore_errors=True)
87+
if os.path.isfile(index_file):
88+
os.remove(index_file)
89+
os.makedirs(cell_sets_folder)
4890

4991

5092
def transform_cas(cas, project_config):

src/tdta/git_utils.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import zipfile
3+
4+
from tdta.command_line_utils import runcmd
5+
6+
GITHUB_SIZE_LIMIT = 100 * 1000 * 1000 # 100 MB
7+
8+
9+
def add_new_files_to_git(project_folder, new_files):
10+
"""
11+
Runs git add command to add imported files to the version control.
12+
Parameters:
13+
project_folder: project folder path
14+
new_files: imported/created file paths to add to the version control
15+
"""
16+
for file_path in new_files:
17+
if os.path.getsize(file_path) > GITHUB_SIZE_LIMIT:
18+
zip_path = zip_file(file_path)
19+
new_files.remove(file_path)
20+
runcmd("cd {dir} && git add {zip_path}".format(dir=project_folder, zip_path=zip_path))
21+
runcmd("cd {dir} && git reset {file_path}".format(dir=project_folder, file_path=file_path))
22+
23+
runcmd("cd {dir} && git add {files}".
24+
format(dir=project_folder,
25+
files=" ".join([t.replace(project_folder, ".", 1) for t in new_files])))
26+
27+
28+
def zip_file(file_path):
29+
"""
30+
Zips the file if it exceeds the GitHub size limit.
31+
Parameters:
32+
file_path: file path to zip
33+
Returns: zipped file path
34+
"""
35+
folder = os.path.dirname(file_path)
36+
base_name = os.path.basename(file_path)
37+
zip_base = os.path.splitext(base_name)[0]
38+
39+
single_zip_path = os.path.join(folder, f"{zip_base}.zip")
40+
with zipfile.ZipFile(single_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
41+
zipf.write(file_path, base_name)
42+
43+
return single_zip_path

src/test/generate_docs_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setUp(self):
1818

1919
def test_documentation_generation(self):
2020
generate_documentation(TEST_DB, TEST_OUTPUT, project_config={"id": "CS202210140",
21-
"custom_purl": "https://purl.brain-bican.org/taxonomy/CS202210140/CS202210140_non-neuronal/"})
21+
"custom_purl": "https://purl.brain-bican.org/taxonomy/CS202210140/CS202210140_non-neuronal/"}, git_push=False)
2222
self.assertTrue(os.path.exists(TEST_OUTPUT))
2323

2424
def test_hierarchy_breadcrumb(self):

0 commit comments

Comments
 (0)