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
|
"""
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# standard imports
import json
# exports
__all__ = ('Frame', )
## code ##
class Frame(dict):
def __init__(self, cursor=None, selection=None, offset=0, **kwargs):
super(Frame, self).__init__(**kwargs)
selection = selection if selection is not None else []
self['cursor'] = cursor
self['selection'] = selection
self['offset'] = offset
@property
def selection(self):
return self['selection']
@property
def cursor(self):
return self['cursor']
@property
def offset(self):
return self['offset']
def copy(self):
return Frame(**super(Frame, self).copy())
def serialize(self):
return json.dumps({
'cursor': self.cursor.guid if self.cursor is not None else 'None',
'group': self.cursor.group if hasattr(self.cursor, 'group') else 'None',
'selection': [img.guid for img in self.selection],
'offset': self.offset
})
@staticmethod
def from_serialized(lib, serialized, ignore_errors=True):
d = json.loads(serialized)
# load cursor
cursor = None
try:
if 'cursor' in d and d['cursor'] is not None and d['cursor'].lower() != 'none':
cursor = lib.entity(d['cursor'])
except KeyError as err:
if not ignore_errors:
raise err
if 'group' in d and d['group'] is not None and d['group'].lower() != 'none':
try:
# FIXME: late import; breaks module dependency structure
from tagit.storage.library.entity import Representative
cursor = Representative.Representative(lib, d['group'])
except ValueError:
# group doesn't exist anymore; ignore
pass
# load selection
selection = []
for guid in d.get('selection', []):
try:
selection.append(lib.entity(guid))
except KeyError as err:
if not ignore_errors:
raise err
return Frame(
cursor = cursor,
selection = selection,
offset = d.get('offset', 0)
)
## EOF ##
|