Completed
Branch master (9edffc)
by Jordi
04:36
created

Import()   C

Complexity

Conditions 11

Size

Total Lines 57
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 57
rs 5.4
c 0
b 0
f 0
cc 11
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.thermoscientific.gallery.Ts9861x.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
""" Thermo Scientific 'Gallery 9861x'
9
"""
10
from bika.lims import bikaMessageFactory as _
11
from bika.lims.utils import t
12
from . import ThermoGalleryImporter, ThermoGalleryTSVParser
13
import json
14
import traceback
15
16
title = "Thermo Scientific - Gallery 9861x"
17
18
19
def Import(context, request):
20
    """ Thermo Scientific - Gallery 9861x analysis results
21
    """
22
    infile = request.form['thermoscientific_gallery_9861x_file']
23
    fileformat = request.form['thermoscientific_gallery_9861x_format']
24
    artoapply = request.form['thermoscientific_gallery_9861x_artoapply']
25
    override = request.form['thermoscientific_gallery_9861x_override']
26
    instrument = request.form.get('thermoscientific_gallery_9861x_instrument', None)
27
    errors = []
28
    logs = []
29
30
    # Load the most suitable parser according to file extension/options/etc...
31
    parser = None
32
    if not hasattr(infile, 'filename'):
33
        errors.append(_("No file selected"))
34
    if fileformat == 'tsv_40':
35
        parser = ThermoGallery9861xTSVParser(infile)
36
    else:
37
        errors.append(t(_("Unrecognized file format ${fileformat}",
38
                          mapping={"fileformat": fileformat})))
39
40
    if parser:
41
        # Load the importer
42
        status = ['sample_received', 'attachment_due', 'to_be_verified']
43
        if artoapply == 'received':
44
            status = ['sample_received']
45
        elif artoapply == 'received_tobeverified':
46
            status = ['sample_received', 'attachment_due', 'to_be_verified']
47
48
        over = [False, False]
49
        if override == 'nooverride':
50
            over = [False, False]
51
        elif override == 'override':
52
            over = [True, False]
53
        elif override == 'overrideempty':
54
            over = [True, True]
55
56
        importer = ThermoGallery9861xImporter(parser=parser,
57
                                              context=context,
58
                                              allowed_ar_states=status,
59
                                              allowed_analysis_states=None,
60
                                              override=over,
61
                                              instrument_uid=instrument)
62
        tbex = ''
63
        try:
64
            importer.process()
65
        except:
66
            tbex = traceback.format_exc()
67
        errors = importer.errors
68
        logs = importer.logs
69
        warns = importer.warns
70
        if tbex:
71
            errors.append(tbex)
72
73
    results = {'errors': errors, 'log': logs, 'warns': warns}
0 ignored issues
show
introduced by
The variable warns does not seem to be defined in case parser on line 40 is False. Are you sure this can never be the case?
Loading history...
74
75
    return json.dumps(results)
76
77
78
class ThermoGallery9861xTSVParser(ThermoGalleryTSVParser):
79
80
    def getAttachmentFileType(self):
81
        return "Thermo Scientific Gallery 9861x TSV/XLS"
82
83
84
class ThermoGallery9861xImporter(ThermoGalleryImporter):
85
86
    def getKeywordsToBeExcluded(self):
87
        return []
88