|
1 | | -from rsa.utils.checks import checks |
2 | | -from rsa.context import context |
| 1 | +""" |
| 2 | +Write File module for the encryption_helper package. |
| 3 | +
|
| 4 | +This module provides functions to write text files in binary mode. It includes |
| 5 | +functions to write files given a directory and file name or an absolute file path. |
| 6 | +
|
| 7 | +Functions: |
| 8 | + write_text_in_binary_mode: Writes text to a file in binary mode given a directory and file name. |
| 9 | + write_text_in_binary_mode_abs: Writes text to a file in binary mode given an absolute file path. |
| 10 | +""" |
| 11 | + |
3 | 12 | import os |
| 13 | +from encryption_helper.utils.checks import checks |
| 14 | +from encryption_helper.context import context |
4 | 15 |
|
5 | 16 |
|
6 | 17 | def write_text_in_binary_mode(directory, file_name, text): |
7 | | - logger = context.Context.getinstance().get_logger() |
| 18 | + """ |
| 19 | + Write text to a file in binary mode given a directory and file name. |
| 20 | +
|
| 21 | + This function constructs the absolute file path from the given directory and file name, |
| 22 | + checks if the inputs are valid, and writes the text to the file in binary mode. |
| 23 | +
|
| 24 | + Args: |
| 25 | + directory (str): The directory containing the file. |
| 26 | + file_name (str): The name of the file to be written. |
| 27 | + text (bytes): The text to be written to the file. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + str: The absolute file path of the written file. |
| 31 | +
|
| 32 | + Raises: |
| 33 | + Exception: If one or more arguments are empty. |
| 34 | + OSError: If there is an error writing to the file. |
| 35 | +
|
| 36 | + Examples: |
| 37 | + >>> path = write_text_in_binary_mode('path/to/dir', 'file.txt', b'test data') |
| 38 | + >>> print(path) |
| 39 | +
|
| 40 | + """ |
| 41 | + logger = context.Context.get_instance().get_logger() |
8 | 42 | if checks.str_none_or_empty(directory, file_name, text): |
9 | | - logger.exception('One or more arguments are empty') |
10 | | - raise Exception('One or more arguments are empty') |
| 43 | + logger.exception("One or more arguments are empty") |
| 44 | + raise Exception("One or more arguments are empty") |
11 | 45 |
|
12 | 46 | absolute_file_path = os.path.join(directory, file_name) |
13 | 47 | return write_text_in_binary_mode_abs(absolute_file_path, text) |
14 | 48 |
|
15 | 49 |
|
16 | 50 | def write_text_in_binary_mode_abs(absolute_file_path, text): |
17 | | - logger = context.Context.getinstance().get_logger() |
| 51 | + """ |
| 52 | + Write text to a file in binary mode given an absolute file path. |
| 53 | +
|
| 54 | + This function checks if the inputs are valid and writes the text to the file in binary mode. |
| 55 | +
|
| 56 | + Args: |
| 57 | + absolute_file_path (str): The absolute path to the file to be written. |
| 58 | + text (bytes): The text to be written to the file. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + str: The absolute file path of the written file. |
| 62 | +
|
| 63 | + Raises: |
| 64 | + Exception: If one or more arguments are empty. |
| 65 | + OSError: If there is an error writing to the file. |
| 66 | +
|
| 67 | + Examples: |
| 68 | + >>> path = write_text_in_binary_mode_abs('/path/to/dir/file.txt', b'test data') |
| 69 | + >>> print(path) |
| 70 | +
|
| 71 | + """ |
| 72 | + logger = context.Context.get_instance().get_logger() |
18 | 73 | if checks.str_none_or_empty(absolute_file_path, text): |
19 | | - logger.exception('One or more arguments are empty') |
20 | | - raise Exception('One or more arguments are empty') |
| 74 | + logger.exception("One or more arguments are empty") |
| 75 | + raise Exception("One or more arguments are empty") |
21 | 76 |
|
22 | | - logger.info('Writing to file : {0}'.format(absolute_file_path)) |
23 | | - with open(absolute_file_path, 'wb') as f: |
| 77 | + logger.info("Writing to file : {0}".format(absolute_file_path)) |
| 78 | + with open(absolute_file_path, "wb") as f: |
24 | 79 | f.write(text) |
25 | | - logger.info('Writing to file complete') |
| 80 | + logger.info("Writing to file complete") |
26 | 81 |
|
27 | 82 | return absolute_file_path |
0 commit comments