-
Notifications
You must be signed in to change notification settings - Fork 4
Restructured spaghetti code to fix #17 #64
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
Open
joshua2705
wants to merge
10
commits into
numbbo:main
Choose a base branch
from
joshua2705:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6817395
Created a new save_folder_index that uses a html template
6799f0c
Updated venv in gitignore
9fc979a
For Mac path changes
b45df20
Another attempt
4f6774c
Making it JS and not delete re-write
c659f4e
Writing directly on top
77b4d5b
SEparated the JS script
d548077
Comment mods
arisfvr cd2a184
Merge pull request #1 from joshua2705/feature/redefine-save-folder-in…
joshua2705 378c38a
Add links to documentation and source code in README
henridollars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,4 +18,5 @@ src/cocopp/**/*.dat | |
| src/cocopp/**/*.tdat | ||
| src/cocopp/**/*.info | ||
| dist/ | ||
| ppdata/ | ||
| ppdata/ | ||
| venv/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """HTML content generator for COCO post-processing.""" | ||
|
|
||
| import json | ||
| import os | ||
|
|
||
| class HtmlGenerator: | ||
| """Generates HTML content with dynamic JavaScript updates.""" | ||
|
|
||
| STATIC_TEMPLATE = """<!DOCTYPE html> | ||
| <HTML> | ||
| <HEAD> | ||
| <META NAME="description" CONTENT="COCO/BBOB figures by function"> | ||
| <META NAME="keywords" CONTENT="COCO, BBOB"> | ||
| <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> | ||
| <TITLE>COCO Post-Processing Results</TITLE> | ||
| <SCRIPT SRC="sorttable.js"></SCRIPT> | ||
| <style> | ||
| body { font-family: Arial, sans-serif; margin: 20px; } | ||
| h1 { color: #333; } | ||
| h2 { color: #666; margin-top: 30px; } | ||
| a { color: #0066cc; text-decoration: none; } | ||
| a:hover { text-decoration: underline; } | ||
| #linksContainer { margin: 20px 0; } | ||
| .link-item { margin: 8px 0; } | ||
| .nav-link { font-weight: bold; margin: 5px 0; } | ||
| </style> | ||
| </HEAD> | ||
| <BODY> | ||
| <H1>COCO Post-Processing Results</H1> | ||
| <div id="linksContainer"></div> | ||
| <div id="imagesContainer"></div> | ||
|
|
||
| <script> | ||
| // Data injected by server | ||
| window.contentData = {data}; | ||
| </script> | ||
| <SCRIPT SRC="renderer.js"></SCRIPT> | ||
|
|
||
| </BODY> | ||
| </HTML>""" | ||
|
|
||
| def __init__(self): | ||
| pass | ||
|
|
||
| def generate_parent_index_data(self, algo_data, single_file_name, many_file_name): | ||
| """Generate data structure for parent index.""" | ||
| return { | ||
| 'title': 'COCO Post-Processing Results', | ||
| 'header': 'COCO Post-Processing Results', | ||
| 'nav_links': [], | ||
| 'single': sorted(algo_data.get('single', [])), | ||
| 'comparison': sorted(algo_data.get('comparison', [])), | ||
| 'images': [], | ||
| 'single_file_name': single_file_name, | ||
| 'many_file_name': many_file_name | ||
| } | ||
|
|
||
| def generate_folder_content(self, current_dir, image_extension): | ||
| """Generate data structure for folder index.""" | ||
| nav_links = [ | ||
| '<a href="../index.html">Home</a>', | ||
| '<a href="pprldflex.html">Runtime profiles (with arrow keys navigation)</a>', | ||
| '<a href="pptable.html">Tables for selected targets</a>', | ||
| '<a href="pprldistr.html">Runtime profiles for selected targets</a>' | ||
| ] | ||
|
|
||
| images = [] | ||
| image_path = 'pprldmany-single-functions/pprldmany.%s' % image_extension | ||
| if os.path.isfile(os.path.join(current_dir, image_path)): | ||
| images.append(image_path) | ||
|
|
||
| return { | ||
| 'title': 'COCO Post-Processing Results', | ||
| 'header': 'Results Overview', | ||
| 'nav_links': nav_links, | ||
| 'single': [], | ||
| 'comparison': [], | ||
| 'images': images, | ||
| 'single_file_name': '', | ||
| 'many_file_name': '' | ||
| } | ||
|
|
||
| def render(self, data): | ||
| """Render HTML with injected data.""" | ||
| json_data = json.dumps(data, ensure_ascii=False) | ||
| html = self.STATIC_TEMPLATE.replace('{data}', json_data) | ||
| return html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Main interface for generating folder index pages.""" | ||
|
|
||
| import logging | ||
| import os | ||
| from .generator import HtmlGenerator | ||
| from .writer import HtmlWriter | ||
| from .. import genericsettings | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def collect_algorithm_data(directory): | ||
| """Collect algorithm data from directory structure.""" | ||
| data = {'single': [], 'comparison': []} | ||
|
|
||
| if not os.path.isdir(directory): | ||
| return data | ||
|
|
||
| try: | ||
| for item in os.listdir(directory): | ||
| item_path = os.path.join(directory, item) | ||
| if not os.path.isdir(item_path): | ||
| continue | ||
|
|
||
| single_file = os.path.join(item_path, '%s.html' % genericsettings.single_algorithm_file_name) | ||
| if os.path.isfile(single_file): | ||
| data['single'].append(item) | ||
|
|
||
| many_file = os.path.join(item_path, '%s.html' % genericsettings.many_algorithm_file_name) | ||
| if os.path.isfile(many_file): | ||
| data['comparison'].append(item) | ||
| except OSError as e: | ||
| logger.warning("Error reading directory %s: %s" % (directory, str(e))) | ||
|
|
||
| return data | ||
|
|
||
| def update_parent_index(parent_index_path): | ||
| """Update the parent index.html file with links to algorithm results. | ||
|
|
||
| This function only writes the HTML file once. Links are managed | ||
| dynamically via JavaScript based on available algorithm directories. | ||
|
|
||
| Args: | ||
| parent_index_path: Path to the parent index.html file | ||
|
|
||
| Raises: | ||
| IOError: If there are issues reading/writing the index file | ||
| """ | ||
| try: | ||
| generator = HtmlGenerator() | ||
| parent_dir = os.path.dirname(os.path.realpath(parent_index_path)) | ||
|
|
||
| # to collect data from available algorithms | ||
| algo_data = collect_algorithm_data(parent_dir) | ||
|
|
||
| # generating data structure for dynamic rendering | ||
| data = generator.generate_parent_index_data( | ||
| algo_data, | ||
| genericsettings.single_algorithm_file_name, | ||
| genericsettings.many_algorithm_file_name | ||
| ) | ||
|
|
||
| # HTML rendering | ||
| html = generator.render(data) | ||
|
|
||
| # initial creation of the file (will be created only if it doesn't already exist) | ||
| writer = HtmlWriter() | ||
| if not os.path.isfile(parent_index_path): | ||
| writer.write_safely(parent_index_path, html) | ||
| logger.info("Created parent index at %s" % parent_index_path) | ||
| else: | ||
| logger.info("Parent index already exists at %s (using dynamic JS updates)" % parent_index_path) | ||
|
|
||
| except Exception as e: | ||
| logger.error("Failed to update parent index: %s" % str(e)) | ||
| raise IOError("Failed to update parent index: %s" % str(e)) | ||
|
|
||
| def save_folder_index(filepath, image_extension): | ||
| """Generate and save a folder index file. | ||
|
|
||
| The HTML file is created once with static structure. Dynamic content | ||
| is managed via JavaScript and server-side data updates. | ||
|
|
||
| Args: | ||
| filepath: Path where the index file should be saved | ||
| image_extension: Extension for image files (e.g. 'svg', 'png') | ||
| """ | ||
| if not filepath: | ||
| return | ||
|
|
||
| try: | ||
| # content data generation | ||
| generator = HtmlGenerator() | ||
| current_dir = os.path.dirname(os.path.realpath(filepath)) | ||
| data = generator.generate_folder_content(current_dir, image_extension) | ||
|
|
||
| # rendering to HTML | ||
| html = generator.render(data) | ||
|
|
||
| # initial creation of the file (created only if it doesn't already exist) | ||
| writer = HtmlWriter() | ||
| if not os.path.isfile(filepath): | ||
| writer.write_safely(filepath, html) | ||
| logger.info("Created folder index at %s" % filepath) | ||
| else: | ||
| logger.info("Folder index already exists at %s (using dynamic JS updates)" % filepath) | ||
|
|
||
| # update parent index if needed | ||
| parent_dir = os.path.dirname(current_dir) | ||
| parent_index_path = os.path.join(parent_dir, 'index.html') | ||
| if not os.path.isfile(parent_index_path): | ||
| update_parent_index(parent_index_path) | ||
|
|
||
| except Exception as e: | ||
| logger.error("Failed to save folder index at %s: %s" % (filepath, str(e))) | ||
| raise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // data injected by server | ||
| var contentData = window.contentData || {}; | ||
|
|
||
| function renderLinks(data) { | ||
| var container = document.getElementById('linksContainer'); | ||
| var html = ''; | ||
|
|
||
| // Navigation links | ||
| if (data.nav_links && data.nav_links.length > 0) { | ||
| data.nav_links.forEach(function(link) { | ||
| if (link) { | ||
| html += '<div class="nav-link">' + link + '</div>'; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // comparison code | ||
| if (data.comparison && data.comparison.length > 0) { | ||
| html += '<h2>Comparison Data</h2>'; | ||
| data.comparison.forEach(function(algo) { | ||
| var path = algo + '/' + data.many_file_name + '.html'; | ||
| html += '<div class="link-item"> <a href="' + path + '">' + algo + '</a></div>'; | ||
| }); | ||
| } | ||
|
|
||
| // single algorithm code | ||
| if (data.single && data.single.length > 0) { | ||
| html += '<h2>Single Algorithm Data</h2>'; | ||
| data.single.forEach(function(algo) { | ||
| var path = algo + '/' + data.single_file_name + '.html'; | ||
| html += '<div class="link-item"> <a href="' + path + '">' + algo + '</a></div>'; | ||
| }); | ||
| } | ||
|
|
||
| container.innerHTML = html; | ||
| } | ||
|
|
||
| function renderImages(data) { | ||
| var container = document.getElementById('imagesContainer'); | ||
| var html = ''; | ||
|
|
||
| if (data.images && data.images.length > 0) { | ||
| data.images.forEach(function(img) { | ||
| html += '<div><a href="' + img + '"><img src="' + img + '" height="380em"></a></div>'; | ||
| }); | ||
| } | ||
|
|
||
| container.innerHTML = html; | ||
| } | ||
|
|
||
| // rendering code | ||
| document.addEventListener('DOMContentLoaded', function() { | ||
| if (contentData) { | ||
| renderLinks(contentData); | ||
| renderImages(contentData); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| """Safe file writing utilities for HTML output.""" | ||
|
|
||
| import os | ||
|
|
||
| class HtmlWriter: | ||
| """Handles safe writing of HTML files with atomic operations.""" | ||
|
|
||
| @staticmethod | ||
| def write_safely(filepath, content): | ||
| """Write content to file atomically using a temporary file. | ||
|
|
||
| Args: | ||
| filepath: Path to the output file | ||
| content: HTML content to write | ||
| """ | ||
| filepath = str(filepath) | ||
|
|
||
| # creating parent directories (if they don't already exist) | ||
| parent_dir = os.path.dirname(filepath) | ||
| if parent_dir and not os.path.exists(parent_dir): | ||
| os.makedirs(parent_dir) | ||
|
|
||
| try: | ||
| with open(filepath, 'w', encoding='utf-8', newline='') as f: | ||
| f.write(content) | ||
| f.flush() | ||
| os.fsync(f.fileno()) # forcing writing to disk (for mac users) | ||
| except Exception as e: | ||
| raise IOError("Failed to write %s: %s" % (filepath, str(e))) |
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the line mentioned in the issue #17 (line no 499) which gets called repeatedly by the old architecture. The function to this new line is imported in line 17. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used a venv for testing.
This can be removed if necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't exactly understand the causal link between these two.
In any case, you should rebase your branch on the current main branch, resolve possible conflicts, and update the pull request.