Repository navigation
Expand file tree
/
Copy pathreader.lua
More file actions
284 lines (262 loc) · 8.57 KB
/
Copy pathreader.lua
File metadata and controls
284 lines (262 loc) · 8.57 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require("yaci")
local lfs = require("lfs")
local time = require("time")
CReader = newclass("CReader")
function CReader:init(is_debug, is_block, timer_ms, is_skip_empty)
self.is_debug = false
self.is_block = true
self.timer_ms = 50
self.is_skip_empty = true
self.co = nil
self.file_handle = nil
self.last_size = 0
self.last_modification = 0
if type(is_debug) == "boolean" then
self.is_debug = is_debug
end
if type(is_block) == "boolean" then
self.is_block = is_block
end
if type(timer_ms) == "number" then
self.timer_ms = timer_ms
end
if type(is_skip_empty) == "boolean" then
self.is_skip_empty = is_skip_empty
end
end
function CReader:print(...)
if self.is_debug then
print(...)
end
end
function CReader:is_modification()
local attr, err = lfs.attributes(self.file_path)
if not attr then
return false, err
elseif self.last_size > attr["size"] then
return true, "truncate"
elseif self.last_size < attr["size"] then
return true, "change size"
elseif self.last_modification ~= attr["modification"] then
return true, "updated"
end
return false, "nothing"
end
function CReader:get_modification()
local attr, err = lfs.attributes(self.file_path)
if not attr then
return nil, err
end
return attr["modification"]
end
function CReader:check_modification(await, is_close)
repeat
if is_close() then
return false, "closed"
end
if not self.is_follow then
return true, "not follow"
end
local is_mod, msg = self:is_modification()
if msg == "nothing" then
if self.is_block then
return true, msg
else
await(self.timer_ms)
end
elseif msg == "updated" then
self.last_modification = self:get_modification()
elseif msg == "truncate" and not self:open() then
return false, "error"
elseif is_mod == false then
return false, msg
else
return true, "changed"
end
until not self.is_block
end
function CReader:get_size()
local attr, err = lfs.attributes(self.file_path)
if not attr then
return nil, err
end
return attr["size"]
end
function CReader:open(file_path, file_op, is_follow, limit, step)
if self.file_handle and io.type(self.file_handle) == "file" then
self:print("Reader already initialized and it will be closed")
self.file_handle:close()
else
self.file_path = nil
self.file_op = nil
self.is_follow = false
self.limit = -1
self.step = 1
if type(file_path) == "string" then
self.file_path = file_path
end
if type(file_op) == "string" then
self.file_op = file_op
end
if type(is_follow) == "boolean" then
self.is_follow = is_follow
end
if type(limit) == "number" and limit >= 0 then
self.limit = limit
end
if type(step) == "number" and step > 0 then
self.step = step
end
end
if self.limit ~= -1 and self.step > self.limit then
return false, "limit should be greater than step"
end
if self.limit < -1 then
return false, "limit should be greater or equal than -1"
end
if self.step <= 0 then
return false, "step should be greater than 0"
end
if self.file_path then
self.dir, self.file_name, self.file_ext =
string.match(self.file_path, "(.-)([^\\/]-%.?([^%.\\/]*))$")
else
return false, "File path doesn't set"
end
self.file_handle = io.open(self.file_path, "rb")
if not self.file_handle then
return false, "Can't open file: " .. file_path
end
if self.file_op == "tail" and self.is_follow then
self.file_handle:seek("end")
elseif self.file_op == "tail" and not self.is_follow then
self.file_handle:seek("set")
if self.limit ~= -1 then
local ofsets = { 0 }
for line in self.file_handle:lines() do
local is_empty = string.match(line, "([^\r\n]*)")
if is_empty and #is_empty > 0 then
table.insert(ofsets, self.file_handle:seek())
end
end
if #ofsets <= self.limit then
self.file_handle:seek("set")
else
self.file_handle:seek("set", ofsets[#ofsets - self.limit])
end
end
elseif self.file_op == "head" then
self.file_handle:seek("set")
else
self:print("File operation doesn't set")
end
self.last_size = self.file_handle:seek()
self.last_modification = self:get_modification()
return true
end
function CReader:get_lines(await, is_close)
local nline = 0
local lines = {}
while self.limit == -1 or nline < self.limit do
local res, msg = self:check_modification(await, is_close)
if not res then
self:print("Check modifications failed: ", msg)
return res, msg
end
local date_marker = os.date("%Y-%m-%d %H:%M:%S ", os.time())
for line in self.file_handle:lines() do
self:print("<" .. date_marker .. "> " .. line)
local is_empty = string.match(line, "([^\r\n]*)")
if is_empty and #is_empty > 0 then
nline = nline + 1
table.insert(lines, line)
if #lines == self.step or nline == self.limit then
self.last_size = self.file_handle:seek()
coroutine.yield(lines)
lines = {}
end
if nline == self.limit then
self:print("Lines limit exceeded: ", nline, self.limit)
break
end
end
end
if not self.is_follow or nline == self.limit or not self.is_block then
if #lines ~= 0 then
self.last_size = self.file_handle:seek()
coroutine.yield(lines)
lines = {}
end
self:print("Get lines function was skipped: ",
self.is_follow, nline, self.limit, self.is_block)
break
end
end
self:print("Get lines function was done")
return true
end
function CReader:close()
if io.type(self.file_handle) == "file" then
self.file_handle:close()
end
self.co = nil
self.file_handle = nil
self.last_size = 0
self.last_modification = 0
end
function CReader:get_sync_func(await, is_close)
if type(await) ~= "function" then
self:print("Replace await function to default: ", await)
await = time.sleep
end
if type(is_close) ~= "function" then
self:print("Replace is_close function to default: ", is_close)
is_close = function()
if not self.file_handle then
return true
end
return false
end
end
return await, is_close
end
function CReader:read_line(await, is_close)
await, is_close = self:get_sync_func(await, is_close)
if not self.co or type(self.co) ~= "thread" then
self.co = coroutine.create(self.get_lines)
end
while coroutine.status(self.co) == "suspended" do
local _, lines = coroutine.resume(self.co, self, await, is_close)
if coroutine.status(self.co) ~= "dead" then
return lines
end
end
self:print("Read line function was done")
if self.is_block then
self:close()
else
self.co = nil
end
end
function CReader:read_line_cb(callback, await, is_close)
if type(callback) ~= "function" then
return false, "Callback doesn't set"
end
await, is_close = self:get_sync_func(await, is_close)
if not self.co or type(self.co) ~= "thread" then
self.co = coroutine.create(self.get_lines)
end
while coroutine.status(self.co) == "suspended" do
local _, lines = coroutine.resume(self.co, self, await, is_close)
if coroutine.status(self.co) ~= "dead" then
callback(lines)
end
end
self:print("Read line function callback was done")
if self.is_block then
self:close()
else
self.co = nil
end
return true
end