Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | t00mlabs | 1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- |
||
3 | """ |
||
4 | # File: srv_application.py |
||
5 | # Author: Tomás Vírseda |
||
6 | # License: GPL v3 |
||
7 | # Description: Application service |
||
8 | """ |
||
9 | |||
3 | t00mlabs | 10 | import os |
11 | import uuid |
||
1 | t00mlabs | 12 | from mex.core.mod_srv import Service |
3 | t00mlabs | 13 | from mex.core.mod_env import GPATH, LPATH, APP, SEP |
4 | t00mlabs | 14 | from mex.core.mod_rdf import BASE, URIRef, Literal |
3 | t00mlabs | 15 | from pprint import pprint |
1 | t00mlabs | 16 | |
17 | |||
18 | class MexAppSrv(Service): |
||
19 | def initialize(self): |
||
2 | t00mlabs | 20 | """ |
21 | Missing method docstring (missing-docstring) |
||
22 | """ |
||
23 | self.get_services() |
||
1 | t00mlabs | 24 | |
25 | |||
2 | t00mlabs | 26 | def get_services(self): |
27 | """ |
||
28 | Missing method docstring (missing-docstring) |
||
29 | """ |
||
30 | self.srvutl = self.get_service('Utils') |
||
31 | self.srvplg = self.get_service('Plugins') |
||
32 | |||
33 | |||
34 | |||
3 | t00mlabs | 35 | def run(self, params): |
36 | self.log.debug("TARGET_PATH: %s" % params.TARGET_PATH) |
||
37 | if params.TARGET_PATH is None: |
||
38 | TARGET_PATH = os.path.join(LPATH['EXPORT'], 'repository.dot') |
||
39 | else: |
||
40 | TARGET_PATH = os.path.join(params.TARGET_PATH, 'repository.dot') |
||
41 | |||
42 | repodict = {} |
||
43 | repoobjs = {} |
||
4 | t00mlabs | 44 | samerank = "" |
45 | |||
1 | t00mlabs | 46 | self.log.debug("Starting Metadata eXplorer") |
2 | t00mlabs | 47 | self.srv_plugins = self.get_service('Plugins') |
48 | self.plugins = self.srv_plugins.get_plugins_by_category('Metadata') |
||
49 | self.log.debug("Found %d plugins for 'Metadata' category" % len(self.plugins)) |
||
3 | t00mlabs | 50 | fileset = self.srvutl.get_files_from_dir(params.SOURCE_PATH) |
51 | self.log.debug("Got %d files" % len(fileset)) |
||
1 | t00mlabs | 52 | |
3 | t00mlabs | 53 | for path in fileset: |
54 | tuples = [] |
||
55 | s = URIRef("%s%s" % (BASE, path)) |
||
56 | rid = uuid.uuid4() |
||
57 | repodict[path] = {} |
||
58 | repodict[path]['uuid'] = str(rid) |
||
4 | t00mlabs | 59 | samerank += "\"%s\"; " % rid |
3 | t00mlabs | 60 | for plugin in self.plugins: |
61 | try: |
||
62 | metadata = plugin.plugin_object.get_metadata(self.app, path) |
||
63 | except: |
||
64 | metadata = [] |
||
65 | tuples.extend(metadata) |
||
66 | for p, o in tuples: |
||
67 | predicate = self.srvutl.pname(p) |
||
68 | if isinstance(o, URIRef) or isinstance(o, Literal): |
||
69 | o = str(self.srvutl.pname(o)) |
||
70 | repodict[path][predicate] = o |
||
71 | try: |
||
72 | objid = repoobjs[o] |
||
73 | except: |
||
74 | repoobjs[o] = str(uuid.uuid4()) |
||
75 | |||
4 | t00mlabs | 76 | # Graph |
77 | output = "graph repository{\n" |
||
78 | output += " rankdir = LR;\n" |
||
79 | |||
80 | # Nodes |
||
3 | t00mlabs | 81 | nodes = [] |
82 | relations = [] |
||
83 | for path in fileset: |
||
84 | try: |
||
4 | t00mlabs | 85 | node = " \"%s\" [shape=cylinder label=\"%s\" URL=\"file://%s\"]\n" % (repodict[path]['uuid'], repodict[path]['fileName'], path) |
3 | t00mlabs | 86 | output += node |
87 | nodes.append(node) |
||
88 | for prop in repodict[path]: |
||
89 | if prop not in ['uuid', 'fileName', 'fileUrl']: |
||
90 | pid = repoobjs[repodict[path][prop]] |
||
4 | t00mlabs | 91 | node = " \"%s\" [label=\"%s\"]\n" % (pid, repodict[path][prop]) |
3 | t00mlabs | 92 | nodes.append(node) |
93 | output += node |
||
4 | t00mlabs | 94 | relation = " \"%s\" -- \"%s\" [label=\"%s\"]\n" % (repodict[path]['uuid'], repoobjs[repodict[path][prop]], prop) |
3 | t00mlabs | 95 | relations.append(relation) |
96 | except: |
||
97 | pass |
||
4 | t00mlabs | 98 | |
99 | # Same Rank |
||
100 | output += " {rank = same; %s}\n" % samerank |
||
101 | |||
102 | # Relations |
||
3 | t00mlabs | 103 | for relation in relations: |
104 | output += relation |
||
4 | t00mlabs | 105 | |
106 | # End graph |
||
3 | t00mlabs | 107 | output += "}" |
108 | |||
109 | with open(TARGET_PATH, 'w') as dot: |
||
110 | dot.write(output) |
||
111 | self.log.debug("Generated dot file in: %s" % TARGET_PATH) |
||
112 | |||
4 | t00mlabs | 113 | cmd = "circo -Tsvg %s > repository.html" % TARGET_PATH |
114 | self.log.debug("Execute command: %s" % cmd) |
||
3 | t00mlabs | 115 | |
1 | t00mlabs | 116 | def end(self): |
117 | self.log.debug("Stopping Metadata eXplorer") |
||
118 |