|
1
|
|
|
"""Functions to interact with mapped classes and instances.""" |
|
2
|
|
|
|
|
3
|
1 |
|
from . import common, exceptions |
|
4
|
|
|
|
|
5
|
1 |
|
log = common.logger(__name__) |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
1 |
|
def create(cls, *args): |
|
9
|
|
|
"""Create a new mapped object.""" |
|
10
|
1 |
|
instance = cls(*args) |
|
11
|
1 |
|
mapper = _ensure_mapped(instance) |
|
12
|
|
|
|
|
13
|
1 |
|
if mapper.auto_create: |
|
14
|
|
|
msg = "'create' is called automatically with 'auto_create' enabled" |
|
15
|
|
|
log.warning(msg) |
|
16
|
|
|
|
|
17
|
1 |
|
if mapper.exists: |
|
18
|
1 |
|
msg = "{!r} already exists".format(mapper.path) |
|
19
|
1 |
|
raise exceptions.DuplicateMappingError(msg) |
|
20
|
|
|
|
|
21
|
1 |
|
return save(instance) |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
1 |
|
def find(cls, *args, create=False): # pylint: disable=redefined-outer-name |
|
25
|
|
|
"""Find a matching mapped object or return None.""" |
|
26
|
1 |
|
instance = cls(*args) |
|
27
|
1 |
|
mapper = _ensure_mapped(instance) |
|
28
|
|
|
|
|
29
|
1 |
|
if mapper.exists: |
|
30
|
1 |
|
return instance |
|
31
|
1 |
|
elif create: |
|
32
|
1 |
|
return save(instance) |
|
33
|
|
|
else: |
|
34
|
1 |
|
return None |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
1 |
|
def find_all(cls, **kwargs): |
|
38
|
|
|
"""Return a list of all matching mapped objects.""" |
|
39
|
1 |
|
log.debug((cls, kwargs)) |
|
40
|
1 |
|
raise NotImplementedError |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
1 |
|
def load(instance): |
|
44
|
|
|
"""Force the loading of a mapped object's file.""" |
|
45
|
1 |
|
mapper = _ensure_mapped(instance) |
|
46
|
|
|
|
|
47
|
1 |
|
log.warning("'load' is called automatically") |
|
48
|
|
|
|
|
49
|
1 |
|
mapper.load() |
|
50
|
|
|
|
|
51
|
1 |
|
return instance |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
def save(instance): |
|
55
|
|
|
"""Save a mapped object to file.""" |
|
56
|
1 |
|
mapper = _ensure_mapped(instance) |
|
57
|
|
|
|
|
58
|
1 |
|
if mapper.auto_save: |
|
59
|
1 |
|
log.warning("'save' is called automatically with 'auto_save' enabled") |
|
60
|
|
|
|
|
61
|
1 |
|
if mapper.deleted: |
|
62
|
1 |
|
msg = "{!r} was deleted".format(mapper.path) |
|
63
|
1 |
|
raise exceptions.DeletedFileError(msg) |
|
64
|
|
|
|
|
65
|
1 |
|
if not mapper.exists: |
|
66
|
1 |
|
mapper.create() |
|
67
|
|
|
|
|
68
|
1 |
|
mapper.save() |
|
69
|
|
|
|
|
70
|
1 |
|
return instance |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
1 |
|
def delete(instance): |
|
74
|
|
|
"""Delete a mapped object's file.""" |
|
75
|
1 |
|
mapper = _ensure_mapped(instance) |
|
76
|
|
|
|
|
77
|
1 |
|
mapper.delete() |
|
78
|
|
|
|
|
79
|
1 |
|
return None |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
1 |
|
def _ensure_mapped(obj, *, expected=True): |
|
83
|
1 |
|
mapper = common.get_mapper(obj) |
|
84
|
|
|
|
|
85
|
1 |
|
if mapper and not expected: |
|
86
|
1 |
|
msg = "{!r} is already mapped".format(obj) |
|
87
|
1 |
|
raise TypeError(msg) |
|
88
|
|
|
|
|
89
|
1 |
|
if not mapper and expected: |
|
90
|
1 |
|
msg = "{!r} is not mapped".format(obj) |
|
91
|
1 |
|
raise TypeError(msg) |
|
92
|
|
|
|
|
93
|
|
|
return mapper |
|
94
|
|
|
|