Created
December 9, 2023 07:41
-
-
Save y-yoshinoya/b72e293da07904a43907f528910751e7 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
import os | |
import yaml | |
import xml.etree.ElementTree as ET | |
directory_path = "/path/to/src/main/mule" | |
global_config_path = "/path/to/src/main/resources/properties/global-config.yaml" | |
def load_global_config(path): | |
# YAMLファイルの読み込み | |
with open(path, 'r') as file: | |
data = yaml.safe_load(file) | |
return data | |
config_yaml = load_global_config(global_config_path) | |
def get_nested_element(data, key): | |
keys = key.split('.') | |
result = data | |
for k in keys: | |
result = result.get(k) | |
if result is None: | |
break | |
return result | |
def cache_xml_structures(directory): | |
xml_structures = {} | |
for filename in os.listdir(directory): | |
if filename.endswith(".xml"): | |
file_path = os.path.join(directory, filename) | |
try: | |
tree = ET.parse(file_path) | |
xml_structures[filename] = tree.getroot() | |
except ET.ParseError as e: | |
print(f"Error parsing {filename}: {e}") | |
return xml_structures | |
def get_http_request_urls(filename, root, flow_name, nest = 3): | |
tab_repeated = '\t' * nest | |
filepath = directory_path + '/' + filename | |
namespaces = { | |
'mule': 'http://www.mulesoft.org/schema/mule/core', | |
'http': 'http://www.mulesoft.org/schema/mule/http', | |
} | |
# 指定された名前のflow要素を探す | |
xpath_flow = f".//mule:flow[@name='{flow_name}']" | |
flow_elements = root.findall(xpath_flow, namespaces) | |
# 該当するflow内のhttp:request要素のurl属性を取得する | |
for flow_element in flow_elements: | |
print(f"{tab_repeated}File: {filepath}") | |
http_requests = flow_element.findall(".//http:request", namespaces) | |
print(f"{tab_repeated}Flow: {flow_name}") | |
# Find HTTP requests within the flow | |
flow_refs = flow_element.findall('.//mule:flow-ref', namespaces) | |
for flow_ref in flow_refs: | |
name = flow_ref.get('name') | |
print(f"{tab_repeated}Flow Ref: {name}") | |
for filename, root in xml_structures.items(): | |
get_http_request_urls(filename, root, name, nest + 1) | |
for request in http_requests: | |
path_key = request.get('path') | |
path = get_nested_element(config_yaml, path_key.replace('${', '').replace('}', '')) | |
print(f"{tab_repeated}URL in Flow '{flow_name}': {path}") | |
def extract_structure_from_router(xml_structures): | |
namespaces = { | |
'apikit': 'http://www.mulesoft.org/schema/mule/mule-apikit', | |
} | |
flow_namespaces = { | |
'mule': 'http://www.mulesoft.org/schema/mule/core', | |
} | |
for filename, root in xml_structures.items(): | |
# print(f"File: {filename}") | |
routers = root.findall('.//apikit:router', namespaces) | |
for router in routers: | |
router_name = router.get('config-ref') | |
print(f"Router: {router_name}") | |
flows = root.findall('.//mule:flow', flow_namespaces) | |
for flow in flows: | |
flow_name = flow.get('name') | |
print(f"\tFlow: {flow_name}") | |
# Find HTTP requests within the flow | |
flow_refs = flow.findall('.//mule:flow-ref', flow_namespaces) | |
for flow_ref in flow_refs: | |
name = flow_ref.get('name') | |
print(f"\t\tFlow Ref: {name}") | |
for filename, root in xml_structures.items(): | |
get_http_request_urls(filename, root, name) | |
# ディレクトリパスを指定して実行する例 | |
xml_structures = cache_xml_structures(directory_path) | |
extract_structure_from_router(xml_structures) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment