Completed
Push — develop ( 827c8b...266d34 )
by Jace
03:21
created

create()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
crap 3
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__)
0 ignored issues
show
Coding Style Naming introduced by
The name log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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.

Loading history...
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
0 ignored issues
show
introduced by
Locally disabling redefined-outer-name (W0621)
Loading history...
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):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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