1
|
|
|
import os |
2
|
|
|
from argparse import ArgumentParser |
3
|
|
|
|
4
|
|
|
|
5
|
|
View Code Duplication |
class TextDump: |
|
|
|
|
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(): |
|
|
|
|
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
|
|
|
|