Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3659 Fixed copyright detection normalization #3939

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/cluecode/copyrights.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@

from cluecode import copyrights_hint

from cluecode.normalizer import normalize_copyright_symbols

def detect_copyrights(file_path):
# Read the content of the file
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()

# Normalize the text before processing it
normalized_text = normalize_copyright_symbols(text)

# Save the normalized content back to the file (optional)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(normalized_text)

return normalized_text

# Specify the path to your document directly here
file_path = "./copyright.py"

# Call the function and print the result
normalized_content = detect_copyrights(file_path)
print(normalized_content)

# Tracing flags
TRACE = False or os.environ.get('SCANCODE_DEBUG_COPYRIGHT', False)

Expand Down
23 changes: 23 additions & 0 deletions src/cluecode/copyrights_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@
# A regex to match a string that may contain a copyright year.
# This is a year between 1960 and today prefixed and suffixed with
# either a white-space or some punctuation.
from cluecode.normalizer import normalize_copyright_symbols

def detect_copyrights(file_path):
# Read the content of the file
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()

# Normalize the text before processing it
normalized_text = normalize_copyright_symbols(text)

# Save the normalized content back to the file (optional)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(normalized_text)

return normalized_text

# Specify the path to your document directly here
file_path = "./copyright.py"

# Call the function and print the result
normalized_content = detect_copyrights(file_path)
print(normalized_content)


all_years = tuple(str(year) for year in range(1960, datetime.today().year))
years = r'[\(\.,\-\)\s]+(' + '|'.join(all_years) + r')([\(\.,\-\)\s]+|$)'
Expand Down
9 changes: 9 additions & 0 deletions src/cluecode/normalizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import re

def normalize_copyright_symbols(text):
"""
Replace [C] or [c] with (C) to ensure proper copyright detection.
"""
# Replace [C] or [c] with (C)
text = re.sub(r'\[C\]', '(C)', text, flags=re.IGNORECASE)
return text
Loading