diff options
| author | Alex Schofield <git@ajschof.me> | 2025-02-20 19:11:22 +0000 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2025-02-20 19:11:22 +0000 |
| commit | ff3a5630fe5440f61fffc2b2732ba38fad2b67a7 (patch) | |
| tree | d24bae78c95bc08859fc96b3262d5da3e72c1c2f | |
| parent | 21e5b8fdd204f9d1ca8549fa512fe825760e9366 (diff) | |
| download | gdpr-obfuscator-ff3a5630fe5440f61fffc2b2732ba38fad2b67a7.tar.gz gdpr-obfuscator-ff3a5630fe5440f61fffc2b2732ba38fad2b67a7.zip | |
move create_byte_stream() to utils.py
| -rw-r--r-- | gdpr_obfuscator/utils.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/gdpr_obfuscator/utils.py b/gdpr_obfuscator/utils.py index 740db90..2dffbc2 100644 --- a/gdpr_obfuscator/utils.py +++ b/gdpr_obfuscator/utils.py @@ -1,5 +1,8 @@ # Utility functions from .logger import get_logger +from typing import List, Dict +import csv +import io class Utilities: @@ -14,3 +17,20 @@ class Utilities: key = "/".join(parts) self.logger.debug(f"Key: {key}") return bucket, key + + def create_byte_stream(self, data: List[Dict[str, str]]) -> bytes: + if not data: + self.logger.error("Invalid or empty data was provided to write") + return b"" + + output = io.StringIO() + + headers = list(data[0].keys()) + + writer = csv.DictWriter(output, fieldnames=headers) + writer.writeheader() + writer.writerows(data) + + csv_string = output.getvalue() + + return csv_string.encode("utf-8") |
