Passed
Pull Request — master (#48)
by Matěj
03:33
created

nextcloud.base.OCSMRD._get_error()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
import enum
3
4
5
class WithRequester(object):
6
7
    API_URL = NotImplementedError
8
9
    def __init__(self, requester):
10
        self._requester = requester
11
12
    @property
13
    def requester(self):
14
        """ Get requester instance """
15
        # dynamically set API_URL for requester
16
        self._requester.API_URL = self.API_URL
17
        self._requester.SUCCESS_CODE = getattr(self, 'SUCCESS_CODE', None)
18
        return self._requester
19
20
21
class NextcloudError(RuntimeError):
22
    def __init__(self, msg, code=None):
23
        super().__init__(msg)
24
        self.code = code
25
26
27
class MapResultData:
28
    def __init__(self, where=None, what=None):
29
        """
30
        Args:
31
            where: To which attribute to map the result
32
            what: How to get the result
33
        """
34
        self.where = where
35
        self.what = what
36
37
    def _get_error(self, result):
38
        raise NotImplementedError()
39
40
    def _map_result(self, result):
41
        raise NotImplementedError()
42
43
    def __call__(self, wrapped):
44
        def wrapper(* args, ** kwargs):
45
            res = wrapped(* args, ** kwargs)
46
            if res.is_ok:
47
                if self.where:
48
                    self._map_result(res)
49
            else:
50
                raise self._get_error(res)
51
            return res
52
            wrapped.__doc__ = self._update_doc(wrapped.__doc__)
53
        return wrapper
54
55
    def _update_doc(self, original_doc):
56
        return original_doc
57
58
59
class WebDavMRD(MapResultData):
60
    def _get_error(self, result):
61
        exc = NextcloudError(result.raw.reason, result.raw.status_code)
62
        return exc
63
64
65
class OCSMRD(MapResultData):
66
    def _map_result(self, result):
67
        if self.what:
68
            setattr(result, self.where, result.data[self.what])
69
        else:
70
            setattr(result, self.where, result.data)
71
72
    def _result_doc_string(self):
73
        ret = ""
74
        if self.what:
75
            ret = "The result is available as .data[XXX] key, or the .YYY attribute"
76
        return ret
77
78
    def _update_doc(self, original_doc):
79
        if ":return:" in original_doc:
80
            doc = original_doc + "\n" + self._result_doc_string()
81
        return doc
0 ignored issues
show
introduced by
The variable doc does not seem to be defined in case ":return:" in original_doc on line 79 is False. Are you sure this can never be the case?
Loading history...
82
83
    def _get_error(self, result):
84
        exc = NextcloudError(result.meta['message'], result.meta['statuscode'])
85
        return exc
86
87
88
class OCSCode(enum.IntEnum):
89
    OK = 100
90
    SERVER_ERROR = 996
91
    NOT_AUTHORIZED = 997
92
    NOT_FOUND = 998
93
    UNKNOWN_ERROR = 999
94
95
96
class ShareType(enum.IntEnum):
97
    USER = 0
98
    GROUP = 1
99
    PUBLIC_LINK = 3
100
    FEDERATED_CLOUD_SHARE = 6
101
102
103
class Permission(enum.IntEnum):
104
    """ Permission for Share have to be sum of selected permissions """
105
    READ = 1
106
    UPDATE = 2
107
    CREATE = 4
108
    DELETE = 8
109
    SHARE = 16
110
    ALL = 31
111
112
113
QUOTA_UNLIMITED = -3
114
115
116
def datetime_to_expire_date(date):
117
    return date.strftime("%Y-%m-%d")
118