mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-09 01:05:53 +02:00
update slicer to use utils.line_slice()
This commit is contained in:
45
jc/cli.py
45
jc/cli.py
@ -652,18 +652,6 @@ class JcCli():
|
|||||||
utils.error_message(['Parser returned an unsupported object type.'])
|
utils.error_message(['Parser returned an unsupported object type.'])
|
||||||
self.exit_error()
|
self.exit_error()
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def lazy_splitlines(text: str) -> Iterable[str]:
|
|
||||||
start = 0
|
|
||||||
for m in NEWLINES_RE.finditer(text):
|
|
||||||
begin, end = m.span()
|
|
||||||
if begin != start:
|
|
||||||
yield text[start:begin]
|
|
||||||
start = end
|
|
||||||
|
|
||||||
if text[start:]:
|
|
||||||
yield text[start:]
|
|
||||||
|
|
||||||
def slicer(self) -> None:
|
def slicer(self) -> None:
|
||||||
"""Slice input data lazily, if possible. Updates self.data_in"""
|
"""Slice input data lazily, if possible. Updates self.data_in"""
|
||||||
if self.slice_str:
|
if self.slice_str:
|
||||||
@ -673,37 +661,7 @@ class JcCli():
|
|||||||
if slice_end_str:
|
if slice_end_str:
|
||||||
self.slice_end = int(slice_end_str)
|
self.slice_end = int(slice_end_str)
|
||||||
|
|
||||||
if not self.slice_start is None or not self.slice_end is None:
|
self.data_in = utils.line_slice(self.data_in, self.slice_start, self.slice_end)
|
||||||
# standard parsers UTF-8 input
|
|
||||||
if isinstance(self.data_in, str):
|
|
||||||
data_in_iter = self.lazy_splitlines(self.data_in)
|
|
||||||
|
|
||||||
# positive slices
|
|
||||||
if (self.slice_start is None or self.slice_start >= 0) \
|
|
||||||
and (self.slice_end is None or self.slice_end >= 0):
|
|
||||||
|
|
||||||
self.data_in = '\n'.join(islice(data_in_iter, self.slice_start, self.slice_end))
|
|
||||||
|
|
||||||
# negative slices found (non-lazy, uses more memory)
|
|
||||||
else:
|
|
||||||
self.data_in = '\n'.join(list(data_in_iter)[self.slice_start:self.slice_end])
|
|
||||||
|
|
||||||
# standard parsers bytes input
|
|
||||||
elif isinstance(self.data_in, bytes):
|
|
||||||
utils.warning_message(['Cannot slice bytes data.'])
|
|
||||||
|
|
||||||
# streaming parsers UTF-8 input
|
|
||||||
else:
|
|
||||||
# positive slices
|
|
||||||
if (self.slice_start is None or self.slice_start >= 0) \
|
|
||||||
and (self.slice_end is None or self.slice_end >= 0) \
|
|
||||||
and self.data_in:
|
|
||||||
|
|
||||||
self.data_in = islice(self.data_in, self.slice_start, self.slice_end)
|
|
||||||
|
|
||||||
# negative slices found (non-lazy, uses more memory)
|
|
||||||
elif self.data_in:
|
|
||||||
self.data_in = list(self.data_in)[self.slice_start:self.slice_end]
|
|
||||||
|
|
||||||
def create_slurp_output(self) -> None:
|
def create_slurp_output(self) -> None:
|
||||||
"""Slurp output into an array. Only works for single-line strings."""
|
"""Slurp output into an array. Only works for single-line strings."""
|
||||||
@ -742,7 +700,6 @@ class JcCli():
|
|||||||
self.run_timestamp = datetime.now(timezone.utc)
|
self.run_timestamp = datetime.now(timezone.utc)
|
||||||
self.add_metadata_to_output()
|
self.add_metadata_to_output()
|
||||||
|
|
||||||
|
|
||||||
def streaming_parse_and_print(self) -> None:
|
def streaming_parse_and_print(self) -> None:
|
||||||
"""only supports UTF-8 string data for now"""
|
"""only supports UTF-8 string data for now"""
|
||||||
self.data_in = sys.stdin
|
self.data_in = sys.stdin
|
||||||
|
@ -409,10 +409,10 @@ def _lazy_splitlines(text: str) -> Iterable[str]:
|
|||||||
|
|
||||||
|
|
||||||
def line_slice(
|
def line_slice(
|
||||||
data: Union[str, Iterable],
|
data: Union[str, Iterable[str], TextIO, bytes, None],
|
||||||
slice_start: Optional[int] = None,
|
slice_start: Optional[int] = None,
|
||||||
slice_end: Optional[int] = None
|
slice_end: Optional[int] = None
|
||||||
) -> Union[str, Iterable]:
|
) -> Union[str, Iterable[str], TextIO, bytes, None]:
|
||||||
"""
|
"""
|
||||||
Slice input data by lines - lazily, if possible.
|
Slice input data by lines - lazily, if possible.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user