diff --git a/CHANGELOG.rst b/CHANGELOG.rst index aeca5ea0..0e66237c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,14 @@ are used for versioning (schema follows below): 0.3.4 to 0.4). - All backwards incompatible changes are mentioned in this document. +0.10.5 +------ +2023-01-09 + +- Add ``fuzzy_choice_create_inner_file`` inner function. +- Documentation improvements. +- Add ``MaryTTS`` example (another MP3 generator for ``Mp3FileProvider``). + 0.10.4 ------ 2023-01-08 diff --git a/docs/recipes.rst b/docs/recipes.rst index 18f13076..a731dc7f 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -138,6 +138,44 @@ contain 5 DOCX files. } ) +Create a ZIP file with variety of different file types within +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- 50 files in the ZIP archive (limited to DOCX, EPUB and TXT types). +- Content is generated dynamically. +- Prefix the filename of the archive itself with ``zzz_archive_``. +- Inside the ZIP, put all files in directory ``zzz``. + +.. code-block:: python + + from faker import Faker + from faker_file.providers.helpers.inner import ( + create_inner_docx_file, + create_inner_epub_file, + create_inner_txt_file, + fuzzy_choice_create_inner_file, + ) + from faker_file.providers.zip_file import ZipFileProvider + from faker_file.storages.filesystem import FileSystemStorage + + FAKER = Faker() + STORAGE = FileSystemStorage() + + kwargs = {"storage": STORAGE, "generator": FAKER} + file = ZipFileProvider(FAKER).zip_file( + prefix="zzz_archive_", + options={ + "count": 50, + "create_inner_file_func": fuzzy_choice_create_inner_file, + "create_inner_file_args": { + "func_choices": [ + (create_inner_docx_file, kwargs), + (create_inner_epub_file, kwargs), + (create_inner_txt_file, kwargs), + ], + }, + "directory": "zzz", + } + ) Create a EML file consisting of TXT files with static content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -146,6 +184,11 @@ Create a EML file consisting of TXT files with static content .. code-block:: python + from faker import Faker + from faker_file.providers.eml_file import EmlFileProvider + + FAKER = Faker() + file = EmlFileProvider(FAKER).eml_file(options={"content": "Lorem ipsum"}) Create a EML file consisting of 3 DOCX files with dynamically generated content @@ -158,7 +201,12 @@ Create a EML file consisting of 3 DOCX files with dynamically generated content .. code-block:: python - from faker_file.providers.eml_file import create_inner_docx_file + from faker import Faker + from faker_file.providers.eml_file import EmlFileProvider + from faker_file.providers.helpers.inner import create_inner_docx_file + + FAKER = Faker() + file = EmlFileProvider(FAKER).eml_file( prefix="zzz", options={ @@ -186,7 +234,15 @@ contain 5 DOCX files. .. code-block:: python - from faker_file.providers.eml_file import create_inner_docx_file, create_inner_eml_file + from faker import Faker + from faker_file.providers.eml_file import EmlFileProvider + from faker_file.providers.helpers.inner import ( + create_inner_docx_file, + create_inner_eml_file, + ) + + FAKER = Faker() + file = EmlFileProvider(FAKER).eml_file( prefix="nested_level_0_", options={ @@ -206,6 +262,44 @@ contain 5 DOCX files. } ) +Create an EML file with variety of different file types within +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- 10 files in the EML file (limited to DOCX, EPUB and TXT types). +- Content is generated dynamically. +- Prefix the filename of the EML itself with ``zzz``. + +.. code-block:: python + + from faker import Faker + from faker_file.providers.helpers.inner import ( + create_inner_docx_file, + create_inner_epub_file, + create_inner_txt_file, + fuzzy_choice_create_inner_file, + ) + from faker_file.providers.eml_file import EmlFileProvider + from faker_file.storages.filesystem import FileSystemStorage + + FAKER = Faker() + STORAGE = FileSystemStorage() + + kwargs = {"storage": STORAGE, "generator": FAKER} + + file = EmlFileProvider(FAKER).eml_file( + prefix="zzz", + options={ + "count": 10, + "create_inner_file_func": fuzzy_choice_create_inner_file, + "create_inner_file_args": { + "func_choices": [ + (create_inner_docx_file, kwargs), + (create_inner_epub_file, kwargs), + (create_inner_txt_file, kwargs), + ], + }, + } + ) + Create a TXT file with static content ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/setup.py b/setup.py index 499c6e64..b48485ba 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup -version = "0.10.4" +version = "0.10.5" try: readme = open(os.path.join(os.path.dirname(__file__), "README.rst")).read() diff --git a/src/faker_file/__init__.py b/src/faker_file/__init__.py index 06d462dc..08647514 100644 --- a/src/faker_file/__init__.py +++ b/src/faker_file/__init__.py @@ -1,5 +1,5 @@ __title__ = "faker_file" -__version__ = "0.10.4" +__version__ = "0.10.5" __author__ = "Artur Barseghyan " __copyright__ = "2022-2023 Artur Barseghyan" __license__ = "MIT" diff --git a/src/faker_file/providers/helpers/inner.py b/src/faker_file/providers/helpers/inner.py index fa0c0f27..030d6b68 100644 --- a/src/faker_file/providers/helpers/inner.py +++ b/src/faker_file/providers/helpers/inner.py @@ -1,4 +1,15 @@ -from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union +from random import choice +from typing import ( + Any, + Callable, + Dict, + List, + Optional, + Sequence, + Tuple, + Type, + Union, +) from faker import Faker from faker.generator import Generator @@ -35,6 +46,7 @@ "create_inner_webp_file", "create_inner_xlsx_file", "create_inner_zip_file", + "fuzzy_choice_create_inner_file", ) @@ -490,3 +502,72 @@ def create_inner_zip_file( options=options, **kwargs, ) + + +def fuzzy_choice_create_inner_file( + func_choices: List[Tuple[Callable, Dict[str, Any]]], + **kwargs, +) -> StringValue: + """Create inner file from given list of function choices. + + :param func_choices: List of functions to choose from. + :return: StringValue. + + Usage example: + + from faker import Faker + from faker_file.providers.helpers.inner import ( + create_inner_docx_file, + create_inner_epub_file, + create_inner_txt_file, + fuzzy_choice_create_inner_file, + ) + from faker_file.storages.filesystem import FileSystemStorage + + FAKER = Faker() + STORAGE = FileSystemStorage() + + kwargs = {"storage": STORAGE, "generator": FAKER} + file = fuzzy_choice_create_inner_file( + [ + (create_inner_docx_file, kwargs), + (create_inner_epub_file, kwargs), + (create_inner_txt_file, kwargs), + ] + ) + + You could use it in archives to make a variety of different file types + within the archive. + + from faker import Faker + from faker_file.providers.helpers.inner import ( + create_inner_docx_file, + create_inner_epub_file, + create_inner_txt_file, + fuzzy_choice_create_inner_file, + ) + from faker_file.providers.zip_file import ZipFileProvider + from faker_file.storages.filesystem import FileSystemStorage + + FAKER = Faker() + STORAGE = FileSystemStorage() + + kwargs = {"storage": STORAGE, "generator": FAKER} + file = ZipFileProvider(FAKER).zip_file( + prefix="zzz_archive_", + options={ + "count": 50, + "create_inner_file_func": fuzzy_choice_create_inner_file, + "create_inner_file_args": { + "func_choices": [ + (create_inner_docx_file, kwargs), + (create_inner_epub_file, kwargs), + (create_inner_txt_file, kwargs), + ], + }, + "directory": "zzz", + } + ) + """ + _func, _kwargs = choice(func_choices) + return _func(**_kwargs) diff --git a/src/faker_file/tests/test_providers.py b/src/faker_file/tests/test_providers.py index c87cbcff..1269e48e 100644 --- a/src/faker_file/tests/test_providers.py +++ b/src/faker_file/tests/test_providers.py @@ -35,6 +35,7 @@ create_inner_webp_file, create_inner_xlsx_file, create_inner_zip_file, + fuzzy_choice_create_inner_file, ) from ..providers.ico_file import IcoFileProvider from ..providers.jpeg_file import JpegFileProvider @@ -506,38 +507,61 @@ def test_standalone_providers_allow_failures( self.assertTrue(storage.exists(_file)) @parametrize( - "create_inner_file_func, content", + "create_inner_file_func, content, create_inner_file_args", [ - (None, None), - (create_inner_bin_file, b"Lorem ipsum"), - (create_inner_csv_file, "Lorem ipsum"), - (create_inner_docx_file, "Lorem ipsum"), - (create_inner_eml_file, None), - (create_inner_epub_file, "Lorem ipsum"), - (create_inner_ico_file, "Lorem ipsum"), - (create_inner_jpeg_file, "Lorem ipsum"), - (create_inner_mp3_file, "Lorem ipsum"), - (create_inner_ods_file, None), - (create_inner_pdf_file, "Lorem ipsum"), - (create_inner_png_file, "Lorem ipsum"), - (create_inner_pptx_file, "Lorem ipsum"), - (create_inner_rtf_file, "Lorem ipsum"), - (create_inner_svg_file, "Lorem ipsum"), - (create_inner_txt_file, "Lorem ipsum"), - # (create_inner_webp_file, "Lorem ipsum"), - (create_inner_xlsx_file, None), - (create_inner_zip_file, None), + (None, None, None), + (create_inner_bin_file, b"Lorem ipsum", {}), + (create_inner_csv_file, "Lorem ipsum", {}), + (create_inner_docx_file, "Lorem ipsum", {}), + (create_inner_eml_file, None, {}), + (create_inner_epub_file, "Lorem ipsum", {}), + (create_inner_ico_file, "Lorem ipsum", {}), + (create_inner_jpeg_file, "Lorem ipsum", {}), + (create_inner_mp3_file, "Lorem ipsum", {}), + (create_inner_ods_file, None, {}), + (create_inner_pdf_file, "Lorem ipsum", {}), + (create_inner_png_file, "Lorem ipsum", {}), + (create_inner_pptx_file, "Lorem ipsum", {}), + (create_inner_rtf_file, "Lorem ipsum", {}), + (create_inner_svg_file, "Lorem ipsum", {}), + (create_inner_txt_file, "Lorem ipsum", {}), + # (create_inner_webp_file, "Lorem ipsum", {}), + (create_inner_xlsx_file, None, {}), + (create_inner_zip_file, None, {}), + ( + fuzzy_choice_create_inner_file, + None, + { + "func_choices": [ + ( + create_inner_docx_file, + {"storage": FS_STORAGE, "generator": _FAKER}, + ), + ( + create_inner_epub_file, + {"storage": FS_STORAGE, "generator": _FAKER}, + ), + ( + create_inner_txt_file, + {"storage": FS_STORAGE, "generator": _FAKER}, + ), + ] + }, + ), ], ) def test_standalone_zip_file( self: "ProvidersTestCase", create_inner_file_func: Optional[Callable] = None, content: Union[str, Dict] = None, + create_inner_file_args: Dict[str, Any] = None, ) -> None: """Test standalone ZIP file provider.""" _options = {"content": content} if create_inner_file_func is not None: _options["create_inner_file_func"] = create_inner_file_func + if create_inner_file_args is not None: + _options["create_inner_file_args"] = create_inner_file_args _file = ZipFileProvider(None).zip_file(options=_options) self.assertTrue(FS_STORAGE.exists(_file))