Passed
Pull Request — master (#3)
by Yang
04:58
created

Python.LocalDB.create   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B LocalDB_create.database() 0 64 8
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 sqlite3
3
import logging
4
from pathlib import Path
5
from LocalDB.schema import CNBP_blueprint
0 ignored issues
show
Bug introduced by
The name schema does not seem to exist in module LocalDB.
Loading history...
introduced by
Unable to import 'LocalDB.schema'
Loading history...
6
7
# This file database a SQLite local database for CNBP requirement.
8
# Creation: 2018-07-11T160228EST
9
# Author: Yang Ding
10
11
12
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
13
logger = logging.getLogger('database')
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...
14
15
class LocalDB_create:
0 ignored issues
show
Coding Style Naming introduced by
The name LocalDB_create does not conform to the class naming conventions ([A-Z_][a-zA-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 introduced by
This class 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...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
16
17
    @staticmethod
18
    def database(PathString, TableName, KeyFieldString, ColumnsNameTypeList):
0 ignored issues
show
Coding Style Naming introduced by
The name PathString 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...
Coding Style Naming introduced by
The name TableName 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...
Coding Style Naming introduced by
The name KeyFieldString 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...
Coding Style Naming introduced by
The name ColumnsNameTypeList 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...
19
        """
20
        Create the local database based on schema.
21
        :param PathString:
22
        :param TableName:
23
        :param KeyFieldString:
24
        :param ColumnsNameTypeList:
25
        :return:
26
        """
27
28
29
        # if SQL already exist, quit script.
30
        SQLPath = Path(PathString)
0 ignored issues
show
Coding Style Naming introduced by
The name SQLPath 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...
31
32
        # check if path is a fiela nd exist.
33
        if SQLPath.is_file():
34
            logger.info('SQLite database file already exist. Not gonna mess with it!')
35
            return False
36
        '''Delete current database! During testing only'''
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
37
        '''os.remove(sqliteFile)
38
        logger.info('DEBUG: database file already exist. Deleted it!')'''
0 ignored issues
show
Unused Code introduced by
This string statement has no effect and could be removed.
Loading history...
39
40
        #Create the PRIMARY KEY column.
41
        KeyFieldType = CNBP_blueprint.keyfield_type  # column data type
0 ignored issues
show
Coding Style Naming introduced by
The name KeyFieldType 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...
42
43
        #Try to connect the database to start the process:
44
45
        try:
46
            # Create on Connecting to the database file
47
            ConnectedDatabase = sqlite3.connect(PathString)
0 ignored issues
show
Coding Style Naming introduced by
The name ConnectedDatabase 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...
48
49
            c = ConnectedDatabase.cursor()
0 ignored issues
show
Coding Style Naming introduced by
The name c 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...
50
51
            logger.info('Creating PRIMARY KEY DBKEY column in database.')
52
53
            # Creating a new SQLite table with DBKey column (inspired by: https://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (148/100).

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

Loading history...
54
            c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY)'.format(tn=TableName, nf=KeyFieldString, ft=KeyFieldType))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/100).

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

Loading history...
55
56
            logger.info('PRIMARY KEY DBKEY column successfully created in database.')
57
58
            logger.info('Creating secondary columns in database.')
59
60
            # Adding accessory columns via a loop
61
            for column in ColumnsNameTypeList:
62
                if (column[1] != "TEXT" and
63
                    column[1] != "REAL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
64
                    column[1] != "BLOB" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
65
                    #column[1] != "NULL" and
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
66
                    column[1] != "INTEGER"):
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation before block (add 4 spaces).
Loading history...
67
                    continue  # skip iteration is the data type is not specified properly.
68
                else:
69
                    c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}".format(tn=TableName, cn=column[0], ct=column[1]))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (121/100).

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

Loading history...
70
71
            logger.info('Secondary columns created in database.')
72
73
            # Committing changes and closing the connection to the database file
74
            ConnectedDatabase.commit()
75
            ConnectedDatabase.close()
76
        except Exception as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e 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...
77
            logger.info(e)
78
            logger.info('SQLite database creation/update issue, suspect schema non-compliant SQLite database. Did you corrupt this SQLite database somehow?')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (157/100).

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

Loading history...
79
            raise IOError
80
        return True
81