Skip to content

Writing export scripts

Alexander Makeev edited this page Jun 24, 2013 · 1 revision

<< Back

After we have created a new configuration we could make an export script that could be activated by URL.

1. Create django view

1.1. Create script file. Go to facility_tools\ folder and create a file to store our export script, let it be simple_export.py:

from django.http import HttpResponse

def export(request):
    return HttpResponse("Success.")

1.2. Register export function in facility_tools/urls.py file. Add the mapping:

urlpatterns = patterns('',
    url(r'^$', 'facility_tools.simple_export.export', name="facility-simple-export"),
)

1.3. Make an item in the menu to address the export script. Add menu item to facility_tools/menu.py MENU_ITEMS list:

MENU_ITEMS = [
    {
        "title" : "Facility",
        "subitems" : [
            {
                "title" : "Simple Export",
                "url" : reverse("facility-simple-export")
            }
        ]
    }
]

You also could use facility-simple-export in any place you want to create a URL for GUI, for html it will be:

{% url facility-simple-export %}

for python code it will be:

from django.core.urlresolvers import reverse
reverse("facility-simple-export")

Now you could run web-server and try newly created menu item:

$ ./zrun

If all were made properly you should see Success. plain text after clicking newly created menu item Facility > Simple Export

2. Query graph DB

2.1. Edit facility_tools\simple_export.py by getting configuration graph request.configuration:

def index(request):
    conf = request.configuration
    return HttpResponse("Success.")

2.2. Get all entities by class device.

    devs = conf.getAllEntities("device")

2.3. Get device by name test_dev.

from portal.utils.array_helpers import getFirstOrNone

...
    devs = conf.getAllEntities("device", filter_func=lambda ent: ent["name"]=="test_dev")
    dev = getFirstOrNone(devs)

Function getFirstOrNone is a utility function of portal application.

You could pass any filter function instead of lambda function. It will take each entity in the graph (already filtered by class_name if it is specified) and return True if the entity should appear in the result list.

3. Navigate the graph

3.1. Get all neighbours of dev.

    nbs = dev.getNeighbours()

3.2. Get all to neighbours of dev.

    nbs = dev.getNeighboursTo()

The configuration graph is directed and you could use only To or From edges to navigate.

3.3. Filter neighbours by class pc.

from portal.utils.filters import getFilterNeighboursByClassName

...
    nbs = dev.getNeighbours(filter_func=getFilterNeighboursByClassName(conf, "pc")))

Function getFilterNeighboursByClassName is a utility function of portal application.

You could pass any filter function instead of getFilterNeighboursByClassName(). It will take each entity in the neighbours list and return True if the entity should appear in the result list.

4. Read objects attributes

Object attribute attr1 are accessible as follows:

    print ent.attributes['attr1']

Here ent.attributes is a real python dictionary

Also you could use a shortcut:

    print ent['attr1']

Here ent is a class that just have __getitem__ and __setitem__ methods defined.

5. Save results to file

This is just a python, so you could use standard python file IO:

    from settings import PROJECT_DIR

    values = {
        'v1' : 10.0
    }
    f = open(PROJECT_DIR + '/output.txt', 'w')
    f.write("value1=%(v1)s\n" % values)
    f.writelines(["a=1", "b=2"])
    f.close()

The output will be a file output.txt at PyCDB root folder:

value1=10.000000
a=1
b=2

Clone this wiki locally