Overview
The CSV Operations module provides three essential functions for managing persistent storage of license plate scan records. These functions handle data initialization, reading, and writing operations for the application’s CSV-based database.Functions
ensure_csv()
Initializes the CSV database file with proper headers if it doesn’t already exist.Function Signature
Parameters
This function takes no parameters.Behavior
- Checks if the CSV file exists at the configured
DATA_PATH - If the file does not exist, creates it with the standard CSV header
- If the file already exists, performs no action (idempotent operation)
Implementation
Usage Example
read_csv()
Reads all license plate records from the CSV database and returns them as a list of dictionaries.Function Signature
Parameters
This function takes no parameters.Return Value
A list of dictionaries, where each dictionary represents one license plate record with the following keys:
Unique identifier for the record (sequential number starting from 1)
Timestamp when the record was created (format: “YYYY-MM-DD HH:MM:SS”)
The extracted license plate text (uppercase, alphanumeric)
Vehicle owner name (optional, user-provided)
Type of vehicle (e.g., “auto”, “moto”, “camion”)
Additional notes or observations about the vehicle/scan
Filename of the uploaded image stored in the uploads directory
Implementation
The function automatically calls
ensure_csv() before reading, guaranteeing that the CSV file exists even on first access.Usage Example
write_csv()
Writes a complete list of records to the CSV database, replacing all existing data.Function Signature
Parameters
A list of dictionaries representing license plate records to write. Each dictionary must contain all required CSV header fields:
id: Unique record identifierfecha_hora: Timestamp stringmatricula: License plate textpropietario: Owner nametipo_vehiculo: Vehicle typeobservacion: Additional notesimagen: Image filename
Behavior
- Opens the CSV file in write mode, replacing all existing content
- Writes the standard CSV header row
- Writes all provided rows to the file
- Uses UTF-8 encoding to support international characters
- Uses
newline=""parameter to ensure proper CSV formatting across platforms
This function performs a complete replacement of the CSV file. To modify data, you must:
- Read existing records with
read_csv() - Modify the list of records
- Write the modified list back with
write_csv()
Implementation
Usage Examples
Adding a New RecordConfiguration
These functions use globally configured paths defined inapp.py:
Directory Initialization
The application ensures required directories exist at startup:Application Integration
These CSV operations are used throughout the Flask application:In the Save Route (/guardar)
In the Records Display Route (/registros)
In the Delete Route (/eliminar/<id>)
Error Handling
These functions do not include explicit error handling. File I/O errors (permission denied, disk full, etc.) will raise exceptions that should be caught by calling code.
Thread Safety
These functions are not thread-safe. Concurrent writes can result in data loss or corruption. For production deployments with multiple workers or concurrent requests, consider:
- Implementing file locking mechanisms
- Using a proper database (SQLite, PostgreSQL, etc.)
- Implementing request queuing for write operations
Performance Considerations
- Full File Read/Write: Every operation reads or writes the entire CSV file. For large datasets (>10,000 records), consider migrating to a database.
- No Indexing: Searching records requires iterating through the entire list. Response time degrades linearly with dataset size.
- Memory Usage: The entire dataset is loaded into memory. Monitor memory consumption for large deployments.
Migration Path
For applications outgrowing CSV storage, these functions provide a clean abstraction layer. To migrate to a database:- Keep the function signatures unchanged
- Replace CSV file operations with database queries
- Application code using
read_csv()andwrite_csv()requires no changes
Related Functions
extract_plate_from_image()- Extracts license plate text before saving to CSV- Flask routes:
/guardar,/registros,/eliminar/<id>- Primary consumers of these operations
