| 1 | import json |
| 2 | |
| 3 | |
| 4 | def pretty_print_header(header_text, width=50, char='-'): |
| 5 | """ |
| 6 | Formatting output used for the CLI |
| 7 | |
| 8 | :param header_text: The text to be displayed |
| 9 | :type header_text: str |
| 10 | :param width: total width of the display |
| 11 | :type width: int |
| 12 | :param char: which char to be used as a filler |
| 13 | :type char: str |
| 14 | """ |
| 15 | padding = max(0, width - len(header_text)) // 2 |
| 16 | formatted_header = f"\n{char * padding} {header_text} {char * padding}" |
| 17 | print(formatted_header) |
| 18 | |
| 19 | |
| 20 | def save_data_to_file(filename, data): |
| 21 | """ |
| 22 | Write data to file |
| 23 | |
| 24 | :param data: the actual data |
| 25 | :type data: bytes |
| 26 | :param filename: file to be saved in |
| 27 | :type filename: str |
| 28 | """ |
| 29 | try: |
| 30 | with open(filename, 'wb') as output_file: |
| 31 | output_file.write(data) |
| 32 | print(f"Data saved to {filename}") |
| 33 | except Exception as e: |
| 34 | print(f"Error while saving data to {filename}: {e}") |
| 35 | |
| 36 | |
| 37 | def save_to_json(filename, dictionary): |
| 38 | """ |
| 39 | Simple method to save a dictionary as JSON into the filename. |
| 40 | |
| 41 | :param filename: the name of the file to be saved as |
| 42 | :type filename: str |
| 43 | :param dictionary: the dictionary to be saved as JSON |
| 44 | :type dictionary: dict |
| 45 | """ |
| 46 | with open(filename, "w") as h_file: |
| 47 | json.dump(dictionary, h_file, indent=4) |
| 48 | |
| 49 | |
| 50 | def escape_xml_entities(data): |
| 51 | """ |
| 52 | Escaping characters that cant be included within an XML file. |
| 53 | |
| 54 | :param data: The string to escape |
| 55 | :type data: str |
| 56 | :return: The escaped output |
| 57 | :rtype: str |
| 58 | """ |
| 59 | replacements = { |
| 60 | '<': '<', |
| 61 | '>': '>', |
| 62 | '&': '&', |
| 63 | '"': '"', |
| 64 | "'": ''' |
| 65 | } |
| 66 | return ''.join(replacements.get(c, c) for c in data) |