service-request-regex-attri.../main.py

58 lines
2.2 KiB
Python

#!/usr/bin/env python
"""
Info: Please add the regex definition into the config.ini file. It could be possible that specific Regex semantics
require escaping, especially for terraform, e.g.:
,([0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}),\w{32} becomes
,([0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}),\\w{32} as an example!
Usage: python3 main.py -n Connected_Friction
"""
import argparse
import configparser
import csv
import jinja2
import logging
import pathlib
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger('main')
logger.setLevel(logging.INFO)
config = configparser.ConfigParser()
config.read("config.ini")
DEFAULT_OUTPUT_PATH = pathlib.Path(config['PATHS']['Output']).absolute()
DEFAULT_TEMPLATES_PATH = pathlib.Path(config['PATHS']['Templates']).absolute()
REGEX = config['MISC']['RegEx']
parser = argparse.ArgumentParser(description="Name of the Services' Chain, e.g. Connected_Friction")
parser.add_argument("--name", "-n", type=str, metavar='', required=True, help="Example: Connected_Friction")
args = parser.parse_args()
env = jinja2.Environment(loader=jinja2.FileSystemLoader(DEFAULT_TEMPLATES_PATH))
template = env.get_template("regex.request_attribute.j2")
def checkDirectories(DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH):
for dir in [DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH]:
try:
pathlib.Path.mkdir(dir)
logger.info("Directory created: %s", dir)
except FileExistsError:
logger.info("Directory already exists: %s", dir)
def renderFile(args, DEFAULT_OUTPUT_PATH):
logger.info("Generating file for: %s", args.name)
content = template.render(ServiceName = str(args.name), RegEx = REGEX)
filename = pathlib.PurePath.joinpath(DEFAULT_OUTPUT_PATH, str(args.name) + ".request_attribute.tf")
with open(filename, mode='w+', encoding="utf-8") as output:
output.write(content)
logger.info("Generated: %s", pathlib.PurePath(filename).name)
if __name__ == '__main__':
checkDirectories(DEFAULT_OUTPUT_PATH, DEFAULT_TEMPLATES_PATH)
renderFile(args, DEFAULT_OUTPUT_PATH)