1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00

add tests and doc update

This commit is contained in:
Kelly Brazil
2024-09-22 20:01:40 -07:00
parent 97e9798cef
commit f2a8e0087b
8 changed files with 77 additions and 4 deletions

View File

@ -1,12 +1,14 @@
jc changelog jc changelog
20240907 v1.25.4 20240922 v1.25.4
- Enhance `ping-s` streaming parser to support error replies - Enhance `ping-s` streaming parser to support error replies
- Enhance `ethtool` parser to support `link_partner_advertised_link_modes` - Enhance `ethtool` parser to support `link_partner_advertised_link_modes`
- Enhance `ifconfig` parser to support `utun` interfaces with assigned IPv4 addresses on macOS - Enhance `ifconfig` parser to support `utun` interfaces with assigned IPv4 addresses on macOS
- Fix `df` parser to correctly output binary vs. decimal size outputs
- Fix `mount` parser for cases where there are spaces in the filesystem name - Fix `mount` parser for cases where there are spaces in the filesystem name
- Fix `ip-address` parser for Python 3.13 changes to IPv4 mapped IPv6 addresses - Fix `ip-address` parser for Python 3.13 changes to IPv4 mapped IPv6 addresses
- Fix `uptime` parser for data that contains `user` instead of `users` - Fix `uptime` parser for data that contains `user` instead of `users`
- Enhance `jc.utils.convert_size_to_int()` to add `posix_mode` and `decimal_bias` parameters
20240609 v1.25.3 20240609 v1.25.3
- Enhance `bluetoothctl` parser with added `battery_percentage` field - Enhance `bluetoothctl` parser with added `battery_percentage` field

View File

@ -124,4 +124,4 @@ Compatibility: linux, darwin, freebsd
Source: [`jc/parsers/df.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/df.py) Source: [`jc/parsers/df.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/df.py)
Version 2.0 by Kelly Brazil (kellyjonbrazil@gmail.com) Version 2.1 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@ -55,7 +55,10 @@ Returns:
### convert_size_to_int ### convert_size_to_int
```python ```python
def convert_size_to_int(size: str, binary: bool = False) -> Optional[int] def convert_size_to_int(size: str,
binary: bool = False,
posix_mode: bool = False,
decimal_bias: bool = False) -> Optional[int]
``` ```
Parse a human readable data size and return the number of bytes. Parse a human readable data size and return the number of bytes.
@ -66,6 +69,11 @@ Parameters:
binary: (boolean) `True` to use binary multiples of bytes binary: (boolean) `True` to use binary multiples of bytes
(base-2) for ambiguous unit symbols and names, (base-2) for ambiguous unit symbols and names,
`False` to use decimal multiples of bytes (base-10). `False` to use decimal multiples of bytes (base-10).
posix_mode: Treat one-letter units (k, m, g, etc.) as binary.
decimal_bias: (boolean) `True` to treat slightly ambiguous two-
letter unit symbols ending in "i" (e.g. Ki, Gi) to
use decimal multiples of bytes (base-10). `False`
(default) to use binary multiples of bytes.
Returns: Returns:
integer/None Integer if successful conversion, otherwise None integer/None Integer if successful conversion, otherwise None
@ -85,6 +93,10 @@ gigabytes, terabytes and petabytes. Some examples:
1000 1000
>>> convert_size_to_int('1 KiB') >>> convert_size_to_int('1 KiB')
1024 1024
>>> convert_size_to_int('1 Ki')
1024
>>> convert_size_to_int('1 Ki', decimal_bias=True)
1000
>>> convert_size_to_int('1 KB', binary=True) >>> convert_size_to_int('1 KB', binary=True)
1024 1024
>>> convert_size_to_int('1.5 GB') >>> convert_size_to_int('1.5 GB')

View File

@ -1,4 +1,4 @@
.TH jc 1 2024-09-07 1.25.4 "JSON Convert" .TH jc 1 2024-09-22 1.25.4 "JSON Convert"
.SH NAME .SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types,
and strings and strings
@ -507,6 +507,11 @@ openvpn-status.log file parser
\fB--os-release\fP \fB--os-release\fP
`/etc/os-release` file parser `/etc/os-release` file parser
.TP
.B
\fB--pacman\fP
`pacman` command parser
.TP .TP
.B .B
\fB--passwd\fP \fB--passwd\fP

1
tests/fixtures/osx-10.14.6/df-hh.json vendored Normal file
View File

