From f752a6b3d7bbaa0966e3beea73237612ccc4c1bd Mon Sep 17 00:00:00 2001 From: tomasvana10 Date: Sat, 4 May 2024 12:08:19 +1000 Subject: [PATCH] Improve output error handling and shortened code in constructor of Commits --- commits2pdf/cli.py | 7 +++---- commits2pdf/commits.py | 16 +++------------- commits2pdf/constants.py | 4 ++++ commits2pdf/render_cairo.py | 13 ++++++++++--- commits2pdf/render_fpdf.py | 16 +++++++++++----- setup.py | 2 +- 6 files changed, 32 insertions(+), 26 deletions(-) diff --git a/commits2pdf/cli.py b/commits2pdf/cli.py index 2bb6092..8aede4c 100644 --- a/commits2pdf/cli.py +++ b/commits2pdf/cli.py @@ -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( @@ -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 = ( diff --git a/commits2pdf/commits.py b/commits2pdf/commits.py index 792927c..46f5ed3 100644 --- a/commits2pdf/commits.py +++ b/commits2pdf/commits.py @@ -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 @@ -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 diff --git a/commits2pdf/constants.py b/commits2pdf/constants.py index 5f3950f..f458bc4 100644 --- a/commits2pdf/constants.py +++ b/commits2pdf/constants.py @@ -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""" diff --git a/commits2pdf/render_cairo.py b/commits2pdf/render_cairo.py index 7d52dda..2868c56 100644 --- a/commits2pdf/render_cairo.py +++ b/commits2pdf/render_cairo.py @@ -18,6 +18,7 @@ MARGIN, WIDTH, WRITING_PDF_INFO, + INVALID_OUTPUT_DIR_ERROR ) from .logger import logger @@ -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) @@ -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 diff --git a/commits2pdf/render_fpdf.py b/commits2pdf/render_fpdf.py index 9adcb44..d9d91d7 100644 --- a/commits2pdf/render_fpdf.py +++ b/commits2pdf/render_fpdf.py @@ -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 @@ -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: @@ -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 @@ -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) diff --git a/setup.py b/setup.py index 289b706..5fe1f95 100644 --- a/setup.py +++ b/setup.py @@ -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.",