Let's proceed to generate the actual Python code for the classes and interfaces in the CommitMessageSystem
. The code will adhere to PEP 8 standards and reflect the functionality described in the pseudocode.
class CommitMessage:
def __str__(self):
# Placeholder for string representation of the commit message
return "Commit Message Content"
from abc import ABC, abstractmethod
class CommitMessageBuilder(ABC):
def __init__(self):
# Constructor for the builder interface
super().__init__()
@abstractmethod
def build(self):
# Abstract method to build and return a CommitMessage object
pass
class AcnCommitMessageBuilder(CommitMessageBuilder):
def __init__(self):
# Constructor initializing attributes
self._summary = None
self._description = None
self._author = None
self._co_authors = []
self._risk = None
self._intent = None
self._include_author_in_co_authors = False
def withSummary(self, summary):
self._summary = summary
return self
def withDescription(self, description):
self._description = description
return self
def withAuthor(self, author):
self._author = author
return self
def withCoAuthors(self, co_authors):
if isinstance(co_authors, list):
self._co_authors = co_authors
elif isinstance(co_authors, str):
self._co_authors = self._parse_co_authors(co_authors)
return self
def withAuthorAlsoAsCoAuthor(self):
self._include_author_in_co_authors = True
return self
def withRisk(self, risk):
self._risk = risk
return self
def withIntent(self, intent):
self._intent = intent
return self
def _parse_co_authors(self, co_authors_str):
# Parsing logic for co_authors string
return co_authors_str.split(',')
def build(self):
# Constructing the commit message
message_parts = [f"Risk/Intent: {self._risk}/{self._intent}", f"Summary: {self._summary}", f"Description: {self._description}"]
co_authors_str = "\n".join([f"Co-Authored-By: {author}" for author in self._co_authors])
return "\n".join(message_parts + [co_authors_str]).strip()
CommitMessageSystem/
│
├── __init__.py
│
├── commit_message.py
│ └── Class CommitMessage
│
├── commit_message_builder.py
│ └── Interface CommitMessageBuilder
│
└── acn_commit_message_builder.py
└── Class AcnCommitMessageBuilder
This Python code reflects the functionality and structure described in the pseudocode. The CommitMessage
class provides a basic string representation, the CommitMessageBuilder
is an abstract base class, and AcnCommitMessageBuilder
implements the specific logic for building a commit message. The code is ready to be integrated into your Python project within the CommitMessageSystem
directory structure.
================
class AcnCommitMessageBuilder(CommitMessageBuilder): def init(self): self._summary = None self._description = None self._author = None self._co_authors = [] self._risk = None self._intent = None self._include_author_in_co_authors = False
def withSummary(self, summary):
self._summary = summary
return self
def withDescription(self, description):
self._description = description
return self
def withAuthor(self, author):
self._author = author
return self
def withCoAuthors(self, co_authors):
if isinstance(co_authors, list):
self._co_authors = [self._strip_co_authored_by_prefix(author) for author in co_authors]
elif isinstance(co_authors, str):
processed_co_authors = self._process_co_authors_string(co_authors)
self.withCoAuthors(processed_co_authors)
return self
def withAuthorAlsoAsCoAuthor(self):
self._include_author_in_co_authors = True
return self
def withRisk(self, risk):
self._risk = risk
return self
def withIntent(self, intent):
self._intent = intent
return self
def _process_co_authors_string(self, co_authors_str):
co_authors_str = co_authors_str.replace('Co-Authored-By:', '').strip()
return [author.strip() for author in re.split('[,\n]', co_authors_str) if author.strip()]
def _strip_co_authored_by_prefix(self, author):
return author.replace('Co-Authored-By:', '').strip()
def build(self):
message_parts = [f"{self._risk}/{self._intent} - {self._summary}", self._description]
if self._include_author_in_co_authors and self._author:
self._co_authors.insert(0, self._author)
co_authors_str = "\n".join([f"Co-Authored-By: {author}" for author in self._co_authors])
commit_message = "\n".join(part for part in message_parts + [co_authors_str] if part)
return CommitMessage(commit_message.rstrip())
================================================================
tests └── test_acn_commit_message_builder.py
import unittest from acn_commit_message_builder import AcnCommitMessageBuilder
class TestAcnCommitMessageBuilder(unittest.TestCase):
def test_build_with_all_attributes(self):
builder = AcnCommitMessageBuilder()
builder.withSummary("Added new feature")
.withDescription("Implemented feature X to improve performance.")
.withAuthor("Jane Doe <[email protected]>")
.withCoAuthors(["John Smith <[email protected]>"])
.withAuthorAlsoAsCoAuthor()
.withRisk("Low")
.withIntent("Feature Addition")
expected_message = "Low/Feature Addition - Added new feature\n" \
"Implemented feature X to improve performance.\n\n" \
"Co-Authored-By: Jane Doe <[email protected]>\n" \
"Co-Authored-By: John Smith <[email protected]>"
commit_message = builder.build()
self.assertEqual(str(commit_message), expected_message)
# Additional test cases can be added here to cover more scenarios
if name == 'main': unittest.main()