Move each_chunk into util.py
This commit is contained in:
parent
f917ba20bd
commit
2afcf59ba0
2 changed files with 32 additions and 23 deletions
|
@ -4,30 +4,8 @@ import argparse
|
|||
|
||||
from xmet.db import Database
|
||||
from xmet.afos import AFOSMessageParser
|
||||
from xmet.util import each_chunk
|
||||
|
||||
CHUNK_SIZE = 4096
|
||||
|
||||
def each_chunk(fh, sep: str):
|
||||
buf = ''
|
||||
|
||||
while True:
|
||||
chunk = fh.read(CHUNK_SIZE)
|
||||
|
||||
if chunk == '' or chunk is None:
|
||||
yield buf.strip()
|
||||
break
|
||||
|
||||
buf += chunk
|
||||
|
||||
while True:
|
||||
try:
|
||||
part, buf = buf.split(sep, 1)
|
||||
except ValueError:
|
||||
break
|
||||
else:
|
||||
yield part.strip()
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description = 'Ingest National Weather Service text bulletin products'
|
||||
)
|
||||
|
||||
|
|
31
lib/xmet/util.py
Normal file
31
lib/xmet/util.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import io
|
||||
|
||||
CHUNK_SIZE = 4096
|
||||
CHUNK_STRIP = "\x01\x03\x0a\x20"
|
||||
|
||||
def each_chunk(fh: io.TextIOBase, sep: str, strip=None):
|
||||
buf = ''
|
||||
|
||||
while True:
|
||||
chunk = fh.read(CHUNK_SIZE)
|
||||
|
||||
if chunk == '':
|
||||
ret = buf.strip(strip)
|
||||
|
||||
if ret != '':
|
||||
yield ret
|
||||
|
||||
break
|
||||
|
||||
buf += chunk
|
||||
|
||||
while True:
|
||||
try:
|
||||
part, buf = buf.split(sep, 1)
|
||||
except ValueError:
|
||||
break
|
||||
else:
|
||||
ret = part.strip(strip)
|
||||
|
||||
if ret != '':
|
||||
yield ret
|
Loading…
Add table
Reference in a new issue