Skip to content

Commit

Permalink
Add fuzzy_choice_create_inner_file
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jan 9, 2023
1 parent c55b85a commit a8d816c
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 25 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 96 additions & 2 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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
Expand All @@ -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={
Expand Down Expand Up @@ -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={
Expand All @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/faker_file/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = "faker_file"
__version__ = "0.10.4"
__version__ = "0.10.5"
__author__ = "Artur Barseghyan <[email protected]>"
__copyright__ = "2022-2023 Artur Barseghyan"
__license__ = "MIT"
83 changes: 82 additions & 1 deletion src/faker_file/providers/helpers/inner.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,6 +46,7 @@
"create_inner_webp_file",
"create_inner_xlsx_file",
"create_inner_zip_file",
"fuzzy_choice_create_inner_file",
)


Expand Down Expand Up @@ -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)
64 changes: 44 additions & 20 deletions src/faker_file/tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit a8d816c

Please sign in to comment.