Subversion Repositories MeX

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
# File: srv_application.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Application service
"""


import os
import uuid
from mex.core.mod_srv import Service
from mex.core.mod_env import GPATH, LPATH, APP, SEP
from mex.core.mod_rdf import BASE, URIRef, Literal
from pprint import pprint


class MexAppSrv(Service):
    def initialize(self):
        """
        Missing method docstring (missing-docstring)
        """

        self.get_services()


    def get_services(self):
        """
        Missing method docstring (missing-docstring)
        """

        self.srvutl = self.get_service('Utils')
        self.srvplg = self.get_service('Plugins')



    def run(self, params):
        self.log.debug("TARGET_PATH: %s" % params.TARGET_PATH)
        if params.TARGET_PATH is None:
            TARGET_PATH = os.path.join(LPATH['EXPORT'], 'repository.dot')
        else:
            TARGET_PATH = os.path.join(params.TARGET_PATH, 'repository.dot')

        repodict = {}
        repoobjs = {}
        samerank = ""

        self.log.debug("Starting Metadata eXplorer")
        self.srv_plugins = self.get_service('Plugins')
        self.plugins = self.srv_plugins.get_plugins_by_category('Metadata')
        self.log.debug("Found %d plugins for 'Metadata' category" % len(self.plugins))
        fileset = self.srvutl.get_files_from_dir(params.SOURCE_PATH)
        self.log.debug("Got %d files" % len(fileset))

        for path in fileset:
            tuples = []
            s = URIRef("%s%s" % (BASE, path))
            rid = uuid.uuid4()
            repodict[path] = {}
            repodict[path]['uuid'] = str(rid)
            samerank += "\"%s\"; " % rid
            for plugin in self.plugins:
                try:
                    metadata = plugin.plugin_object.get_metadata(self.app, path)
                except:
                    metadata = []
                tuples.extend(metadata)
            for p, o in tuples:
                predicate = self.srvutl.pname(p)
                if isinstance(o, URIRef) or isinstance(o, Literal):
                    o = str(self.srvutl.pname(o))
                repodict[path][predicate] = o
                try:
                    objid = repoobjs[o]
                except:
                    repoobjs[o] = str(uuid.uuid4())

        # Graph
        output = "graph repository{\n"
        output += "    rankdir = LR;\n"

        # Nodes
        nodes = []
        relations = []
        for path in fileset:
            try:
                node = "    \"%s\" [shape=cylinder label=\"%s\" URL=\"file://%s\"]\n" % (repodict[path]['uuid'], repodict[path]['fileName'], path)
                output += node
                nodes.append(node)
                for prop in repodict[path]:
                    if prop not in ['uuid', 'fileName', 'fileUrl']:
                        pid = repoobjs[repodict[path][prop]]
                        node = "    \"%s\" [label=\"%s\"]\n" % (pid, repodict[path][prop])
                        nodes.append(node)
                        output += node
                        relation = "    \"%s\" -- \"%s\" [label=\"%s\"]\n" % (repodict[path]['uuid'], repoobjs[repodict[path][prop]], prop)
                        relations.append(relation)
            except:
                pass

        # Same Rank
        output += "    {rank = same; %s}\n" % samerank

        # Relations
        for relation in relations:
            output += relation

        # End graph
        output += "}"

        with open(TARGET_PATH, 'w') as dot:
            dot.write(output)
            self.log.debug("Generated dot file in: %s" % TARGET_PATH)

        cmd = "circo -Tsvg %s > repository.html" % TARGET_PATH
        self.log.debug("Execute command: %s" % cmd)

    def end(self):
        self.log.debug("Stopping Metadata eXplorer")