Skip to content

Commit

Permalink
Pylint touches.
Browse files Browse the repository at this point in the history
  • Loading branch information
MobiTikula committed Nov 5, 2024
1 parent 59bf373 commit 656c21e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 59 deletions.
107 changes: 54 additions & 53 deletions living_documentation_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from living_documentation_generator.model.project_issue import ProjectIssue
from living_documentation_generator.utils.decorators import safe_call_decorator
from living_documentation_generator.utils.github_rate_limiter import GithubRateLimiter
from living_documentation_generator.utils.utils import make_issue_key, generate_root_level_index_page
from living_documentation_generator.utils.utils import make_issue_key, generate_root_level_index_page, load_template
from living_documentation_generator.utils.constants import (
ISSUES_PER_PAGE_LIMIT,
ISSUE_STATE_ALL,
Expand Down Expand Up @@ -294,57 +294,16 @@ def _generate_markdown_pages(self, issues: dict[str, ConsolidatedIssue]) -> None
is_structured_output = ActionInputs.get_is_structured_output_enabled()
is_grouping_by_topics = ActionInputs.get_is_grouping_by_topics_enabled()
output_path = ActionInputs.get_output_directory()
issue_page_detail_template = None
index_page_template = None
index_root_level_page = None
index_org_level_template = None
index_repo_page_template = None
index_data_level_template = None

# Load the template files for generating the Markdown pages
try:
with open(LivingDocumentationGenerator.ISSUE_PAGE_TEMPLATE_FILE, "r", encoding="utf-8") as f:
issue_page_detail_template = f.read()
except IOError:
logger.error("Issue page template file was not successfully loaded.", exc_info=True)

try:
with open(LivingDocumentationGenerator.INDEX_NO_STRUCT_TEMPLATE_FILE, "r", encoding="utf-8") as f:
index_page_template = f.read()
except IOError:
logger.error("Index page template file was not successfully loaded.", exc_info=True)

try:
with open(LivingDocumentationGenerator.INDEX_ROOT_LEVEL_TEMPLATE_FILE, "r", encoding="utf-8") as f:
index_root_level_page = f.read()
except IOError:
logger.error(
"Structured index page template file for root level was not successfully loaded.", exc_info=True
)

try:
with open(LivingDocumentationGenerator.INDEX_ORG_LEVEL_TEMPLATE_FILE, "r", encoding="utf-8") as f:
index_org_level_template = f.read()
except IOError:
logger.error(
"Structured index page template file for organization level was not successfully loaded.", exc_info=True
)

try:
with open(LivingDocumentationGenerator.INDEX_TOPIC_PAGE_TEMPLATE_FILE, "r", encoding="utf-8") as f:
index_repo_page_template = f.read()
except IOError:
logger.error(
"Structured index page template file for repository level was not successfully loaded.", exc_info=True
)

try:
with open(LivingDocumentationGenerator.INDEX_DATA_LEVEL_TEMPLATE_FILE, "r", encoding="utf-8") as f:
index_data_level_template = f.read()
except IOError:
logger.error(
"Structured index page template file for data level was not successfully loaded.", exc_info=True
)
(
issue_page_detail_template,
index_page_template,
index_root_level_page,
index_org_level_template,
index_repo_page_template,
index_data_level_template,
) = self._load_all_templates()

# Generate a markdown page for every issue
for consolidated_issue in issues.values():
Expand All @@ -361,7 +320,7 @@ def _generate_markdown_pages(self, issues: dict[str, ConsolidatedIssue]) -> None
# Generate an index page with a summary table about all issues grouped by topics
elif is_grouping_by_topics:
issues = list(issues.values())
topics = set([issue.topic for issue in issues])
topics = {issue.topic for issue in issues}
generate_root_level_index_page(index_root_level_page, output_path)

for topic in topics:
Expand Down Expand Up @@ -447,14 +406,15 @@ def _generate_structured_index_pages(
organization_name,
)

# Generate an index pages for the documentation based on the grouped issues by topics
if ActionInputs.get_is_grouping_by_topics_enabled():
self._generate_sub_level_index_page(index_repo_level_template, "repo", repository_id)
logger.debug(
"Generated repository level _index.md` for repository: %s.",
repository_name,
)

topics = set([issue.topic for issue in issues])
topics = {issue.topic for issue in issues}
for topic in topics:
self._generate_index_page(index_data_level_template, issues, repository_id, topic)
logger.debug(
Expand All @@ -465,7 +425,7 @@ def _generate_structured_index_pages(
else:
self._generate_index_page(index_data_level_template, issues, repository_id)
logger.debug(
"Generated repository level `_index.md` for %s",
"Generated data level `_index.md` for %s",
repository_id,
)

Expand Down Expand Up @@ -700,3 +660,44 @@ def _generate_index_directory_path(repository_id: Optional[str], topic: Optional
os.makedirs(output_path, exist_ok=True)

return output_path

@staticmethod
def _load_all_templates() -> tuple[str, ...]:
"""
Load all template files for generating the Markdown pages.
@return: A tuple containing all loaded template files.
"""
issue_page_detail_template = load_template(
LivingDocumentationGenerator.ISSUE_PAGE_TEMPLATE_FILE,
"Issue page template file was not successfully loaded.",
)
index_page_template = load_template(
LivingDocumentationGenerator.INDEX_NO_STRUCT_TEMPLATE_FILE,
"Index page template file was not successfully loaded.",
)
index_root_level_page = load_template(
LivingDocumentationGenerator.INDEX_ROOT_LEVEL_TEMPLATE_FILE,
"Structured index page template file for root level was not successfully loaded.",
)
index_org_level_template = load_template(
LivingDocumentationGenerator.INDEX_ORG_LEVEL_TEMPLATE_FILE,
"Structured index page template file for organization level was not successfully loaded.",
)
index_repo_page_template = load_template(
LivingDocumentationGenerator.INDEX_TOPIC_PAGE_TEMPLATE_FILE,
"Structured index page template file for repository level was not successfully loaded.",
)
index_data_level_template = load_template(
LivingDocumentationGenerator.INDEX_DATA_LEVEL_TEMPLATE_FILE,
"Structured index page template file for data level was not successfully loaded.",
)

return (
issue_page_detail_template,
index_page_template,
index_root_level_page,
index_org_level_template,
index_repo_page_template,
index_data_level_template,
)
16 changes: 10 additions & 6 deletions living_documentation_generator/model/consolidated_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ def __init__(self, repository_id: str, repository_issue: Issue = None):
# save issue from repository (got from GitHub library & keep connection to repository for lazy loading)
# Warning: several issue properties requires additional API calls - use wisely to keep low API usage
self.__issue: Issue = repository_issue

self.__repository_id: str = repository_id
parts = repository_id.split("/")
self.__organization_name: str = parts[0] if len(parts) == 2 else ""
self.__repository_name: str = parts[1] if len(parts) == 2 else ""
self.__topic: str = ""

# Extra project data (optionally provided from GithubProjects class)
Expand All @@ -69,12 +65,14 @@ def repository_id(self) -> str:
@property
def organization_name(self) -> str:
"""Getter of the organization where the issue was fetched from."""
return self.__organization_name
parts = self.__repository_id.split("/")
return parts[0] if len(parts) == 2 else ""

@property
def repository_name(self) -> str:
"""Getter of the repository name where the issue was fetched from."""
return self.__repository_name
parts = self.__repository_id.split("/")
return parts[1] if len(parts) == 2 else ""

@property
def topic(self) -> str:
Expand Down Expand Up @@ -172,6 +170,12 @@ def generate_page_filename(self) -> str:
return page_filename

def generate_directory_path(self, issue_table: str) -> str:
"""
Generate a directory path based on enabled features.
@param issue_table: The consolidated issue summary table.
@return: The generated directory path.
"""
output_path = ActionInputs.get_output_directory()

# If structured output is enabled, create a directory path based on the repository
Expand Down
16 changes: 16 additions & 0 deletions living_documentation_generator/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ def generate_root_level_index_page(index_root_level_page: str, output_path: str)
f.write(index_root_level_page)


def load_template(file_path: str, error_message: str) -> Optional[str]:
"""
Load the content of the template file.
@param file_path: The path to the template file.
@param error_message: The error message to log if the file cannot be read.
@return: The content of the template file or None if the file cannot be read.
"""
try:
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
except IOError:
logger.error(error_message, exc_info=True)
return None


# GitHub action utils
def get_action_input(name: str, default: Optional[str] = None) -> str:
"""
Expand Down

0 comments on commit 656c21e

Please sign in to comment.