dirutility.dump   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 129
Duplicated Lines 79.07 %

Importance

Changes 0
Metric Value
eloc 81
dl 102
loc 129
rs 10
c 0
b 0
f 0
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
B TextDump.append() 12 13 6
A TextDump.read() 7 7 3
A TextDump._encode_data() 9 9 4
A TextDump.__init__() 3 3 1
A TextDump.printer() 3 3 2
A TextDump.write() 8 8 3

4 Functions

Rating   Name   Duplication   Size   Complexity  
A reader() 0 3 1
A writer() 0 3 1
A main() 53 53 1
A appender() 0 3 1

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 os
2
from argparse import ArgumentParser
3
4
5 View Code Duplication
class TextDump:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
7
    def __init__(self, file_path, verbose=0):
8
        self.file_path = file_path
9
        self._verbose = verbose
10
11
    def printer(self, statement):
12
        if self._verbose > 0:
13
            print(statement)
14
15
    @staticmethod
16
    def _encode_data(data, split=None, skip=None):
17
        """Encode data as a string in order to write to a text file."""
18
        data = data.split(split) if split else data
19
        if isinstance(data, (list, tuple, set)):
20
            data = [d for d in data if skip not in d] if skip else data
21
            return '\n'.join(data)
22
        else:
23
            return data
24
25
    def read(self, return_type=None):
26
        self.printer('Reading from text file `{}`'.format(self.file_path))
27
        with open(self.file_path, 'r') as txt:
28
            if str(return_type) == 'list':
29
                return ' '.join(txt.read().splitlines())
30
            else:
31
                return txt.read()
32
33
    def write(self, data, split=None, unique=False, skip=None):
34
        self.printer('Writing to text file `{}`'.format(self.file_path))
35
        with open(self.file_path, 'w') as txt:
36
            result = txt.write(self._encode_data(data, split, skip))
37
38
        # Remove repeated lines if unique is True
39
        if unique:
40
            self.write(list(set(self.read().split('\n'))))
41
42
    def append(self, data, split=None, unique=False, skip=None):
43
        self.printer('Appending to text file `{}`'.format(self.file_path))
44
        write_newline = False
45
        if os.path.exists(self.file_path):
46
            write_newline = True if len(self.read(list)) > 0 else False
47
        with open(self.file_path, 'a') as txt:
48
            if write_newline:
49
                txt.write('\n')
50
            result = txt.write(self._encode_data(data, split, skip))
51
52
        # Remove repeated lines if unique is True
53
        if unique:
54
            self.write(list(set(self.read().split('\n'))))
55
56
57
def reader(file_path, return_type):
58
    """Read a text file and return its contents."""
59
    return TextDump(file_path).read(return_type)
60
61
62
def writer(file_path, data, split=None, unique=False, skip=False):
63
    """Write to a text file and return its contents."""
64
    return TextDump(file_path).write(data, split, unique, skip)
65
66
67
def appender(file_path, data, split=None, unique=False, skip=False):
68
    """Append a text file and return its contents."""
69
    return TextDump(file_path).append(data, split, unique, skip)
70
71
72 View Code Duplication
def main():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
    """
74
    Example Usage:
75
76
    $ text-dump append --file-path domains.txt --data "projects.localhost" --split ' ' --unique
77
    $ text-dump write --file-path domains.txt --data "dev.hpadesign.com staging.hpadesign.com beta.hpadesign.com public.localhost" --split ' ' --skip 'localhost'
78
    $ text-dump read --file-path domains.txt
79
    public.localhost"
80
    """
81
    # Declare argparse argument descriptions
82
    usage = 'Text dump utility.'
83
    description = 'Read, write and append text files.'
84
    helpers = {
85
        'file-path': "Path to text file to read/write to.",
86
        'data': "Data to write/append to the text file.",
87
        'split': "Character used separate a plain text list.",
88
        'unique': "Only write unique values to the text file.",
89
        'skip': "Skip writing a datapoint if the 'skip string' is found.",
90
        'return-type': "Type to return data in.",
91
    }
92
93
    # construct the argument parse and parse the arguments
94
    parser = ArgumentParser(usage=usage, description=description)
95
    sub_parser = parser.add_subparsers()
96
97
    # Read
98
    parser_read = sub_parser.add_parser('read')
99
    parser_read.add_argument('-f', '--file-path', help=helpers['file-path'], type=str)
100
    parser_read.add_argument('-t', '--return-type', help=helpers['return-type'], type=str)
101
    parser_read.set_defaults(func=reader)
102
103
    # Write
104
    parser_write = sub_parser.add_parser('write')
105
    parser_write.add_argument('-f', '--file-path', help=helpers['file-path'], type=str)
106
    parser_write.add_argument('-d', '--data', help=helpers['data'])
107
    parser_write.add_argument('-s', '--split', help=helpers['split'], type=str, default=None)
108
    parser_write.add_argument('-u', '--unique', help=helpers['unique'], action='store_true', default=False)
109
    parser_write.add_argument('--skip', help=helpers['skip'], type=str, default=False)
110
    parser_write.set_defaults(func=writer)
111
112
    # Append
113
    parser_write = sub_parser.add_parser('append')
114
    parser_write.add_argument('-f', '--file-path', help=helpers['file-path'], type=str)
115
    parser_write.add_argument('-d', '--data', help=helpers['data'])
116
    parser_write.add_argument('-s', '--split', help=helpers['split'], type=str, default=None)
117
    parser_write.add_argument('-u', '--unique', help=helpers['unique'], action='store_true', default=False)
118
    parser_write.add_argument('--skip', help=helpers['skip'], type=str, default=False)
119
    parser_write.set_defaults(func=appender)
120
121
    # Parse Arguments
122
    args = vars(parser.parse_args())
123
    func = args.pop('func')
124
    return func(**args)
125
126
127
if __name__ == '__main__':
128
    main()
129