Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Divide table_templates.py into common templates & word-translations.yml #31

Merged
merged 7 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 77 additions & 19 deletions automation/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import subprocess
from pprint import pprint
from datetime import datetime
import pypandoc
import yaml
import validators
Expand Down Expand Up @@ -71,6 +72,7 @@ def convert_md2tex(md_filename, latex_filename):
assert output == ""
print(f"Created successfully: {latex_filename}")


def clean_directory(folder_path):
"""Deletes the specified directory"""

Expand Down Expand Up @@ -267,42 +269,65 @@ def extract_goal(focus_area_README):

return focus_area_name, focus_area_goal

def generate_focus_areas(focus_area_filename, focus_area_README, metrics, english_template):
"""Dynamically creates the table of metrics"""
def read_file(filename):
"""Returns data from given file in the form of a string"""

table_head = english_template.template_focus_areas
table_tail = english_template.template_end
with open(filename, "r") as f:
return f.read()


def replace_metric_table_keywords(table_head, focus_area_README, word_translation_yaml_data, language):
"""Replaces specific keywords from the metric table templates.
Also adds focus area name and goal to the file
"""

focus_area_name, focus_area_goal = extract_goal(focus_area_README)

table_head = table_head.replace("$FOCUS_AREA_NAME$", focus_area_name)
table_head = table_head.replace("$FOCUS_AREA_GOAL$", focus_area_goal)
keywords_dict = {
"$FOCUS_AREA_NAME$" : focus_area_name,
"$FOCUS_AREA_GOAL$" : focus_area_goal,
"$FOCUS_AREA$" : word_translation_yaml_data[language]["focus-area"],
"$GOAL$" : word_translation_yaml_data[language]["goal"],
"$METRIC$" : word_translation_yaml_data[language]["metric"],
"$QUESTION$" : word_translation_yaml_data[language]["question"]
}

for k, v in keywords_dict.items():
table_head = table_head.replace(k, v)

return table_head


def generate_metric_table(table_head, table_tail, focus_area_filename, metric_list):
"""Creates the metric tables for a particular focus area """

for metric in metrics:
for metric in metric_list:
metric_name, metric_question = extract_question(metric)
table_head += '\t\t' + metric_name + ' & ' + metric_question + ' \\\\ \n\t\t\hline\n'

table_head += table_tail

## TODO: add the name of metrics files
# \input{techical-fork}
# .... metrics...

# file_name = focus_area_name + '.tex'
with open(focus_area_filename, 'w') as f:
f.write(table_head)

print(f"\nGenerating focus-area file = {focus_area_filename}")

def focus_areas_table(wg_tex_file, section_name, focus_areas_list, language_template):
"""Dynamically creates tables of focus areas in a working group"""

table_head = language_template.template_working_group
table_tail = language_template.template_end
def replace_fa_table_keywords(table_head, section_name, word_translation_yaml_data, language):
"""Replaces specific keywords from the focus area table templates.
Also adds WG heading as section_name
"""

table_head = table_head.replace("$SECTION_NAME$", section_name)
table_head = table_head.replace("$FOCUS_AREA$", word_translation_yaml_data[language]["focus-area"])
table_head = table_head.replace("$GOAL$", word_translation_yaml_data[language]["goal"])

return table_head


def generate_fa_table(table_head, table_tail, wg_filename, focus_area_README_list):
"""Creates the table of focus areas starting from a blank WG.tex file"""

for FA in focus_areas_list:
for FA in focus_area_README_list:

