k3cat
Like nix command cat or tail, continuously scan a file line by line.
k3cat is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3cat
Quick Start
import k3cat
# Iterate over lines in a file
for line in k3cat.Cat('/path/to/file', strip=True).iterate(timeout=0):
print(line)
API Reference
k3cat
Just like nix command cat or tail, it continuously scan a file line by line.
It provides with two way for user to handle lines: as a generator or specifying a handler function.
It also remembers the offset of the last scanning in a file in /tmp/.
If a file does not change(inode number does not change), it scans from the last
offset, or it scan from the first byte.
Cat
Bases: object
Source code in k3cat/cat.py
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
__init__(fn, handler=None, file_end_handler=None, exclusive=True, id=None, strip=False, read_chunk_size=read_size)
:param fn: specifies the file to scan.
:param handler: specifies a callable to handle each line, if `Cat()` is not used in
generator mode.
It can be a callable or a list of callable.
See method `Cat.cat()`.
:param file_end_handler: specifies a callable when file end reached.
Every time it scans to end of file, `file_end_handler` is called, but it is still
able to not quit and to wait for new data for a while.
Thus `file_end_handler` will be called more than one time.
:param exclusive: is `True` means at the same time there can be only one same progress
scanning a same file, which means, `Cat` with the same `id` and the same `fn`.
Two `Cat` instances with different id are able to scan a same file at the
same time and they record their own offset in separate file.
By default it is `True`.
:param id: specifies the instance id.
`id` is used to identify a `Cat` instance and is used as part of offset
record file(in `/tmp/`) and is used to exclude other instance.
See `exclusive`.
By default `id` is the file name of the currently running python script.
Thus normally a user does not need to specify `id` explicitly.
:param strip: is `True` or `False` to specifies if to strip blank chars(space, tab, `
and
) before returning each line.
By default it isFalse.
:param read_chunk_size: is the buffer size to read data once, appropriate smallread_chunk_sizewill return stream data quickly.
By default it is161024*2`.
Source code in k3cat/cat.py
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 | |
cat(timeout=None, default_seek=None)
Similar to Cat.iterate except it blocks until timeout or reaches file end and
let Cat.handler to deal with each line.
:return: Nothing.
Source code in k3cat/cat.py
130 131 132 133 134 135 136 137 138 139 140 141 | |
iterate(timeout=None, default_seek=None)
Make a generator to yield every line.
:param timeout: specifies the time in second to wait for new data.
If timeout is `0` or smaller than `0`, it means to scan a file no more than one time:
- If it sees any data, it returning them until it reaches file end.
-
If there is not any data, it raises
NoDataerror.By default it is 3600. :param default_seek: specify a default offset when the last scanned offset is not avaliable or not valid.
Not avaliable mean the stat file used to store the scanning offset is not exist or has broken. For example, when it is the first time to scan a file, the stat file will not exist.
Not valid mean the info stored in stat file is not for the file we are about to scan, this will happen when the same file is deleted and then created, the info stored in stat file is for the deleted file not for the created new file.
We will also treat the last offset stored in stat file as not valid if it is too small than the file size when you set
default_seekto a negative number. And the absolute value ofdefault_seekis the maximum allowed difference.It can take following values:
-
fsutil.SEEK_START: scan from the beginning of the file.
-
fsutil.SEEK_END: scan from the end of the file, mean only new data will be scanned.
-
x(a positive number, includes0). scan from offsetx. -
-x(a negative number). it is used to specify the maximum allowed difference between last offset and file size. If the difference is bigger thanx, then scan fromxbytes before the end of the file, not scan from the last offset. This is usefull when you want to scan from near the end of the file. Usefsutil.SEEK_ENDcan not solve the problem, because it only take effect when the last offset is not avaliable. By default it isk3cat.SEEK_START. :return: a generator.
-
Source code in k3cat/cat.py
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 | |
reset_stat()
Remove the file used to store scanning offset. :return: Nothing
Source code in k3cat/cat.py
480 481 482 483 484 485 486 487 488 489 | |
stat_path()
Returns the full path of the file to store scanning offset. :return: string
Source code in k3cat/cat.py
334 335 336 337 338 339 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)