Skip to content

Commit

Permalink
Removed dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Manovich committed Feb 12, 2019
1 parent f4c2330 commit 98bb2f9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 526 deletions.
4 changes: 0 additions & 4 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ def update(self, instance, validated_data):
db_attr.values = attr.get('values', db_attr.values)
db_attr.save()





return instance


Expand Down
253 changes: 0 additions & 253 deletions cvat/apps/engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,113 +34,13 @@

############################# Low Level server API

@transaction.atomic
def create_empty(params):
"""Create empty directory structure for a new task, add it to DB."""

db_task = models.Task()

db_task.name = params['task_name']
db_task.bug_tracker = params['bug_tracker_link']
db_task.size = 0
db_task.owner = params['owner']
db_task.save()

task_path = db_task.get_task_dirname()
if os.path.isdir(task_path):
shutil.rmtree(task_path)
os.mkdir(task_path)

upload_dir = db_task.get_upload_dirname()
os.makedirs(upload_dir)
output_dir = db_task.get_data_dirname()
os.makedirs(output_dir)

return db_task

def create(tid, params):
"""Schedule the task"""
q = django_rq.get_queue('default')
q.enqueue_call(func=_create_thread, args=(tid, params),
job_id="/api/v1/tasks/{}".format(tid))

def check(tid):
"""Check status of the scheduled task"""
response = {}
queue = django_rq.get_queue('default')
job = queue.fetch_job("task.create/{}".format(tid))
if job is None:
response = {"state": "unknown"}
elif job.is_failed:
response = {"state": "error", "stderr": "Could not create the task. " + job.exc_info }
elif job.is_finished:
response = {"state": "created"}
else:
response = {"state": "started"}

if 'status' in job.meta:
response['status'] = job.meta['status']

return response

@transaction.atomic
def delete(tid):
"""Delete the task"""
db_task = models.Task.objects.select_for_update().get(pk=tid)
if db_task:
db_task.delete()
shutil.rmtree(db_task.get_task_dirname(), ignore_errors=True)
else:
raise Exception("The task doesn't exist")

@transaction.atomic
def update(tid, labels):
"""Update labels for the task"""

db_task = models.Task.objects.select_for_update().get(pk=tid)
db_labels = list(db_task.label_set.prefetch_related('attributespec_set').all())

new_labels = _parse_labels(labels)
old_labels = _parse_db_labels(db_labels)

for label_name in new_labels:
if label_name in old_labels:
db_label = [l for l in db_labels if l.name == label_name][0]
for attr_name in new_labels[label_name]:
if attr_name in old_labels[label_name]:
db_attr = [attr for attr in db_label.attributespec_set.all()
if attr.get_name() == attr_name][0]
new_attr = new_labels[label_name][attr_name]
old_attr = old_labels[label_name][attr_name]
if new_attr['prefix'] != old_attr['prefix']:
raise Exception("new_attr['prefix'] != old_attr['prefix']")
if new_attr['type'] != old_attr['type']:
raise Exception("new_attr['type'] != old_attr['type']")
if set(old_attr['values']) - set(new_attr['values']):
raise Exception("set(old_attr['values']) - set(new_attr['values'])")

db_attr.text = "{}{}={}:{}".format(new_attr['prefix'],
new_attr['type'], attr_name, ",".join(new_attr['values']))
db_attr.save()
else:
db_attr = models.AttributeSpec()
attr = new_labels[label_name][attr_name]
db_attr.text = "{}{}={}:{}".format(attr['prefix'],
attr['type'], attr_name, ",".join(attr['values']))
db_attr.label = db_label
db_attr.save()
else:
db_label = models.Label()
db_label.name = label_name
db_label.task = db_task
db_label.save()
for attr_name in new_labels[label_name]:
db_attr = models.AttributeSpec()
attr = new_labels[label_name][attr_name]
db_attr.text = "{}{}={}:{}".format(attr['prefix'],
attr['type'], attr_name, ",".join(attr['values']))
db_attr.label = db_label
db_attr.save()

