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

Python.oshelper.file_operation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 48
dl 0
loc 99
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A recursive_list_files() 0 15 3
A is_file_name_unique() 0 13 2
A decompress_folder() 0 25 4
A copy_files_to_flat_folder() 0 30 4
A zip_with_name() 0 2 1
1
import os
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
3
from DICOM.validate import DICOM_validator
0 ignored issues
show
introduced by
Unable to import 'DICOM.validate'
Loading history...
4
from DICOM.decompress import DICOM_RequireDecompression, DICOM_TransferSyntax
0 ignored issues
show
introduced by
Unable to import 'DICOM.decompress'
Loading history...
5
6
7
def recursive_list_files(root_dicom_path):
8
    """
9
    load all the files, validate and then pass to decompress or anonimize.
10
    :param path:
11
    :return:
12
    """
13
    global file_list
0 ignored issues
show
Bug introduced by
Global variable 'file_list' undefined at the module level
Loading history...
Coding Style Naming introduced by
The name file_list 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
    file_list = []
15
16
    for root, directories, filenames in os.walk(root_dicom_path):
0 ignored issues
show
Unused Code introduced by
The variable directories seems to be unused.
Loading history...
17
        #for directory in directories:
18
            #file_list.append(os.path.join(root, directory))
19
        for filename in filenames:
20
            file_list.append(os.path.join(root,filename))
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
21
    return file_list
22
23
24
def decompress_folder(file_list):
0 ignored issues
show
Comprehensibility Bug introduced by
file_list is re-defining a name which is already available in the outer-scope (previously defined on line 14).

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...
25
    """
26
    Decompress all compressed file within the lsit.
27
    :param file_list:
28
    :return:
29
    """
30
31
    for file in file_list:
32
        logger = logging.getLogger("Compression checking file: " + file)
33
34
        # find if the file is DICOM, if not, skip this file.
35
        is_DICOM_file, _ = DICOM_validator(file)
0 ignored issues
show
Coding Style Naming introduced by
The name is_DICOM_file 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
        if not is_DICOM_file:
37
            continue
38
39
        # check if the file is compressed.
40
        TransferSyntax = DICOM_TransferSyntax(file)
0 ignored issues
show
Coding Style Naming introduced by
The name TransferSyntax 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
        try:
42
            RequireDecompression = DICOM_RequireDecompression(TransferSyntax)
0 ignored issues
show
Coding Style Naming introduced by
The name RequireDecompression 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...
Unused Code introduced by
The variable RequireDecompression seems to be unused.
Loading history...
43
            # if RequireDecompression:
44
                # DecompressFile
45
46
        except ValueError:
47
            logger.info("Unknwonw DICOM syntax. You sure it is DICOM?")
48
            continue
49
50
51
52
def copy_files_to_flat_folder(file_list, destination_path):
0 ignored issues
show
Comprehensibility Bug introduced by
file_list is re-defining a name which is already available in the outer-scope (previously defined on line 14).

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...
53
    """
54
    Takes in a list of files and flatten them to the desintation path while ensure they are guarnteed to be unique files.
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...
55
    :param file_list: the list of files from different path.
56
    :param destination_path:
57
    """
58
    logger = logging.getLogger(__name__)
59
    logger.info("Copying checking and checking files to destination: " + destination_path)
60
61
    from shutil import copyfile
62
63
    for file in file_list:
64
65
        # find if the file is DICOM, if not, skip this file.
66
        is_DICOM_file, _ = DICOM_validator(file)
0 ignored issues
show
Coding Style Naming introduced by
The name is_DICOM_file 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...
67
        if not is_DICOM_file:
68
            continue
69
70
        # get the final path name.
71
        file_name = os.path.basename(file)
72
        destination_path_name = os.path.join(destination_path, file_name)
73
74
        # check if the final path is unique.
75
        unique, new_name = is_file_name_unique(destination_path_name)
76
77
        # append date time microsecond string if the file is not unique.
78
        if not unique:
79
            destination_path_name = new_name
80
81
        copyfile(file, destination_path_name)
82
83
def is_file_name_unique(path):
84
    """
85
    Determine if the proposed file exist and suggest alternative name.
86
    :param path:
87
    :return:
88
    """
89
    import datetime
90
    timestamp = datetime.datetime.now().isoformat()
91
    timestamp = timestamp.replace(':','') # string : which are incompatible with path
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
92
    if os.path.exists(path):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
93
        return False, path + "_" + timestamp
94
    else:
95
        return True, path
96
97
def zip_with_name(path, name):
0 ignored issues
show
Coding Style introduced by
This function 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 argument path seems to be unused.
Loading history...
Unused Code introduced by
The argument name seems to be unused.
Loading history...
98
    return
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...