Skip to content

Commit 6ce7331

Browse files
Parse Int Properties Where Possible
1 parent 50bf74b commit 6ce7331

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zfslib"
3-
version = "0.10.0"
3+
version = "0.11.0"
44
description = "ZFS Utilities For Python3"
55
license = "MIT"
66
authors = ["Timothy C. Quinn"]

src/zfslib/zfslib.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
import fnmatch
1616
import pathlib
1717
import inspect
18-
import sys
1918
from collections import OrderedDict
2019
from datetime import datetime, timedelta, date as dt_date
2120

22-
is_py2=(sys.version_info[0] == 2)
23-
2421
class __DEFAULT__(object):pass
2522

2623
class Connection:
@@ -108,7 +105,6 @@ def lookup(self, name):
108105
# [_test_data_zfs] testing only
109106
# [_test_data_zpool] testing only
110107
def _load(self, get_mounts=True, zfs_props=None, zpool_props=None, _test_data_zfs=None, _test_data_zpool=None):
111-
global is_py2
112108

113109
# setup zfs list properties (zfs list -o <props>)
114110
_zfs_pdef=['name', 'creation']
@@ -146,11 +142,20 @@ def _load(self, get_mounts=True, zfs_props=None, zpool_props=None, _test_data_zf
146142

147143
def extract_properties(s, zpool:bool=False):
148144
props = zpool_props if zpool else zfs_props
149-
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
145+
if isinstance(s, bytes): s = s.decode('utf-8')
150146
items = s.strip().split( '\t' )
151147
assert len( items ) == len( props ), (props, items)
152-
propvalues = map( lambda x: None if x == '-' else x, items[1:] )
153-
return [ items[ 0 ], zip( props[ 1: ], propvalues ) ]
148+
for i in range(1,len(props)):
149+
v = items[i]
150+
if v == '-':
151+
items[i] = None
152+
elif props[i] in ZFS_INT_PROPS:
153+
try:
154+
items[i] = int(v)
155+
except:
156+
pass
157+
158+
return [ items[ 0 ], zip( props[ 1: ], items[ 1: ] ) ]
154159

155160
# Gather zfs list data
156161
if _test_data_zfs is None:
@@ -575,7 +580,6 @@ def __init__(self, pool, name, parent=None):
575580
# - M The path has been modified
576581
# - R The path has been renamed
577582
def get_diffs(self, snap_from, snap_to=None, include=None, exclude=None, file_type=None, chg_type=None):
578-
global is_py2
579583
self.assertHaveMounts()
580584
assert self.mounted, "Cannot get diffs for Unmounted Dataset. Verify mounted flag on Dataset before calling"
581585

@@ -620,7 +624,7 @@ def __tv(k, v):
620624

621625

622626
def __row(s):
623-
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
627+
if isinstance(s, bytes): s = s.decode('utf-8')
624628
return s.strip().split( '\t' )
625629

626630
rows = list(map(lambda s: __row(s), stdout.splitlines()))
@@ -893,7 +897,7 @@ def __str__(self):
893897

894898
''' END ZFS Entities '''
895899

896-
900+
ZFS_INT_PROPS = set("allocated,available,capacity,checkpoint,createtxg,expandsize,filesystem_count,filesystem_limit,fragmentation,free,freeing,leaked,logicalreferenced,logicalused,objsetid,quota,referenced,refquota,refreservation,reservation,size,snapshot_count,snapshot_limit,used,usedbychildren,usedbydataset,usedbyrefreservation,usedbysnapshots,userrefs,volsize,written".split(','))
897901

898902
''' General Utilities '''
899903

tests/test_zfslib.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,23 @@ def test_zfsitem_get_child_n_properties(self):
171171

172172
pool = poolset.lookup('rpool')
173173
self.assertIsInstance(pool, zfs.Pool)
174-
self.assertEqual(pool.get_property('size'), '249108103168')
175-
self.assertEqual(pool.get_property('allocated'), '156426862592')
176-
self.assertEqual(pool.get_property('free'), '92681240576')
174+
self.assertEqual(pool.get_property('size'), 249108103168)
175+
self.assertEqual(pool.get_property('allocated'), 156426862592)
176+
self.assertEqual(pool.get_property('free'), 92681240576)
177177
self.assertEqual(pool.get_property('checkpoint'), None)
178-
self.assertEqual(pool.get_property('fragmentation'), '50')
179-
self.assertEqual(pool.get_property('capacity'), '62')
178+
self.assertEqual(pool.get_property('fragmentation'), 50)
179+
self.assertEqual(pool.get_property('capacity'), 62)
180180
self.assertEqual(pool.get_property('health'), 'OFFLINE')
181181
self.assertEqual(pool.get_property('readonly'), 'off')
182182

183183
ds = pool.lookup('ROOT')
184184
self.assertIsInstance(ds, zfs.Dataset)
185185
ds2=ds.get_child('ubuntu_n2qr5q')
186186
self.assertIsInstance(ds2, zfs.Dataset)
187-
self.assertEqual(ds2.get_property('used'), '12518498304')
187+
self.assertEqual(ds2.get_property('used'), 12518498304)
188188
ds3=ds2.get_child('srv')
189189
self.assertIsInstance(ds3, zfs.Dataset)
190-
self.assertEqual(ds3.get_property('used'), '327680')
190+
self.assertEqual(ds3.get_property('used'), 327680)
191191
snap = ds3.get_child('autozsys_j57yyo')
192192
self.assertIsInstance(snap, zfs.Snapshot)
193193

@@ -201,9 +201,9 @@ def test_zfsitem_get_child_n_properties(self):
201201
s_ts='1608446351'
202202
self.assertEqual(ds.get_property('creation'), s_ts)
203203
self.assertEqual(ds.creation, dt_from_creation(s_ts))
204-
self.assertEqual(ds.get_property('used'), '280644734976')
205-
self.assertEqual(ds.get_property('available'), '3564051083264')
206-
self.assertEqual(ds.get_property('referenced'), '266918285312')
204+
self.assertEqual(ds.get_property('used'), 280644734976)
205+
self.assertEqual(ds.get_property('available'), 3564051083264)
206+
self.assertEqual(ds.get_property('referenced'), 266918285312)
207207
self.assertEqual(ds.mountpoint, '/dpool/other')
208208
self.assertEqual(ds.mounted, True)
209209
self.assertEqual(ds.has_mount, True)
@@ -212,9 +212,9 @@ def test_zfsitem_get_child_n_properties(self):
212212
ds = poolset.lookup('bpool/BOOT')
213213
self.assertIsInstance(ds, zfs.Dataset)
214214
self.assertEqual(ds.get_property('creation'), '1608154061')
215-
self.assertEqual(ds.get_property('used'), '190410752')
216-
self.assertEqual(ds.get_property('available'), '1686265856')
217-
self.assertEqual(ds.get_property('referenced'), '98304')
215+
self.assertEqual(ds.get_property('used'), 190410752)
216+
self.assertEqual(ds.get_property('available'), 1686265856)
217+
self.assertEqual(ds.get_property('referenced'), 98304)
218218
self.assertEqual(ds.mountpoint, 'none')
219219
self.assertEqual(ds.has_mount, False)
220220
self.assertEqual(ds.mounted, False)

0 commit comments

Comments
 (0)