#!/usr/bin/env python3 from argparse import ArgumentParser, Namespace import os import base64 import json def main(srcFile: str, dstFile:str) -> None: if not os.path.isfile(srcFile): print( '-s argument is invalid. Is it a proper BLNS txt file? Aborting!' ) return with open(srcFile, 'r') as f: # put all lines in the file into a Python list content = f.readlines() # above line leaves trailing newline characters; strip them out content = [x.strip('\n') for x in content] # remove empty-lines and comments content = [x for x in content if x and not x.startswith('#')] # Base64 encode all content to make Postman Collection Runner parser to not break content = [base64.b64encode(x.encode('utf-8')).decode('utf-8') for x in content] # insert empty string since all are being removed content.insert(0, "") encodedContent: list = [] for c in content: encodedContent.append({"encodedNaughtyString": c}) with open(dstFile, 'w') as f: # write JSON to file; note the ensure_ascii parameter json.dump(encodedContent, f, indent=2, ensure_ascii=False) if __name__ == '__main__': parser = ArgumentParser() parser.add_argument( '-s', '--src', help='The source BLNS txt file to convert', type=str, required=True) parser.add_argument( '-d', '--dst', help='The destination filename of the encoded BLNS CSV', type=str, required=True) args: Namespace = parser.parse_args() main(args.src, args.dst)