Bin
2025-12-16 971a2a12c03b74dd2d7d668b9dbc599f5131bcaf
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
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import logging
import os
from copy import deepcopy
 
import ujson as json
from core.utils.io import delete_dir_content, iter_files, json_load, remove_file_or_dir
 
from .base import BaseForm, BaseStorage, CloudStorage
 
logger = logging.getLogger(__name__)
 
 
class JSONStorage(BaseStorage):
 
    description = 'JSON task file'
 
    def __init__(self, **kwargs):
        super(JSONStorage, self).__init__(**kwargs)
        tasks = {}
        if os.path.exists(self.path):
            tasks = json_load(self.path, int_keys=True)
        if len(tasks) == 0:
            self.data = {}
        elif isinstance(tasks, dict):
            self.data = tasks
        elif isinstance(self.data, list):
            self.data = {int(task['id']): task for task in tasks}
        self._save()
 
    def _save(self):
        with open(self.path, mode='w', encoding='utf8') as fout:
            json.dump(self.data, fout, ensure_ascii=False)
 
    @property
    def readable_path(self):
        return self.path
 
    def get(self, id):
        return self.data.get(int(id))
 
    def set(self, id, value):
        self.data[int(id)] = value
        self._save()
 
    def __contains__(self, id):
        return id in self.data
 
    def set_many(self, ids, values):
        for id, value in zip(ids, values):
            self.data[int(id)] = value
        self._save()
 
    def ids(self):
        return self.data.keys()
 
    def max_id(self):
        return max(self.ids(), default=-1)
 
    def items(self):
        return self.data.items()
 
    def remove(self, key):
        self.data.pop(int(key), None)
        self._save()
 
    def remove_all(self, ids=None):
        if ids is None:
            self.data = {}
        else:
            [self.data.pop(i, None) for i in ids]
        self._save()
 
    def empty(self):
        return len(self.data) == 0
 
    def sync(self):
        pass
 
 
def already_exists_error(what, path):
    raise RuntimeError(
        '{path} {what} already exists. Use "--force" option to recreate it.'.format(path=path, what=what)
    )
 
 
class DirJSONsStorage(BaseStorage):
 
    description = 'Directory with JSON task files'
 
    def __init__(self, **kwargs):
        super(DirJSONsStorage, self).__init__(**kwargs)
        os.makedirs(self.path, exist_ok=True)
        self.cache = {}
 
    @property
    def readable_path(self):
        return self.path
 
    def get(self, id):
        if id in self.cache:
            return self.cache[id]
        else:
            filename = os.path.join(self.path, str(id) + '.json')
            if os.path.exists(filename):
                data = json_load(filename)
                self.cache[id] = data
                return data
 
    def __contains__(self, id):
        return id in set(self.ids())
 
    def set(self, id, value):
        filename = os.path.join(self.path, str(id) + '.json')
        with open(filename, 'w', encoding='utf8') as fout:
            json.dump(value, fout, indent=2, sort_keys=True)
        self.cache[id] = value
 
    def set_many(self, keys, values):
        self.cache.clear()
        raise NotImplementedError
 
    def ids(self):
        for f in iter_files(self.path, '.json'):
            yield int(os.path.splitext(os.path.basename(f))[0])
 
    def max_id(self):
        return max(self.ids(), default=-1)
 
    def sync(self):
        pass
 
    def items(self):
        for id in self.ids():
            filename = os.path.join(self.path, str(id) + '.json')
            yield id, self.cache[id] if id in self.cache else json_load(filename)
 
    def remove(self, id):
        filename = os.path.join(self.path, str(id) + '.json')
        if os.path.exists(filename):
            os.remove(filename)
            self.cache.pop(id, None)
 
    def remove_all(self, ids=None):
        if ids is None:
            self.cache.clear()
            delete_dir_content(self.path)
        else:
            for i in ids:
                self.cache.pop(i, None)
                path = os.path.join(self.path, str(i) + '.json')
                try:
                    remove_file_or_dir(path)
                except OSError:
                    logger.warning('Storage file already removed: ' + path)
 
    def empty(self):
        return next(self.ids(), None) is None
 
 
