Skip to content

Commit

Permalink
Improve output error handling and shortened code in constructor of Co…
Browse files Browse the repository at this point in the history
…mmits
  • Loading branch information
tomasvana10 committed May 4, 2024
1 parent b4fdad4 commit f752a6b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
7 changes: 3 additions & 4 deletions commits2pdf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ def _make_pdf(
gen_args.append(scaling)

pdf = cls(*gen_args)
if hasattr(pdf, "err_flag"):
if pdf.err_flag:
return
if pdf.err_flag:
return

p = path.abspath(args.output)
logger.info(
Expand Down Expand Up @@ -177,7 +176,7 @@ def _validate_args(args: Namespace) -> tuple[None | str | list[str]]:
appearance: dict[str, tuple[int]] = (
CAIRO_LIGHT if not args.dark else CAIRO_DARK
)
if args.scaling:
if args.scaling != 1.0:
logger.warning(CANNOT_USE_SCALE_WARNING)
else:
gen, mode = (
Expand Down
16 changes: 3 additions & 13 deletions commits2pdf/commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,8 @@ def __init__(self, **kwargs) -> None:
"""Save filter and repo information from kwargs."""
self.err_flag: bool = False # Detected in ``cli.py`` to stop execution

self.rpath: str = kwargs["rpath"]
self.owner: str = kwargs["owner"]
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.include: Optional[list[str]] = kwargs["include"]
self.exclude: Optional[list[str]] = kwargs["exclude"]
for arg in kwargs:
setattr(self, arg, kwargs[arg])

self.r: Repo = self._get_repo()
if isinstance(self.r, Repo): # Repo was successfully found, continue
Expand Down Expand Up @@ -105,7 +95,7 @@ def _validate_branch(self, r: Repo) -> bool | None:
"""Ensure that ``self.branch`` exists. If not, attempt to set it to the
repo's active branch.
"""
if self.branch == r.active_branch:
if self.branch == str(r.active_branch):
return True

try: # The branch they want does not exist, so access the active branch
Expand Down
4 changes: 4 additions & 0 deletions commits2pdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@

"""General PDF messages"""
WRITING_PDF_INFO = "Writing your PDF to {}"
INVALID_OUTPUT_DIR_ERROR = (
"Your output directory is invalid. Please ensure it contains valid "
"characters and try again."
)


"""For the pycairo PDF implementation"""
Expand Down
13 changes: 10 additions & 3 deletions commits2pdf/render_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
MARGIN,
WIDTH,
WRITING_PDF_INFO,
INVALID_OUTPUT_DIR_ERROR
)
from .logger import logger

Expand All @@ -42,6 +43,7 @@ def __init__(
self.timestamp = datetime.fromtimestamp(int(time())).strftime(
"%d/%m/%Y %H:%M:%S"
)
self.err_flag: bool = False

self._s = PDFSurface(path.join(output, filename), WIDTH, HEIGHT)
self._c = Context(self._s)
Expand All @@ -59,19 +61,24 @@ def __init__(
self._draw_commits()
logger.info(
WRITING_PDF_INFO.format(
self._output + " ..."
path.normpath(self._output) + " ..."
if self._output != "."
else "your current directory..."
)
)
self._s.finish()
try:
self._s.finish()
except OSError:
logger.error(INVALID_OUTPUT_DIR_ERROR)
self.err_flag = True
exit(1)

def _draw_commits(self) -> None:
"""Driver function to draw all the commits."""
for commit in tqdm(
self._commits.filtered_commits,
ncols=85,
desc="Generating",
desc="GENERATING",
):
text = self._get_commit_text(commit)
height = sum(len(t) for t in text) * 14 + 50
Expand Down
16 changes: 11 additions & 5 deletions commits2pdf/render_fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
TITLE_FONT,
TITLE_PAGE_INFO_FONT,
WRITING_PDF_INFO,
FPDF_DARK
FPDF_DARK,
INVALID_OUTPUT_DIR_ERROR
)
from .logger import logger

Expand Down Expand Up @@ -73,12 +74,17 @@ def __init__(
self._prepare_and_draw()
logger.info(
WRITING_PDF_INFO.format(
self._output + " ..."
path.normpath(self._output) + " ..."
if self._output != "."
else "your current directory..."
)
)
self._write()
try:
self._write()
except OSError:
logger.error(INVALID_OUTPUT_DIR_ERROR)
self.err_flag = True
exit(1)

@staticmethod
def _set_scaling(scaling: float) -> None:
Expand Down Expand Up @@ -175,7 +181,7 @@ def _draw_commits(self) -> None:
for commit in tqdm(
self._commits.filtered_commits,
ncols=85,
desc="Generating",
desc="GENERATING",
):
result = self._draw_commit(commit, pre_vis=True)
if result == "NEW_PAGE_OK": # Break page
Expand All @@ -195,7 +201,7 @@ def _draw_commits(self) -> None:
for commit in tqdm(
self._commits.filtered_commits,
ncols=85,
desc="Generating",
desc="GENERATING",
):
if self._commit_exceeds_size(commit): # Break page
self._draw_newpage_commit(commit)
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.10",
version="1.1.11",
author="Tomas Vana",
url="https://github.com/tomasvana10/commits2pdf",
description="View a filtered commit history in PDF form.",
Expand Down

0 comments on commit f752a6b

Please sign in to comment.