1

So I am having here one big JSON file which looks like this:

data = {
    "Module1": {
        "Description": "",
        "Layer": "1",
        "SourceDir": "pathModule1",
        "Attributes": {
            "some",
        },
        "Vendor": "comp",
        "components":{
            "Component1": {
               "path": "pathToCom1",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            },
            "Component2":{
               "path": "pathToCom2",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            }
        }
    },
    "Module2": {
        "Description": "",
        "Layer": "2",
        "SourceDir": "pathModule2",
        "Attributes": {
            "some",
        },
        "Vendor": "comp",
        "components":{
            "Component1": {
               "path": "pathToCom1",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            },
            "Component2":{
               "path": "pathToCom2",
               "includes": [
                   "include1",
                   "include2",
                   "include3",
                   "include4",
                   "include5"
               ]
               "generated:" "txt"
               "memory:" "txt"
               etc
            }
        }
    },
    "Module3": {
        "Description": "",
        "Layer": "3",
        "SourceDir": "path",
        "Attributes": {
            "some",
        },
        "Vendor": "",
    },
    "Module4": {
        "Description": "",
        "Layer": "4",
        "SourceDir": "path",
        "Attributes": {
            "some",
        }
    }
}

Then I filter only ones that have field "Vendor" == "comp" with:

data = {k: v for k,v in data.items() if v.get("Vendor") == "comp"}

And after that I filter out and get final output:

Module1 pathModule1 [('Component1', 'pathToCom1', ['include1', 'include2', 'include3', 'include4', 'include5']), ('Component2', 'pathToCom2', ['include1', 'include2', 'include3', 'include4', 'include5'])]
Module2 pathModule2 [('Component1', 'pathToCom1', ['include1', 'include2', 'include3', 'include4', 'include5']), ('Component2', 'pathToCom2', ['include1', 'include2', 'include3', 'include4', 'include5'])]

code for this:

for k,v in data2.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    print(k, v["SourceDir"], components)

Now next thing that I have to do, as final output -> create in folder some .txt files which will be named as Module name, and contain paths to it's components, like this:

Module1.txt should contain only paths to it's components, so

Module1.txt has inside:
pathToCom1
pathToCom2

Module2.txt with:
pathToCom1
pathToCom2

And also, includes should be stored inside of corresponding .txt files, so we would have at the end name of the Component, and it's "includes" fields, so we would have:

Component1.txt with inside:
 include1
 include2
 include3
 include4
 include5

Component2.txt with inside:
 include1
 include2
 include3
 include4
 include5

EDIT:

So I have managed to get this, code is:

for k,v in data.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    with open(components_path+k+'.txt', 'w') as f:
        for i,n in v['components '].items():
            path_to_write = n['path'] 
            f.write(path_to_write+'\n')
        f.close()
        for i,n in v['components'].items():
            with open(path_to_includes+i+'.txt', 'w') as f:
                includes_to_write = n['includes'] 
                f.write(str(includes_to_write)+'\n')
            f.close()

Now the only problem is that I get includes as one line:

['include1', 'include2', 'include3', 'include4'..]

I need them to be:
 include1
 include2
 include3
 include4
 include5
John
  • 230
  • 2
  • 12

1 Answers1

2

So finally have managed to get this working, here is the code might help someone someday:

for k,v in data.items():
    components = [(comp_name, comp_data["path"], comp_data["includes"]) for comp_name, comp_data in v["components"].items()]
    with open(components_path+k+'.txt', 'w') as f:
        for i,n in v['components '].items():
            path_to_write = n['path'] 
            f.write(path_to_write+'\n')
        f.close()
        for i,n in v['components'].items():
            with open(path_to_includes+i+'.txt', 'w') as f:
                includes_to_write = n['includes'] 
                for line in includes_to_write:
                    f.write(line+'\n')
            f.close()
John
  • 230
  • 2
  • 12