-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloss.py
264 lines (215 loc) · 8.77 KB
/
floss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import time
import os
import pathlib
import sys
import argparse
import json
import csv
import cmd
class Floss:
def __init__(self):
self.speed = None
self.stream_path = None
self.input_path = None
self.output_path = None
def argument_handler(self):
"""Returns arguments given from command line.
Returns:
stream_path: path to stream data-paths from
output_path: path to write data to
"""
parser = argparse.ArgumentParser(prog="floss",
description="Converts stock data from text and csv files to json",
usage="python %(prog)s.py [options] input_path output_path")
parser.add_argument("-s", "--speed", help="Changes how quickly the stream_path is re-checked.",
type=float, action='store', default=1)
parser.add_argument(
"input_path", help="Text file containing path to data", type=str)
parser.add_argument(
"output_path", help="JSON file to write data to", type=str)
args = parser.parse_args()
if args.speed < 0:
print(f'speed cannot be negative')
self.speed = args.speed
print(f'speed = {self.speed}')
stream_path = args.input_path
output_path = args.output_path
self.stream_path = output_path
self.output_path = output_path
return stream_path, output_path
def get_txt_data(self, txt_file):
"""Handles retrieval of data from .txt files."""
if not os.path.isfile(txt_file):
print('File does not exist.')
return
with open(txt_file, 'r') as reader:
stocks = []
for line in reader:
cur = {}
company = ''
ticker = ''
x = 0
while line[x] != ':':
company += line[x]
x += 1
x += 1
for i in range(x, len(line) - 1):
ticker += line[i]
cur['name'] = company
cur['ticker'] = ticker
stocks.append(cur)
return {'data': stocks}
def txt_service(self, data_path, output_path):
"""
* Handles .txt input files.
* Retrieves data from file specified in txt_file, converts data to JSON,
then writes data to file specified in json_file.
:param data_path: source of stock data
:param output_path: file to write json data to
:return: None
"""
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data_path, f, ensure_ascii=False, indent=4)
# Get last part of path for cleaner printing.
# cur_path = os.path.basename(os.path.normpath(txt_file))
file_path = pathlib.PurePath(output_path)
print(f'floss wrote data to {file_path}')
def get_csv_data(self, path):
with open(path, 'r', encoding='utf-8-sig') as file:
reader = csv.DictReader(file)
data = []
for row in reader:
data.append(row)
return data
def csv_service(self, csv_path, output_path):
"""
* Extracts data from .csv file and writes it to the
specified output in JSON format.
* csvfile: path to .cvs file containing data.
* json_file: path to .json file to write data.
"""
data = self.get_csv_data(csv_path)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
# Get last part of path for cleaner printing.
# cur_path = os.path.basename(os.path.normpath(txt_file))
file_path = pathlib.PurePath(output_path)
print(f'floss wrote data to {file_path}')
def validate_arguments(self, stream_path, output_path):
# Check if stream path exists
if not os.path.isfile(stream_path):
print('Stream file not found.')
return False
# Check if output path exists
if not os.path.isfile(output_path):
print('Output file not found.')
return False
# Extract stream/output file type.
stream_type = os.path.splitext(stream_path)[1]
output_type = os.path.splitext(output_path)[1]
# Validate stream/output file types.
if stream_type != '.txt':
print(f'Invalid stream file type {stream_type}, streams must be .txt files.')
return False
if output_type != '.json':
print(f'Invalid output file type {output_type}, output must be a .json file.')
return False
return True
def file_service(self, input_path, output_path):
# input_file_size = os.path.getsize(input_path)
# Check if input path exists
if not os.path.isfile(input_path):
print('Input file not found.')
return False
# Check if output path exists
if not os.path.isfile(output_path):
print('Output file not found.')
return False
# Extract input/output file type.
input_type = os.path.splitext(input_path)[1]
output_type = os.path.splitext(output_path)[1]
if input_type == '.txt':
data = self.get_txt_data(input_path)
try:
self.txt_service(data, output_path)
except Exception as err:
print(f'Unexpected error opening {input_path} is', repr(err))
return False
elif input_type == '.csv':
try:
self.csv_service(input_path, output_path)
except Exception as err:
print(f'Unexpected error opening {input_path} is', repr(err))
return False
elif output_type != '.json':
print(f'Unsupported input file type: {input_type}')
return False
else:
print(f'Unsupported input file type: {input_type}')
return False
return True
def read_file_stream(self, stream_path, output_path):
"""Main loop of program."""
# Wait for changes in input file.
last_size = 0
run = True
while run:
# Attempt to read the path input from stream file
with open(stream_path, "r") as f:
data_path = f.readline()
time.sleep(self.speed)
# On change, copy contents of input file. Write contents to given output JSON file.
file_size = os.path.getsize(stream_path)
output_size = os.path.getsize(output_path)
if file_size == 0:
print(f'Waiting for input!')
elif file_size != last_size and output_size == 0 and len(data_path) > 0:
print(f'data_path: {data_path}')
# Track the last stock_path entered
last_size = file_size
result = self.file_service(data_path, output_path)
run = result
elif output_size == 0 and len(data_path) > 0:
# Output file is empty, write data to output
last_size = file_size
result = self.file_service(data_path, output_path)
run = result
elif file_size != last_size and output_size > 0:
print('Waiting for space in output.')
# else:
# print('Waiting for space in output.')
print(f'\nStream stopped.')
def main_loop(self):
# get args
stream_path, output_path = self.argument_handler()
# stream_path = 'stream.txt'
# output_path = 'output.json'
print(F'stream_path: {stream_path}')
print(F'output_path: {output_path}\n')
# Validate args.
if not self.validate_arguments(stream_path, output_path):
return 'Invalid args'
# Check if output_path is empty.
if os.path.getsize(output_path) > 0:
# Ask user if they want to rewrite the output_path, or exit.
text = input(f"{output_path} is not empty. Overwrite? y/n ")
text = text.lower()
if len(text) > 1:
print("Invalid response. Exiting floss.")
sys.exit(1)
if text == 'y':
# Erase contents of output_path
open(output_path, 'w').close()
else:
# Exit
print("Exiting floss.")
sys.exit(1)
# Read from stream file for input paths.
stream = self.read_file_stream(stream_path, output_path)
class Controller:
def run_program(self):
floss = Floss()
floss.main_loop()
if __name__ == '__main__':
controller = Controller()
controller.run_program()