-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added BOM capability for output files (1267)
- Added the '--add-bom' parameter for almost utilities Signed-off-by: Álvaro Osvaldo <[email protected]>
- Loading branch information
1 parent
c327a1b
commit e9c8aa1
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from argparse import ArgumentParser, Namespace | ||
from io import TextIOWrapper, BytesIO | ||
from typing import Union | ||
|
||
|
||
class AddBOM: | ||
|
||
@staticmethod | ||
def _get_BOM() -> bytes: | ||
|
||
from codecs import BOM_UTF8 | ||
|
||
return BOM_UTF8 | ||
|
||
@staticmethod | ||
def enabled(arguments: Union[Namespace,list, None] = None) -> bool: | ||
|
||
if isinstance(arguments, Namespace) or isinstance(arguments, list): | ||
return "add_bom" in arguments and arguments.add_bom | ||
|
||
return False | ||
|
||
@staticmethod | ||
def argument(arguments: ArgumentParser, utility: object): | ||
|
||
# These string usage to validate the class is an architecture | ||
# fail as is not possible to check the class type before | ||
# is initialized | ||
|
||
if "SQL2CSV" in str(utility.__class__): | ||
return | ||
|
||
if "CSVPy" in str(utility.__class__): | ||
return | ||
|
||
arguments.add_argument( | ||
"--add-bom", | ||
dest="add_bom", | ||
action="store_true", | ||
default=False, | ||
help="Add Byte Order Mark (BOM) to the output", | ||
) | ||
|
||
@staticmethod | ||
def run( | ||
output: TextIOWrapper, | ||
arguments: Union[Namespace, None] = None, | ||
): | ||
|
||
if not AddBOM.enabled(arguments): | ||
return | ||
|
||
BOM = AddBOM._get_BOM() | ||
output.buffer.write(BOM) |