Skip to content

Commit

Permalink
Include funders list macro in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
HafeezOJ committed Jan 15, 2025
1 parent e40d233 commit 18c0952
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
9 changes: 8 additions & 1 deletion ldcoolp/curation/inspection/readme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Template engine
from jinja2 import Environment, FileSystemLoader
from jinja2.ext import loopcontrols
from html2text import html2text

# Logging
Expand Down Expand Up @@ -97,6 +98,7 @@ def __init__(self, dn: DepositorName, config_dict=config_default_dict,
self.folderName = self.dn.folderName
self.article_id = self.dn.article_id
self.article_dict = self.dn.curation_dict
self.funders_dict = 'funders_dict.jinja'

if isinstance(log, type(None)):
self.log = log_stdout()
Expand Down Expand Up @@ -260,6 +262,10 @@ def save_template(self):

dest_file = join(self.metadata_path, self.readme_template)

funders_dict_file_src = join(dirname(__file__), 'templates',
self.funders_dict)
funders_dict_file_dest = join(self.metadata_path, self.funders_dict)

if not exists(dest_file):
self.log.info(f"Saving {self.readme_template} template in METADATA ...")

Expand All @@ -271,6 +277,7 @@ def save_template(self):

self.log.info(f"Source file name: {src_file}")
shutil.copy(src_file, dest_file)
shutil.copy(funders_dict_file_src, funders_dict_file_dest)
else:
self.log.info(f"{dest_file} exists. Not overwriting template!")

Expand All @@ -283,7 +290,7 @@ def import_template(self):
"""Returns a jinja2 template by importing README markdown template (README_template.md)"""

file_loader = FileSystemLoader(self.metadata_path)
env = Environment(loader=file_loader, lstrip_blocks=True, trim_blocks=True)
env = Environment(loader=file_loader, lstrip_blocks=True, trim_blocks=True, extensions=[loopcontrols])

jinja_template = env.get_template(self.default_readme_file)
return jinja_template
Expand Down
7 changes: 4 additions & 3 deletions ldcoolp/curation/inspection/readme/templates/DVC.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ DOI:
---------------------------------------------
## Funders

{% import 'funders_dict.jinja' as funds %}
{% set ns = namespace(funder_found = false) %}
{% for funder in figshare_dict.funders %}
{% if funder.is_user_defined == 0 %}
- {{ funder.funder_name }} (Grant no: {{ funder.grant_code }})
{{ funder.url }}
{% if funder.is_user_defined == 0 %}
{{ funds.funders_(funder) }}
{% else %}
- {{ funder.title }}
{% endif %}
Expand Down
7 changes: 4 additions & 3 deletions ldcoolp/curation/inspection/readme/templates/Main.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ DOI:
---------------------------------------------
## Funders

{% import 'funders_dict.jinja' as funds %}
{% set ns = namespace(funder_found = false) %}
{% for funder in figshare_dict.funders %}
{% if funder.is_user_defined == 0 %}
- {{ funder.funder_name }} (Grant no: {{ funder.grant_code }})
{{ funder.url }}
{% if funder.is_user_defined == 0 %}
{{ funds.funders_(funder) }}
{% else %}
- {{ funder.title }}
{% endif %}
Expand Down
7 changes: 4 additions & 3 deletions ldcoolp/curation/inspection/readme/templates/Space_Grant.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ DOI:
---------------------------------------------
## Funders

{% import 'funders_dict.jinja' as funds %}
{% set ns = namespace(funder_found = false) %}
{% for funder in figshare_dict.funders %}
{% if funder.is_user_defined == 0 %}
- {{ funder.funder_name }} (Grant no: {{ funder.grant_code }})
{{ funder.url }}
{% if funder.is_user_defined == 0 %}
{{ funds.funders_(funder) }}
{% else %}
- {{ funder.title }}
{% endif %}
Expand Down
7 changes: 4 additions & 3 deletions ldcoolp/curation/inspection/readme/templates/WCCFL.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ DOI:
---------------------------------------------
## Funders

{% import 'funders_dict.jinja' as funds %}
{% set ns = namespace(funder_found = false) %}
{% for funder in figshare_dict.funders %}
{% if funder.is_user_defined == 0 %}
- {{ funder.funder_name }} (Grant no: {{ funder.grant_code }})
{{ funder.url }}
{% if funder.is_user_defined == 0 %}
{{ funds.funders_(funder) }}
{% else %}
- {{ funder.title }}
{% endif %}
Expand Down
28 changes: 28 additions & 0 deletions ldcoolp/curation/inspection/readme/templates/funders_dict.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% macro funders_(given_funder) %}
{% set funders = {
'National Science Foundation': ['Directorate for Biological Sciences',
'Directorate for Computer and Information Science and Engineering',
'Directorate for Engineering',
'Directorate of Geosciences',
'Directorate for Mathematical and Physical Sciences',
'Directorate for Social, Behavioral and Economic Sciences',
'Directorate for STEM Education',
'Directorate for Technology, Innovation and Partnerships'
]
} %}
{% set ns = namespace(given_funder_found = false) %}
{% for funder, offices in funders.items() %}
{% if given_funder.funder_name in offices %}
{% set ns.given_funder_found = true %}
- {{ funder }} (Grant no: {{ given_funder.grant_code }})
{{ given_funder.url }}
{% break %}
{% else %}
{% set ns.given_funder_found = false %}
{% endif %}
{% endfor %}
{% if ns.given_funder_found == false %}
- {{ given_funder.funder_name}} (Grant no: {{ given_funder.grant_code }})
{{ given_funder.url }}
{% endif %}
{% endmacro %}

0 comments on commit 18c0952

Please sign in to comment.