def get_frame_path(tid, frame):
"""Read corresponding frame for the task"""
Expand All @@ -149,111 +49,6 @@ def get_frame_path(tid, frame):

return path

def get(tid):
"""Get the task as dictionary of attributes"""
db_task = models.Task.objects.get(pk=tid)
if db_task:
db_labels = db_task.label_set.prefetch_related('attributespec_set').order_by('-pk').all()
im_meta_data = get_image_meta_cache(db_task)
attributes = {}
for db_label in db_labels:
attributes[db_label.id] = {}
for db_attrspec in db_label.attributespec_set.all():
attributes[db_label.id][db_attrspec.id] = db_attrspec.text
db_segments = list(db_task.segment_set.prefetch_related('job_set').all())
segment_length = max(db_segments[0].stop_frame - db_segments[0].start_frame + 1, 1)
job_indexes = []
for segment in db_segments:
db_job = segment.job_set.first()
job_indexes.append({
"job_id": db_job.id,
"max_shape_id": db_job.max_shape_id,
})

response = {
"status": db_task.status,
"spec": {
"labels": OrderedDict((db_label.id, db_label.name) for db_label in db_labels),
"attributes": attributes
},
"size": db_task.size,
"taskid": db_task.id,
"name": db_task.name,
"mode": db_task.mode,
"segment_length": segment_length,
"jobs": job_indexes,
"overlap": db_task.overlap,
"z_orded": db_task.z_order,
"flipped": db_task.flipped,
"image_meta_data": im_meta_data
}
else:
raise Exception("Cannot find the task: {}".format(tid))

return response


@transaction.atomic
def save_job_status(jid, status, user):
db_job = models.Job.objects.select_related("segment__task").select_for_update().get(pk = jid)
db_task = db_job.segment.task
status = StatusChoice(status)

slogger.job[jid].info('changing job status from {} to {} by an user {}'.format(db_job.status, str(status), user))

db_job.status = status.value
db_job.save()
db_segments = list(db_task.segment_set.prefetch_related('job_set').all())
db_jobs = [db_segment.job_set.first() for db_segment in db_segments]

if len(list(filter(lambda x: StatusChoice(x.status) == StatusChoice.ANNOTATION, db_jobs))) > 0:
db_task.status = StatusChoice.ANNOTATION
elif len(list(filter(lambda x: StatusChoice(x.status) == StatusChoice.VALIDATION, db_jobs))) > 0:
db_task.status = StatusChoice.VALIDATION
else:
db_task.status = StatusChoice.COMPLETED

db_task.save()

def get_job(jid):
"""Get the job as dictionary of attributes"""
db_job = models.Job.objects.select_related("segment__task").get(id=jid)
if db_job:
db_segment = db_job.segment
db_task = db_segment.task
im_meta_data = get_image_meta_cache(db_task)

# Truncate extra image sizes
if db_task.mode == 'annotation':
im_meta_data = im_meta_data['original_size'][db_segment.start_frame:db_segment.stop_frame + 1]

db_labels = db_task.label_set.prefetch_related('attributespec_set').order_by('-pk').all()
attributes = {}
for db_label in db_labels:
attributes[db_label.id] = {}
for db_attrspec in db_label.attributespec_set.all():
attributes[db_label.id][db_attrspec.id] = db_attrspec.text

response = {
"status": db_job.status,
"labels": OrderedDict((db_label.id, db_label.name) for db_label in db_labels),
"stop": db_segment.stop_frame,
"taskid": db_task.id,
"slug": db_task.name,
"jobid": jid,
"start": db_segment.start_frame,
"mode": db_task.mode,
"overlap": db_task.overlap,
"attributes": attributes,
"z_order": db_task.z_order,
"flipped": db_task.flipped,
"image_meta_data": im_meta_data,
"max_shape_id": db_job.max_shape_id,
}
else:
raise Exception("Cannot find the job: {}".format(jid))

