Last active
November 5, 2021 09:36
-
-
Save arkandas/e1b03ff2ea15a7380cdba5a24c3b98cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# ---- RUN ---- | |
# python3 main.py --header MyHeader --version 1.0.1 --path /home/user/pathToFile/CHANGELOG.md | |
# ------------- | |
import argparse | |
import re | |
from datetime import datetime | |
syncVersion = 'v1.0.0' | |
my_parser = argparse.ArgumentParser(prog='changelogParser.py', | |
description='CHANGELOG.md parser', | |
epilog='Happy Hacking', allow_abbrev=False) | |
my_parser.version = syncVersion | |
my_parser.add_argument('-j', action='version') | |
my_parser.add_argument('--header', action='store', type=str, required=True, | |
help='') | |
my_parser.add_argument('--version', action='store', type=str, required=True, | |
help='') | |
my_parser.add_argument('--path', action='store', type=str, required=True, | |
help='') | |
args = my_parser.parse_args() | |
print('CLI arguments: ' + str(args)) | |
if __name__ == '__main__': | |
filepath = args.path | |
tempVal = '' | |
finalVal = '' | |
with open(filepath, 'r+') as orFile: | |
fileContent = orFile.readlines() | |
flag = False | |
copy = False | |
for line in fileContent: | |
if flag is True: | |
if line != '\n' or line.startswith('[Task]'): | |
tempVal = tempVal + line if line != '\n' else line | |
task = True | |
else: | |
flag = False | |
else: | |
if line.startswith('##') and copy is False: | |
if re.search(r'\((.*?)\)', line).group(1) != '': | |
finalVal = finalVal + tempVal + '\n' | |
copy = True | |
if line.startswith('##'): | |
if args.header == re.search(r'\[(.*?)\]', line).group(1) and re.search(r'\((.*?)\)', line).group(1) == '': | |
finalVal = finalVal + line + '\n' | |
flag = True | |
tempVal = tempVal + f'## [{args.header} {args.version}] ({datetime.now().strftime("%Y-%m-%d")})\n' | |
else: | |
finalVal = finalVal + line | |
else: | |
finalVal = finalVal + line | |
if copy is False: | |
finalVal = finalVal + '\n' + tempVal | |
orFile.truncate(0) | |
orFile.write(finalVal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with:
python3 main.py --header Zum --version 1.0.1 --path /home/user/pathToFile/CHANGELOG.md