OpenCF Core: The File Convertion Framework
The opencf-core package provides a robust framework for handling file conversion tasks in Python. It offers a set of classes and utilities designed to simplify the process of reading from and writing to different file formats efficiently.
Features
- Modular Input/Output Handlers: Defines abstract base classes for file readers and writers, allowing for easy extension and customization.
- Support for Various File Formats: Provides built-in support for common file formats such as text, CSV, JSON, XML, Excel, and image files.
- MIME Type Detection: Includes a MIME type guesser utility to automatically detect the MIME type of files, facilitating seamless conversion based on file content.
- File Type Enumeration: Defines an enum for representing different file types, enabling easy validation and processing of input and output files.
- Exception Handling: Implements custom exceptions for handling errors related to unsupported file types, empty suffixes, file not found, and mismatches between file types.
- Base Converter Class: Offers an abstract base class for implementing specific file converters, providing a standardized interface for file conversion operations.
- Resolved Input File Representation: Introduces a class for representing input files with resolved file types, ensuring consistency and correctness in conversion tasks.
Conversion Strategies
When using the opencf-core, you can adopt different strategies for file conversion based on your specific requirements:
1. Direct Conversion
In this approach, conversion is achieved without utilizing a dedicated writer. The reader module parses the input files into a list of objects. Subsequently, the _convert method orchestrates the writing process into a file or folder. This method is suitable for scenarios where direct manipulation of data structures suffices for conversion.
2. Indirect Conversion
Conversely, indirect conversion employs a converter that supports a dedicated writer. Here, the convert function's primary role is to transform the parsed list of objects into a format compatible with the writer. The actual conversion process may be executed by the writer, leveraging its capabilities. For instance, converting images to videos involves parsing images into a list of Pillow objects, which are then reformatted into a numpy array. This array, encapsulating frame dimensions and color channels, serves as input for the video writer.
Component Instances
The file conversion process can be dissected into three distinct instances:
-
Reader: Handles input-output (IO) operations, transforming files into objects. Readers are implementations of the abstract class
Readerpresent inio_handler.py. -
Converter: Facilitates object-to-object conversion, acting as an intermediary for data transformation. Converters are implementations of the abstract class
BaseConverterpresent inbase_converter.py. -
Writer (Optional): Reverses the IO process, converting objects back into files. Writers are implementations of the abstract class
Writerpresent inio_handler.py.
Modules
- io_handler.py: Contains classes for reading from and writing to files, including text, CSV, JSON, XML, and image files. It includes abstract classes for
ReaderandWriter. - mimes.py: Provides a MIME type guesser utility for detecting file MIME types based on file content.
- filetypes.py: Defines enums and classes for representing different file types and handling file type validation.
- base_converter.py: Implements the base converter class and the resolved input file class for performing file conversion tasks. It includes the
BaseConverterabstract class.
Installation
pip install opencf-core
Usage
The opencf-core package can be used independently to build custom file conversion utilities or integrated into larger projects for handling file format transformations efficiently.
from opencf_core.io_handler import CsvToListReader, ListToCsvWriter
from opencf_core.base_converter import BaseConverter, ResolvedInputFile
from opencf_core.filetypes import FileType
class CSVToJSONConverter(BaseConverter):
file_reader = CsvToListReader()
file_writer = DictToJsonWriter()
@classmethod
def _get_supported_input_type(cls) -> FileType:
return FileType.CSV
@classmethod
def _get_supported_output_type(cls) -> FileType:
return FileType.JSON
def _convert(self, input_path: Path, output_file: Path):
# Implement conversion logic from CSV to JSON
pass
# Usage
input_file_path = "input.csv"
output_file_path = "output.json"
input_file = ResolvedInputFile(input_file_path, is_dir=False, should_exist=True)
output_file = ResolvedInputFile(output_file_path, is_dir=False, should_exist=False, add_suffix=True)
converter = CSVToJSONConverter(input_file, output_file)
converter.convert()
More Examples
The examples folder in this repository contains practical demonstrations of how to use the opencf-core package for file conversion tasks. Currently, it includes the following examples:
-
simple_converter.py: Demonstrates a basic file converter that converts Excel (XLSX) files to CSV format. It utilizes the
XLXSToCSVConverterclass defined within theopencf-corepackage to perform the conversion. -
cli_app_example.py: Illustrates how to build a command-line interface (CLI) application using the
ConverterAppclass from theopencf-core.converter_appmodule. This CLI app allows users to specify input and output files, as well as input and output file types, for performing file conversions.
These examples serve as practical demonstrations of how to leverage the capabilities of the opencf-core package in real-world scenarios. Users can refer to these examples for guidance on building their own file conversion utilities or integrating file conversion functionality into existing projects.
You can have a more practical insight by reading the support associated to the examples
Todo
Backend Support
- Introduce the concept of backend labeling for
ReaderandWriterimplementations. - Enable multiple file readers/writers to share common backends. For instance, if an
ImageOpenCVReaderutilizes both numpy and OpenCV, theVideoWritercan leverage the same dependencies. - Allow users to specify preferred backend configurations, ensuring that conversion methods accommodate all selected backends seamlessly.
Contributing
Contributions to the opencf-core package are welcome! Feel free to submit bug reports, feature requests, or pull requests via the GitHub repository.
Disclaimer
Please note that while the opencf-core package aims to provide a versatile framework for file conversion tasks, it may not cover every possible use case or handle all edge cases. Users are encouraged to review and customize the code according to their specific requirements.
Metadata
Release files for opencf-core 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opencf_core-0.3.4.tar.gz | 23.6 kB | Without provenance | Details for opencf_core-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| opencf_core-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for opencf_core-0.3.4-py3-none-any.whl |
Total release size: 48.5 kB
Release files / opencf_core-0.3.4.tar.gz
| Download URL | opencf_core-0.3.4.tar.gz |
|---|---|
| Size | 23.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7cb750ab823f05af6a4351bc2cefbd29da0579119454d70b33ede279150cf0a1
|
|
BLAKE2b-256 checksum How to use checksums |
0e02d14d6671defb91bd6517d1bcffb5b20a2b89cba857e78f1ff82f173f1f04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.4 Linux/6.5.0-25-generic
|
Release files / opencf_core-0.3.4-py3-none-any.whl
| Download URL | opencf_core-0.3.4-py3-none-any.whl |
|---|---|
| Size | 24.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1c39b518bcb8de3bff69c190e0ec5f7c0ed896952b5820ce4bf06c46836ee47
|
|
BLAKE2b-256 checksum How to use checksums |
099688e3e09782ac229e9ea1adc5e800efeba5fa36201b90d0e0511e62300968
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.4 Linux/6.5.0-25-generic
|
Security for opencf-core 0.3.4
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.