class TasksJSONStorage(JSONStorage):
 
    form = BaseForm
    description = 'Local [loading tasks from "tasks.json" file]'
 
    def __init__(self, path, project_path, **kwargs):
        super(TasksJSONStorage, self).__init__(
            project_path=project_path, path=os.path.join(project_path, 'tasks.json')
        )
 
 
class ExternalTasksJSONStorage(CloudStorage):
 
    form = BaseForm
    description = 'Local [loading tasks from "tasks.json" file]'
 
    def __init__(self, name, path, project_path, prefix=None, create_local_copy=False, regex='.*', **kwargs):
        super(ExternalTasksJSONStorage, self).__init__(
            name=name,
            project_path=project_path,
            path=os.path.join(project_path, 'tasks.json'),
            use_blob_urls=False,
            prefix=None,
            regex=None,
            create_local_copy=False,
            sync_in_thread=False,
            **kwargs,
        )
        # data is used as a local cache for tasks.json file
        self.data = {}
 
    def _save(self):
        with open(self.path, mode='w', encoding='utf8') as fout:
            json.dump(self.data, fout, ensure_ascii=False)
 
    def _get_client(self):
        pass
 
    def validate_connection(self):
        pass
 
    @property
    def url_prefix(self):
        return ''
 
    @property
    def readable_path(self):
        return self.path
 
    def _get_value(self, key, inplace=False):
        return self.data[int(key)] if inplace else deepcopy(self.data[int(key)])
 
    def _set_value(self, key, value):
        self.data[int(key)] = value
 
    def set(self, id, value):
        with self.thread_lock:
            super(ExternalTasksJSONStorage, self).set(id, value)
            self._save()
 
    def set_many(self, ids, values):
        with self.thread_lock:
            for id, value in zip(ids, values):
                super(ExternalTasksJSONStorage, self)._pre_set(id, value)
            self._save_ids()
            self._save()
 
    def _extract_task_id(self, full_key):
        return int(full_key.split(self.key_prefix, 1)[-1])
 
    def iter_full_keys(self):
        return (self.key_prefix + key for key in self._get_objects())
 
    def _get_objects(self):
        self.data = json_load(self.path, int_keys=True)
        return (str(id) for id in self.data)
 
    def _remove_id_from_keys_map(self, id):
        full_key = self.key_prefix + str(id)
        assert id in self._ids_keys_map, 'No such task id: ' + str(id)
        assert self._ids_keys_map[id]['key'] == full_key, (self._ids_keys_map[id]['key'], full_key)
        self._selected_ids.remove(id)
        self._ids_keys_map.pop(id)
        self._keys_ids_map.pop(full_key)
 
    def remove(self, id):
        with self.thread_lock:
            id = int(id)
 
            logger.debug('Remove id=' + str(id) + ' from ids.json')
            self._remove_id_from_keys_map(id)
            self._save_ids()
 
            logger.debug('Remove id=' + str(id) + ' from tasks.json')
            self.data.pop(id, None)
            self._save()
 
    def remove_all(self, ids=None):
        with self.thread_lock:
            remove_ids = self.data if ids is None else ids
 
            logger.debug('Remove ' + str(len(remove_ids)) + ' records from ids.json')
            for id in remove_ids:
                self._remove_id_from_keys_map(id)
            self._save_ids()
 
            logger.debug('Remove all data from tasks.json')
            # remove record from tasks.json
            if ids is None:
                self.data = {}
            else:
                for id in remove_ids:
                    self.data.pop(id, None)
            self._save()
 
 
class AnnotationsDirStorage(DirJSONsStorage):
 
    form = BaseForm
    description = 'Local [annotations are in "annotations" directory]'
 
    def __init__(self, name, path, project_path, **kwargs):
        super(AnnotationsDirStorage, self).__init__(
            name=name, project_path=project_path, path=os.path.join(project_path, 'annotations')
        )