-
Notifications
You must be signed in to change notification settings - Fork 21
refactor: centralize output generation in TemplateManager #87
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,15 +102,16 @@ def format_response(issues: list) -> list: | |
| Takes a list of issues, and returns them sorted by language and comments. | ||
| """ | ||
| unique_languages = sorted(set([issue['language'] for issue in issues])) | ||
| formatted_results = [] | ||
| formatted_results = [] | ||
|
|
||
| for language in unique_languages: | ||
| results = [issue for issue in issues if issue['language'] == language] | ||
| sorted_results = sorted(results, key=lambda x: x['comments']) | ||
| formatted_results.append(sorted_results) | ||
| sorted_formatted_results = Utils.create_list_from_lists(formatted_results) | ||
|
|
||
|
|
||
| sorted_formatted_results = Utils.create_list_from_lists(formatted_results) | ||
| return sorted_formatted_results | ||
|
|
||
| @staticmethod | ||
| def render_template(csv_path, template_path, today): | ||
| """ | ||
|
|
@@ -121,24 +122,61 @@ def render_template(csv_path, template_path, today): | |
|
|
||
| with open(csv_path, newline='', encoding='utf-8') as f: | ||
| reader = csv.DictReader(f) | ||
|
|
||
| for row in reader: | ||
| results.append({ | ||
| 'repo': row['repo'], | ||
| 'repo': row['repo'], | ||
| 'language': row['language'], | ||
| 'title': row['title'], | ||
| 'url': row['url'], | ||
| 'title': row['title'], | ||
| 'url': row['url'], | ||
| 'comments': row['comments'], | ||
| }) | ||
|
|
||
| env = Environment(loader=FileSystemLoader(template_path)) | ||
| env = Environment(loader=FileSystemLoader(template_path)) | ||
|
|
||
| template = env.get_template('README.md.j2') | ||
| rendered_readme = template.render(results=results, today=today) | ||
| with open("README.md", "w+") as f: | ||
|
|
||
| with open("README.md", "w+", encoding="utf-8") as f: | ||
| f.write(rendered_readme) | ||
|
|
||
| return rendered_readme | ||
|
|
||
| @staticmethod | ||
|
Owner
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. Perfect implementation with the static method, and improved readability in the CSV columns by using new lines instead all in the same one! |
||
| def write_output(issues, output_file): | ||
| """ | ||
| Write issues to a file in CSV or JSON format. | ||
| """ | ||
| import json | ||
|
|
||
| ext = output_file.lower().split('.')[-1] | ||
|
|
||
| if ext == 'csv': | ||
| with open(output_file, 'w', newline='', encoding='utf-8') as f: | ||
| writer = csv.DictWriter( | ||
| f, | ||
| fieldnames=[ | ||
| 'repo', | ||
| 'language', | ||
| 'title', | ||
| 'url', | ||
| 'comments', | ||
| 'labels', | ||
| 'state' | ||
| ] | ||
| ) | ||
| writer.writeheader() | ||
| writer.writerows(issues) | ||
|
|
||
| elif ext == 'json': | ||
| with open(output_file, 'w', encoding='utf-8') as f: | ||
| json.dump(issues, f, indent=2) | ||
|
|
||
| else: | ||
| raise ValueError( | ||
| f"Unsupported output format: .{ext} " | ||
| f"(use --output issues.csv or --output issues.json)" | ||
| ) | ||
|
|
||
| class Utils: | ||
| @staticmethod | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,6 @@ | |
| import datetime | ||
| import time | ||
| import argparse | ||
| import csv | ||
| import json | ||
| import requests | ||
|
|
||
| from jinja2 import Environment, FileSystemLoader | ||
|
|
@@ -23,21 +21,6 @@ | |
| Utils, | ||
| ) | ||
|
|
||
|
|
||
| def write_output(issues, output_file): | ||
| """Write issues to a file in CSV or JSON format.""" | ||
| ext = output_file.lower().split('.')[-1] | ||
| if ext == 'csv': | ||
| with open(output_file, 'w', newline='', encoding='utf-8') as f: | ||
| writer = csv.DictWriter(f, fieldnames=['repo', 'language', 'title', 'url', 'comments', 'labels', 'state']) | ||
| writer.writeheader() | ||
| writer.writerows(issues) | ||
| elif ext == 'json': | ||
| with open(output_file, 'w', encoding='utf-8') as f: | ||
| json.dump(issues, f, indent=2) | ||
| else: | ||
| raise ValueError(f"Unsupported output format: .{ext} (use --output issues.csv or --output issues.json)") | ||
|
|
||
| def main(args): | ||
| """ | ||
| 1- Gathers all issues associated with all public repos | ||
|
|
@@ -74,7 +57,7 @@ def main(args): | |
| formatted_response = TemplateManager.format_response(issues) | ||
|
|
||
| if args.output: | ||
| write_output(formatted_response, args.output) | ||
| TemplateManager.write_output(formatted_response, args.output) | ||
|
Owner
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. Perfect call for a static method here! |
||
| logging.info(f"Wrote {len(issues)} issues to {args.output}") | ||
|
|
||
| logging.info(f"Rendered README file.") | ||
|
|
||
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 like that you deleted those unnecesary tabs there!