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

88 lines
3.3 KiB
Markdown
Raw Normal View History

2021-09-10 14:27:50 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
# jc.parsers.ls_s
jc - JSON CLI output utility `ls` and `vdir` command output streaming parser
2021-09-13 21:15:08 -07:00
This streaming parser 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
2021-09-13 21:15:08 -07:00
The `jc` `-q` 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
The `epoch` calculated timestamp field is naive (i.e. based on the local time of the system the parser is run on)
The `epoch_utc` calculated timestamp field is timezone-aware and is only available if the timezone field is UTC.
Usage (cli):
$ ls | jc --ls-s
Usage (module):
import jc.parsers.ls_s
result = jc.parsers.ls_s.parse(ls_command_output) # result is an iterable object
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,
"epoch": integer, # naive timestamp if date field exists and can be converted
"epoch_utc": integer, # timezone aware timestamp if date field is in UTC and can be converted
2021-09-13 21:15:08 -07:00
"_meta": # This object only exists if using -q or quiet=True
2021-09-10 14:27:50 -07:00
{
2021-09-10 15:02:15 -07:00
"success": booean, # true if successfully parsed, false if error
"error_msg": string, # exists if "success" is false
"line": string # exists if "success" is false
2021-09-10 14:27:50 -07:00
}
}
Examples:
$ ls -l /usr/bin | jc --ls-s
2021-09-13 21:15:08 -07:00
{"filename":"2to3-","flags":"-rwxr-xr-x","links":4,"owner":"root","group":"wheel","size":925,"date":"Feb 22 2019"}
{"filename":"2to3-2.7","link_to":"../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/2to3-2.7","flags":"lrwxr-xr-x","links":1,"owner":"root","group":"wheel","size":74,"date":"May 4 2019"}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":1,"owner":"root","group":"wheel","size":55152,"date":"May 3 2019"}
2021-09-10 14:27:50 -07:00
...
$ ls -l /usr/bin | jc --ls-s -r
2021-09-13 21:15:08 -07:00
{"filename":"2to3-","flags":"-rwxr-xr-x","links":"4","owner":"root","group":"wheel","size":"925","date":"Feb 22 2019"}
{"filename":"2to3-2.7","link_to":"../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/2to3-2.7","flags":"lrwxr-xr-x","links":"1","owner":"root","group":"wheel","size":"74","date":"May 4 2019"}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":"1","owner":"root","group":"wheel","size":"55152","date":"May 3 2019"}
2021-09-10 14:27:50 -07:00
...
## info
```python
info()
```
Provides parser metadata (version, author, etc.)
## parse
```python
parse(data, raw=False, quiet=False)
```
2021-09-13 21:15:08 -07:00
Main text parsing generator function. Produces an iterable object.
2021-09-10 14:27:50 -07:00
Parameters:
data: (string) line-based text data to parse
raw: (boolean) output preprocessed JSON if True
2021-09-13 21:15:08 -07:00
quiet: (boolean) suppress warning messages and ignore parsing errors 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
## Parser Information
Compatibility: linux, darwin, cygwin, aix, freebsd
2021-09-13 21:15:08 -07:00
Version 0.5 by Kelly Brazil (kellyjonbrazil@gmail.com)