From patchwork Fri Oct 18 19:32:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 21699 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5D3BDC32AF for ; Fri, 18 Oct 2024 19:33:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A90BC65391; Fri, 18 Oct 2024 21:33:03 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="hPO9l+ew"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D4706538A for ; Fri, 18 Oct 2024 21:32:56 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8715521C; Fri, 18 Oct 2024 21:31:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1729279872; bh=HibV6QN6HFJ32OlNJ2YQbp2Pu7qHJi1MPs3Y6vOalj8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hPO9l+ewnG25bDCbYeCDueB34D1b9Q068fWHQcnf7REPAS98s50nsF4YW9v3a/GrL sMm5Vff9JE4YEX6a/NpMb21AAwXLyVoJpPDcxqbU6PJHqtETFBkvxM/AIj1uCVxr9O oLbO/FxVOkRBwyciDcbXy4J0MTreIQe9EBNhLq0k= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH 3/4] utils: checkstyle.py: Print issues using __str__ Date: Fri, 18 Oct 2024 22:32:45 +0300 Message-ID: <20241018193246.805-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018193246.805-1-laurent.pinchart@ideasonboard.com> References: <20241018193246.805-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" CommitIssue and StyleIssue classes have different string representations, which are handled by type-specific print() calls in the code that handles the issues. This requires the user to know which type of issue it is dealing with. Simplify that by moving the string representation logic to a __str__() method. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- utils/checkstyle.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index bc0ddfad8743..3f841a54d54a 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -391,6 +391,9 @@ class CommitIssue(object): def __init__(self, msg): self.msg = msg + def __str__(self): + return f'{Colours.fg(Colours.Yellow)}{self.msg}{Colours.reset()}' + class HeaderAddChecker(CommitChecker): commit_types = (Commit, StagedChanges, Amendment) @@ -592,6 +595,24 @@ class StyleIssue(object): self.line = line self.msg = msg + def __str__(self): + s = [] + s.append(f'{Colours.fg(Colours.Yellow)}#{self.line_number}: {self.msg}{Colours.reset()}') + if self.line is not None: + s.append(f'{Colours.fg(Colours.Yellow)}+{self.line.rstrip()}{Colours.reset()}') + + if self.position is not None: + # Align the position marker by using the original line with + # all characters except for tabs replaced with spaces. This + # ensures proper alignment regardless of how the code is + # indented. + start = self.position[0] + prefix = ''.join([c if c == '\t' else ' ' for c in self.line[:start]]) + length = self.position[1] - start - 1 + s.append(f' {prefix}^{"~" * length}') + + return '\n'.join(s) + class HexValueChecker(StyleChecker): patterns = ('*.c', '*.cpp', '*.h') @@ -936,21 +957,7 @@ def check_file(top_level, commit, filename, checkers): if len(issues): issues = sorted(issues, key=lambda i: i.line_number) for issue in issues: - print('%s#%u: %s%s' % (Colours.fg(Colours.Yellow), issue.line_number, - issue.msg, Colours.reset())) - if issue.line is not None: - print('%s+%s%s' % (Colours.fg(Colours.Yellow), issue.line.rstrip(), - Colours.reset())) - - if issue.position is not None: - # Align the position marker by using the original line with - # all characters except for tabs replaced with spaces. This - # ensures proper alignment regardless of how the code is - # indented. - start = issue.position[0] - prefix = ''.join([c if c == '\t' else ' ' for c in issue.line[:start]]) - length = issue.position[1] - start - 1 - print(' ' + prefix + '^' + '~' * length) + print(issue) return len(formatted_diff) + len(issues) @@ -967,7 +974,7 @@ def check_style(top_level, commit, checkers): # Apply the commit checkers first. for checker in CommitChecker.instances(commit, checkers): for issue in checker.check(commit, top_level): - print('%s%s%s' % (Colours.fg(Colours.Yellow), issue.msg, Colours.reset())) + print(issue) issues += 1 # Filter out files we have no checker for.