Skip to content

Commit

Permalink
Bin and bin_file
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Sep 8, 2024
1 parent 1e822d0 commit 4996530
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,14 @@ def odt(
_odt = OdtGenerator(faker=self)
return _odt.create(nb_pages=nb_pages, texts=texts, metadata=metadata)

@provider(tags=("Binary",))
def bin(
self,
size: int = 16,
) -> bytes:
"""Create random bytes."""
return os.urandom(size)

@provider(tags=("Archive",))
def zip(self, options: Optional[Dict[str, Any]] = None, **kwargs):
"""Create a ZIP archive."""
Expand Down Expand Up @@ -2815,6 +2823,38 @@ def odt_file(
FILE_REGISTRY.add(file)
return file

@provider(
tags=(
"Binary",
"File",
)
)
def bin_file(
self,
storage: Optional[BaseStorage] = None,
basename: Optional[str] = None,
prefix: Optional[str] = None,
size: int = 16,
**kwargs,
) -> StringValue:
"""Create a BIN file."""
if storage is None:
storage = FileSystemStorage()
filename = storage.generate_filename(
extension="bin",
prefix=prefix,
basename=basename,
)
data = self.bin()
storage.write_bytes(filename=filename, data=data)
file = StringValue(storage.relpath(filename))
file.data = {
"storage": storage,
"filename": filename,
}
FILE_REGISTRY.add(file)
return file

@provider(
tags=(
"Archive",
Expand Down

0 comments on commit 4996530

Please sign in to comment.