Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
proxy.py
Go to the documentation of this file.
1 # Copyright (C) 2013-2016 Roland Lutz
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 ## \namespace xorn.proxy
18 ## High-level proxy classes for the storage backend.
19 
20 import xorn.storage
21 
22 ## High-level revision proxy class.
23 
25  def __init__(self, rev):
26  if not isinstance(rev, xorn.storage.Revision):
27  raise ValueError
28  self.rev = rev
29 
30  def __eq__(self, other):
31  return isinstance(other, RevisionProxy) and self.rev == other.rev
32 
33  def __ne__(self, other):
34  return not isinstance(other, RevisionProxy) or self.rev != other.rev
35 
36  def __hash__(self):
37  return hash(self.rev)
38 
39  def toplevel_objects(self):
40  return [ObjectProxy(self.rev, ob)
41  for ob in xorn.storage.get_objects_attached_to(self.rev, None)]
42 
43  def all_objects(self):
44  return [ObjectProxy(self.rev, ob) for ob in self.rev.get_objects()]
45 
46  def selected_objects(self, sel):
47  return [ObjectProxy(self.rev, ob)
48  for ob in xorn.storage.get_selected_objects(self.rev, sel)]
49 
50  def add_object(self, data):
51  return ObjectProxy(self.rev, self.rev.add_object(data))
52 
53  def copy_object(self, ob):
54  return ObjectProxy(self.rev, self.rev.copy_object(ob.rev, ob.ob))
55 
56  def copy_objects(self, rev, sel):
57  if isinstance(rev, RevisionProxy):
58  rev = rev.rev
59  return self.rev.copy_objects(rev, sel)
60 
61  def delete_object(self, ob):
62  if isinstance(ob, ObjectProxy):
63  ob = ob.ob
64  self.rev.delete_object(ob)
65 
66  def delete_objects(self, sel):
67  self.rev.delete_objects(sel)
68 
69  def is_transient(self):
70  return self.rev.is_transient()
71 
72  def finalize(self):
73  self.rev.finalize()
74 
75 ## High-level object proxy class.
76 
78  def __init__(self, rev, ob):
79  if not isinstance(rev, xorn.storage.Revision):
80  raise ValueError
81  if not isinstance(ob, xorn.storage.Object):
82  raise ValueError
83  self.rev = rev
84  self.ob = ob
85 
86  def __eq__(self, other):
87  return isinstance(other, ObjectProxy) and self.rev == other.rev \
88  and self.ob == other.ob
89 
90  def __ne__(self, other):
91  return not isinstance(other, ObjectProxy) or self.rev != other.rev \
92  or self.ob != other.ob
93 
94  def __hash__(self):
95  return hash((self.rev, self.ob))
96 
97  def exists(self):
98  return self.rev.object_exists(self.ob)
99 
100  def data(self):
101  return self.rev.get_object_data(self.ob)
102 
103  def set_data(self, data):
104  self.rev.set_object_data(self.ob, data)
105 
106  def location(self):
107  attached_to, pos = self.rev.get_object_location(self.ob)
108  if attached_to is None:
109  return None, pos
110  return ObjectProxy(self.rev, attached_to), pos
111 
112  def attached_objects(self):
113  return [ObjectProxy(self.rev, ob) for ob in
115 
116  def relocate(self, attach_to, insert_before):
117  if isinstance(attach_to, ObjectProxy):
118  attach_to = attach_to.ob
119  if isinstance(insert_before, ObjectProxy):
120  insert_before = insert_before.ob
121  self.rev.relocate_object(self.ob, attach_to, insert_before)
122 
123  def is_selected(self, sel):
124  return xorn.storage.object_is_selected(self.rev, sel, self.ob)
125 
126  def __getattr__(self, name):
127  if name.startswith('_'):
128  raise AttributeError
129  if 'rev' not in self.__dict__ or 'ob' not in self.__dict__:
130  raise AttributeError
131  if name in ['line', 'fill']:
132  return AttributeProxy(self.rev, self.ob, name)
133  return self.data().__getattribute__(name)
134 
135  def __setattr__(self, name, value):
136  if 'rev' not in self.__dict__ or 'ob' not in self.__dict__:
137  self.__dict__[name] = value
138  return
139  data = self.data()
140  try:
141  data.__setattr__(name, value)
142  except AttributeError:
143  self.__dict__[name] = value
144  else:
145  self.rev.set_object_data(self.ob, data)
146 
147 ## High-level line/fill attribute proxy class.
148 
150  def __init__(self, rev, ob, name):
151  if not isinstance(rev, xorn.storage.Revision):
152  raise ValueError
153  if not isinstance(ob, xorn.storage.Object):
154  raise ValueError
155  if not isinstance(name, str):
156  raise ValueError
157  self.rev = rev
158  self.ob = ob
159  self.name = name
160 
161  def exists(self):
162  try:
163  ob_data = self.rev.get_object_data(self.ob)
164  except KeyError:
165  return False
166  try:
167  ob_data.__getattribute__(self.name)
168  except AttributeError:
169  return False
170  return True
171 
172  def data(self):
173  return self.rev.get_object_data(self.ob).__getattribute__(self.name)
174 
175  def __getattr__(self, name):
176  if name.startswith('_'):
177  raise AttributeError
178  if 'rev' not in self.__dict__ or 'ob' not in self.__dict__ \
179  or 'name' not in self.__dict__:
180  raise AttributeError
181  return self.data().__getattribute__(name)
182 
183  def __setattr__(self, name, value):
184  if 'rev' not in self.__dict__ or 'ob' not in self.__dict__ \
185  or 'name' not in self.__dict__:
186  self.__dict__[name] = value
187  return
188  data0 = self.rev.get_object_data(self.ob)
189  data1 = data0.__getattribute__(self.name)
190  try:
191  data1.__setattr__(name, value)
192  except AttributeError:
193  self.__dict__[name] = value
194  else:
195  self.rev.set_object_data(self.ob, data0)
def object_is_selected
Return whether an object exists in a revision and is selected in a selection.
Definition: storage.py:466
High-level revision proxy class.
Definition: proxy.py:24
High-level object proxy class.
Definition: proxy.py:77
def get_objects_attached_to
Return a list of objects in a revision which are attached to a certain object.
Definition: storage.py:333
High-level line/fill attribute proxy class.
Definition: proxy.py:149
A particular state of the contents of a file.
Definition: storage.py:35
The identity of an object across revisions.
Definition: storage.py:302
Xorn storage backend.
Definition: storage.py:1
def get_selected_objects
Return a list of objects which are in a revision as well as in a selection.
Definition: storage.py:343