faker-file¶
Generate fake files
Prerequisites¶
All of core dependencies of this package are MIT licensed. Most of optional dependencies of this package are MIT licensed, while a few are BSD- or Apache 2 licensed. All licenses are mentioned below between the brackets.
Core package requires Python 3.7, 3.8, 3.9, 3.10 and 3.11.
Django
(BSD) integration withfactory_boy
(MIT) has been tested withDjango
2.2, 3.0, 3.1, 3.2, 4.0 and 4.1.DOCX
file support requirespython-docx
(MIT).ICO
,JPEG
,PNG
,SVG
andWEBP
files support requiresimgkit
(MIT).PDF
file support requirespdfkit
(MIT).PPTX
file support requirespython-pptx
(MIT).ODS
file support requirestablib
(MIT) andodfpy
(Apache 2).XLSX
file support requirestablib
(MIT) andopenpyxl
(MIT).PathyFileSystemStorage
storage support requirespathy
(Apache 2).AWSS3Storage
storage support requirespathy
(Apache 2) andboto3
(Apache 2).AzureCloudStorage
storage support requirespathy
(Apache 2) andazure-storage-blob
(MIT).GoogleCloudStorage
storage support requirespathy
(Apache 2) andgoogle-cloud-storage
(Apache 2).
Documentation¶
Documentation is available on Read the Docs.
Installation¶
Latest stable version on PyPI:
pip install faker-file[all]
Or development version from GitHub:
pip install https://github.com/barseghyanartur/faker-file/archive/main.tar.gz
Supported file types¶
BIN
CSV
DOCX
ICO
JPEG
ODS
PDF
PNG
PPTX
SVG
TXT
WEBP
XLSX
ZIP
Usage examples¶
With Faker
¶
One way
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file()
Or another
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
FAKER = Faker()
FAKER.add_provider(TxtFileProvider)
file = FAKER.txt_file()
With factory_boy
¶
upload/models.py¶
from django.db import models
class Upload(models.Model):
# ...
file = models.FileField()
upload/factory.py¶
Note, that when using faker-file
with Django
, you need to pass your
MEDIA_ROOT
setting as root_path
value (which is by default set
to tempfile.gettempdir()
).
import factory
from django.conf import settings
from factory import Faker
from factory.django import DjangoModelFactory
from faker_file.providers.docx_file import DocxFileProvider
from upload.models import Upload
factory.Faker.add_provider(DocxFileProvider)
class UploadFactory(DjangoModelFactory):
# ...
file = Faker("docx_file", root_path=settings.MEDIA_ROOT)
class Meta:
model = Upload
Supported storages¶
All file operations are delegated to a separate abstraction layer of storages.
The following storages are implemented:
FileSystemStorage
: Does not have additional requirements.PathyFileSystemStorage
: Requires pathy.AzureCloudStorage
: Requires pathy and Azure related dependencies.GoogleCloudStorage
: Requires pathy and Google Cloud related dependencies.AWSS3Storage
: Requires pathy and AWS S3 related dependencies.
Usage example with storages¶
FileSystemStorage example¶
Native file system storage. Does not have dependencies.
import tempfile
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.filesystem import FileSystemStorage
FS_STORAGE = FileSystemStorage("root_path", "rel_path")
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=FS_STORAGE)
FS_STORAGE.exists(file)
PathyFileSystemStorage example¶
Native file system storage. Requires pathy.
import tempfile
from pathy import use_fs
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.cloud import PathyFileSystemStorage
use_fs(tempfile.gettempdir())
PATHY_FS_STORAGE = PathyFileSystemStorage("bucket_name", "rel_path")
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=PATHY_FS_STORAGE)
PATHY_FS_STORAGE.exists(file)
AWSS3Storage example¶
AWS S3 storage. Requires pathy.
import tempfile
from pathy import use_fs
from faker import Faker
from faker_file.providers.txt_file import TxtFileProvider
from faker_file.storages.aws_s3 import AWSS3Storage
S3_STORAGE = AWSS3Storage(
bucket_name="bucket_name",
rel_path="rel_path",
credentials={"key_id": "YOUR KEY ID", "key_secret": "YOUR KEY SECRET"},
)
FAKER = Faker()
file = TxtFileProvider(FAKER).txt_file(storage=S3_STORAGE)
S3_STORAGE.exists(file)
Testing¶
Simply type:
pytest -vvv
Or use tox:
tox
Or use tox to check specific env:
tox -e py310-django41
Writing documentation¶
Keep the following hierarchy.
=====
title
=====
header
======
sub-header
----------
sub-sub-header
~~~~~~~~~~~~~~
sub-sub-sub-header
^^^^^^^^^^^^^^^^^^
sub-sub-sub-sub-header
++++++++++++++++++++++
sub-sub-sub-sub-sub-header
**************************
License¶
MIT
Support¶
For any security issues contact me at the e-mail given in the Author section.
For overall issues, go to GitHub.
Project documentation¶
Contents:
- faker-file
- Prerequisites
- Documentation
- Installation
- Supported file types
- Usage examples
- Supported storages
- Testing
- Writing documentation
- License
- Support
- Author
- Project documentation
- Quick start
- Recipes
- When using with
Faker
- Imports and initializations
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a ZIP file consisting of TXT files with static content
- Create a ZIP file consisting of 3 DOCX files with dynamically generated content
- Create a nested ZIP file
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a PDF file with predefined template containing dynamic fixtures
- Pick a random file from a directory given
- When using with
Django
(andfactory_boy
)
- When using with
- Release history and notes
- Package
- faker_file package
- Subpackages
- faker_file.providers package
- Submodules
- faker_file.providers.bin_file module
- faker_file.providers.csv_file module
- faker_file.providers.docx_file module
- faker_file.providers.ico_file module
- faker_file.providers.jpeg_file module
- faker_file.providers.mixins module
- faker_file.providers.ods_file module
- faker_file.providers.pdf_file module
- faker_file.providers.png_file module
- faker_file.providers.pptx_file module
- faker_file.providers.random_file_from_dir module
- faker_file.providers.svg_file module
- faker_file.providers.txt_file module
- faker_file.providers.webp_file module
- faker_file.providers.xlsx_file module
- faker_file.providers.zip_file module
ZipFileProvider
create_inner_bin_file()
create_inner_csv_file()
create_inner_docx_file()
create_inner_ico_file()
create_inner_jpeg_file()
create_inner_ods_file()
create_inner_pdf_file()
create_inner_png_file()
create_inner_pptx_file()
create_inner_svg_file()
create_inner_txt_file()
create_inner_webp_file()
create_inner_xlsx_file()
create_inner_zip_file()
- Module contents
- faker_file.storages package
- faker_file.tests package
- Submodules
- faker_file.tests.test_django_integration module
- faker_file.tests.test_providers module
ProvidersTestCase
ProvidersTestCase.FAKER
ProvidersTestCase.setUp()
ProvidersTestCase.test_broken_imports
ProvidersTestCase.test_faker
ProvidersTestCase.test_generate_filename_failure()
ProvidersTestCase.test_standalone_providers
ProvidersTestCase.test_standalone_providers_allow_failures
ProvidersTestCase.test_standalone_zip_file
ProvidersTestCase.test_standalone_zip_file_allow_failures
- faker_file.tests.test_storages module
- Module contents
- faker_file.providers package
- Submodules
- faker_file.base module
- faker_file.constants module
- faker_file.helpers module
- Module contents
- Subpackages
- faker_file package
- Indices and tables
- Quick start
- Recipes
- When using with
Faker
- Imports and initializations
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a ZIP file consisting of TXT files with static content
- Create a ZIP file consisting of 3 DOCX files with dynamically generated content
- Create a nested ZIP file
- Create a TXT file with static content
- Create a DOCX file with dynamically generated content
- Create a PDF file with predefined template containing dynamic fixtures
- Pick a random file from a directory given
- When using with
Django
(andfactory_boy
)
- When using with
- Release history and notes
- Package
- faker_file package
- Subpackages
- faker_file.providers package
- Submodules
- faker_file.providers.bin_file module
- faker_file.providers.csv_file module
- faker_file.providers.docx_file module
- faker_file.providers.ico_file module
- faker_file.providers.jpeg_file module
- faker_file.providers.mixins module
- faker_file.providers.ods_file module
- faker_file.providers.pdf_file module
- faker_file.providers.png_file module
- faker_file.providers.pptx_file module
- faker_file.providers.random_file_from_dir module
- faker_file.providers.svg_file module
- faker_file.providers.txt_file module
- faker_file.providers.webp_file module
- faker_file.providers.xlsx_file module
- faker_file.providers.zip_file module
ZipFileProvider
create_inner_bin_file()
create_inner_csv_file()
create_inner_docx_file()
create_inner_ico_file()
create_inner_jpeg_file()
create_inner_ods_file()
create_inner_pdf_file()
create_inner_png_file()
create_inner_pptx_file()
create_inner_svg_file()
create_inner_txt_file()
create_inner_webp_file()
create_inner_xlsx_file()
create_inner_zip_file()
- Module contents
- faker_file.storages package
- faker_file.tests package
- Submodules
- faker_file.tests.test_django_integration module
- faker_file.tests.test_providers module
ProvidersTestCase
ProvidersTestCase.FAKER
ProvidersTestCase.setUp()
ProvidersTestCase.test_broken_imports
ProvidersTestCase.test_faker
ProvidersTestCase.test_generate_filename_failure()
ProvidersTestCase.test_standalone_providers
ProvidersTestCase.test_standalone_providers_allow_failures
ProvidersTestCase.test_standalone_zip_file
ProvidersTestCase.test_standalone_zip_file_allow_failures
- faker_file.tests.test_storages module
- Module contents
- faker_file.providers package
- Submodules
- faker_file.base module
- faker_file.constants module
- faker_file.helpers module
- Module contents
- Subpackages
- faker_file package