|
1
|
|
|
"""Functions to interact with mapped classes and instances.""" |
|
2
|
|
|
|
|
3
|
1 |
|
import inspect |
|
4
|
1 |
|
import logging |
|
5
|
|
|
import string |
|
6
|
1 |
|
import glob |
|
7
|
|
|
import types |
|
8
|
1 |
|
|
|
9
|
|
|
from . import common, exceptions |
|
10
|
|
|
|
|
11
|
1 |
|
log = logging.getLogger(__name__) |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def create(class_or_instance, *args, overwrite=False, **kwargs): |
|
15
|
|
|
"""Create a new mapped object. |
|
16
|
|
|
|
|
17
|
1 |
|
NOTE: Calling this function is unnecessary with 'auto_create' enabled. |
|
18
|
1 |
|
|
|
19
|
|
|
""" |
|
20
|
1 |
|
instance = _instantiate(class_or_instance, *args, **kwargs) |
|
21
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
|
22
|
1 |
|
|
|
23
|
|
|
if mapper.exists and not overwrite: |
|
24
|
1 |
|
msg = "{!r} already exists".format(mapper.path) |
|
25
|
|
|
raise exceptions.DuplicateMappingError(msg) |
|
26
|
|
|
|
|
27
|
1 |
|
return load(save(instance)) |
|
28
|
|
|
|
|
29
|
1 |
|
|
|
30
|
1 |
|
def find(class_or_instance, *args, create=False, **kwargs): # pylint: disable=redefined-outer-name |
|
|
|
|
|
|
31
|
|
|
"""Find a matching mapped object or return None.""" |
|
32
|
1 |
|
instance = _instantiate(class_or_instance, *args, **kwargs) |
|
33
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
|
34
|
1 |
|
|
|
35
|
1 |
|
if mapper.exists: |
|
36
|
|
|
return instance |
|
37
|
1 |
|
elif create: |
|
38
|
|
|
return save(instance) |
|
39
|
|
|
else: |
|
40
|
1 |
|
return None |
|
41
|
|
|
|
|
42
|
1 |
|
|
|
43
|
1 |
|
class GlobFormatter(string.Formatter): |
|
44
|
|
|
""" |
|
45
|
|
|
Uses '*' for all unknown fields |
|
46
|
1 |
|
""" |
|
47
|
|
|
|
|
48
|
|
|
WILDCARD = object() |
|
49
|
|
|
|
|
50
|
|
|
def get_value(self, key, args, kwargs): |
|
51
|
|
|
try: |
|
52
|
|
|
return super().get_value(key, args, kwargs) |
|
53
|
1 |
|
except (KeyError, IndexError): |
|
54
|
|
|
return self.WILDCARD |
|
55
|
1 |
|
|
|
56
|
|
|
def convert_field(self, value, conversion): |
|
57
|
1 |
|
if value is self.WILDCARD: |
|
58
|
|
|
return self.WILDCARD |
|
59
|
|
|
else: |
|
60
|
1 |
|
return super().convert_field(value, conversion) |
|
61
|
|
|
|
|
62
|
|
|
def format_field(self, value, format_spec): |
|
63
|
|
|
if value is self.WILDCARD: |
|
64
|
|
|
return '*' |
|
65
|
|
|
else: |
|
66
|
1 |
|
return super().format_field(value, format_spec) |
|
67
|
|
|
|
|
68
|
1 |
|
|
|
69
|
1 |
|
def match(cls, **kwargs): |
|
70
|
1 |
|
"""Yield all matching mapped objects.""" |
|
71
|
|
|
log.debug((cls, kwargs)) |
|
72
|
1 |
|
gf = GlobFormatter() |
|
|
|
|
|
|
73
|
1 |
|
mock = types.SimpleNamespace(**kwargs) |
|
74
|
|
|
|
|
75
|
1 |
|
pattern = gf.format(..., self=mock) # FIXME: Get the path_format given the class |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
for filename in glob.iglob(pattern, recursive=False): |
|
|
|
|
|
|
78
|
|
|
pathfields = {...: ...} # FIXME: Extract the fields from the path (pypi:parse) |
|
|
|
|
|
|
79
|
|
|
inst = cls(...) # FIXME: Thaw class without invoking a bunch of stuff |
|
|
|
|
|
|
80
|
1 |
|
common.sync_object(inst, filename) |
|
|
|
|
|
|
81
|
|
|
yield inst |
|
82
|
1 |
|
|
|
83
|
|
|
|
|
84
|
1 |
|
def load(instance): |
|
85
|
|
|
"""Force the loading of a mapped object's file. |
|
86
|
1 |
|
|
|
87
|
|
|
NOTE: Calling this function is unnecessary. It exists for the |
|
88
|
|
|
aesthetic purpose of having symmetry between save and load. |
|
89
|
1 |
|
|
|
90
|
1 |
|
""" |
|
91
|
1 |
|
mapper = common.get_mapper(instance, expected=True) |
|
92
|
|
|
|
|
93
|
1 |
|
mapper.load() |
|
94
|
1 |
|
|
|
95
|
|
|
return instance |
|
96
|
1 |
|
|
|
97
|
|
|
|
|
98
|
|
|
def save(instance): |
|
99
|
|
|
"""Save a mapped object to file. |
|
100
|
|
|
|
|
101
|
|
|
NOTE: Calling this function is unnecessary with 'auto_save' enabled. |
|
102
|
|
|
|
|
103
|
|
|
""" |
|
104
|
|
|
mapper = common.get_mapper(instance, expected=True) |
|
105
|
|
|
|
|
106
|
|
|
if mapper.deleted: |
|
107
|
|
|
msg = "{!r} was deleted".format(mapper.path) |
|
108
|
|
|
raise exceptions.DeletedFileError(msg) |
|
109
|
|
|
|
|
110
|
|
|
if not mapper.exists: |
|
111
|
|
|
mapper.create() |
|
112
|
|
|
|
|
113
|
|
|
mapper.save() |
|
114
|
|
|
|
|
115
|
|
|
return instance |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
def delete(instance): |
|
119
|
|
|
"""Delete a mapped object's file.""" |
|
120
|
|
|
mapper = common.get_mapper(instance, expected=True) |
|
121
|
|
|
|
|
122
|
|
|
mapper.delete() |
|
123
|
|
|
|
|
124
|
|
|
return None |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def _instantiate(class_or_instance, *args, **kwargs): |
|
|
|
|
|
|
128
|
|
|
if inspect.isclass(class_or_instance): |
|
129
|
|
|
instance = class_or_instance(*args, **kwargs) |
|
130
|
|
|
else: |
|
131
|
|
|
assert not args |
|
132
|
|
|
instance = class_or_instance |
|
133
|
|
|
|
|
134
|
|
|
return instance |
|
135
|
|
|
|
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.