Skip to content

Commit cf64e4a

Browse files
committed
extra normalizer method "normal" and consistent eq/hash semantics for use as dict keys and/or set members.
1 parent 84ad371 commit cf64e4a

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,15 +2149,19 @@ The following code sorts the objects based on their lexical order starting with
21492149
normalized `access_name`, which serves to group identical permissions together:
21502150
```py
21512151
from irods.access import *
2152-
normalize = lambda acl: acl.copy(decanonicalize=-1, implied_zone='tempZone')
2152+
import irods.helpers
21532153
acls = [
21542154
iRODSAccess('read_object', '/tempZone/home/alice', 'bob', 'tempZone'),
21552155
ACLOperation('write', 'rods'),
21562156
ACLOperation('read', 'bob'),
21572157
]
2158+
2159+
session = irods.helpers.make_session()
2160+
normalize = lambda acl: acl.normal(session)
2161+
21582162
print(normalize(acls[0]) == normalize(acls[2]))
21592163
acls.sort(key=normalize)
2160-
print(normalize(iRODSAccess('read', '/tempZone/home/alice', 'bob')) in map(normalize, acls))
2164+
print(normalize(iRODSAccess('read', '', 'bob')) in map(normalize,acls))
21612165
```
21622166

21632167
If strict order of permissions is desired, we can use code such as the following:
@@ -2166,11 +2170,11 @@ from irods.access import *
21662170
from pprint import pp
21672171
pp(sorted(
21682172
[
2169-
ACLOperation('read', 'bob'),
2173+
ACLOperation('read', 'bob' ),
21702174
ACLOperation('own', 'rods'),
21712175
ACLOperation('read_object', 'alice')
21722176
],
2173-
key=lambda acl: (all_permissions[acl.access_name], normalize(acl))
2177+
key=lambda acl: (all_permissions[acl.access_name], acl.normal())
21742178
))
21752179
```
21762180

irods/access.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from irods.collection import iRODSCollection
66
from irods.data_object import iRODSDataObject
7-
from irods.path import iRODSPath
87

98
_permissions = (
109
"own",
@@ -102,23 +101,28 @@ def __init__(self, access_name, path, user_name, user_zone, user_type):
102101
self.user_type = user_type
103102

104103
def __lt__(self, other):
105-
return (self.access_name, self.user_name, self.user_zone, iRODSPath(self.path)) < (
104+
return (self.access_name, self.user_name, self.user_zone, str(self.path)) < (
106105
other.access_name,
107106
other.user_name,
108107
other.user_zone,
109-
iRODSPath(other.path),
108+
str(other.path),
110109
)
111110

112111
def __eq__(self, other):
113112
return (
114113
self.access_name == other.access_name
115-
and iRODSPath(self.path) == iRODSPath(other.path)
114+
and str(self.path) == str(other.path)
116115
and self.user_name == other.user_name
117116
and self.user_zone == other.user_zone
118117
)
119118

120119
def __hash__(self):
121-
return hash((self.access_name, iRODSPath(self.path), self.user_name, self.user_zone))
120+
return hash((self.access_name, str(self.path), self.user_name, self.user_zone))
121+
122+
def normal(self, session = None):
123+
normal_form = self.copy(decanonicalize=-1, implied_zone = (session.zone if session else ''))
124+
normal_form.path = ""
125+
return normal_form
122126

123127
def copy(self, decanonicalize=False, implied_zone=''):
124128
"""

irods/test/access_test.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,16 +514,13 @@ def test_atomic_acls__issue_505(self):
514514
a4 := ACLOperation("read", group.name),
515515
)
516516

517-
def normalize(access):
518-
return access.copy(decanonicalize=-1, implied_zone=ses.zone)
519-
520-
accesses = [normalize(acl) for acl in ses.acls.get(self.coll)]
517+
accesses = {acl.normal(ses) for acl in ses.acls.get(self.coll)}
521518

522519
# Assert that the ACLs we added are among those listed for the object in the catalog.
523-
self.assertIn(normalize(a1), accesses)
524-
self.assertIn(normalize(a2), accesses)
525-
self.assertIn(normalize(a3), accesses)
526-
self.assertIn(normalize(a4), accesses)
520+
self.assertIn(a1.normal(ses), accesses)
521+
self.assertIn(a2.normal(ses), accesses)
522+
self.assertIn(a3.normal(ses), accesses)
523+
self.assertIn(a4.normal(ses), accesses)
527524

528525
finally:
529526
if user1:

0 commit comments

Comments
 (0)