1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/docs/parsers/ls_s.md

118 lines
3.2 KiB
Markdown
Raw Normal View History

2021-09-10 14:27:50 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
# jc.parsers.ls_s
2022-01-19 17:30:14 -08:00
jc - JSON CLI output utility `ls` and `vdir` command output streaming
parser
2021-09-10 14:27:50 -07:00
> This streaming parser outputs JSON Lines
2022-01-19 17:30:14 -08:00
Requires the `-l` option to be used on `ls`. If there are newline characters
in the filename, then make sure to use the `-b` option on `ls`.
2021-09-10 14:27:50 -07:00
2022-01-19 17:30:14 -08:00
The `jc` `-qq` option can be used to ignore parsing errors. (e.g. filenames
with newline characters, but `-b` was not used)
2021-09-10 14:27:50 -07:00
2022-01-19 21:32:21 -08:00
The `epoch` calculated timestamp field is naive (i.e. based on the local
time of the system the parser is run on)
2021-09-10 14:27:50 -07:00
2022-01-19 17:30:14 -08:00
The `epoch_utc` calculated timestamp field is timezone-aware and is only
available if the timezone field is UTC.
2021-09-10 14:27:50 -07:00
Usage (cli):
$ ls | jc --ls-s
Usage (module):
2022-01-18 15:38:03 -08:00
import jc
2022-01-19 17:30:14 -08:00
# result is an iterable object (generator)
result = jc.parse('ls_s', ls_command_output.splitlines())
2022-01-18 15:38:03 -08:00
for item in result:
# do something
or
2021-09-10 14:27:50 -07:00
import jc.parsers.ls_s
2022-01-19 17:30:14 -08:00
# result is an iterable object (generator)
result = jc.parsers.ls_s.parse(ls_command_output.splitlines())
2021-09-10 14:27:50 -07:00
for item in result:
# do something
Schema:
{
2021-09-10 15:01:02 -07:00
"filename": string,
"flags": string,
"links": integer,
2021-09-13 21:15:08 -07:00
"parent": string,
2021-09-10 15:01:02 -07:00
"owner": string,
"group": string,
"size": integer,
"date": string,
2022-01-19 17:30:14 -08:00
"epoch": integer, # [0]
"epoch_utc": integer, # [1]
# Below object only exists if using -qq or ignore_exceptions=True
"_jc_meta":
2021-09-10 14:27:50 -07:00
{
2022-01-19 17:30:14 -08:00
"success": boolean, # false if error parsing
"error": string, # exists if "success" is false
2021-09-10 15:02:15 -07:00
"line": string # exists if "success" is false
2021-09-10 14:27:50 -07:00
}
}
2022-01-19 21:32:21 -08:00
[0] naive timestamp if date field exists and can be converted.
[1] timezone aware timestamp if date field is in UTC and can
be converted
2022-01-19 17:30:14 -08:00
2021-09-10 14:27:50 -07:00
Examples:
$ ls -l /usr/bin | jc --ls-s
2022-01-19 17:30:14 -08:00
{"filename":"2to3-","flags":"-rwxr-xr-x","links":4,"owner":"root","...}
{"filename":"2to3-2.7","link_to":"../../System/Library/Frameworks/P...}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":1,...}
2021-09-10 14:27:50 -07:00
...
$ ls -l /usr/bin | jc --ls-s -r
2022-01-19 17:30:14 -08:00
{"filename":"2to3-","flags":"-rwxr-xr-x","links":"4","owner":"roo"..."}
{"filename":"2to3-2.7","link_to":"../../System/Library/Frameworks/P...}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":"1...}
2021-09-10 14:27:50 -07:00
...
## info
```python
info()
```
Provides parser metadata (version, author, etc.)
## parse
```python
2021-09-23 11:48:39 -07:00
parse(data, raw=False, quiet=False, ignore_exceptions=False)
2021-09-10 14:27:50 -07:00
```
2021-09-21 16:03:00 -07:00
Main text parsing generator function. Returns an iterator object.
2021-09-10 14:27:50 -07:00
Parameters:
2022-01-19 17:30:14 -08:00
data: (iterable) line-based text data to parse
(e.g. sys.stdin or str.splitlines())
2022-01-21 07:42:03 -08:00
raw: (boolean) unprocessed output if True
2021-09-23 11:48:39 -07:00
quiet: (boolean) suppress warning messages if True
ignore_exceptions: (boolean) ignore parsing exceptions if True
2021-09-10 14:27:50 -07:00
2021-09-13 21:15:08 -07:00
Yields:
2021-09-10 14:27:50 -07:00
2021-09-13 21:15:08 -07:00
Dictionary. Raw or processed structured data.
2021-09-10 14:27:50 -07:00
2021-09-21 16:03:00 -07:00
Returns:
Iterator object
2021-09-10 14:27:50 -07:00
## Parser Information
Compatibility: linux, darwin, cygwin, aix, freebsd
2021-12-01 16:12:51 -08:00
Version 0.6 by Kelly Brazil (kellyjonbrazil@gmail.com)