-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
91 lines (77 loc) · 2.44 KB
/
Model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'''
@author: cgueret
'''
import os
import sys
TESTS_PATH = os.path.dirname(os.path.realpath(__file__))
ERS_PATH = os.path.join(os.path.dirname(TESTS_PATH), 'ers/ers-local')
sys.path.insert(0, ERS_PATH)
from ers import ERS
import getpass
class Model(object):
def __init__(self):
'''
Constructor
'''
# Create an instance of ERS
self._ers = ERS()
# Create the local profile ?
entity_name = self.get_own_contact_name()
if not self._ers.contains_entity(entity_name):
entity = self._ers.create_entity(entity_name)
entity.add_property("rdf:type", "foaf:Person")
entity.add_property("foaf:name", getpass.getuser())
self._ers.persist_entity(entity)
def get_own_contact_name(self):
'''
Get the identifier of the profile for the user of the app
'''
# Compose a resource name for the owner profile
entity_name = 'urn:ers:app:Contact:' + self._ers.get_machine_uuid() + ":" + getpass.getuser()
return entity_name
def get_contacts(self):
'''
Get a list of contacts from the reachable peers
'''
# Issue a search with ERS
list = self._ers.search("rdf:type", "foaf:Person")
# Return the list
return list
def get_entity_description(self, entity_name):
'''
Get all the available data about the selected entity
'''
# Get all the (accessible) documents describing that identifier
entity = self._ers.get_entity(entity_name)
# Get the aggregated description
description = entity.get_properties()
# Eliminate duplicates in the aggregated description
# TODO Move this to the entity code
description = dict([(k, list(set(v))) for (k, v) in description.iteritems()])
# Return it
return description
def is_cached(self, entity_name):
'''
Check if this entity is cached in ERS
'''
# Return true if the entity is in the cache
return self._ers.is_cached(entity_name)
def cache_entity(self, entity_name):
'''
Save an entity to the cache
'''
self._ers.cache_entity(self._ers.get_entity(entity_name))
def add_property(self, entity_name, property, value):
'''
Complete the description of an entity with a new property/value
'''
entity = self._ers.get_entity(entity_name)
entity.add_property(property, value)
self._ers.persist_entity(entity)
def delete_property(self, entity_name, property, value):
'''
Remove a given triple entity_name/property/value
'''
entity = self._ers.get_entity(entity_name)
entity.delete_property(property, value)
self._ers.persist_entity(entity)