Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify generatorConfigs.yaml #410

Merged
merged 1 commit into from
Apr 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/evolution-generator/src/helpers/generator_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def add_generator_comment() -> str:

# TODO: Add types for rows and headers
# Read data from Excel and return rows and headers
def get_data_from_excel(input_file: str, sheet_name: str) -> tuple:
def get_data_from_excel(excel_file_path: str, sheet_name: str) -> tuple:
try:
# Load Excel file
workbook: Workbook = openpyxl.load_workbook(input_file, data_only=True)
workbook: Workbook = openpyxl.load_workbook(excel_file_path, data_only=True)
sheet = workbook[sheet_name] # Get InputRange sheet
rows = list(sheet.rows) # Get all rows in the sheet
headers = [cell.value for cell in rows[0]] # Get headers from the first row
Expand Down
31 changes: 17 additions & 14 deletions packages/evolution-generator/src/scripts/generate_libelles.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ def replace(string):

# Class for managing translations in a specific language and section
class TranslationLangNs:
def __init__(self, inputFile):
def __init__(self, excel_file_path):
self.modified = False
self.data = {}
self.file = inputFile
self.file = excel_file_path
self.startBoldHtml = "<strong>"
self.endBoldHtml = "</strong>"

Expand Down Expand Up @@ -140,9 +140,9 @@ def addTranslation(self, path, value, overwrite, keepMarkdown):

# Class for managing translations for all languages and sections
class TranslationData:
def __init__(self, localesPath):
def __init__(self, libelles_output_folder_path):
self.translations = {} # Dictionary to store translations
self.localesPath = localesPath # Path to the locales directory
self.libelles_output_folder_path = libelles_output_folder_path # Path to the locales directory

