annotate wsgraph/model.py @ 31:5f14a4183bf2

fix things
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 13 Dec 2012 18:52:04 -0800
parents 4bed1424bb3f
children 943a4b7097af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
1
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
2 import sys
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 from abc import abstractmethod
9
0affca1f4dc0 start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
4 from copy import deepcopy
15
ee45f44394a0 i need a name, Bastian
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
5 from utils import isiterable
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
25
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
7 class Graph(object):
28
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
8 """
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
9 abstract base class for WSGraph model of a graph
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
10
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
11 WSGraph is interacted with by implementing
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
12 wsgraph.model.Graph object for a desired interface
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
13 """
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 @abstractmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def node(self, name, **values):
10
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
17 """
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
18 get or set a node
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
19
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
20 When setting a node, a value of `None` will pop the value from
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
21 the nodal values
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
22 """
11
7b8e40eda563 more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
23 # TODO: values should not be **kwargs as you could conceivably want
7b8e40eda563 more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
24 # to set a node or edge to an empty dict
7b8e40eda563 more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
25 # Instead, should be (self, name, values=None)
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 @abstractmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 def nodes(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 """returns a list of all nodes"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 @abstractmethod
10
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
32 def edge(self, node1, node2, **values):
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
33 """
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
34 get or set edge from node1 to node2
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
35 """
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
36
81d68388ec97 some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
37 @abstractmethod
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 def edges(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 """returns a list of all edges"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
23
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
41 def __call__(self):
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
42 """
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
43 returns JSGN format of graph:
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
44 {'nodes': {'node1': {},
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
45 'node2': {}, ...},
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
46 'edges': {'node1': {'node2': {},
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
47 'node3': {}, ...},
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
48 'node2': {'node1': {}, ...}}
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
49 }
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
50 """
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
51 retval = {'nodes': {}, 'edges': {}}
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
52 for node in self.nodes():
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
53 retval['nodes'][node] = self.node(node)
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
54 for node1, node2 in self.edges():
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
55 retval['edges'].setdefault(node1, {})[node2] = self.edge(node1, node2)
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
56 return retval
24d57daaca21 well, now the request dispatches
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
57
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 def __getitem__(self, key):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if key is a basestring, return the node of that name;
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 if key is a 2-tuple/list, return the edge of that name
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
15
ee45f44394a0 i need a name, Bastian
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
64 if isinstance(key, basestring) or (not isiterable(key)):
8
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
65 return self.node(key)
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
66 else:
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
67 return self.edge(*key)
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
68
31
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
69 def __setitem__(self, key, value):
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
70 if isinstance(key, basestring) or (not isiterable(key)):
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
71 self.node(key, value)
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
72 else:
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
73 key1, key2 = key
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
74 self.edge(key1, key2, value)
5f14a4183bf2 fix things
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
75
8
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
76 def __contains__(self, key):
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
77 """
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
78 if key is ..., returns if that node is in the graph
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
79 if key is a 2-tuple/list, returns if the edge is in the graph
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
80 """
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
81 # XXX not necessarily the best implementation!
15
ee45f44394a0 i need a name, Bastian
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
82 if isinstance(key, basestring) or (not isiterable(key)):
8
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
83 return key in self.nodes()
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
84 else:
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
85 return tuple(key) in self.edges()
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86
15
ee45f44394a0 i need a name, Bastian
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
87
25
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
88 class MemoryCache(Graph):
15
ee45f44394a0 i need a name, Bastian
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
89 """volatile in-memory representation of a graph"""
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 def __init__(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 self._edges = {}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 self._nodes = {}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 def node(self, name, **values):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 if values:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 # setter
9
0affca1f4dc0 start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
98 self._nodes[name] = deepcopy(values)
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 else:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 # getter
9
0affca1f4dc0 start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
101 # TODO: deepcopy
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 return self._nodes.get(name, None)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104 def nodes(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 return self._nodes.keys()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107 def edge(self, node1, node2, **values):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 if values:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109 # setter
9
0affca1f4dc0 start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
110 self._edges[(node1, node2)] = deepcopy(values)
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 else:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 # getter
9
0affca1f4dc0 start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
113 # TODO: deepcopy
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 return self._edges.get((node1, node2), None)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
115
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 def edges(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 return self._edges.keys()
25
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
118
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
119 class FileCache(MemoryCache):
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
120 """on-disk JSON file cache"""
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
121
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
122 def __init__(self, filename):
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
123 self.filename = filename
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
124 raise NotImplementedError
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
125
d1a8c1436ded notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 23
diff changeset
126 # TODO: CLI entry point to convert from one model to another
28
4bed1424bb3f more notes to self
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
127 # def main(args=sys.argv[1:]):