init.py

Overview

This init.py file provides utility functions to handle filename uniqueness in the system, primarily by detecting and managing filename counters appended in parentheses (e.g., "file(1).txt"). It is part of the InfiniFlow project and facilitates generating unique filenames by checking existing names and appending incremental counters where necessary.

The key functionalities include:

This file typically serves as a utility module within a larger file management or user content system, ensuring that filename collisions are gracefully resolved.


Imported Entities


Functions

split_name_counter(filename: str) -> tuple[str, int | None]

Purpose

Parses a filename string to separate the main part from an optional numeric counter enclosed in parentheses at the end of the base filename (before extension).

Parameters

Returns

Behavior

Example

split_name_counter("file(3)")       # Returns: ("file", 3)
split_name_counter("report")         # Returns: ("report", None)
split_name_counter("image (12)")     # Returns: ("image ", 12)

duplicate_name(query_func, **kwargs) -> str

Purpose

Generates a unique filename by appending or incrementing a numeric counter in parentheses to avoid duplicates. It repeatedly queries an external function to check if the candidate filename already exists.

Parameters

Returns

Raises

Algorithm / Implementation Details

Example Usage

existing_files = {"document.pdf", "document(1).pdf"}

def name_exists(name):
    return name in existing_files

unique_name = duplicate_name(name_exists, name="document.pdf")
print(unique_name)  # Outputs: "document(2).pdf"

Imported Classes

UserService

Imported and re-exported for convenience. No additional modifications are made here.


Interaction with Other Parts of the System


Implementation Details and Notes


Mermaid Class Diagram

Since this file contains utility functions (not classes with properties), a flowchart illustrating the relationship between the functions is most appropriate.

flowchart TD
    A[split_name_counter(filename)] -->|Returns main_part, counter| B[duplicate_name(query_func, **kwargs)]
    B -->|Calls query_func with current name| C{Name Exists?}
    C -- Yes --> B
    C -- No --> D[Return unique filename]

Diagram Explanation:


Summary

This init.py file provides robust utilities for managing filename uniqueness by parsing and incrementing counters within filenames. It is designed to integrate flexibly with external systems checking name availability and prevents conflicts through systematic renaming. The presence of UserService import indicates a broader user-related functionality within the module. This file is essential for any component requiring conflict-resistant filename management in the InfiniFlow project.