Tool: Sectors.yaml fixer

Discussion in 'The Hangar Bay' started by Xango2000, May 31, 2017.

  1. Xango2000

    Xango2000 Captain

    Joined:
    Jun 15, 2016
    Messages:
    385
    Likes Received:
    202
    Some of the yaml creator tools available now have a tendency to... complicate the look of the yaml file.

    So, I created a simple little program to convert

    This format
    Code:
    - Coordinates:
      - 15
      - -89
      - -778
      Color: 0.8, 0.83, 0
      Icon: Rhombus
      Playfields:
      - - 0, 0, 0
        - Instanced Omicron Orbit
        - SpaceRingDebris
        - ''
      - - -10500, 0, 0
        - Instanced Omicron
        - InstanceOmicron
    

    back to the Easy to read format that looks like
    Code:
    - Coordinates: [ -227, -89, -796 ]
      Color: 0.8, 0.83, 0
      Icon: Rhombus
      Playfields:
      - [ "0, 0, 0", Instanced Cerberus Orbit, SpaceRingDebris ]
      - [ "-10500, 0, 0", Instanced Cerberus, InstanceCerberus ]
    
    https://drive.google.com/open?id=0BwtLXvAMvkc-LVdqUzJQRGVOZFU
     
    #1
    Last edited: Jun 1, 2017
    Idrona and Alexandra like this.
  2. Captain Jack II

    Captain Jack II Rear Admiral

    Joined:
    Nov 1, 2016
    Messages:
    783
    Likes Received:
    1,268
    Work with @jmcburn and his playfield designer.
     
    #2
  3. Xango2000

    Xango2000 Captain

    Joined:
    Jun 15, 2016
    Messages:
    385
    Likes Received:
    202
    Python code probably wouldnt help much but here it is anyways
    Code:
    import os
    
    global listname
    listname = ""
    global listdata
    listdata = []
    mainlist = []
    playfields = []
    
    def makelist(line, listdata):
        if line.startswith("  - "):
            listdata.append(line[3:])
        if line.startswith("    - "):
            if ":" in line:
                line2 = "'" + line[6:] + "'"
                listdata.append(line2)
            else:
                listdata.append(line[6:])
        return listdata
    
    filename = os.path.join("Sectors" + ".yaml")
    fh = open("backup.yaml", "w")
    fh.write(" ")
    fh.close()
    
    with open(filename) as f:
        filedata = [line.strip('\n') for line in open(filename)]
    for line in filedata:
        fh = open("backup.yaml", "a")
        try:
            fh.write("\r")
            fh.write(line)
        except:
            junk = "junk"
        fh.close()
        if line.endswith(":"):
            if "Playfields" in line:
                mainlist.append("  Playfields:")
            elif len(listdata) > 0 :
                listdata = ", ".join(listdata)
                if listname == "Playfields":
                    listname = "  - [ " + listdata + " ]"
                else:
                    listname = "  " + listname + ": [ " + listdata + " ]"
                mainlist.append(listname)
                listdata = []
            listname = line[2:-1]
        elif line.startswith("  - - "):
            if len(listdata) > 0:
                listdata = ", ".join(listdata)
                listdata = "  - [ " + listdata + " ]"
                mainlist.append(listdata) 
            playfields = "\"" + line[6:] + "\""
            listdata = []
            listdata.append(playfields)
        elif line.startswith("  - "):
            if "[" in line:
                mainlist.append(line)
            else:
                listdata = makelist(line, listdata)
        elif line.startswith("    - "):
            listdata = makelist(line, listdata)
    
        else:
            if len(listname) > 0:
                if len(listdata) > 0 :
                    listdata = ", ".join(listdata)
                    if listname == "Coordinates":
                        mainlist.append("\r")
                        listname = "- " + listname + ": [" + listdata + " ]"
                    elif listname == "Playfields":
                        listname = "  - [ " + listdata + " ]"
                    else:
                        listname = "  " + listname + ": [" + listdata + " ]"
                    mainlist.append(listname)
                    listdata = []
           
            mainlist.append(line)
    
    for line in mainlist:
        print (line)
    fh = open("Sectors.yaml", "w")
    #fh.write("")
    fh.write("\n")
    fh.close()
    
    fh = open("Sectors.yaml", "a")
    for line in mainlist:
        try:
            fh.write(line)
            fh.write("\r")
        except:
            junk = "junk"
    fh.close()
    
     
    #3
    shortybsd likes this.
  4. Xango2000

    Xango2000 Captain

    Joined:
    Jun 15, 2016
    Messages:
    385
    Likes Received:
    202
    New version I posted yesterday fixed all the bugs I knew about.

    If you find any bugs, please let me know
     
    #4

Share This Page