return response

@transaction.atomic
def rq_handler(job, exc_type, exc_value, traceback):
Expand Down Expand Up @@ -373,54 +168,6 @@ def _get_frame_path(frame, base_dir):

return path

def _parse_labels(labels):
parsed_labels = OrderedDict()

last_label = ""
for token in shlex.split(labels):
if token[0] != "~" and token[0] != "@":
if token in parsed_labels:
raise ValueError("labels string is not corect. " +
"`{}` label is specified at least twice.".format(token))

parsed_labels[token] = {}
last_label = token
else:
attr = models.AttributeSpec.parse(token)
attr['text'] = token
if not attr['type'] in ['checkbox', 'radio', 'number', 'text', 'select']:
raise ValueError("labels string is not corect. " +
"`{}` attribute has incorrect type {}.".format(
attr['name'], attr['type']))

values = attr['values']
if attr['type'] == 'checkbox': # <prefix>checkbox=name:true/false
if not (len(values) == 1 and values[0] in ['true', 'false']):
raise ValueError("labels string is not corect. " +
"`{}` attribute has incorrect value.".format(attr['name']))
elif attr['type'] == 'number': # <prefix>number=name:min,max,step
try:
if len(values) != 3 or float(values[2]) <= 0 or \
float(values[0]) >= float(values[1]):
raise ValueError
except ValueError:
raise ValueError("labels string is not correct. " +
"`{}` attribute has incorrect format.".format(attr['name']))

if attr['name'] in parsed_labels[last_label]:
raise ValueError("labels string is not corect. " +
"`{}` attribute is specified at least twice.".format(attr['name']))

parsed_labels[last_label][attr['name']] = attr

return parsed_labels

def _parse_db_labels(db_labels):
result = []
for db_label in db_labels:
result += [db_label.name]
result += [attr.text for attr in db_label.attributespec_set.all()]
return _parse_labels(" ".join(result))


'''
Expand Down
1 change: 1 addition & 0 deletions cvat/apps/engine/tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,4 @@ def test_api_v1_users_id_no_auth_partial(self):
data = {"username": "user12"}
response = self._run_api_v1_users_id(None, self.user.id, data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

26 changes: 4 additions & 22 deletions cvat/apps/engine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,11 @@
router.register('plugins', views.PluginViewSet)

urlpatterns = [
# Entry point for a client
path('', views.dispatch_request),

# documentation for API
path('api/docs/', include_docs_urls(title='CVAT REST API', public=True)),
# entry point for API
path('api/v1/', include((router.urls, 'cvat'), namespace='v1')),


# deprecated API
path('', views.dispatch_request),
#path('create/task', views.create_task),
#path('get/task/<int:tid>/frame/<int:frame>', views.get_frame),
path('check/task/<int:tid>', views.check_task),
#path('delete/task/<int:tid>', views.delete_task),
path('update/task/<int:tid>', views.update_task),
path('get/job/<int:jid>', views.get_job),
path('get/task/<int:tid>', views.get_task),
path('dump/annotation/task/<int:tid>', views.dump_annotation),
path('check/annotation/task/<int:tid>', views.check_annotation),
path('download/annotation/task/<int:tid>', views.download_annotation),
path('save/annotation/job/<int:jid>', views.save_annotation_for_job),
path('save/annotation/task/<int:tid>', views.save_annotation_for_task),
path('delete/annotation/task/<int:tid>', views.delete_annotation_for_task),
path('get/annotation/job/<int:jid>', views.get_annotation),
path('get/username', views.get_username),
path('save/exception/<int:jid>', views.catch_client_exception),
path('save/status/job/<int:jid>', views.save_job_status),
path('api/v1/', include((router.urls, 'cvat'), namespace='v1'))
]
Loading

0 comments on commit 98bb2f9

Please sign in to comment.