Passed
Pull Request — master (#946)
by Mofenyi
10:09 queued 03:58
created

Import()   F

Complexity

Conditions 15

Size

Total Lines 71
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 71
rs 2.9998
c 0
b 0
f 0
cc 15
nop 2

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like bika.lims.exportimport.instruments.facscalibur.calibur.facscalibur.Import() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
""" Beckman Coulter Access 2
9
"""
10
from bika.lims import bikaMessageFactory as _
11
from bika.lims.utils import t
12
from . import FacsCaliburParser, FacsCaliburImporter
13
import json
14
import traceback
15
16
title = "TaqMan96 DNA212BS"
17
18
19
def Import(context, request):
20
    """ TaqMan96 DNA212BS analysis results
21
    """
22
    infile = request.form['taqman96_dna212bs_file']
23
    fileformat = request.form['taqman96_dna212bs_format']
24
    artoapply = request.form['taqman96_dna212bs_artoapply']
25
    override = request.form['taqman96_dna212bs_override']
26
    sample = request.form.get('taqman96_dna212bs_sample',
27
                              'requestid')
28
    instrument = request.form.get('instrument', None)
29
    errors = []
30
    logs = []
31
    warns = []
32
33
    # Load the most suitable parser according to file extension/options/etc...
34
    parser = None
35
    if not hasattr(infile, 'filename'):
36
        errors.append(_("No file selected"))
37
    if fileformat == 'csv':
38
        parser = FacsCalibur2Parser(infile)
39
    else:
40
        errors.append(t(_("Unrecognized file format ${fileformat}",
41
                          mapping={"fileformat": fileformat})))
42
43
    if parser:
44
        # Load the importer
45
        status = ['sample_received', 'attachment_due', 'to_be_verified']
46
        if artoapply == 'received':
47
            status = ['sample_received']
48
        elif artoapply == 'received_tobeverified':
49
            status = ['sample_received', 'attachment_due', 'to_be_verified']
50
51
        over = [False, False]
52
        if override == 'nooverride':
53
            over = [False, False]
54
        elif override == 'override':
55
            over = [True, False]
56
        elif override == 'overrideempty':
57
            over = [True, True]
58
59
        sam = ['getId', 'getSampleID', 'getClientSampleID']
60
        if sample == 'requestid':
61
            sam = ['getId']
62
        if sample == 'sampleid':
63
            sam = ['getSampleID']
64
        elif sample == 'clientsid':
65
            sam = ['getClientSampleID']
66
        elif sample == 'sample_clientsid':
67
            sam = ['getSampleID', 'getClientSampleID']
68
69
        importer = FacsCalibur2Importer(parser=parser,
70
                                              context=context,
71
                                              idsearchcriteria=sam,
72
                                              allowed_ar_states=status,
73
                                              allowed_analysis_states=None,
74
                                              override=over,
75
                                              instrument_uid=instrument)
76
        tbex = ''
77
        try:
78
            importer.process()
79
        except:
80
            tbex = traceback.format_exc()
81
        errors = importer.errors
82
        logs = importer.logs
83
        warns = importer.warns
84
        if tbex:
85
            errors.append(tbex)
86
87
    results = {'errors': errors, 'log': logs, 'warns': warns}
88
89
    return json.dumps(results)
90
91
92
class FacsCalibur2Parser(FacsCaliburCSVParser):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable FacsCaliburCSVParser does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'FacsCaliburCSVParser'
Loading history...
93
    def getAttachmentFileType(self):
94
        return "TaqMan96 DNA212BS "
95
96
97
class FacsCalibur2Importer(FacsCaliburImporter):
98
    def getKeywordsToBeExcluded(self):
99
        return []
100