-
Notifications
You must be signed in to change notification settings - Fork 40
Ахаладзе М.М. ПИ-19-1 #7
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
MSenso
wants to merge
6
commits into
MNV:main
Choose a base branch
from
MSenso:add-additional-formatting
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
6 commits
Select commit
Hold shift + click to select a range
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
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,115 @@ | ||
| """ | ||
| Стиль цитирования по American Psychological Association | ||
| """ | ||
| from string import Template | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from formatters.models import BookModel, InternetResourceModel, NewsPaperModel | ||
| from formatters.styles.base import BaseCitationStyle | ||
| from logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class APABook(BaseCitationStyle): | ||
| """ | ||
| Форматирование для книг. | ||
| """ | ||
|
|
||
| data: BookModel | ||
|
|
||
| @property | ||
| def template(self) -> Template: | ||
| return Template("$authors ($year). $title. $publishing_house.") | ||
|
|
||
| def substitute(self) -> str: | ||
| logger.info('Форматирование книги "%s" ...', self.data.title) | ||
|
|
||
| return self.template.substitute( | ||
| authors=self.data.authors, | ||
| title=self.data.title, | ||
| publishing_house=self.data.publishing_house, | ||
| year=self.data.year, | ||
| ) | ||
|
|
||
|
|
||
| class APAInternetResource(BaseCitationStyle): | ||
| """ | ||
| Форматирование для интернет-ресурсов. | ||
| """ | ||
|
|
||
| data: InternetResourceModel | ||
|
|
||
| @property | ||
| def template(self) -> Template: | ||
| return Template("$article ($access_date) $website $link") | ||
|
|
||
| def substitute(self) -> str: | ||
| logger.info('Форматирование интернет-ресурса "%s" ...', self.data.article) | ||
|
|
||
| return self.template.substitute( | ||
| article=self.data.article, | ||
| website=self.data.website, | ||
| link=self.data.link, | ||
| access_date=self.data.access_date, | ||
| ) | ||
|
|
||
|
|
||
| class APANewsPaperResource(BaseCitationStyle): | ||
| """ | ||
| Форматирование для газеты. | ||
| """ | ||
|
|
||
| data: NewsPaperModel | ||
|
|
||
| @property | ||
| def template(self) -> Template: | ||
| return Template( | ||
| "$authors ($publishing_year, $publishing_date). $article_title. $news_title." | ||
| ) | ||
|
|
||
| def substitute(self) -> str: | ||
| logger.info('Форматирование газеты "%s" ...', self.data.article_title) | ||
|
|
||
| return self.template.substitute( | ||
| article_title=self.data.article_title, | ||
| authors=self.data.authors, | ||
| news_title=self.data.news_title, | ||
| publishing_year=self.data.publishing_year, | ||
| publishing_date=self.data.publishing_date, | ||
| ) | ||
|
|
||
|
|
||
| class APACitationFormatter: | ||
| """ | ||
| Базовый класс для итогового форматирования списка источников. | ||
| """ | ||
|
|
||
| formatters_map = { | ||
| BookModel.__name__: APABook, | ||
| InternetResourceModel.__name__: APAInternetResource, | ||
| NewsPaperModel.__name__: APANewsPaperResource, | ||
| } | ||
|
|
||
| def __init__(self, models: list[BaseModel]) -> None: | ||
| """ | ||
| Конструктор. | ||
| :param models: Список объектов для форматирования | ||
| """ | ||
|
|
||
| formatted_items = [] | ||
| for model in models: | ||
| formatter = self.formatters_map.get(type(model).__name__) | ||
| if formatter is not None: | ||
| formatted_items.append(formatter(model)) # type: ignore | ||
|
|
||
| self.formatted_items = formatted_items | ||
|
|
||
| def format(self) -> list[BaseCitationStyle]: | ||
| """ | ||
| Форматирование списка источников. | ||
| :return: | ||
| """ | ||
|
|
||
| return sorted(self.formatted_items, key=lambda item: item.formatted) |
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| import click | ||
|
|
||
| from formatters.styles.apa import APACitationFormatter | ||
| from formatters.styles.gost import GOSTCitationFormatter | ||
| from logger import get_logger | ||
| from readers.reader import SourcesReader | ||
|
|
@@ -25,6 +26,19 @@ class CitationEnum(Enum): | |
| APA = "apa" # American Psychological Association | ||
|
|
||
|
|
||
| style_map = { | ||
| CitationEnum.APA.name: APACitationFormatter, | ||
| CitationEnum.GOST.name: GOSTCitationFormatter, | ||
| } | ||
|
|
||
|
|
||
| def format_by_style(citation: str, models: list, path_output: str) -> None: | ||
| logger.info(style_map[citation]) | ||
| formatted_models = tuple(str(item) for item in style_map[citation](models).format()) | ||
|
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. Что, если |
||
| logger.info(f"Генерация выходного файла в формате ${citation}...") | ||
| Renderer(formatted_models).render(path_output) | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option( | ||
| "--citation", | ||
|
|
@@ -77,12 +91,7 @@ def process_input( | |
| ) | ||
|
|
||
| models = SourcesReader(path_input).read() | ||
| formatted_models = tuple( | ||
| str(item) for item in GOSTCitationFormatter(models).format() | ||
| ) | ||
|
|
||
| logger.info("Генерация выходного файла ...") | ||
| Renderer(formatted_models).render(path_output) | ||
| format_by_style(citation, models, path_output) | ||
|
|
||
| logger.info("Команда успешно завершена.") | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Новые методы тоже нужно документировать :)