# FA[0] = focus_area_name
# FA[1] = focus_area_README.md
Expand All @@ -311,7 +336,40 @@ def focus_areas_table(wg_tex_file, section_name, focus_areas_list, language_temp
table_head += '\t\t' + focus_area_name + ' & ' + focus_area_goal + ' \\\\ \n\t\t\hline\n'

table_head += table_tail
wg_tex_file.write(table_head)

with open(wg_filename, 'w') as f:
f.write(table_head)


def convert_tex2pdf(tex_filename, word_translation_yaml_data, language, cover_filename):
"""Converts master latex file to PDF. Adds the toc headings and
cover page depending on language
"""

toc_heading = word_translation_yaml_data[language]["toc-heading"]


pdf_filename = f"{language.title()}-Release-" + datetime.today().strftime('%Y-%m-%d') + ".pdf"

print(f"\nConverting {tex_filename} file to PDF")
output = pypandoc.convert_file(tex_filename, 'pdf', outputfile=pdf_filename, extra_args=['-f', 'latex',
'--pdf-engine=xelatex',
'--include-in-header', 'header_1.tex',
'--highlight-style', 'zenburn',
'-V', 'geometry:margin=0.8in',
'-V', 'monofont:DejaVuSansMono.ttf',
'-V', 'mathfont:texgyredejavu-math.otf',
'-V', 'geometry:a4paper',
'-V', 'colorlinks=true',
'-V', f'toc-title:{toc_heading}',
'-V', 'linkcolour:blue',
'-V', 'fontsize=12pt',
'--toc', '--toc-depth= 3',
'--include-before-body', cover_filename,
'--include-after-body', 'end-matter.tex'])

return pdf_filename


def print_summary(wg_count, focus_area_count, metric_count):

Expand Down
100 changes: 46 additions & 54 deletions automation/metrics_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import shutil
import main
import helper
import table_templates
import inspect
import sys
from pydoc import locate

def check_is_yml_file(language, yml_filename):

Expand All @@ -29,75 +26,63 @@ def check_is_cover_file(language, cover_filename):
print(f"Specify/check if the filename is: {cover_filename}",helper.color.END)
sys.exit(1)

def check_is_class_exist(language, class_name):

print("Check: Finding language class in templates")

if bool(class_name in [class_tuple[0] for class_tuple in inspect.getmembers(table_templates)]):
print(f"Class found successfully: {class_name}\n")
else:
print(helper.color.RED,f"Error: Unable to detect language class - '{class_name}' for language {language}")
print("Make sure that class for language exists or create one in 'table_templates.py' ",helper.color.END)
sys.exit(1)


def release_main(language):

included_wgs = []
focus_area_count = 0
metric_count = 0
yml_filename = language + "_working-groups-config.yml"
wg_config_yml_filename = language + "_working-groups-config.yml"
cover_filename = language + "_cover.tex"
class_name = language.title()
word_translation_yml_filename = "word-translations.yml"
table_fa_top_filename = "table-focus-areas-top.tex"
table_metric_top_filename = "table-metrics-top.tex"
table_end_filename = "table-end.tex"

print()
check_is_yml_file(language, yml_filename)
check_is_yml_file(language, wg_config_yml_filename)
check_is_cover_file(language, cover_filename)
check_is_class_exist(language, class_name)
# check_is_class_exist(language, class_name)
print(helper.color.GREEN, "Passed all checks successfully", helper.color.END)


# Read the yml file
print("\nReading the YML file:\n")
yaml_data = helper.load_yaml(yml_filename)
wg_config_yaml_data = helper.load_yaml(wg_config_yml_filename)
word_translation_yaml_data = helper.load_yaml(word_translation_yml_filename)

# add front and end matter
helper.add_front_matter(yaml_data)
helper.add_end_matter(yaml_data)
helper.add_front_matter(wg_config_yaml_data)
helper.add_end_matter(wg_config_yaml_data)

# delete front and end matter from yml
helper.delete_dictkey("front-matter", yaml_data)
helper.delete_dictkey("end-matter", yaml_data)


language_class = locate('table_templates.' + class_name)
language_template = language_class()
helper.delete_dictkey("front-matter", wg_config_yaml_data)
helper.delete_dictkey("end-matter", wg_config_yaml_data)

# LOOP #1: For Working Groups
for wg_name in yaml_data.keys():
if yaml_data[wg_name]['include-wg']:
for wg_name in wg_config_yaml_data.keys():
if wg_config_yaml_data[wg_name]['include-wg']:

if helper.is_url(yaml_data[wg_name]['repo-link']):
if helper.is_url(wg_config_yaml_data[wg_name]['repo-link']):
# clone repo with specified branch in yaml data
print(f"\nCloning from URL: {yaml_data[wg_name]['repo-link']}\nBranch: {yaml_data[wg_name]['repo-branch']}\n")
helper.clone_repo(yaml_data[wg_name]['repo-link'], wg_name, yaml_data[wg_name]['repo-branch'])
print(f"\nCloning from URL: {wg_config_yaml_data[wg_name]['repo-link']}\nBranch: {wg_config_yaml_data[wg_name]['repo-branch']}\n")
helper.clone_repo(wg_config_yaml_data[wg_name]['repo-link'], wg_name, wg_config_yaml_data[wg_name]['repo-branch'])

else:
print(helper.color.RED, f"Warning: In {yaml_data[wg_name]['wg-fullname']}, {yaml_data[wg_name]['repo-link']} is not a valid URL ")
print(helper.color.RED, f"Warning: In {wg_config_yaml_data[wg_name]['wg-fullname']}, {wg_config_yaml_data[wg_name]['repo-link']} is not a valid URL ")
print("Check the repository details in the YAML file", helper.color.END)

included_wgs.append(wg_name)
included_focus_areas = []
focus_area_README_list = []

# LOOP #2: For Focus Areas
for focus_area, metrics in yaml_data[wg_name]["focus-areas"].items():
for focus_area, metrics in wg_config_yaml_data[wg_name]["focus-areas"].items():
converted_tex_files = []
if metrics is not None:

# LOOP #3: For Metrics
for metric in metrics:
metric_path = os.path.join(yaml_data[wg_name]["focus-areas-location"], focus_area, metric)
metric_path = os.path.join(wg_config_yaml_data[wg_name]["focus-areas-location"], focus_area, metric)

shutil.copy2(metric_path, "./")
# helper.copy_file(metric_path, "./")
Expand All @@ -112,43 +97,49 @@ def release_main(language):
print(f"\nMaking images directory")
os.makedirs("images")
helper.copy_dir_files(
os.path.join(yaml_data[wg_name]["focus-areas-location"], focus_area, "images"),
os.path.join(wg_config_yaml_data[wg_name]["focus-areas-location"], focus_area, "images"),
os.path.join("./", "images"))

focus_area_README = os.path.join(yaml_data[wg_name]["focus-areas-location"], focus_area, "README.md")
focus_area_README = os.path.join(wg_config_yaml_data[wg_name]["focus-areas-location"], focus_area, "README.md")

# to be used in focus-areas table for WG.tex
focus_area_README_list.append([focus_area, focus_area_README])

# create focus_area.tex file and add table
# Read the metric table template and replace keywords requiring translations
table_metric_head = helper.read_file(table_metric_top_filename)
table_metric_head = helper.replace_metric_table_keywords(table_metric_head, focus_area_README, word_translation_yaml_data, language)

# Create focus area latex file to include metric table
focus_area_filename = wg_name + "_" + focus_area + ".tex"
helper.generate_focus_areas(focus_area_filename, focus_area_README, metrics,
language_template)
table_metric_tail = helper.read_file(table_end_filename)
helper.generate_metric_table(table_metric_head, table_metric_tail, focus_area_filename, metrics)

included_focus_areas.append(focus_area_filename)

# Add inclusion commands for metrics
# Add inclusion commands for metric files
with open(focus_area_filename, "a") as fa_tex_file:
fa_tex_file.write("\n")
for metric_tex_file in converted_tex_files:
fa_tex_file.write(f"\input{{{os.path.splitext(metric_tex_file)[0]}}} \n")

metric_count += len(converted_tex_files)

# create WG.tex file
wg_tex_file_path = os.path.join("./", wg_name + ".tex")

with open(wg_tex_file_path, "w") as wg_tex_file:
wg_tex_file.write("\n")
# Read the focus area table template and replace keywords requiring translations
table_fa_head = helper.read_file(table_fa_top_filename)
table_fa_head = helper.replace_fa_table_keywords(table_fa_head, wg_config_yaml_data[wg_name]['wg-fullname'], word_translation_yaml_data, language)

# add focus areas table to WG.tex
helper.focus_areas_table(wg_tex_file, yaml_data[wg_name]['wg-fullname'], focus_area_README_list,
language_template)
wg_tex_file.write("\n\clearpage\n")
# Create working group latex file to include focus area table
wg_filename = wg_name + ".tex"
table_fa_tail = helper.read_file(table_end_filename)
helper.generate_fa_table(table_fa_head, table_fa_tail, wg_filename, focus_area_README_list)

# Add inclusion commands for focus area latex file
with open(wg_filename, "a") as wg_file:
wg_file.write("\n\clearpage\n")
for fa in included_focus_areas:
wg_tex_file.write(f"\input{{{os.path.splitext(fa)[0]}}} \n")
wg_file.write(f"\input{{{os.path.splitext(fa)[0]}}} \n")

focus_area_count += len(included_focus_areas)
focus_area_count += len(included_focus_areas)

# create master file to include WG.tex files
with open(main.master_file_path, "a") as master_file:
Expand All @@ -160,7 +151,8 @@ def release_main(language):
master_file.write("\n\end{document}\n")

# create final PDF
pdf_filename = language_template.convert_tex2pdf(main.master_file_path)
# pdf_filename = language_template.convert_tex2pdf(main.master_file_path)
pdf_filename = helper.convert_tex2pdf(main.master_file_path, word_translation_yaml_data, language, cover_filename)
helper.copy_file(pdf_filename, "../output")

helper.print_summary(len(included_wgs), focus_area_count, metric_count)
Expand Down
2 changes: 2 additions & 0 deletions automation/passive_user_input/table-end.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\end{tabular}
\end{table}
7 changes: 7 additions & 0 deletions automation/passive_user_input/table-focus-areas-top.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\section{$SECTION_NAME$}
\begin{table}[ht!]
\centering
\begin{tabular}{|p{0.35\linewidth} | p{0.6\linewidth}|}
\hline
\hfil \textbf{$FOCUS_AREA$} & \hfil \textbf{$GOAL$} \\
\hline
8 changes: 8 additions & 0 deletions automation/passive_user_input/table-metrics-top.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
\subsection{$FOCUS_AREA$ - $FOCUS_AREA_NAME$}
\textbf{$GOAL$:} $FOCUS_AREA_GOAL$
\begin{table}[ht!]
\centering
\begin{tabular}{|p{0.35\linewidth} | p{0.6\linewidth}|}
\hline
\hfil \textbf{$METRIC$} & \hfil \textbf{$QUESTION$} \\
\hline
24 changes: 24 additions & 0 deletions automation/passive_user_input/word-translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file contains the translations of specific keywords used to dynamically create tables

english:
focus-area: Focus Area
goal: Goal
metric: Metric
question: Question
toc-heading: Contents

chinese:
focus-area: 关注领域
goal: 目标
metric: 度量指标
question: 问题
toc-heading: 内容

spanish:
focus-area: Área de trabajo
goal: Objetivo
metric: Métrica
question: Pregunta
toc-heading: Índice