abc.py 2.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright 2016 ZEROFAIL
#
# This file is part of Goblin.
#
# Goblin is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Goblin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Goblin.  If not, see <http://www.gnu.org/licenses/>.

18
import abc
19
20
21
22
23
24
import logging

from goblin import cardinality, manager, exception


logger = logging.getLogger(__name__)
25
26
27


class DataType(abc.ABC):
28
29
30
31
    """
    Abstract base class for Goblin Data Types. All custom data types should
    inherit from :py:class:`DataType`.
    """
32
33
    def __init__(self, val=None):
        self._val = val
34
35

    @abc.abstractmethod
36
    def validate(self, val):
37
        """Validate property value"""
38
        return val
39
40

    @abc.abstractmethod
41
    def to_db(self, val=None):
davebshow's avatar
davebshow committed
42
43
44
45
        """
        Convert property value to db compatible format. If no value passed, try
        to use default bound value
        """
46
        if val is None:
47
            val = self._val
48
49
50
51
        return val

    @abc.abstractmethod
    def to_ogm(self, val):
52
        """Convert property value to a Python compatible format"""
53
54
        return val

55
56
57
58
59
60
61
62
    def validate_vertex_prop(self, val, card, vertex_prop, data_type):
        if card == cardinality.Cardinality.list:
            if isinstance(val, list):
                val = val
            elif isinstance(val, (set, tuple)):
                val = list(val)
            else:
                val = [val]
63
64
65
66
67
            vertex_props = []
            for v in val:
                vp = vertex_prop(data_type, card=card)
                vp.value = self.validate(v)
                vertex_props.append(vp)
68
            val = manager.ListVertexPropertyManager(
69
                data_type, vertex_prop, card, vertex_props)
70
71
72
73
74
75
76
        elif card == cardinality.Cardinality.set:
            if isinstance(val, set):
                val = val
            elif isinstance(val, (list, tuple)):
                val = set(val)
            else:
                val = set([val])
77
78
79
80
81
            vertex_props = set([])
            for v in val:
                vp = vertex_prop(data_type, card=card)
                vp.value = self.validate(v)
                vertex_props.add(vp)
82
            val = manager.SetVertexPropertyManager(
83
                data_type, vertex_prop, card, vertex_props)
84
        else:
85
86
87
            vp = vertex_prop(data_type)
            vp.value = self.validate(val)
            val = vp
88
89
        return val

90
91

class BaseProperty:
92
    """Abstract base class that implements the property interface"""
93
94
95
    @property
    def data_type(self):
        raise NotImplementedError