1
|
|
|
"""Functions to interact with mapped classes and instances.""" |
2
|
|
|
|
3
|
1 |
|
import inspect |
4
|
1 |
|
import logging |
5
|
|
|
|
6
|
1 |
|
from . import common, exceptions |
7
|
|
|
|
8
|
1 |
|
log = logging.getLogger(__name__) |
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
def create(class_or_instance, *args, overwrite=False): |
12
|
|
|
"""Create a new mapped object. |
13
|
|
|
|
14
|
|
|
NOTE: Calling this function is unnecessary with 'auto_create' enabled. |
15
|
|
|
|
16
|
|
|
""" |
17
|
1 |
|
instance = _instantiate(class_or_instance, *args) |
18
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
19
|
|
|
|
20
|
1 |
|
if mapper.exists and not overwrite: |
21
|
1 |
|
msg = "{!r} already exists".format(mapper.path) |
22
|
1 |
|
raise exceptions.DuplicateMappingError(msg) |
23
|
|
|
|
24
|
1 |
|
return save(instance) |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
def find(class_or_instance, *args, create=False): # pylint: disable=redefined-outer-name |
|
|
|
|
28
|
|
|
"""Find a matching mapped object or return None.""" |
29
|
1 |
|
instance = _instantiate(class_or_instance, *args) |
30
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
31
|
|
|
|
32
|
1 |
|
if mapper.exists: |
33
|
1 |
|
return instance |
34
|
1 |
|
elif create: |
35
|
1 |
|
return save(instance) |
36
|
|
|
else: |
37
|
1 |
|
return None |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
def match(cls, **kwargs): |
41
|
|
|
"""Yield all matching mapped objects.""" |
42
|
1 |
|
log.debug((cls, kwargs)) |
43
|
1 |
|
raise NotImplementedError |
44
|
|
|
|
45
|
|
|
|
46
|
1 |
|
def load(instance): |
47
|
|
|
"""Force the loading of a mapped object's file. |
48
|
|
|
|
49
|
|
|
NOTE: Calling this function is unnecessary. It exists for the |
50
|
|
|
aesthetic purpose of having symmetry between save and load. |
51
|
|
|
|
52
|
|
|
""" |
53
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
54
|
|
|
|
55
|
1 |
|
mapper.load() |
56
|
|
|
|
57
|
1 |
|
return instance |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
def save(instance): |
61
|
|
|
"""Save a mapped object to file. |
62
|
|
|
|
63
|
|
|
NOTE: Calling this function is unnecessary with 'auto_save' enabled. |
64
|
|
|
|
65
|
|
|
""" |
66
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
67
|
|
|
|
68
|
1 |
|
if mapper.deleted: |
69
|
1 |
|
msg = "{!r} was deleted".format(mapper.path) |
70
|
1 |
|
raise exceptions.DeletedFileError(msg) |
71
|
|
|
|
72
|
1 |
|
if not mapper.exists: |
73
|
1 |
|
mapper.create() |
74
|
|
|
|
75
|
1 |
|
mapper.save() |
76
|
|
|
|
77
|
1 |
|
return instance |
78
|
|
|
|
79
|
|
|
|
80
|
1 |
|
def delete(instance): |
81
|
|
|
"""Delete a mapped object's file.""" |
82
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
83
|
|
|
|
84
|
1 |
|
mapper.delete() |
85
|
|
|
|
86
|
1 |
|
return None |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
def _instantiate(class_or_instance, *args): |
|
|
|
|
90
|
1 |
|
if inspect.isclass(class_or_instance): |
91
|
1 |
|
instance = class_or_instance(*args) |
92
|
|
|
else: |
93
|
1 |
|
assert not args |
94
|
1 |
|
instance = class_or_instance |
95
|
|
|
|
96
|
|
|
return instance |
97
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.