Code Duplication    Length = 37-37 lines in 2 locations

excelexporters/offlinemetercost.py 1 location

@@ 22-58 (lines=37) @@
19
# Step 3: Encode the excelexporters file to Base64
20
####################################################################################################################
21
22
def export(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type):
23
    ####################################################################################################################
24
    # Step 1: Validate the report data
25
    ####################################################################################################################
26
    if report is None:
27
        return None
28
29
    if "reporting_period" not in report.keys() or \
30
            "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0:
31
        return None
32
    ####################################################################################################################
33
    # Step 2: Generate excel file from the report data
34
    ####################################################################################################################
35
    filename = generate_excel(report,
36
                              name,
37
                              reporting_start_datetime_local,
38
                              reporting_end_datetime_local,
39
                              period_type)
40
    ####################################################################################################################
41
    # Step 3: Encode the excel file to Base64
42
    ####################################################################################################################
43
    try:
44
        with open(filename, 'rb') as binary_file:
45
            binary_file_data = binary_file.read()
46
    except IOError as ex:
47
        pass
48
49
    # Base64 encode the bytes
50
    base64_encoded_data = base64.b64encode(binary_file_data)
51
    # get the Base64 encoded data using human-readable characters.
52
    base64_message = base64_encoded_data.decode('utf-8')
53
    # delete the file from server
54
    try:
55
        os.remove(filename)
56
    except NotImplementedError as ex:
57
        pass
58
    return base64_message
59
60
61
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type):

excelexporters/metercost.py 1 location

@@ 22-58 (lines=37) @@
19
# Step 3: Encode the excelexporters file to Base64
20
####################################################################################################################
21
22
def export(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type):
23
    ####################################################################################################################
24
    # Step 1: Validate the report data
25
    ####################################################################################################################
26
    if report is None:
27
        return None
28
29
    if "reporting_period" not in report.keys() or \
30
            "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0:
31
        return None
32
    ####################################################################################################################
33
    # Step 2: Generate excel file from the report data
34
    ####################################################################################################################
35
    filename = generate_excel(report,
36
                              name,
37
                              reporting_start_datetime_local,
38
                              reporting_end_datetime_local,
39
                              period_type)
40
    ####################################################################################################################
41
    # Step 3: Encode the excel file to Base64
42
    ####################################################################################################################
43
    try:
44
        with open(filename, 'rb') as binary_file:
45
            binary_file_data = binary_file.read()
46
    except IOError as ex:
47
        pass
48
49
    # Base64 encode the bytes
50
    base64_encoded_data = base64.b64encode(binary_file_data)
51
    # get the Base64 encoded data using human-readable characters.
52
    base64_message = base64_encoded_data.decode('utf-8')
53
    # delete the file from server
54
    try:
55
        os.remove(filename)
56
    except NotImplementedError as ex:
57
        pass
58
    return base64_message
59
60
61
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type):