-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlarge_file_reader.py
executable file
·222 lines (159 loc) · 5.57 KB
/
large_file_reader.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
# coding=utf-8
__author__ = 'Dani'
from Tkinter import *
from tkFileDialog import *
## ROOT
root = Tk()
root.resizable(0, 0)
root.geometry("830x470")
def execute_function(f):
root.after(200, f)
## BT_FUNCTIONS AND LOGIC
def f_clear(text_widget):
text_widget.delete("1.0", END)
def f_path(entry_widget):
file_path = askopenfilename()
entry_widget.delete(0, END)
entry_widget.insert(0, file_path)
def is_valid_lines(text):
try:
number = int(text)
if number > 0:
return True
return False
except:
return False
def is_valid_start_line(text):
if text in [None, ""]:
return True
try:
number = int(text)
if number >= 0:
return True
return False
except:
return False
def is_valid_path(text):
try:
with open(text, "r"):
return True
except:
return False
def is_valid_break_char(text):
if len(text) == 0:
return True
if len(text) == 1:
return True
elif len(text) == 2 and text[0] == "\\":
return True
return False
def error_message(text_widget, message):
text_widget.delete("1.0", END)
text_widget.insert("1.0", "ERROR: " + message)
def read_lines_in_chunks(in_stream, break_char):
previous_result = ""
while True:
data = in_stream.read(1024)
if not data:
break
last_index = 0
for i in range(0, len(data)):
if data[i] == break_char:
yield previous_result + data[last_index:i + 1]
previous_result = ""
last_index = i + 1
previous_result += data[last_index:]
def decide_break_char(text):
if len(text) == 0:
return "\n"
elif len(text) == 1:
return text
elif text == "\\n":
return "\n"
elif text == "\\t":
return "\t"
elif text == "\\r":
return "\r"
else:
return "\n" # This shuold not happen
def f_view(spinbox, entry_path, entry_break_char, text_widget, progress_bar=None):
text_widget.delete("1.0", END)
if not is_valid_path(entry_path.get()):
error_message(text_widget, "The specified path is not valid or points to a not accessible file")
return
if not is_valid_lines(spinbox.get()):
error_message(text_widget, "Invalid number of lines")
return
if not is_valid_start_line(spin_start_line.get()):
error_message(text_widget, "Invalid start line")
return
if not is_valid_break_char(entry_break_char.get()):
error_message(text_widget, "Invalid line separator. Use a single char (you can scape it with '\\')")
return
n_lines = int(spinbox.get())
start_line = int(spin_start_line.get())
counter = 0
result = ""
target_line = n_lines + start_line
break_char = decide_break_char(entry_break_char.get())
with open(entry_path.get(), "r") as in_stream:
for line in read_lines_in_chunks(in_stream, break_char):
if counter >= target_line:
break
if counter >= start_line:
result += line
counter += 1
text_widget.insert(1.0, result)
## TEXT
frame_text = Frame(root)
frame_text.pack(side=BOTTOM, fill=X)
scrollbar_y = Scrollbar(frame_text)
scrollbar_y.pack(side=RIGHT, fill=Y)
scrollbar_x = Scrollbar(frame_text, orient=HORIZONTAL)
scrollbar_x.pack(side=BOTTOM, fill=X)
target_text = Text(frame_text, wrap=NONE, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
target_text.pack(fill=BOTH)
scrollbar_y.config(command=target_text.yview)
scrollbar_x.config(command=target_text.xview)
## PATH
frame_path = Frame(master=root)
string_label_path = StringVar()
string_label_path.set("Path: ")
label_path = Label(frame_path, textvar=string_label_path)
label_path.pack(side=LEFT)
button_path = Button(frame_path, text="File...", command=lambda: execute_function(f_path(entry_path)))
entry_path = Entry(frame_path, width=125)
entry_path.pack(side=LEFT, fill=X)
button_path.pack(side=LEFT)
frame_path.pack(side=TOP, fill=X)
## MAIN CONTROLS
frame_controls = Frame(root)
#Spinner N LINES
string_label_spin = StringVar()
string_label_spin.set("Number of lines: ")
label_spin = Label(frame_controls, textvar=string_label_spin)
label_spin.pack(side=LEFT)
spinner = Spinbox(frame_controls, from_=0, to=1000000, width=10)
spinner.pack(side=LEFT)
#Spiner Start line
string_label_start = StringVar()
string_label_start.set("Start line (def. 0): ")
label_start = Label(frame_controls, textvar=string_label_start)
label_start.pack(side=LEFT)
spin_start_line = Spinbox(frame_controls, from_=0, to=10000000, width=10)
spin_start_line.pack(side=LEFT)
frame_controls.pack(side=TOP, fill=BOTH)
# bt clear and view
bt_clear = Button(frame_controls, text="Clear", command=lambda: execute_function(f_clear(target_text)))
bt_view = Button(frame_controls, text="View", command=lambda: execute_function(f_view(spinner, entry_path, entry_break, target_text)))
bt_view.pack(side=RIGHT)
bt_clear.pack(side=RIGHT)
# Breaking char
string_label_break = StringVar()
string_label_break.set(" Line separator (default \\n, special chars scaped with \\ are allowed): ")
label_break = Label(frame_controls, textvar=string_label_break)
label_break.pack(side=LEFT)
entry_break = Entry(frame_controls, width=4)
entry_break.pack(side=LEFT)
## EXECUTE
root.mainloop()