1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00
Files
jc/docs/parsers/ls_s.md
2021-09-10 15:02:15 -07:00

3.4 KiB

Home

jc.parsers.ls_s

jc - JSON CLI output utility ls and vdir command output streaming parser

Options supported:

  • lbaR1
  • --time-style=full-iso

Note: The -1, -l, or -b option of ls should be used to correctly parse filenames that include newline characters. Since ls does not encode newlines in filenames when outputting to a pipe it will cause jc to see multiple files instead of a single file if -1, -l, or -b is not used. Alternatively, vdir can be used, which is the same as running ls -lb.

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:

{
  "filename":       string,
  "flags":          string,
  "links":          integer,
  "parent":         string,      # not yet implemented
  "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
  "_meta":
    {
      "success":    booean,      # true if successfully parsed, false if error
      "error_msg":  string,      # exists if "success" is false
      "line":       string       # exists if "success" is false
    }
}

Examples:

$ ls -l /usr/bin | jc --ls-s
{"filename":"2to3-","flags":"-rwxr-xr-x","links":4,"owner":"root","group":"wheel","size":925,"date":"Feb 22 2019","_meta":{"success":true}}
{"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","_meta":{"success":true}}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":1,"owner":"root","group":"wheel","size":55152,"date":"May 3 2019","_meta":{"success":true}}
...

$ ls -l /usr/bin | jc --ls-s -r
{"filename":"2to3-","flags":"-rwxr-xr-x","links":"4","owner":"root","group":"wheel","size":"925","date":"Feb 22 2019","_meta":{"success":true}}
{"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","_meta":{"success":true}}
{"filename":"AssetCacheLocatorUtil","flags":"-rwxr-xr-x","links":"1","owner":"root","group":"wheel","size":"55152","date":"May 3 2019","_meta":{"success":true}}
...

info

info()

Provides parser metadata (version, author, etc.)

parse

parse(data, raw=False, quiet=False)

Main text parsing function

Parameters:

data:        (string)  line-based text data to parse
raw:         (boolean) output preprocessed JSON if True
quiet:       (boolean) suppress warning messages if True

Returns:

List of Dictionaries. Raw or processed structured data.

Parser Information

Compatibility: linux, darwin, cygwin, aix, freebsd

Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com)