Skip to content

Commit

Permalink
fix: FPDF decoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvana10 committed Apr 11, 2024
1 parent 02b32a7 commit 04aef80
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.py
test.*
__pycache__
venv
tempCodeRunnerFile.py
Expand Down
47 changes: 25 additions & 22 deletions commits2pdf/commits.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from os import path, listdir
from datetime import datetime
from shutil import rmtree
from typing import List, Union, Dict
from typing import List, Optional, Dict, Union

from git import Repo, GitCommandError, InvalidGitRepositoryError

from .logger import logger
from .constants import (
REPO_ALREADY_EXISTS_WARNING, DETACHED_BRANCH_ERROR,
BRANCH_ALREADY_EXISTS_WARNING, INVALID_REPO_ERROR, INVALID_REPO_ERROR_2,
FILTER_INFO, N_COMMITS_INFO, N_COMMITS_WARN, GATHERED_COMMITS_INFO
FILTER_INFO, N_COMMITS_INFO, N_COMMITS_WARN, GATHERED_COMMITS_INFO, CODING
)


Expand All @@ -21,16 +21,16 @@ def __init__(self, **kwargs) -> None:

self.rpath: str = kwargs["rpath"]
self.owner: str = kwargs["owner"]
self.url: Union[str, None] = kwargs["url"]
self.branch: Union[str, None] = kwargs["branch"]
self.authors: Union[List[str], None] = kwargs["authors"]
self.start_date: Union[datetime, None] = kwargs["start_date"]
self.end_date: Union[datetime, None] = kwargs["end_date"]
self.reverse: bool = kwargs["reverse"]
self.newest_n_commits: int = kwargs["newest_n_commits"]
self.oldest_n_commits: int= kwargs["oldest_n_commits"]
self.queries_any: List[str] = kwargs["queries_any"]
self.queries_all: List[str] = kwargs["queries_all"]
self.url: Optional[str] = kwargs["url"]
self.branch: Optional[str] = kwargs["branch"]
self.authors: Optional[List[str]] = kwargs["authors"]
self.start_date: Optional[datetime] = kwargs["start_date"]
self.end_date: Optional[datetime] = kwargs["end_date"]
self.reverse: Optional[bool] = kwargs["reverse"]
self.newest_n_commits: Optional[int] = kwargs["newest_n_commits"]
self.oldest_n_commits: Optional[int] = kwargs["oldest_n_commits"]
self.queries_any: Option[List[str]] = kwargs["queries_any"]
self.queries_all: Optional[List[str]] = kwargs["queries_all"]

self.r: Repo = self.get_repo()
if isinstance(self.r, Repo): # Repo was successfully found, continue
Expand Down Expand Up @@ -165,7 +165,7 @@ def gather_commits(self) -> List[Repo.commit]:

return commits

def instantiate_commits(self) -> List[Dict[str, str]]: # Can't use ``Commit`` yet bruh
def instantiate_commits(self) -> List[Dict[str, str]]:
"""Instantiate all the filtered ``Repo.commit`` objects into my own
simple class that inherits from a dictionary.
"""
Expand Down Expand Up @@ -221,7 +221,11 @@ def filter_commits(self) -> List[dict]:

class Commit(dict):
"""A simple way of representing a commit as a dictionary."""
def __init__(self, owner, rname, branch, commit: Repo.commit):
def __init__(self,
owner: str,
rname: str,
branch: str,
commit: Repo.commit):
"""Assign commit data to the instance."""
self["rname"] = rname
self["branch"] = branch
Expand All @@ -235,17 +239,16 @@ def __init__(self, owner, rname, branch, commit: Repo.commit):
self["info"] = f"{self['hexsha_short']} | Branch: {self['branch']} | " \
f"By {self['author_name']} ({self['author_email']}) | " \
f"At {self['date'].strftime('%Y-%m-%d')}"
self["info"] = Commit._code(self["info"])

# Extract the message from the title
msg = commit.message.split("\n")
self["title"] = msg[0].encode("latin-1", errors="replace")\
.decode("latin-1")
if len(msg) > 1:
self["description"] = "\n".join(msg[1:]).encode("latin-1",
errors="replace")\
.decode("latin-1")
else:
self["description"] = ""
self["title"] = Commit._code(msg[0])
self["description"] = Commit._code("\n".join(msg[1:])) if len(msg) > 1 else ""

@staticmethod
def _code(text, coding=CODING):
return text.encode(CODING, "replace").decode(CODING)

def __str__(self):
"""View the commit in the terminal."""
Expand Down
1 change: 1 addition & 0 deletions commits2pdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"""For the fpdf PDF implementation"""
RECURSION_ERROR = "An error occured when using the gen2b renderer. Please try " \
"adding the -gen2a flag to your console command and try again."
CODING = "latin-1"
TITLE_FONT = ["Arial", "B", 36]
SUBTITLE_FONT = ["Arial", "", 30]
MARGIN_FONT = ["Arial", "I", 8]
Expand Down
3 changes: 1 addition & 2 deletions commits2pdf/render_fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _draw_commits(self) -> None:
if self._commit_exceeds_size(commit):
self.add_page()
self._draw_page_bg()
self._draw_commit(commit)
self._draw_commit(commit)

def _draw_commit(self,
commit: Dict[str, str],
Expand All @@ -134,7 +134,6 @@ def _draw_commit(self,
self.recursion_err_flag = True
exit(1)


y: int = p.get_y()
p.set_text_color(*self._ap["text"])
p._set_font(*INFO_TEXT_FONT)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="commits2pdf",
version="1.1.6",
version="1.1.7",
author="Tomas Vana",
url="https://github.com/tomasvana10/commits2pdf",
description="View a filtered commit history in PDF form.",
Expand Down

0 comments on commit 04aef80

Please sign in to comment.