Python Forum

Full Version: parse .graphml files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I use python to transform a sphere diagram into a cpp state machine. It works like a charm if.... the provided .graphml file is made by the program yEd.

A coworker tried with .graphml files made by a different program with catastrophic results (it just didn't work)

This code opens the .graphml file and parses it. The names I entered within the spheres are important as are the arrows from and to the spheres. Being new to python, I got it work but it is undoubtedly not very efficient.
#!/usr/bin/python 
import sys
import xml
import os
import platform 

def getStateMachines() : # function tested!
    slash = ""
    Files = []
    print(platform.system())
    if platform.system() == "Windows":
        slash = '\\'
    else:
        slash = '/'
    for root, dirs, fileList in os.walk(".", topdown=False):
        if root == "." + slash + "yEd_stateMachines" :
            Files = fileList

   
    i = 0
    string = []

    for file in Files:
        #if i % 2 == 0:
        string.append(file)
        #print(file)
        i += 1
    return string

stateDiagrams = getStateMachines()

arrowsOut1 = []
arrowsIn1 = []
arrowsOut = []
arrowsIn = []
states1 = []
states = []

file_name = sys.argv[1]
smType = sys.argv[2] # 'c' or 'assembly'

if smType == "main":
    file_name = "mainStateMachines/" + file_name

if smType == "nested":
    file_name = "nestedStateMachines/" + file_name

with open(file_name, "r") as f:
    data = f.readlines()

for line in data: #states
    words = line.split('"')
    if words.count(' autoSizePolicy=') != 0:
        states1.append(words[36])

for arrow in data: # arrows
    words = arrow.split('"')
    if words.count('    <edge id=') != 0:
        arrowsOut1.append(words[3][1:])
        arrowsIn1.append(words[5][1:])
        
for state in states1:
    state = state.split('<')
    states.append(state[0][1:])

f.close()
The problem is that there is too much hard coded stuff in it like words[36]). My coworker told that python processes certain libraries and functions which can do the same but better. It is my hope that I can get this to work for other diagram editors as well.

Can somebody help me with this?

I also have the entire file on github btw:
https://github.com/bask185/State-Machine...nerator.py

kind regards,

Bas