|
| 1 | +#from collections import defaultdict |
| 2 | +#import numpy as np |
| 3 | + |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import argparse |
| 7 | +import math |
| 8 | +import random |
| 9 | +# https://www.daleseo.com/python-typing/ |
| 10 | +from typing import Optional |
| 11 | +from typing import Union |
| 12 | +from typing import List |
| 13 | +from typing import Final |
| 14 | +from typing import Dict |
| 15 | +from typing import Tuple |
| 16 | +from typing import Set |
| 17 | +import re |
| 18 | +import csv |
| 19 | +import json |
| 20 | +import mysetting |
| 21 | +import datetime |
| 22 | + |
| 23 | +from pprint import PrettyPrinter |
| 24 | +import re |
| 25 | + |
| 26 | +custom_printer = PrettyPrinter( |
| 27 | + indent=2, |
| 28 | + width=500, |
| 29 | + depth=6, |
| 30 | + compact=True, |
| 31 | + sort_dicts=False |
| 32 | + #underscore_numbers=True |
| 33 | + ) |
| 34 | +pprint = custom_printer.pprint |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +def extract_braces_content(text): |
| 40 | + # DOTALL 플래그를 사용하여 개행 문자도 포함하여 매치합니다. |
| 41 | + pattern = r'\{([^{}]*?)\}' |
| 42 | + match = re.search(pattern, text, re.DOTALL) |
| 43 | + if match: |
| 44 | + return match.group(1) # 중괄호 안의 내용만 반환합니다. |
| 45 | + else: |
| 46 | + return None |
| 47 | + |
| 48 | +# 테스트를 위한 멀티라인 문자열 |
| 49 | +sample_text = """ |
| 50 | +function example() { |
| 51 | + var config = { |
| 52 | + option1: true, |
| 53 | + option2: "value", |
| 54 | + option3: [1, 2, 3] |
| 55 | + }; |
| 56 | + return config; |
| 57 | +} |
| 58 | +""" |
| 59 | + |
| 60 | +# 함수 호출 및 결과 출력 |
| 61 | +#content = extract_braces_content(sample_text) |
| 62 | +#print("Extracted Content:") |
| 63 | +#print(content) |
| 64 | + |
| 65 | +class Solution: |
| 66 | + def __init__(self,inpuml,outjson,debug): |
| 67 | + self.inpuml = inpuml |
| 68 | + self.outjson = outjson |
| 69 | + self.debug = debug |
| 70 | + self.uml = [] |
| 71 | + self.json = {} |
| 72 | + #a = [1,2,3,4] |
| 73 | + #pprint({ 'a' : a }) |
| 74 | + lines = [] |
| 75 | + with open(self.inpuml, 'r') as file: |
| 76 | + lines = file.readlines() |
| 77 | + |
| 78 | + self.startFlowChart = re.compile('^```puml\s*:\s*func-FlowChart\s*:\s*(?P<func>.*)\s*$') |
| 79 | + self.startSequenceDiagram = re.compile('^```puml\s*:\s*func-SequenceDiagram\s*:\s*(?P<func>.*)\s*$') |
| 80 | + self.startUmlRe = re.compile('^\s*@startuml\s*$') |
| 81 | + self.endUmlRe = re.compile('^\s*@enduml\s*$') |
| 82 | + |
| 83 | + for line in lines: |
| 84 | + if not line.strip(): |
| 85 | + continue |
| 86 | + grp = self.startFlowChart.search(line) |
| 87 | + if grp: |
| 88 | + self.func = grp.group('func') |
| 89 | + self.func_no_space = self.func.replace(' ','') |
| 90 | + self.kind = 'flowchart' |
| 91 | + #print(grp.group('func')) |
| 92 | + continue |
| 93 | + grp = self.startSequenceDiagram.search(line) |
| 94 | + if grp: |
| 95 | + self.func = grp.group('func') |
| 96 | + self.func_no_space = self.func.replace(' ','') |
| 97 | + self.kind = 'sequencediagram' |
| 98 | + #print(grp.group('func')) |
| 99 | + continue |
| 100 | + if self.endUmlRe.search(line): |
| 101 | + self.uml.append(line) |
| 102 | + if self.func_no_space not in self.json: |
| 103 | + self.json[self.func_no_space] = { 'func': self.func, 'flowchart':[] , 'sequencediagram':[] } |
| 104 | + self.json[self.func_no_space][self.kind] = self.uml |
| 105 | + self.uml = [] |
| 106 | + continue |
| 107 | + self.uml.append(line.replace('::','__')) |
| 108 | + |
| 109 | + #pprint({'json':self.json}) |
| 110 | + with open(self.outjson, 'w') as file: |
| 111 | + print('write:',self.outjson , '<- self.json') |
| 112 | + json.dump({ 'json':self.json } ,file,indent = 4) |
| 113 | + |
| 114 | + |
| 115 | +if (__name__ == "__main__"): |
| 116 | + |
| 117 | + parser = argparse.ArgumentParser( |
| 118 | + prog=sys.argv[0], |
| 119 | + description= |
| 120 | + 'parse plantuml from doxygen_flowchart' |
| 121 | + ) |
| 122 | + parser.add_argument( '--debug', default=False , action="store_true" , help="debug mode on default : false") |
| 123 | + parser.add_argument( '--inpuml', type=str,default='plantuml.md' , |
| 124 | + help='input plantuml file default:plantuml.md') |
| 125 | + parser.add_argument( '--outjson', type=str,default='parse-out.json' , |
| 126 | + help='output json from --inpuml default:parse-out.json') |
| 127 | + args = parser.parse_args() |
| 128 | + |
| 129 | + S = Solution(args.inpuml,args.outjson,args.debug) |
| 130 | + |
| 131 | + |
0 commit comments