Completed
Push — master ( 2c16e2...2c16e2 )
by Paolo
13s queued 11s
created

cryoweb.tasks.import_from_cryoweb()   B

Complexity

Conditions 5

Size

Total Lines 72
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 72
rs 8.8373
c 0
b 0
f 0
cc 5
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A cryoweb.tasks.ImportCryowebTask.import_data_from_file() 0 17 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Thu Sep 13 11:33:09 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
8
Inspired from
9
10
http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html
11
12
"""
13
14
from celery.utils.log import get_task_logger
15
16
from common.tasks import ExclusiveTask
17
from image.celery import app as celery_app
18
from submissions.tasks import ImportGenericTaskMixin
19
20
from .helpers import cryoweb_import, upload_cryoweb
21
from .models import truncate_database
22
23
# get a logger for tasks
24
logger = get_task_logger(__name__)
25
26
27
# clean cryoweb database after calling decorated function
28
def clean_cryoweb_database(f):
29
    logger.debug("Decorating %s" % (f))
30
31
    def wrap(*args, **kwargs):
32
        result = f(*args, **kwargs)
33
34
        # cleaning up database without knowing if load is successful or not
35
        logger.info("Cleaning up cryoweb database")
36
        truncate_database()
37
38
        return result
39
40
    wrap.__doc__ = f.__doc__
41
    wrap.__name__ = f.__name__
42
43
    # return decorated function
44
    return wrap
45
46
47
class ImportCryowebTask(ImportGenericTaskMixin, ExclusiveTask):
48
    """
49
    An exclusive task wich upload a *data-only* cryoweb dump in cryoweb
50
    database and then fill up :ref:`UID <The Unified Internal Database>`
51
    tables. After data import (wich could be successful or not) cryoweb
52
    helper database is cleanded and restored in the original status::
53
54
        from cryoweb.tasks import ImportCryowebTask
55
56
        # call task asynchronously
57
        task = ImportCryowebTask()
58
        res = task.delay(submission_id)
59
60
    Args:
61
        submission_id (int): the submission primary key
62
63
    Returns:
64
        str: a message string (ex. success)
65
    """
66
67
    name = "Import Cryoweb"
68
    description = """Import Cryoweb data from Cryoweb dump"""
69
    action = "cryoweb import"
70
71
    # ExclusiveTask attributes
72
    lock_id = 'ImportFromCryoWeb'
73
    blocking = True
74
75
    # decorate function in order to cleanup cryoweb database after data import
76
    @clean_cryoweb_database
77
    def import_data_from_file(self, submission_obj):
78
        """Call the custom import method"""
79
80
        # upload data into cryoweb database
81
        status = upload_cryoweb(submission_obj.id)
82
83
        # if something went wrong, uploaded_cryoweb has token the exception
84
        # ad update submission.message field
85
        if status is False:
86
            return status
87
88
        # load cryoweb data into UID
89
        # check status
90
        status = cryoweb_import(submission_obj)
91
92
        return status
93
94
95
# register explicitly tasks
96
# https://github.com/celery/celery/issues/3744#issuecomment-271366923
97
celery_app.tasks.register(ImportCryowebTask)
98