# Add translations for a specific language and section
def addTranslations(self, lang, section, translations):
Expand All @@ -163,7 +163,7 @@ def addTranslation(self, lang, section, path, value, overwrite, keepMarkdown):
self.translations[lang] = {}
if not section in self.translations[lang]:
self.translations[lang][section] = TranslationLangNs(
os.path.join(self.localesPath, lang, section + ".yml")
os.path.join(self.libelles_output_folder_path, lang, section + ".yml")
)
self.translations[lang][section].addTranslation(
path, value, overwrite, keepMarkdown
Expand All @@ -175,17 +175,17 @@ def addTranslation(self, lang, section, path, value, overwrite, keepMarkdown):

# Class for managing the overall translation process
class FillLocalesTranslations:
def __init__(self, inputFile, localesPath, overwrite, section):
self.inputFile = inputFile
self.localesPath = localesPath
def __init__(self, excel_file_path, libelles_output_folder_path, overwrite, section):
self.excel_file_path = excel_file_path
self.libelles_output_folder_path = libelles_output_folder_path
self.overwrite = overwrite
self.section = section
self.allTranslations = TranslationData(localesPath)
self.allTranslations = TranslationData(libelles_output_folder_path)
super().__init__()

# Load existing translations from YAML files
def loadCurrentTranslations(self):
ymlFiles = glob(escape(self.localesPath) + "/**/*.yml")
ymlFiles = glob(escape(self.libelles_output_folder_path) + "/**/*.yml")
for translationFile in ymlFiles:
path = os.path.normpath(os.path.dirname(translationFile))
paths = path.split(os.sep)
Expand All @@ -202,7 +202,7 @@ def saveAllTranslations(self):
# Function to add translations from Excel input file to the translations data
def addTranslationsFromExcel(self):
try:
workbook = openpyxl.load_workbook(self.inputFile, data_only=True)
workbook = openpyxl.load_workbook(self.excel_file_path, data_only=True)
sheet = workbook["Widgets"] # Get Widgets sheet

for row in sheet.iter_rows(min_row=2, values_only=True):
Expand All @@ -229,14 +229,17 @@ def addTranslationsFromExcel(self):


# Function to generate the libelles locales files
def generate_libelles(inputFile, localesPath, overwrite=False, section=None):
def generate_libelles(
excel_file_path, libelles_output_folder_path, overwrite=False, section=None
):
try:
# Initialize the FillLocalesTranslations task with provided parameters
task = FillLocalesTranslations(inputFile, localesPath, overwrite, section)
task = FillLocalesTranslations(
excel_file_path, libelles_output_folder_path, overwrite, section
)
task.loadCurrentTranslations()
task.addTranslationsFromExcel()
task.saveAllTranslations()
print("Generate translations successfully")
except Exception as e:
print(f"An error occurred: {e}")
raise e
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@


# Function to generate sectionConfigs.ts for each section
def generate_section_configs(input_file: str):
def generate_section_configs(excel_file_path: str):
try:
is_excel_file(input_file) # Check if the input file is an Excel file
workbook = get_workbook(input_file) # Get workbook from Excel file
is_excel_file(excel_file_path) # Check if the input file path is an Excel file
workbook = get_workbook(excel_file_path) # Get workbook from Excel file
sheet_exists(workbook, "Sections") # Check if the sheet exists
sheet = workbook["Sections"] # Get Sections sheet
previousSection = None # Initialize previousSection as None
nextSection = None # Initialize nextSection as None

# Read data from Excel and return rows and headers
rows, headers = get_data_from_excel(input_file, sheet_name="Sections")
rows, headers = get_data_from_excel(excel_file_path, sheet_name="Sections")

# Test headers
get_headers(
Expand Down
23 changes: 16 additions & 7 deletions packages/evolution-generator/src/scripts/generate_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
# Note: This script includes functions that generate the sections.ts file.
# These functions are intended to be invoked from the generate_survey.py script.

from typing import List
from helpers.generator_helpers import INDENT, add_generator_comment
from helpers.generator_helpers import INDENT, get_data_from_excel, add_generator_comment


# Function to generate sections.ts
def generate_sections(output_file: str, sections: List[str]):
def generate_sections(excel_file_path: str, sections_output_file_path: str):
try:
# Read data from Excel and return rows and headers
rows, headers = get_data_from_excel(excel_file_path, sheet_name="Sections")

# Find the index of 'section' in headers
section_index = headers.index("section")
# Get all unique section names
section_names = set(row[section_index].value for row in rows[1:])

ts_code: str = "" # TypeScript code to be written to file

# Add Generator comment at the start of the file
Expand All @@ -20,23 +27,25 @@ def generate_sections(output_file: str, sections: List[str]):
# Generate the import statements
ts_code += "import { SectionsConfigs } from 'evolution-generator/lib/types/sectionsTypes';\n"
# Loop through each section and generate an import statement
for section in sections:
for section in section_names:
ts_code += f"import {section}Configs from './sections/{section}/sectionConfigs';\n"

# Generate the export statement
ts_code += "\n// Export all the sections configs\n"
ts_code += "const sectionsConfigs: SectionsConfigs = {\n"
# Loop through each section and generate an export statement
for section in sections:
for section in section_names:
ts_code += f"{INDENT}{section}: {section}Configs,\n"
ts_code += "};\n"
ts_code += "export default sectionsConfigs;\n"

# Write TypeScript code to a file
with open(output_file, mode="w", encoding="utf-8", newline="\n") as ts_file:
with open(
sections_output_file_path, mode="w", encoding="utf-8", newline="\n"
) as ts_file:
ts_file.write(ts_code)

print(f"Generate {output_file} successfully")
print(f"Generate {sections_output_file_path} successfully")

except Exception as e:
# Handle any other exceptions that might occur during script execution
Expand Down
89 changes: 54 additions & 35 deletions packages/evolution-generator/src/scripts/generate_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from scripts.generate_excel import generate_excel
from scripts.generate_section_configs import generate_section_configs
from scripts.generate_sections import generate_sections
from scripts.generate_widgets_config import generate_widgets_config
from scripts.generate_widgets_configs import generate_widgets_configs
from scripts.generate_widgets import generate_widgets
from scripts.generate_conditionals import generate_conditionals
from scripts.generate_choices import generate_choices
Expand All @@ -29,54 +29,73 @@ def generate_survey(config_path):
with open(config_path, "r") as file:
surveyGenerator = yaml.safe_load(file)

# Get the data from the YAML file
survey = surveyGenerator["survey"]
excel = surveyGenerator["excel"]
sections = surveyGenerator["sections"]
widgets = surveyGenerator["widgets"]
conditionals = surveyGenerator["conditionals"]
choices = surveyGenerator["choices"]
input_range = surveyGenerator["input_range"]
libelles = surveyGenerator["libelles"]

# Call the generate_excel function to generate the Excel file if active script
if excel["active_script"]:
# Get the data from the YAML file
survey_folder_path = surveyGenerator["survey_folder_path"]
excel_file_path = surveyGenerator["excel_file_path"]
enabled_scripts = surveyGenerator["enabled_scripts"]

# Call the generate_excel function to generate the Excel file if script enabled
if enabled_scripts["generate_excel"]:
generate_excel(
os.getenv("SHAREPOINT_URL"),
os.getenv("EXCEL_FILE_PATH"),
survey["excel_file"],
excel_file_path,
os.getenv("OFFICE365_USERNAME_EMAIL"),
os.getenv("OFFICE365_PASSWORD"),
)

# Call the generate_section_configs function to generate sectionConfigs.ts
generate_section_configs(survey["excel_file"])
# Call the generate_section_configs function to generate sectionConfigs.ts if script enabled
if enabled_scripts["generate_section_configs"]:
generate_section_configs(excel_file_path)

# Call the generate_sections function to generate sections.tsx
generate_sections(sections["output_file"], sections["sections"])
# Call the generate_sections function to generate sections.tsx if script enabled
if enabled_scripts["generate_sections"]:
sections_output_file_path = os.path.join(
survey_folder_path, "src", "survey", "sections.ts"
)
generate_sections(excel_file_path, sections_output_file_path)

# Call the generate_widgets_config function to generate widgetsConfig.tsx
generate_widgets_config(widgets["output_file"], sections["sections"])
# Call the generate_widgets_config function to generate widgetsConfigs.tsx if script enabled
if enabled_scripts["generate_widgets_configs"]:
widgets_configs_output_file_path = os.path.join(
survey_folder_path, "src", "survey", "widgetsConfigs.tsx"
)
generate_widgets_configs(excel_file_path, widgets_configs_output_file_path)

# Call the generate_widgets function to generate widgets.tsx for each section
generate_widgets(survey["excel_file"], widgets["output_info_list"])
# Call the generate_widgets function to generate widgets.tsx for each section if script enabled
if enabled_scripts["generate_widgets"]:
widgets_output_folder = os.path.join(
survey_folder_path, "src", "survey", "sections"
)
generate_widgets(excel_file_path, widgets_output_folder)

# Call the generate_conditionals function to generate conditionals.tsx
generate_conditionals(survey["excel_file"], conditionals["output_file"])
# Call the generate_conditionals function to generate conditionals.tsx if script enabled
if enabled_scripts["generate_conditionals"]:
conditionals_output_file_path = os.path.join(
survey_folder_path, "src", "survey", "common", "conditionals.tsx"
)
generate_conditionals(excel_file_path, conditionals_output_file_path)

# Call the generate_choices function to generate choices.tsx
generate_choices(survey["excel_file"], choices["output_file"])
# Call the generate_choices function to generate choices.tsx if script enabled
if enabled_scripts["generate_choices"]:
choices_output_file_path = os.path.join(
survey_folder_path, "src", "survey", "common", "choices.tsx"
)
generate_choices(excel_file_path, choices_output_file_path)

# Call the generate_input_range function to generate labels.tsx
generate_input_range(survey["excel_file"], input_range["output_file"])
# Call the generate_input_range function to generate labels.tsx if script enabled
if enabled_scripts["generate_input_range"]:
input_range_output_file_path = os.path.join(
survey_folder_path, "src", "survey", "common", "inputRange.tsx"
)
generate_input_range(excel_file_path, input_range_output_file_path)

# Call the generate_libelles function to generate the libelles locales folder
generate_libelles(
survey["excel_file"],
libelles["output_folder"],
libelles["overwrite"],
libelles["section"],
)
# Call the generate_libelles function to generate the libelles locales folder if script enabled
if enabled_scripts["generate_libelles"]:
libelles_output_folder_path = os.path.join(survey_folder_path, "locales")
generate_libelles(
excel_file_path, libelles_output_folder_path, overwrite=True, section=None
)


# Call the generate_survey function with the config_path argument
Expand Down
30 changes: 17 additions & 13 deletions packages/evolution-generator/src/scripts/generate_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
# This file is licensed under the MIT License.
# License text available at https://opensource.org/licenses/MIT

# Note: This script includes functions that generate the widgets.tsx file.
# Note: This script includes functions that generate the widgets.tsx and widgetsNames.ts files.
# These functions are intended to be invoked from the generate_survey.py script.
import openpyxl # Read data from Excel
from helpers.generator_helpers import INDENT, add_generator_comment
from helpers.generator_helpers import INDENT, get_data_from_excel, add_generator_comment

# Function to generate widgets.tsx for each section
def generate_widgets(input_path, output_info_list):
def generate_widgets(excel_file_path: str, widgets_output_folder: str):
try:
workbook = openpyxl.load_workbook(input_path, data_only=True)
sheet = workbook['Widgets'] # Get Widgets sheet
rows = list(sheet.rows)
# Read data from Excel and return rows and headers
rows, headers = get_data_from_excel(excel_file_path, sheet_name="Widgets")

# Find the index of 'section' in headers
section_index = headers.index("section")
# Get all unique section names
section_names = set(row[section_index].value for row in rows[1:])

# Transform Excel content into TypeScript code
def convert_excel_to_typescript(section):
Expand Down Expand Up @@ -67,23 +70,24 @@ def convert_excel_to_typescript(section):
return {'widgetsStatements': widgets_statements, 'widgetsNamesStatements': widgets_names_statements}

# Process the output files based on sections
for output_info in output_info_list:
transformed_content = convert_excel_to_typescript(output_info['section'])
for section in section_names:
transformed_content = convert_excel_to_typescript(section)
widgets_output_path = widgets_output_folder + '/' + section

# Add Generator comment at the start of the file
ts_code = add_generator_comment()

# Write the transformed content to the widgets output file
with open(output_info['output_folder'] + '/widgets.tsx', mode='w', encoding='utf-8', newline='\n') as f:
with open(widgets_output_path + '/widgets.tsx', mode='w', encoding='utf-8', newline='\n') as f:
f.write(ts_code)
f.write(transformed_content['widgetsStatements'])
print(f"Generate {output_info['output_folder']}/widgets.tsx successfully")
print(f"Generate {widgets_output_path}/widgets.tsx successfully")

# Write the transformed content to the widgetsNames output file
with open(output_info['output_folder'] + '/widgetsNames.ts', mode='w', encoding='utf-8', newline='\n') as f:
with open(widgets_output_path + '/widgetsNames.ts', mode='w', encoding='utf-8', newline='\n') as f:
f.write(ts_code)
f.write(transformed_content['widgetsNamesStatements'])
print(f"Generate {output_info['output_folder']}/widgetsNames.ts successfully")
print(f"Generate {widgets_output_path}/widgetsNames.ts successfully")

except Exception as e:
print(f"Error with widgets: {e}")
Expand Down
Loading
Loading