Skip to main content

Unified File Reader

A clean, extensible Python library for reading multiple file formats with a unified API. Built following Clean Code and Clean Architecture principles.

Features

  • Unified API: Single read_file() function for all supported formats
  • Multiple Formats: CSV, JSON, TXT, XLSX, PDF, DOCX
  • Clean Architecture: Modular, testable, and maintainable codebase
  • Extensible: Add new file readers without modifying core logic
  • Type-Safe: Full type hints for better IDE support
  • Well-Tested: Comprehensive test suite with high coverage
  • Production-Ready: PyPI packaging, logging, and error handling

Requirements

Developed and tested on Python 3.12.8 (recommended). Other versions within 3.9–3.12 should also work.

  • pip (latest version recommended)
  • Virtual environment tool (python -m venv)

See requirements.txt for the exact dependency versions used with Python 3.12.8.

Installation

From PyPI

pip install unified-file-reader

From source (development setup)

# Clone the repository
git clone https://github.com/praveen9392/unified-file-reader.git
cd unified-file-reader

# Create a virtual environment (recommended)
python -m venv .venv

# Activate
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# Windows (cmd)
.venv\Scripts\activate.bat
# macOS / Linux
source .venv/bin/activate

# Install runtime + dev dependencies
pip install -e ".[dev]"

# Alternatively, to match exact versions from this repo
pip install -r requirements.txt

Documentation

Comprehensive documentation organized by topic is available in the docs/ directory:

  • 📋 Requirements & Specifications: Product goals, functional & non-functional requirements.
  • 🏛️ Architecture & Design: Clean Architecture overview, component diagrams, data flows, and ADRs.
  • ⚙️ Technical Design: Abstract BaseReader interface and extending with custom readers.
  • 📖 API Reference: Complete signature reference for read_file() and registry functions.
  • 🛠️ Development Guide: Environment setup, configuration, and developer workflows.
  • 🚢 Deployment: Packaging architecture, PyPI publishing, and release procedures.
  • 🧪 Testing Strategy: Test suite layout, coverage guidelines, and fixture management.
  • ⚠️ Known Issues: Current constraints, edge cases, and future roadmap.

Quick Start

from unified_file_reader import read_file

# Read any supported file format
data = read_file("data.csv")
data = read_file("config.json")
data = read_file("document.pdf")
data = read_file("spreadsheet.xlsx")

Supported Formats

Format Extension Return Type Notes
CSV .csv List[Dict] Returns list of dictionaries
JSON .json Any Preserves JSON structure
TXT .txt str Returns plain text content
Excel .xlsx List[Dict] Returns list of dictionaries
PDF .pdf str Extracts all text content
DOCX .docx str Extracts all paragraph text

Architecture Overview

unified_file_reader/
├── api.py                 # Public API entry point
├── registry.py            # Reader registry (dependency inversion)
├── exceptions.py          # Custom exceptions
├── interfaces/
│   └── base_reader.py     # Abstract reader contract
├── readers/               # Concrete implementations
│   ├── csv_reader.py
│   ├── json_reader.py
│   ├── txt_reader.py
│   ├── excel_reader.py
│   ├── pdf_reader.py
│   └── docx_reader.py
├── utils/
│   └── file_utils.py      # Utility functions
└── config/
    └── supported_formats.py

Design Principles

  • Single Responsibility Principle (SRP): Each reader handles one format
  • Open/Closed Principle (OCP): Add new readers without modifying existing code
  • Liskov Substitution Principle (LSP): All readers implement the same interface
  • Interface Segregation Principle (ISP): Minimal, focused interfaces
  • Dependency Inversion Principle (DIP): High-level modules depend on abstractions

Usage Examples

Reading CSV Files

from unified_file_reader import read_file

data = read_file("employees.csv")
# Returns: [{"name": "John", "age": "30"}, {"name": "Jane", "age": "28"}]

for row in data:
    print(f"{row['name']}: {row['age']}")

Reading JSON Files

config = read_file("config.json")
# Returns: {"database": {"host": "localhost", "port": 5432}}

print(config["database"]["host"])

Reading Text Files

content = read_file("document.txt")
# Returns: "This is the file content..."

print(content)

Reading Excel Files

data = read_file("report.xlsx")
# Returns: [{"Quarter": "Q1", "Revenue": 100000}, ...]

for row in data:
    print(f"{row['Quarter']}: ${row['Revenue']}")

Reading PDF Files

text = read_file("document.pdf")
# Returns: "Page 1 content...\nPage 2 content..."

print(text)

Reading DOCX Files

text = read_file("report.docx")
# Returns: "Paragraph 1...\nParagraph 2..."

