Skip to content

Commit

Permalink
update calculated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil committed Nov 26, 2024
1 parent 53dd28b commit 3665892
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
19 changes: 16 additions & 3 deletions docs/parsers/pacman.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Supports the following `pacman` arguments:
- `-Qi`
- `-Qii`

The `*_epoch` calculated timestamp fields are naive. (i.e. based on the
local time of the system the parser is run on)

Usage (cli):

$ pacman -Si <package> | jc --pacman
Expand Down Expand Up @@ -62,10 +65,15 @@ Schema:
"replaces": [
string
],
"download_size": integer, # in bytes
"installed_size": integer, # in bytes
"download_size": string,
"download_size_bytes": integer [0]
"installed_size": string,
"installed_size_bytes": integer, [0]
"packager": string,
"build_date": string,
"build_date_epoch": integer, [0]
"install_date": string,
"install_date_epoch": integer, [0]
"validated_by": [
string
],
Expand All @@ -75,6 +83,8 @@ Schema:
}
]

[0] Field exists if conversion successful

Examples:

$ pacman -qii zstd | jc --pacman -p
Expand Down Expand Up @@ -113,10 +123,13 @@ Examples:
],
"conflicts_with": [],
"replaces": [],
"installed_size": "1563648",
"installed_size": "1527.00 KiB",
"installed_size_bytes": 1563648,
"packager": "Levente Polyak <[email protected]>",
"build_date": "Sat 11 May 2024 06:14:19 AM +08",
"build_date_epoch": 1715433259,
"install_date": "Fri 24 May 2024 09:50:31 AM +08",
"install_date_epoch": 1715663342,
"install_reason": "Installed as a dependency for another package",
"install_script": "No",
"validated_by": [
Expand Down
41 changes: 32 additions & 9 deletions jc/parsers/pacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- `-Qi`
- `-Qii`
The `*_epoch` calculated timestamp fields are naive. (i.e. based on the
local time of the system the parser is run on)
Usage (cli):
$ pacman -Si <package> | jc --pacman
Expand Down Expand Up @@ -57,10 +60,15 @@
"replaces": [
string
],
"download_size": integer, # in bytes
"installed_size": integer, # in bytes
"download_size": string,
"download_size_bytes": integer [0]
"installed_size": string,
"installed_size_bytes": integer, [0]
"packager": string,
"build_date": string,
"build_date_epoch": integer, [0]
"install_date": string,
"install_date_epoch": integer, [0]
"validated_by": [
string
],
Expand All @@ -70,6 +78,8 @@
}
]
[0] Field exists if conversion successful
Examples:
$ pacman -qii zstd | jc --pacman -p
Expand Down Expand Up @@ -108,10 +118,13 @@
],
"conflicts_with": [],
"replaces": [],
"installed_size": "1563648",
"installed_size": "1527.00 KiB",
"installed_size_bytes": 1563648,
"packager": "Levente Polyak <[email protected]>",
"build_date": "Sat 11 May 2024 06:14:19 AM +08",
"build_date_epoch": 1715433259,
"install_date": "Fri 24 May 2024 09:50:31 AM +08",
"install_date_epoch": 1715663342,
"install_reason": "Installed as a dependency for another package",
"install_script": "No",
"validated_by": [
Expand Down Expand Up @@ -195,21 +208,18 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
'licenses', 'groups', 'provides', 'depends_on', 'conflicts_with',
'replaces', 'optional_for'
}

space_split_fields = {
'required_by', 'groups', 'provides', 'depends_on',
'conflicts_with', 'replaces', 'validated_by'
}

two_space_fields = {'licenses', 'validated_by'}

name_description_fields = {'optional_deps'}

size_fields = {'download_size', 'installed_size'}
date_fields = {'build_date', 'install_date'}

# initial split for field lists
for item in proc_data:
for key, val in item.items():
for key, val in item.copy().items():
if key in split_fields:
if val is None:
item[key] = []
Expand Down Expand Up @@ -237,7 +247,20 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
item[key] = new_list

if key in size_fields:
item[key] = jc.utils.convert_size_to_int(val)
bts = jc.utils.convert_size_to_int(val)
if bts:
item[key + '_bytes'] = bts

if key in date_fields:
# need to append '00' to date for conversion
ts = jc.utils.timestamp(val + '00', format_hint=(3100,))
if ts.naive:
item[key + '_epoch'] = ts.naive
else:
# try taking off the text TZ identifier
ts = jc.utils.timestamp(val[:-4], format_hint=(3000,))
if ts.naive:
item[key + '_epoch'] = ts.naive

return proc_data

Expand Down
1 change: 1 addition & 0 deletions jc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ def _parse_dt(
{'id': 1800, 'format': '%d/%b/%Y:%H:%M:%S %z', 'locale': None}, # Common Log Format: 10/Oct/2000:13:55:36 -0700
{'id': 2000, 'format': '%a %d %b %Y %I:%M:%S %p %Z', 'locale': None}, # en_US.UTF-8 local format (found in upower cli output): Tue 23 Mar 2021 04:12:11 PM UTC
{'id': 3000, 'format': '%a %d %b %Y %I:%M:%S %p', 'locale': None}, # en_US.UTF-8 local format with non-UTC tz (found in upower cli output): Tue 23 Mar 2021 04:12:11 PM IST
{'id': 3100, 'format': '%a %d %b %Y %I:%M:%S %p %z', 'locale': None}, # pacman format - append 00 to end to make it work: # Sat 11 May 2024 06:14:19 AM +0800
{'id': 3500, 'format': '%a, %d %b %Y %H:%M:%S %Z', 'locale': None}, # HTTP header time format (always GMT so assume UTC): Wed, 31 Jan 2024 00:39:28 GMT
{'id': 4000, 'format': '%A %d %B %Y %I:%M:%S %p %Z', 'locale': None}, # European-style local format (found in upower cli output): Tuesday 01 October 2019 12:50:41 PM UTC
{'id': 5000, 'format': '%A %d %B %Y %I:%M:%S %p', 'locale': None}, # European-style local format with non-UTC tz (found in upower cli output): Tuesday 01 October 2019 12:50:41 PM IST
Expand Down
2 changes: 1 addition & 1 deletion man/jc.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH jc 1 2024-11-24 1.25.4 "JSON Convert"
.TH jc 1 2024-11-25 1.25.4 "JSON Convert"
.SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types,
and strings
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/generic/pacman--packages.json

Large diffs are not rendered by default.

Loading

0 comments on commit 3665892

Please sign in to comment.