Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nicolas Dandrimont
swh-model
Commits
bea256e3
Commit
bea256e3
authored
4 years ago
by
vlorentz
Browse files
Options
Downloads
Patches
Plain Diff
Make SWHID immutable and hashable.
parent
06837d5c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/model/identifiers.py
+11
-5
11 additions, 5 deletions
swh/model/identifiers.py
swh/model/test_identifiers.py
+72
-0
72 additions, 0 deletions
swh/model/test_identifiers.py
with
83 additions
and
5 deletions
swh/model/identifiers.py
+
11
−
5
View file @
bea256e3
...
...
@@ -8,10 +8,11 @@ import datetime
import
hashlib
from
functools
import
lru_cache
from
typing
import
Any
,
Dict
,
NamedTuple
from
typing
import
Any
,
Dict
,
NamedTuple
,
Union
from
deprecated
import
deprecated
from
.collections
import
ImmutableDict
from
.exceptions
import
ValidationError
from
.fields.hashes
import
validate_sha1
from
.hashutil
import
hash_git_data
,
hash_to_hex
,
MultiHash
...
...
@@ -656,7 +657,7 @@ _SWHID = NamedTuple(
(
"
scheme_version
"
,
int
),
(
"
object_type
"
,
str
),
(
"
object_id
"
,
str
),
(
"
metadata
"
,
Dict
[
str
,
Any
]),
(
"
metadata
"
,
Immutable
Dict
[
str
,
Any
]),
],
)
...
...
@@ -707,7 +708,7 @@ class SWHID(_SWHID):
scheme_version
:
int
=
SWHID_VERSION
,
object_type
:
str
=
""
,
object_id
:
str
=
""
,
metadata
:
Dict
[
str
,
Any
]
=
{}
,
metadata
:
Union
[
ImmutableDict
[
str
,
Any
],
Dict
[
str
,
Any
]]
=
ImmutableDict
()
,
):
o
=
_object_type_map
.
get
(
object_type
)
if
not
o
:
...
...
@@ -730,7 +731,12 @@ class SWHID(_SWHID):
validate_sha1
(
object_id
)
# can raise if invalid hash
object_id
=
hash_to_hex
(
object_id
)
return
super
().
__new__
(
cls
,
namespace
,
scheme_version
,
object_type
,
object_id
,
metadata
cls
,
namespace
,
scheme_version
,
object_type
,
object_id
,
ImmutableDict
(
metadata
),
)
def
__str__
(
self
)
->
str
:
...
...
@@ -764,7 +770,7 @@ def swhid(
object_type
:
str
,
object_id
:
str
,
scheme_version
:
int
=
1
,
metadata
:
Dict
[
str
,
Any
]
=
{}
,
metadata
:
Union
[
ImmutableDict
[
str
,
Any
],
Dict
[
str
,
Any
]]
=
ImmutableDict
()
,
)
->
str
:
"""
Compute :ref:`persistent-identifiers`
...
...
This diff is collapsed.
Click to expand it.
swh/model/test_identifiers.py
0 → 100644
+
72
−
0
View file @
bea256e3
# Copyright (C) 2020 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from
swh.model.identifiers
import
SWHID
def
test_swhid_hash
():
object_id
=
"
94a9ed024d3859793618152ea559a168bbcbb5e2
"
assert
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
))
==
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
)
)
assert
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
)
==
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
)
# Different order of the dictionary, so the underlying order of the tuple in
# ImmutableDict is different.
assert
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
)
==
hash
(
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
baz
"
:
"
qux
"
,
"
foo
"
:
"
bar
"
},
)
)
def
test_swhid_eq
():
object_id
=
"
94a9ed024d3859793618152ea559a168bbcbb5e2
"
assert
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
)
==
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
)
assert
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
==
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
assert
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
foo
"
:
"
bar
"
,
"
baz
"
:
"
qux
"
},
)
==
SWHID
(
object_type
=
"
directory
"
,
object_id
=
object_id
,
metadata
=
{
"
baz
"
:
"
qux
"
,
"
foo
"
:
"
bar
"
},
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment