🔍 Code Extractor

function create_new_filename

Maturity: 37

Modifies a PDF filename by inserting '_fully_signed' before the file extension, leaving non-PDF filenames unchanged.

File:
/tf/active/vicechatdev/mailsearch/copy_signed_documents.py
Lines:
86 - 91
Complexity:
simple

Purpose

This function is designed to create a new filename for PDF documents that have been fully signed, following a naming convention that clearly identifies signed documents. It's typically used in document processing workflows where signed PDFs need to be distinguished from unsigned ones. The function safely handles both PDF and non-PDF filenames, returning non-PDF filenames unmodified.

Source Code

def create_new_filename(original_filename):
    """Add '_fully_signed' before the .pdf extension"""
    if original_filename.endswith('.pdf'):
        base = original_filename[:-4]
        return f"{base}_fully_signed.pdf"
    return original_filename

Parameters

Name Type Default Kind
original_filename - - positional_or_keyword

Parameter Details

original_filename: A string representing the original filename (with or without path). Expected to be a filename ending in '.pdf' for modification, though any string is accepted. The function checks for the '.pdf' extension (case-sensitive) and only modifies filenames that match this pattern.

Return Value

Returns a string representing the modified filename. If the input ends with '.pdf', returns the filename with '_fully_signed' inserted before the extension (e.g., 'document.pdf' becomes 'document_fully_signed.pdf'). If the input does not end with '.pdf', returns the original filename unchanged. The return type is always a string.

Usage Example

# Basic usage
original = 'contract.pdf'
new_name = create_new_filename(original)
print(new_name)  # Output: 'contract_fully_signed.pdf'

# With path included
original_with_path = '/documents/agreement.pdf'
new_name_with_path = create_new_filename(original_with_path)
print(new_name_with_path)  # Output: '/documents/agreement_fully_signed.pdf'

# Non-PDF file (unchanged)
text_file = 'readme.txt'
result = create_new_filename(text_file)
print(result)  # Output: 'readme.txt'

# File with multiple dots
complex_name = 'report.v2.final.pdf'
result = create_new_filename(complex_name)
print(result)  # Output: 'report.v2.final_fully_signed.pdf'

Best Practices

  • The function performs case-sensitive matching for '.pdf' extension - files ending in '.PDF' or '.Pdf' will not be modified
  • The function works with both simple filenames and full file paths, as it only modifies the last 4 characters if they match '.pdf'
  • Consider validating that the original_filename is not None or empty before calling this function in production code
  • This function only modifies the filename string; it does not perform any file system operations (no files are renamed or moved)
  • If you need case-insensitive extension matching, consider using original_filename.lower().endswith('.pdf') in a modified version
  • The function assumes the filename is a string; passing non-string types will raise an AttributeError

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function sanitize_filename 53.9% similar

    Sanitizes a filename string by replacing invalid filesystem characters with underscores and ensuring a valid output.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function main_v44 48.9% similar

    Generates sample signature images (PNG files) for a predefined list of names and saves them to a 'signatures' directory.

    From: /tf/active/vicechatdev/document_auditor/generate_sample_signatures.py
  • function add_watermark 46.7% similar

    A wrapper function that adds a customizable text watermark to every page of a PDF document with configurable opacity and color.

    From: /tf/active/vicechatdev/CDocs/utils/pdf_utils.py
  • class PDFManipulator 44.7% similar

    Manipulates existing PDF documents This class provides methods to add watermarks, merge PDFs, extract pages, and perform other manipulation operations.

    From: /tf/active/vicechatdev/CDocs/utils/pdf_utils.py
  • class HashGenerator 43.8% similar

    A class that provides cryptographic hashing functionality for PDF documents, including hash generation, embedding, and verification for document integrity checking.

    From: /tf/active/vicechatdev/document_auditor/src/security/hash_generator.py
← Back to Browse