summaryrefslogtreecommitdiff
path: root/redis/commands/json/commands.py
blob: 92b018c5728518c910314824254560dd2f6f1902 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from deprecated import deprecated

from redis.exceptions import DataError

from .decoders import decode_dict_keys
from .path import Path
from json import loads, JSONDecodeError
import os


class JSONCommands:
    """json commands."""

    def arrappend(self, name, path=Path.rootPath(), *args):
        """Append the objects ``args`` to the array under the
        ``path` in key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrappend
        """  # noqa
        pieces = [name, str(path)]
        for o in args:
            pieces.append(self._encode(o))
        return self.execute_command("JSON.ARRAPPEND", *pieces)

    def arrindex(self, name, path, scalar, start=0, stop=-1):
        """
        Return the index of ``scalar`` in the JSON array under ``path`` at key
        ``name``.

        The search can be limited using the optional inclusive ``start``
        and exclusive ``stop`` indices.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrindex
        """  # noqa
        return self.execute_command(
            "JSON.ARRINDEX", name, str(path), self._encode(scalar), start, stop
        )

    def arrinsert(self, name, path, index, *args):
        """Insert the objects ``args`` to the array at index ``index``
        under the ``path` in key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrinsert
        """  # noqa
        pieces = [name, str(path), index]
        for o in args:
            pieces.append(self._encode(o))
        return self.execute_command("JSON.ARRINSERT", *pieces)

    def arrlen(self, name, path=Path.rootPath()):
        """Return the length of the array JSON value under ``path``
        at key``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrlen
        """  # noqa
        return self.execute_command("JSON.ARRLEN", name, str(path))

    def arrpop(self, name, path=Path.rootPath(), index=-1):
        """Pop the element at ``index`` in the array JSON value under
        ``path`` at key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrpop
        """  # noqa
        return self.execute_command("JSON.ARRPOP", name, str(path), index)

    def arrtrim(self, name, path, start, stop):
        """Trim the array JSON value under ``path`` at key ``name`` to the
        inclusive range given by ``start`` and ``stop``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonarrtrim
        """  # noqa
        return self.execute_command("JSON.ARRTRIM", name, str(path), start, stop)

    def type(self, name, path=Path.rootPath()):
        """Get the type of the JSON value under ``path`` from key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsontype
        """  # noqa
        return self.execute_command("JSON.TYPE", name, str(path))

    def resp(self, name, path=Path.rootPath()):
        """Return the JSON value under ``path`` at key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonresp
        """  # noqa
        return self.execute_command("JSON.RESP", name, str(path))

    def objkeys(self, name, path=Path.rootPath()):
        """Return the key names in the dictionary JSON value under ``path`` at
        key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonobjkeys
        """  # noqa
        return self.execute_command("JSON.OBJKEYS", name, str(path))

    def objlen(self, name, path=Path.rootPath()):
        """Return the length of the dictionary JSON value under ``path`` at key
        ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonobjlen
        """  # noqa
        return self.execute_command("JSON.OBJLEN", name, str(path))

    def numincrby(self, name, path, number):
        """Increment the numeric (integer or floating point) JSON value under
        ``path`` at key ``name`` by the provided ``number``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonnumincrby
        """  # noqa
        return self.execute_command(
            "JSON.NUMINCRBY", name, str(path), self._encode(number)
        )

    @deprecated(version="4.0.0", reason="deprecated since redisjson 1.0.0")
    def nummultby(self, name, path, number):
        """Multiply the numeric (integer or floating point) JSON value under
        ``path`` at key ``name`` with the provided ``number``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonnummultby
        """  # noqa
        return self.execute_command(
            "JSON.NUMMULTBY", name, str(path), self._encode(number)
        )

    def clear(self, name, path=Path.rootPath()):
        """
        Empty arrays and objects (to have zero slots/keys without deleting the
        array/object).

        Return the count of cleared paths (ignoring non-array and non-objects
        paths).

        For more information: https://oss.redis.com/redisjson/commands/#jsonclear
        """  # noqa
        return self.execute_command("JSON.CLEAR", name, str(path))

    def delete(self, key, path=Path.rootPath()):
        """Delete the JSON value stored at key ``key`` under ``path``.

        For more information: https://oss.redis.com/redisjson/commands/#jsondel
        """
        return self.execute_command("JSON.DEL", key, str(path))

    # forget is an alias for delete
    forget = delete

    def get(self, name, *args, no_escape=False):
        """
        Get the object stored as a JSON value at key ``name``.

        ``args`` is zero or more paths, and defaults to root path
        ```no_escape`` is a boolean flag to add no_escape option to get
        non-ascii characters

        For more information: https://oss.redis.com/redisjson/commands/#jsonget
        """  # noqa
        pieces = [name]
        if no_escape:
            pieces.append("noescape")

        if len(args) == 0:
            pieces.append(Path.rootPath())

        else:
            for p in args:
                pieces.append(str(p))

        # Handle case where key doesn't exist. The JSONDecoder would raise a
        # TypeError exception since it can't decode None
        try:
            return self.execute_command("JSON.GET", *pieces)
        except TypeError:
            return None

    def mget(self, keys, path):
        """
        Get the objects stored as a JSON values under ``path``. ``keys``
        is a list of one or more keys.

        For more information: https://oss.redis.com/redisjson/commands/#jsonmget
        """  # noqa
        pieces = []
        pieces += keys
        pieces.append(str(path))
        return self.execute_command("JSON.MGET", *pieces)

    def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
        """
        Set the JSON value at key ``name`` under the ``path`` to ``obj``.

        ``nx`` if set to True, set ``value`` only if it does not exist.
        ``xx`` if set to True, set ``value`` only if it exists.
        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        For the purpose of using this within a pipeline, this command is also
        aliased to jsonset.

        For more information: https://oss.redis.com/redisjson/commands/#jsonset
        """
        if decode_keys:
            obj = decode_dict_keys(obj)

        pieces = [name, str(path), self._encode(obj)]

        # Handle existential modifiers
        if nx and xx:
            raise Exception(
                "nx and xx are mutually exclusive: use one, the "
                "other or neither - but not both"
            )
        elif nx:
            pieces.append("NX")
        elif xx:
            pieces.append("XX")
        return self.execute_command("JSON.SET", *pieces)

    def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False):
        """
        Set the JSON value at key ``name`` under the ``path`` to the contents of the json file ``file_name``.

        ``nx`` if set to True, set ``value`` only if it does not exist.
        ``xx`` if set to True, set ``value`` only if it exists.
        ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
        with utf-8.

        """
        try:
            file_content = loads(file_name)
        except JSONDecodeError:
            raise JSONDecodeError("Inappropriate file type, set_file() requires json file")
        
        return self.set(name, path, file_content, nx, xx, decode_keys)


    def set_path(self, json_path, root_directory , nx=False, xx=False, decode_keys=False):
        """ 

        """
        set_files_result = {}
        for root, dirs, files in os.walk(root_directory):
            for file in files:
                try:
                    file_name = file.rsplit('.')[0]
                    file_path = os.path.join(root, file)
                    self.set_file(file_name, json_path, file_path, nx, xx, decode_keys)
                    set_files_result[os.path.join(root, file)] = True
                except JSONDecodeError:
                    set_files_result[os.path.join(root, file)] = False

        return set_files_result



    def strlen(self, name, path=None):
        """Return the length of the string JSON value under ``path`` at key
        ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsonstrlen
        """  # noqa
        pieces = [name]
        if path is not None:
            pieces.append(str(path))
        return self.execute_command("JSON.STRLEN", *pieces)

    def toggle(self, name, path=Path.rootPath()):
        """Toggle boolean value under ``path`` at key ``name``.
        returning the new value.

        For more information: https://oss.redis.com/redisjson/commands/#jsontoggle
        """  # noqa
        return self.execute_command("JSON.TOGGLE", name, str(path))

    def strappend(self, name, value, path=Path.rootPath()):
        """Append to the string JSON value. If two options are specified after
        the key name, the path is determined to be the first. If a single
        option is passed, then the rootpath (i.e Path.rootPath()) is used.

        For more information: https://oss.redis.com/redisjson/commands/#jsonstrappend
        """  # noqa
        pieces = [name, str(path), self._encode(value)]
        return self.execute_command("JSON.STRAPPEND", *pieces)

    def debug(self, subcommand, key=None, path=Path.rootPath()):
        """Return the memory usage in bytes of a value under ``path`` from
        key ``name``.

        For more information: https://oss.redis.com/redisjson/commands/#jsondebg
        """  # noqa
        valid_subcommands = ["MEMORY", "HELP"]
        if subcommand not in valid_subcommands:
            raise DataError("The only valid subcommands are ", str(valid_subcommands))
        pieces = [subcommand]
        if subcommand == "MEMORY":
            if key is None:
                raise DataError("No key specified")
            pieces.append(key)
            pieces.append(str(path))
        return self.execute_command("JSON.DEBUG", *pieces)

    @deprecated(
        version="4.0.0", reason="redisjson-py supported this, call get directly."
    )
    def jsonget(self, *args, **kwargs):
        return self.get(*args, **kwargs)

    @deprecated(
        version="4.0.0", reason="redisjson-py supported this, call get directly."
    )
    def jsonmget(self, *args, **kwargs):
        return self.mget(*args, **kwargs)

    @deprecated(
        version="4.0.0", reason="redisjson-py supported this, call get directly."
    )
    def jsonset(self, *args, **kwargs):
        return self.set(*args, **kwargs)