Skip to content

Commit

Permalink
Improve copyright debug tracing
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Ombredanne <[email protected]>
  • Loading branch information
pombredanne committed Jun 7, 2024
1 parent 8b2ddf5 commit 1fc7cea
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions src/cluecode/copyrights.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def logger_debug(*args):
pass


if TRACE or TRACE_DEEP or TRACE_TOK:
if TRACE or TRACE_TOK:
import logging

logger = logging.getLogger(__name__)
Expand All @@ -55,6 +55,9 @@ def logger_debug(*args):
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))

if TRACE_DEEP:
logger_debug = print

"""
Detect and collect copyright statements.
Expand Down Expand Up @@ -175,13 +178,11 @@ def detect_copyrights_from_lines(
)

for candidates in candidate_lines_groups:
if TRACE:
from pprint import pformat
can = pformat(candidates, width=160)
logger_debug(
f' detect_copyrights_from_lines: processing candidates group:\n'
f' {can}'
)
if TRACE or TRACE_DEEP:
logger_debug(f'\n========================================================================')
logger_debug(f'detect_copyrights_from_lines: processing candidates group:')
for can in candidates:
logger_debug(f' {can}')

detections = detector.detect(
numbered_lines=candidates,
Expand Down Expand Up @@ -259,14 +260,17 @@ def detect(self,
# first, POS tag each token using token regexes
lexed_text = list(self.lexer.lex_tokens(tokens, trace=TRACE_TOK))

if TRACE:
logger_debug(f'CopyrightDetector: lexed tokens: {lexed_text}')
if TRACE or TRACE_DEEP:
logger_debug(f'CopyrightDetector: lexed tokens:')
for l in lexed_text:
logger_debug(f' {l!r}')

# then build a parse parse_tree based on tagged tokens
parse_tree = self.parser.parse(lexed_text)

if TRACE:
logger_debug(f'CopyrightDetector: parse_tree:\n{tree_pformat(parse_tree)}')
if TRACE or TRACE_DEEP:
logger_debug('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
logger_debug(f'CopyrightDetector: final parse_tree:\n{tree_pformat(parse_tree)}')

non_copyright_labels = frozenset()
if not include_copyright_years:
Expand Down Expand Up @@ -316,8 +320,8 @@ def detect(self,
junk=COPYRIGHTS_JUNK,
)

if TRACE:
logger_debug(f'CopyrightDetector: detection: {copyrght}')
if TRACE or TRACE_DEEP:
logger_debug(f'CopyrightDetector: final copyright: {copyrght}')

if copyrght:
if include_copyrights:
Expand Down Expand Up @@ -500,8 +504,6 @@ def build_detection_from_node(
else:
leaves = node.leaves()

# if TRACE_DEEP: logger_debug(' starting leaves:', leaves)

if include_copyright_allrights:
filtered = leaves
else:
Expand Down Expand Up @@ -3940,15 +3942,13 @@ def candidate_lines(numbered_lines):

if TRACE_TOK:
numbered_lines = list(numbered_lines)
logger_debug(
f'candidate_lines: numbered_lines: {numbered_lines!r}')
logger_debug(f'candidate_lines: numbered_lines: {numbered_lines!r}')

# the previous line (chars only)
previous_chars = None
for numbered_line in numbered_lines:
if TRACE:
logger_debug(
f'# candidate_lines: evaluating line: {numbered_line!r}')
logger_debug(f'# candidate_lines: evaluating line: {numbered_line!r}')

_line_number, line = numbered_line

Expand All @@ -3962,10 +3962,7 @@ def candidate_lines(numbered_lines):

if TRACE:
cands = list(candidates)
logger_debug(
' candidate_lines: is EOS: yielding candidates\n'
f' {cands}r\n\n'
)
logger_debug(f' candidate_lines: is EOS: yielding candidates\n {cands!r}\n')

yield list(candidates)
candidates_clear()
Expand All @@ -3978,7 +3975,8 @@ def candidate_lines(numbered_lines):
candidates_append(numbered_line)

previous_chars = chars_only
if TRACE: logger_debug(' candidate_lines: line is candidate')
if TRACE:
logger_debug(' candidate_lines: line is candidate')

elif 's>' in line:
# this is for debian-style <s></s> copyright name tags
Expand Down Expand Up @@ -4011,10 +4009,7 @@ def candidate_lines(numbered_lines):
# completely empty or only made of punctuations
if TRACE:
cands = list(candidates)
logger_debug(
' candidate_lines: empty: yielding candidates\n'
f' {cands}r\n\n'
)
logger_debug(f' candidate_lines: empty: yielding candidates\n {cands!r}\n')

yield list(candidates)
candidates_clear()
Expand All @@ -4031,10 +4026,7 @@ def candidate_lines(numbered_lines):
elif candidates:
if TRACE:
cands = list(candidates)
logger_debug(
' candidate_lines: not in COP: yielding candidates\n'
f' {cands}r\n\n'
)
logger_debug(f' candidate_lines: not in COP: yielding candidates\n {cands!r}\n')

yield list(candidates)
candidates_clear()
Expand All @@ -4045,10 +4037,7 @@ def candidate_lines(numbered_lines):
if candidates:
if TRACE:
cands = list(candidates)
logger_debug(
'candidate_lines: finally yielding candidates\n'
f' {cands}r\n\n'
)
logger_debug(f'candidate_lines: finally yielding candidates\n {cands!r}\n')

yield list(candidates)

Expand Down

0 comments on commit 1fc7cea

Please sign in to comment.