Passed
Push — master ( 9e6838...cf264a )
by Yang
27s
created

Python.LORIS.query   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 123
Duplicated Lines 54.47 %

Importance

Changes 0
Metric Value
wmc 7
eloc 66
dl 67
loc 123
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A login() 0 29 1
A putCNBP() 24 24 2
A postCNBP() 23 23 2
A getCNBP() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import requests
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 logging
0 ignored issues
show
introduced by
standard import "import logging" should be placed before "import requests"
Loading history...
3
import sys
0 ignored issues
show
introduced by
standard import "import sys" should be placed before "import requests"
Loading history...
4
import os
0 ignored issues
show
introduced by
standard import "import os" should be placed before "import requests"
Loading history...
5
import json
0 ignored issues
show
introduced by
standard import "import json" should be placed before "import requests"
Loading history...
6
from dotenv import load_dotenv
7
from LORIS.helper import is_response_success
0 ignored issues
show
introduced by
Unable to import 'LORIS.helper'
Loading history...
8
9
10
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
11
logger = logging.getLogger('LORISQuery')
0 ignored issues
show
Coding Style Naming introduced by
The name logger does not conform to the constant naming conventions ((([A-Z_][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...
12
13
14
def login():
15
    """
16
    Logs into LORIS using the stored credential. Must use PyCurl as Requests is not working.
17
    :return: BOOL if or not it is successful. also, the JSON token that is necessary to conduct further transactions.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

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

Loading history...
18
    """
19
    logger = logging.getLogger('LORIS_login')
0 ignored issues
show
Comprehensibility Bug introduced by
logger is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
20
21
    #Load environmental variables.
22
    load_dotenv()
23
24
    username = os.getenv("LORISusername")
25
    password = os.getenv("LORISpassword")
26
27
    data = json.dumps({"username":username, "password":password})
28
29
    #Login URL
30
    url = os.getenv("LORISurl")
31
    updated_url = url + 'login'
32
33
34
    # requests style login # NOT WORKING!
35
    r = requests.post(updated_url, data=data)
0 ignored issues
show
Coding Style Naming introduced by
The name r 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...
36
37
38
    logger.info(str(r.status_code) + r.reason)
39
40
    response_json = r.json()
41
42
    return is_response_success(r.status_code, 200), response_json.get('token')
43
44
45 View Code Duplication
def getCNBP(token, endpoint):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style Naming introduced by
The name getCNBP 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 122).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
46
    """
47
    Get from a CNBP LORIS database endpoint
48
    :param endpoint:
49
    :return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already.
50
    """
51
    logger = logging.getLogger('LORIS_get')
0 ignored issues
show
Comprehensibility Bug introduced by
logger is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
52
    logger.info("Getting LORIS endpoing: "+ endpoint + "at")
53
    load_dotenv()
54
    url = os.getenv("LORISurl")
55
    updatedurl = url + endpoint
56
    logger.info(updatedurl)
57
    HEADERS = {'Authorization': 'token {}'.format(token)}
0 ignored issues
show
Coding Style Naming introduced by
The name HEADERS 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...
58
59
    with requests.Session() as s:
0 ignored issues
show
Coding Style Naming introduced by
The name s 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...
60
        s.headers.update(HEADERS)
61
        r = s.get(updatedurl)
0 ignored issues
show
Coding Style Naming introduced by
The name r 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...
62
        logger.info("Get Result:" + str(r.status_code) + r.reason)
63
64
        return r.status_code, r.json()
65
66
67 View Code Duplication
def postCNBP(token, endpoint, data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style Naming introduced by
The name postCNBP 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 122).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
68
    """
69
    post some data to a LORIS end point.
70
    :param endpoint:
71
    :param data:
72
    :return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

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

Loading history...
73
    """
74
    logger = logging.getLogger('LORIS_post')
0 ignored issues
show
Comprehensibility Bug introduced by
logger is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
75
    logger.info("Posting data to: "+endpoint)
76
    logger.info("Data: "+data)
77
    logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

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

Loading history...
78
    load_dotenv()
79
    url = os.getenv("LORISurl")
80
    updatedurl = url + endpoint
81
82
    HEADERS = {'Authorization': 'token {}'.format(token)}
0 ignored issues
show
Coding Style Naming introduced by
The name HEADERS 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...
83
84
    with requests.Session() as s:
0 ignored issues
show
Coding Style Naming introduced by
The name s 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...
85
        s.headers.update(HEADERS)
86
        r = s.post(updatedurl, data=data)
0 ignored issues
show
Coding Style Naming introduced by
The name r 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...
87
        logger.info("Post Result:" + str(r.status_code) + r.reason)
88
89
        return r.status_code, r
90
91 View Code Duplication
def putCNBP(token, endpoint, data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Coding Style Naming introduced by
The name putCNBP 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...
Comprehensibility Bug introduced by
token is re-defining a name which is already available in the outer-scope (previously defined on line 122).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
92
    """
93
    Put some data to a LORIS end point.
94
    :param endpoint:
95
    :param data:
96
    :return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

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

Loading history...
97
    """
98
    logger = logging.getLogger('LORIS_put')
0 ignored issues
show
Comprehensibility Bug introduced by
logger is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
99
    logger.info("Putting data to: "+endpoint)
100
    logger.info("Data: "+data)
101
    logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!")
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (104/100).

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

Loading history...
102
103
    load_dotenv()
104
    url = os.getenv("LORISurl")
105
    updatedurl = url + endpoint
106
107
    HEADERS = {'Authorization': 'token {}'.format(token)}
0 ignored issues
show
Coding Style Naming introduced by
The name HEADERS 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...
108
109
    with requests.Session() as s:
0 ignored issues
show
Coding Style Naming introduced by
The name s 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...
110
        s.headers.update(HEADERS)
111
        r = s.put(updatedurl, data=data)
0 ignored issues
show
Coding Style Naming introduced by
The name r 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...
112
        logger.info("Put Result:" + str(r.status_code) + r.reason)
113
114
        return r.status_code, r
115
116
117
118
# Only executed when running directly.
119
if __name__ == '__main__':
120
    #print(login())
121
    #getCNBP("projects")
122
    Success, token = login()
0 ignored issues
show
Coding Style Naming introduced by
The name Success does not conform to the constant naming conventions ((([A-Z_][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 token does not conform to the constant naming conventions ((([A-Z_][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...
123
    #print("Test complete")
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...