Passed
Pull Request — master (#2)
by Yang
02:09
created

Python.LORIS.candidates   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 82
dl 0
loc 142
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A createCandidateCNBP() 0 33 4
A check_DCCID() 0 16 5
B checkPSCIDExist() 0 42 6
A checkDCCIDExist() 0 32 5
1
import sys
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import os
3
import json
4
import logging
5
from dotenv import load_dotenv
6
from LORIS.query import getCNBP, postCNBP
0 ignored issues
show
introduced by
Unable to import 'LORIS.query'
Loading history...
7
from LORIS.helper import is_response_success
0 ignored issues
show
introduced by
Unable to import 'LORIS.helper'
Loading history...
8
9
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
10
#logger = logging.getLogger('LORISQuery')
11
12
13
def check_DCCID(DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name check_DCCID does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name DCCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
14
    """
15
    Check if DCCID id conform.
16
    :param DCCID:
17
    :return:
18
    """
19
    if not len(str(DCCID)) == 6:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
Unused Code introduced by
Consider changing "not len(str(DCCID)) == 6" to "len(str(DCCID)) != 6"
Loading history...
20
        return False
21
    elif not str(DCCID).isnumeric():
22
        return False
23
    elif DCCID > 999999:
24
        return False
25
    elif DCCID < 0:
26
        return False
27
    else:
28
        return True
29
30
31
def createCandidateCNBP(token, proposded_PSCID):
0 ignored issues
show
Coding Style Naming introduced by
The name createCandidateCNBP does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposded_PSCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
32
    """
33
    Create a candidate using the given PSCID
34
    :param proposded_PSCID:
35
    :return: DCCID
36
    """
37
    logger = logging.getLogger('LORIS_CreateCNBPCandidates')
38
    logger.info("Creating CNBP Candidates")
39
    logger.info(proposded_PSCID)
40
    PSCID_exist = checkPSCIDExist(token, proposded_PSCID)
0 ignored issues
show
Coding Style Naming introduced by
The name PSCID_exist does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
41
    if PSCID_exist:
42
        return False
43
44
    Candidate = {}
0 ignored issues
show
Coding Style Naming introduced by
The name Candidate does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
45
    Candidate['Project'] = 'loris'
46
    Candidate['PSCID'] = proposded_PSCID
47
    Candidate['DoB'] = '2018-05-04'
48
    Candidate['Gender'] = 'Female'
49
50
    data = {"Candidate":Candidate}
51
52
    data_json = json.dumps(data)
53
54
    response_code, response = postCNBP(token, "candidates/", data_json)
55
    if not is_response_success(response_code, 201):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
56
        return False, None
57
    elif response is not None:  # only try to decode if response is not empty!
58
        response_json = response.json()
59
        meta = response_json.get('Meta')
60
        CandID = meta.get('CandID')
0 ignored issues
show
Coding Style Naming introduced by
The name CandID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
61
        return True, CandID
62
    else:
63
        return False, None
64
65
66
def checkPSCIDExist(token, proposed_PSCID):
0 ignored issues
show
Coding Style Naming introduced by
The name checkPSCIDExist does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposed_PSCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
67
    '''
68
    Check if Site/Study already contain the PSCID
69
    :param site:
70
    :param study:
71
    :return: bool for connection, bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (110/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
72
    '''
73
    logger = logging.getLogger('LORIS_checkPSCIDExist')
74
    logger.info("Checking if PSCID exist: "+proposed_PSCID)
75
    load_dotenv()
76
    institution_check = os.getenv("institutionID")
77
78
79
    #Get list of projects
80
    response_success, loris_project = getCNBP(token, r"projects/loris")
81
    if not response_success:
82
        return response_success, None
83
84
    #Get list of candidates (Candidates in v0.0.1)
85
    candidates = loris_project.get("Candidates")
86
    logger.info(candidates)
87
88
    for DCCID in candidates: #these candidates should really be only from the same ID regions.
0 ignored issues
show
Coding Style Naming introduced by
The name DCCID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
89
        response_success, candidate_json = getCNBP(token, r"candidates/"+DCCID)
90
        if not response_success:
91
            return response_success, False
92
        # Each site following the naming convention should have SITE prefix and PI prefix and PROJECT prefix to avoid collision
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
93
94
        # A site check here.
95
        candidate_meta = candidate_json.get("Meta")
96
        candidate_pscID = candidate_meta.get("PSCID")
0 ignored issues
show
Coding Style Naming introduced by
The name candidate_pscID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
97
98
        # Not gonna check is institution ID doesn't even match.
99
        if candidate_pscID[0:3] != institution_check:
100
            continue
101
102
        elif candidate_pscID == proposed_PSCID:
103
            return response_success, True
104
105
            #latest_timepoint = findLatestTimePoint(DCCID)
106
107
    return False
108
109
110
def checkDCCIDExist(token, proposed_DCCID):
0 ignored issues
show
Coding Style Naming introduced by
The name checkDCCIDExist does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name proposed_DCCID does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
111
    """
112
    Check if Site/Study already contain the PSCID
113
    :param token: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
114
    :param proposed_DCCID: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
115
    :return: 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
116
    """
117
    logger = logging.getLogger('LORIS_checkDCCIDExist')
118
    logger.info("Checking if DCCID exist: "+str(proposed_DCCID))
119
    load_dotenv()
120
    institution_check = os.getenv("institutionID")
0 ignored issues
show
Unused Code introduced by
The variable institution_check seems to be unused.
Loading history...
121
122
    assert (check_DCCID(proposed_DCCID))
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after assert.
Loading history...
123
124
    #Get list of projects
125
    response, loris_project = getCNBP(token, r"projects/loris")
126
    response_success = is_response_success(response, 200)
127
128
    if not response_success:
129
        logger.info("FAILED log response: " + str(response))
130
        return response_success, None
131
132
    #Get list of candidates (Candidates in v0.0.1)
133
    candidates = loris_project.get("Candidates")
134
    logger.info(candidates)
135
136
    for DCCID in candidates:
0 ignored issues
show
Coding Style Naming introduced by
The name DCCID does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
137
        if str(proposed_DCCID) == DCCID:
138
            return response_success, True
139
        else:
140
            continue
141
    return response_success, False
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...