Dynamics: No graphs on re-opening
[calf.git] / bigbull / fakeserv.py
blob04c807d969286cbbd255a4853f48ff9f405734ed
1 import re
2 import os
3 import sys
4 import glob
6 lv2 = "http://lv2plug.in/ns/lv2core#"
7 lv2evt = "http://lv2plug.in/ns/ext/event#"
8 rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
9 rdfs = "http://www.w3.org/2000/01/rdf-schema#"
10 rdf_type = rdf + "type"
12 class DumpRDFModel:
13 def addTriple(self, s, p, o):
14 print "%s [%s] %s" % (s, p, repr(o))
16 class SimpleRDFModel:
17 def __init__(self):
18 self.bySubject = {}
19 self.byPredicate = {}
20 def getByType(self, classname):
21 classes = self.bySubject["$classes"]
22 if classname in classes:
23 return classes[classname]
24 return []
25 def getByPropType(self, propname):
26 if propname in self.byPredicate:
27 return self.byPredicate[propname]
28 return []
29 def getProperty(self, subject, props, optional = False, single = False):
30 if type(props) is list:
31 prop = props[0]
32 else:
33 prop = props
34 if type(subject) is str:
35 subject = self.bySubject[subject]
36 elif type(subject) is dict:
37 pass
38 else:
39 if single:
40 return None
41 else:
42 return []
43 anyprops = set()
44 if prop in subject:
45 for o in subject[prop]:
46 anyprops.add(o)
47 if type(props) is list:
48 if len(props) > 1:
49 result = set()
50 for v in anyprops:
51 if single:
52 value = self.getProperty(v, props[1:], optional = optional, single = True)
53 if value != None:
54 return value
55 else:
56 result |= set(self.getProperty(v, props[1:], optional = optional, single = False))
57 if single:
58 return None
59 else:
60 return list(result)
61 if single:
62 if len(anyprops) > 0:
63 if len(anyprops) > 1:
64 raise Exception, "More than one value of " + prop
65 return list(anyprops)[0]
66 else:
67 return None
68 return list(anyprops)
71 def addTriple(self, s, p, o):
72 if p == rdf_type:
73 p = "a"
74 if s not in self.bySubject:
75 self.bySubject[s] = {}
76 if p not in self.bySubject[s]:
77 self.bySubject[s][p] = []
78 self.bySubject[s][p].append(o)
79 if p not in self.byPredicate:
80 self.byPredicate[p] = {}
81 if s not in self.byPredicate[p]:
82 self.byPredicate[p][s] = []
83 self.byPredicate[p][s].append(o)
84 if p == "a":
85 self.addTriple("$classes", o, s)
86 def copyFrom(self, src):
87 for s in src.bySubject:
88 po = src.bySubject[s]
89 for p in po:
90 for o in po[p]:
91 self.addTriple(s, p, o)
92 def dump(self):
93 for s in self.bySubject.keys():
94 for p in self.bySubject[s].keys():
95 print "%s %s %s" % (s, p, self.bySubject[s][p])
97 class FakeServer(object):
98 def __init__(self):
99 pass
101 def start():
102 global instance
103 instance = FakeServer()
105 def queue(cmdObject):
106 global instance
107 #try:
108 cmdObject.calledOnOK(type(instance).__dict__[cmdObject.type](instance, *cmdObject.args))
109 #except:
110 # cmdObject.calledOnError(repr(sys.exc_info()))