print(text)

Error Handling

from unified_file_reader import read_file
from unified_file_reader.exceptions import UnsupportedFormatError, ReaderError

try:
    data = read_file("file.xyz")
except UnsupportedFormatError as e:
    print(f"Format not supported: {e}")
except FileNotFoundError as e:
    print(f"File not found: {e}")
except ReaderError as e:
    print(f"Reading error: {e}")

Extending with New Readers

To add support for a new file format:

  1. Create a new reader class in unified_file_reader/readers/:
from unified_file_reader.interfaces.base_reader import BaseReader
from unified_file_reader.utils.file_utils import ensure_file_exists

class YAMLReader(BaseReader):
    supported_extensions = [".yaml", ".yml"]

    def can_read(self, extension: str) -> bool:
        return extension in self.supported_extensions

    def read(self, path: str):
        ensure_file_exists(path)
        import yaml
        with open(path, 'r', encoding='utf-8') as f:
            return yaml.safe_load(f)
  1. Register it in unified_file_reader/registry.py:
from unified_file_reader.readers.yaml_reader import YAMLReader

readers = [
    # ... existing readers ...
    YAMLReader(),
]
  1. Add tests in tests/test_yaml_reader.py

  2. Update supported_formats.py with metadata

That's it! No changes needed to the core API.

Testing

Run the test suite:

pytest

With coverage:

pytest --cov=unified_file_reader

Run specific tests:

pytest tests/test_api.py -v

Development

Install development dependencies:

pip install -e ".[dev]"

Format code:

black unified_file_reader tests
isort unified_file_reader tests

Type checking:

mypy unified_file_reader

Linting:

flake8 unified_file_reader tests

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details

Changelog

v0.2.0

  • Enterprise-grade exception handling following clean architecture & SOLID principles
  • Base ApplicationException with stable error codes and structured .to_dict() serialization
  • Separated BusinessException and InfrastructureException hierarchies
  • Standardized logging with logger.exception() and explicit exception chaining (raise ... from exc)
  • 100% backward compatibility for legacy exceptions and error handling
  • 100 passed unit tests with comprehensive test coverage
  • Added manual_test integration test suite covering real-world files across all 6 formats

v0.1.3

  • Upgraded pypdf to 7.1.1 and pandas to 3.1.1
  • Modernized core dependencies in requirements.txt to latest stable releases
  • Updated test & QA tooling (pytest 9.2.0, mypy 2.4.1, black 26.10.1)
  • 100% test pass rate maintained (93 passed, 0 skipped, 0 failed)

v0.1.2

  • Modernized dev dependencies (reportlab for complete PDF testing)
  • Aligned static type hints with strict mypy compliance
  • Harmonized Flake8 configuration (max-line-length = 100) with Black and isort
  • Added GitHub Pull Request template and CHANGELOG.md
  • 100% test pass rate (93 passed, 0 skipped, 0 failed)

v0.1.1

  • Updated PyPI package version and metadata
  • Enhanced Python 3.12 compatibility

v0.1.0 (Initial Release)

  • Initial release with support for CSV, JSON, TXT, XLSX, PDF, DOCX
  • Clean Architecture implementation
  • Full test coverage
  • PyPI packaging

Support

For issues, questions, or suggestions, please open an issue on GitHub.

Metadata

Release files for unified-file-reader 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unified-file-reader 0.2.0
File Size Uploaded Provenance Actions
unified_file_reader-0.2.0.tar.gz 25.4 kB Without provenance Details for unified_file_reader-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unified-file-reader 0.2.0
File Interpreter ABI Platform Actions
unified_file_reader-0.2.0-py3-none-any.whl Python 3 none any Details for unified_file_reader-0.2.0-py3-none-any.whl

Total release size: 44.7 kB

Release files / unified_file_reader-0.2.0.tar.gz

Download URL unified_file_reader-0.2.0.tar.gz
Size 25.4 kB
Tags Source
SHA-256 checksum
How to use checksums
730469d04a97dbf6ddcc15376545d3c8be85e379c4b15f3318573261aec52eb3
BLAKE2b-256 checksum
How to use checksums
bfe4ba1ab2f3bac66b61aaeb62814d894099f1f0d4be64e75f60b90817b9e2f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release files / unified_file_reader-0.2.0-py3-none-any.whl

Download URL unified_file_reader-0.2.0-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f3d017dff703a7403db9aecdbbb093c0b8ab4b8a6101f30b58adf987fbe6d11
BLAKE2b-256 checksum
How to use checksums
909a2ace85f185e72c33e87787be9de097dec20df7e51f3dab828bf566025cce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Security for unified-file-reader 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page