Skip to content

Commit

Permalink
Optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jan 4, 2023
1 parent 11b6e5f commit a376f52
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
6 changes: 3 additions & 3 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Create a EML file consisting of TXT files with static content
Create a EML file consisting of 3 DOCX files with dynamically generated content
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 3 DOCX files in the EML archive.
- 3 DOCX files in the EML email.
- Content is generated dynamically.
- Content is limited to 1024 chars.
- Prefix the filenames in email with ``xxx_``.
Expand Down Expand Up @@ -178,8 +178,8 @@ contain 5 DOCX files.

- 5 EML files in the EML file.
- Content is generated dynamically.
- Prefix the filenames in archive with ``nested_level_1_``.
- Prefix the filename of the archive itself with ``nested_level_0_``.
- Prefix the filenames in EML email with ``nested_level_1_``.
- Prefix the filename of the EML email itself with ``nested_level_0_``.
- Each of the EML files inside the EML file in their turn contains 5 other EML
files, prefixed with ``nested_level_2_``, which in their turn contain 5
DOCX files.
Expand Down
28 changes: 12 additions & 16 deletions src/faker_file/providers/eml_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mimetypes
# import mimetypes
import os
from email import generator
from email.message import EmailMessage
from io import BytesIO
from email.policy import default
from typing import Any, Dict, Optional

from faker.providers import BaseProvider
Expand Down Expand Up @@ -31,7 +30,7 @@ class EmlFileProvider(BaseProvider, FileMixin):
file = EmlFileProvider(FAKER).eml_file()
Usage example with options:
Usage example with attachments:
from faker_file.providers.helpers.inner import create_inner_docx_file
from faker_file.providers.eml_file import EmlFileProvider
Expand All @@ -48,7 +47,7 @@ class EmlFileProvider(BaseProvider, FileMixin):
}
)
Usage example of nested EMLs:
Usage example of nested EMLs attachments:
from faker_file.providers.helpers.inner import create_inner_eml_file
Expand Down Expand Up @@ -148,7 +147,7 @@ def eml_file(

else:
# Defaults
_count = 5
_count = 0
_create_inner_file_func = create_inner_txt_file
_create_inner_file_args = {}

Expand All @@ -161,11 +160,12 @@ def eml_file(
**_kwargs,
)
__file_abs_path = fs_storage.abspath(__file)
_content_type, _encoding = mimetypes.guess_type(__file_abs_path)
if _content_type is None or _encoding is not None:
# No guess could be made, or the file is encoded (compressed),
# so use a generic bag-of-bits type.
_content_type = "application/octet-stream"
# _content_type, _encoding = mimetypes.guess_type(__file_abs_path)
# if _content_type is None or _encoding is not None:
# # No guess could be made, or the file is
# # encoded (compressed), so use a generic bag-of-bits type.
# _content_type = "application/octet-stream"
_content_type = "application/octet-stream"
_maintype, _subtype = _content_type.split("/", 1)
with open(__file_abs_path, "rb") as _fp:
_file_data = _fp.read()
Expand All @@ -177,11 +177,7 @@ def eml_file(
)
os.remove(__file_abs_path) # Clean up temporary files

with BytesIO() as _eml_content:
eml_generator = generator.BytesGenerator(_eml_content)
eml_generator.flatten(msg)
_eml_content.seek(0)
storage.write_bytes(filename, _eml_content.read())
storage.write_bytes(filename, msg.as_bytes(policy=default))

# Generic
file_name = StringValue(storage.relpath(filename))
Expand Down

0 comments on commit a376f52

Please sign in to comment.