@ -0,0 +1 @@
[{"filesystem":"/dev/disk3s3s1","size":494000000000,"used":16000000000,"iused":356840,"ifree":3660412200,"mounted_on":"/","available":375000000000,"capacity_percent":5,"iused_percent":0},{"filesystem":"devfs","size":215000,"used":215000,"iused":729,"ifree":0,"mounted_on":"/dev","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk3s6","size":494000000000,"used":20000,"iused":0,"ifree":3660412200,"mounted_on":"/System/Volumes/VM","available":375000000000,"capacity_percent":1,"iused_percent":0},{"filesystem":"/dev/disk3s4","size":494000000000,"used":11000000000,"iused":1466,"ifree":3660412200,"mounted_on":"/System/Volumes/Preboot","available":375000000000,"capacity_percent":3,"iused_percent":0},{"filesystem":"/dev/disk3s2","size":494000000000,"used":349000000,"iused":162,"ifree":3660412200,"mounted_on":"/System/Volumes/Update","available":375000000000,"capacity_percent":1,"iused_percent":0},{"filesystem":"/dev/disk1s2","size":524000000,"used":6300000,"iused":1,"ifree":4933600,"mounted_on":"/System/Volumes/xarts","available":505000000,"capacity_percent":2,"iused_percent":0},{"filesystem":"/dev/disk1s1","size":524000000,"used":6500000,"iused":31,"ifree":4933600,"mounted_on":"/System/Volumes/iSCPreboot","available":505000000,"capacity_percent":2,"iused_percent":0},{"filesystem":"/dev/disk1s3","size":524000000,"used":1300000,"iused":63,"ifree":4933600,"mounted_on":"/System/Volumes/Hardware","available":505000000,"capacity_percent":1,"iused_percent":0},{"filesystem":"/dev/disk3s1","size":494000000000,"used":90000000000,"iused":724666,"ifree":3660412200,"mounted_on":"/System/Volumes/Data","available":375000000000,"capacity_percent":20,"iused_percent":0},{"filesystem":"map auto_home","size":0,"used":0,"iused":0,"ifree":0,"mounted_on":"/System/Volumes/Data/home","available":0,"capacity_percent":100,"iused_percent":100},{"filesystem":"/dev/disk2s1","size":5400000000,"used":1700000000,"iused":62,"ifree":35253480,"mounted_on":"/System/Volumes/Update/SFR/mnt1","available":3600000000,"capacity_percent":33,"iused_percent":0},{"filesystem":"/dev/disk3s3","size":494000000000,"used":16000000000,"iused":404477,"ifree":3660412200,"mounted_on":"/System/Volumes/Update/mnt1","available":375000000000,"capacity_percent":5,"iused_percent":0}]

13
tests/fixtures/osx-10.14.6/df-hh.out vendored Normal file
View File

@ -0,0 +1,13 @@
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk3s3s1 494G 16G 375G 5% 356840 3660412200 0% /
devfs 215k 215k 0B 100% 729 0 100% /dev
/dev/disk3s6 494G 20k 375G 1% 0 3660412200 0% /System/Volumes/VM
/dev/disk3s4 494G 11G 375G 3% 1466 3660412200 0% /System/Volumes/Preboot
/dev/disk3s2 494G 349M 375G 1% 162 3660412200 0% /System/Volumes/Update
/dev/disk1s2 524M 6.3M 505M 2% 1 4933600 0% /System/Volumes/xarts
/dev/disk1s1 524M 6.5M 505M 2% 31 4933600 0% /System/Volumes/iSCPreboot
/dev/disk1s3 524M 1.3M 505M 1% 63 4933600 0% /System/Volumes/Hardware
/dev/disk3s1 494G 90G 375G 20% 724666 3660412200 0% /System/Volumes/Data
map auto_home 0B 0B 0B 100% 0 0 100% /System/Volumes/Data/home
/dev/disk2s1 5.4G 1.7G 3.6G 33% 62 35253480 0% /System/Volumes/Update/SFR/mnt1
/dev/disk3s3 494G 16G 375G 5% 404477 3660412200 0% /System/Volumes/Update/mnt1

View File

@ -33,6 +33,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-h.out'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-h.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_df_h = f.read() osx_10_14_6_df_h = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-hh.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_df_hh = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/df-long-filesystem.out'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/df-long-filesystem.out'), 'r', encoding='utf-8') as f:
generic_df_long_filesystem = f.read() generic_df_long_filesystem = f.read()
@ -61,6 +64,9 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-h.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-h.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_df_h_json = json.loads(f.read()) osx_10_14_6_df_h_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/df-hh.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_df_hh_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/df-long-filesystem.json'), 'r', encoding='utf-8') as f: with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/df-long-filesystem.json'), 'r', encoding='utf-8') as f:
generic_df_long_filesystem_json = json.loads(f.read()) generic_df_long_filesystem_json = json.loads(f.read())
@ -119,6 +125,12 @@ class MyTests(unittest.TestCase):
""" """
self.assertEqual(jc.parsers.df.parse(self.osx_10_14_6_df_h, quiet=True), self.osx_10_14_6_df_h_json) self.assertEqual(jc.parsers.df.parse(self.osx_10_14_6_df_h, quiet=True), self.osx_10_14_6_df_h_json)
def test_df_hh_osx_10_14_6(self):
"""
Test 'df -H' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.df.parse(self.osx_10_14_6_df_hh, quiet=True), self.osx_10_14_6_df_hh_json)
def test_df_long_filesystem(self): def test_df_long_filesystem(self):
""" """
Test older version of 'df' with long filesystem data Test older version of 'df' with long filesystem data

View File

@ -209,6 +209,34 @@ class MyTests(unittest.TestCase):
self.assertEqual(jc.utils.convert_size_to_int(input_string, binary=True), expected_output) self.assertEqual(jc.utils.convert_size_to_int(input_string, binary=True), expected_output)
def test_utils_convert_size_to_int_posix_mode(self):
io_map = {
'1 K': 1024,
'1 KiB': 1024,
'1 KB': 1000,
'1.5 G': 1610612736,
'1.5 GiB': 1610612736,
'1.5 GB': 1500000000
}
for input_string, expected_output in io_map.items():
self.assertEqual(jc.utils.convert_size_to_int(input_string, posix_mode=True), expected_output)
def test_utils_convert_size_to_int_decimal_bias(self):
io_map = {
'1 K': 1000,
'1 Ki': 1000,
'1 KiB': 1024,
'1.5 G': 1500000000,
'1.5 Gi': 1500000000,
'1.5 GiB': 1610612736
}
for input_string, expected_output in io_map.items():
self.assertEqual(jc.utils.convert_size_to_int(input_string, decimal_bias=True), expected_output)
def test_utils_has_data_nodata(self): def test_utils_has_data_nodata(self):
self.assertFalse(jc.utils.has_data(' \n ')) self.assertFalse(jc.utils.has_data(' \n '))