File: constraint.py

package info (click to toggle)
python-marathon 0.13.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: python: 1,969; makefile: 185; sh: 58
file content (73 lines) | stat: -rw-r--r-- 2,273 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from .base import MarathonObject


class MarathonConstraint(MarathonObject):

    """Marathon placement constraint.

    See https://mesosphere.github.io/marathon/docs/constraints.html

    :param str field: constraint operator target
    :param str operator: must be one of [UNIQUE, CLUSTER, GROUP_BY, LIKE, UNLIKE]
    :param value: [optional] if `operator` is CLUSTER, constrain tasks to servers where `field` == `value`.
    If `operator` is GROUP_BY, place at most `value` tasks per group. If `operator`
    is `LIKE` or `UNLIKE`, filter servers using regexp.
    :type value: str, int, or None
    """

    """Valid operators"""

    def __init__(self, field, operator, value=None):
        self.field = field
        self.operator = operator
        self.value = value

    def __repr__(self):
        if self.value:
            template = "MarathonConstraint::{field}:{operator}:{value}"
        else:
            template = "MarathonConstraint::{field}:{operator}"
        return template.format(**self.__dict__)

    def json_repr(self, minimal=False):
        """Construct a JSON-friendly representation of the object.

        :param bool minimal: [ignored]

        :rtype: list
        """
        if self.value:
            return [self.field, self.operator, self.value]
        else:
            return [self.field, self.operator]

    @classmethod
    def from_json(cls, obj):
        """Construct a MarathonConstraint from a parsed response.

        :param dict attributes: object attributes from parsed response

        :rtype: :class:`MarathonConstraint`
        """
        if len(obj) == 2:
            (field, operator) = obj
            return cls(field, operator)
        if len(obj) > 2:
            (field, operator, value) = obj
            return cls(field, operator, value)

    @classmethod
    def from_string(cls, constraint):
        """
        :param str constraint: The string representation of a constraint

        :rtype: :class:`MarathonConstraint`
        """
        obj = constraint.split(':')
        marathon_constraint = cls.from_json(obj)

        if marathon_constraint:
            return marathon_constraint

        raise ValueError("Invalid string format. "
                         "Expected `field:operator:value`")