Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-deposit
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
Antoine R. Dumont
swh-deposit
Commits
c1832762
Verified
Commit
c1832762
authored
5 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
Refactor: Extract date utils manipulation function into utils module
parent
00a8abd4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
swh/deposit/api/private/deposit_read.py
+3
-26
3 additions, 26 deletions
swh/deposit/api/private/deposit_read.py
swh/deposit/tests/test_utils.py
+51
-1
51 additions, 1 deletion
swh/deposit/tests/test_utils.py
swh/deposit/utils.py
+28
-1
28 additions, 1 deletion
swh/deposit/utils.py
with
82 additions
and
28 deletions
swh/deposit/api/private/deposit_read.py
+
3
−
26
View file @
c1832762
...
...
@@ -9,12 +9,12 @@ import shutil
import
tempfile
from
contextlib
import
contextmanager
from
dateutil
import
parser
from
django.http
import
FileResponse
from
rest_framework
import
status
from
swh.core
import
tarball
from
swh.model
import
identifiers
from
swh.deposit.utils
import
normalize_date
from
.
import
DepositReadMixin
from
...config
import
SWH_PERSON
,
ARCHIVE_TYPE
...
...
@@ -137,29 +137,6 @@ class SWHDepositReadMetadata(SWHGetDepositAPI, SWHPrivateAPIView,
if
client_domain
in
metadata
[
field
]:
return
metadata
[
field
]
def
_prepare_date
(
self
,
date
):
"""
Prepare date fields as normalized swh date
If date is a list, elect arbitrarily the first element of that
list
If date is (then) a string, parse it through
dateutil.parser.parse to extract a datetime.
Then normalize it through
swh.model.identifiers.normalize_timestamp.
Returns
The swh date object
"""
if
isinstance
(
date
,
list
):
date
=
date
[
0
]
if
isinstance
(
date
,
str
):
date
=
parser
.
parse
(
date
)
return
identifiers
.
normalize_timestamp
(
date
)
def
_normalize_dates
(
self
,
deposit
,
metadata
):
"""
Normalize the date to use as a tuple of author date, committer date
from the incoming metadata.
...
...
@@ -186,8 +163,8 @@ class SWHDepositReadMetadata(SWHGetDepositAPI, SWHPrivateAPIView,
author_date
=
deposit
.
complete_date
commit_date
=
deposit
.
complete_date
return
(
self
.
_prepar
e_date
(
author_date
),
self
.
_prepar
e_date
(
commit_date
)
normaliz
e_date
(
author_date
),
normaliz
e_date
(
commit_date
)
)
def
metadata_read
(
self
,
deposit
):
...
...
This diff is collapsed.
Click to expand it.
swh/deposit/tests/test_utils.py
+
51
−
1
View file @
c1832762
# Copyright (C) 2018 The Software Heritage developers
# Copyright (C) 2018
-2019
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
import
unittest
from
unittest.mock
import
patch
from
swh.deposit
import
utils
...
...
@@ -130,3 +131,52 @@ class UtilsTestCase(unittest.TestCase):
utils
.
merge
(
d1
)
self
.
assertEqual
(
utils
.
merge
(
d0
),
d0
)
@patch
(
'
swh.deposit.utils.normalize_timestamp
'
,
side_effect
=
lambda
x
:
x
)
def
test_normalize_date_0
(
mock_normalize
):
"""
When date is a list, choose the first date and normalize it
Note: We do not test swh.model.identifiers which is already tested
in swh.model
"""
actual_date
=
utils
.
normalize_date
([
'
2017-10-12
'
,
'
date1
'
])
expected_date
=
'
2017-10-12 00:00:00
'
assert
str
(
actual_date
)
==
expected_date
@patch
(
'
swh.deposit.utils.normalize_timestamp
'
,
side_effect
=
lambda
x
:
x
)
def
test_normalize_date_1
(
mock_normalize
):
"""
Providing a date in a reasonable format, everything is fine
Note: We do not test swh.model.identifiers which is already tested
in swh.model
"""
mock_normalize
.
side_effect
=
lambda
x
:
x
actual_date
=
utils
.
normalize_date
(
'
2018-06-11 17:02:02
'
)
expected_date
=
'
2018-06-11 17:02:02
'
# <- why?
assert
str
(
actual_date
)
==
expected_date
@patch
(
'
swh.deposit.utils.normalize_timestamp
'
,
side_effect
=
lambda
x
:
x
)
def
test_normalize_date_doing_irrelevant_stuff
(
mock_normalize
):
"""
Providing a date in an unknown format, it
'
s completely off
Note: We do not test swh.model.identifiers which is already tested
in swh.model
"""
mock_normalize
.
side_effect
=
lambda
x
:
x
actual_date
=
utils
.
normalize_date
(
'
2017
'
)
expected_date
=
'
2017-04-16 00:00:00
'
# <- why?
assert
str
(
actual_date
)
==
expected_date
This diff is collapsed.
Click to expand it.
swh/deposit/utils.py
+
28
−
1
View file @
c1832762
# Copyright (C) 2018 The Software Heritage developers
# Copyright (C) 2018
-2019
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
dateutil
import
parser
from
types
import
GeneratorType
from
swh.model.identifiers
import
normalize_timestamp
def
merge
(
*
dicts
):
"""
Given an iterator of dicts, merge them losing no information.
...
...
@@ -53,3 +56,27 @@ def merge(*dicts):
new_val
=
_extend
([
existing_val
],
value
)
d
[
key
]
=
new_val
return
d
def
normalize_date
(
date
):
"""
Normalize date fields as expected by swh workers.
If date is a list, elect arbitrarily the first element of that
list
If date is (then) a string, parse it through
dateutil.parser.parse to extract a datetime.
Then normalize it through
swh.model.identifiers.normalize_timestamp.
Returns
The swh date object
"""
if
isinstance
(
date
,
list
):
date
=
date
[
0
]
if
isinstance
(
date
,
str
):
date
=
parser
.
parse
(
date
)
return
normalize_timestamp
(
date
)
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