diff --git a/swh/storage/api/client.py b/swh/storage/api/client.py --- a/swh/storage/api/client.py +++ b/swh/storage/api/client.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import warnings from swh.core.api import SWHRemoteAPI @@ -133,8 +134,17 @@ def origin_add_one(self, origin): return self.post('origin/add', {'origin': origin}) - def origin_visit_add(self, origin, ts): - return self.post('origin/visit/add', {'origin': origin, 'ts': ts}) + def origin_visit_add(self, origin, date, *, ts=None): + if ts is None: + if date is None: + raise TypeError('origin_visit_add expected 2 arguments.') + else: + assert date is None + warnings.warn("argument 'ts' of origin_visit_add was renamed " + "to 'date' in v0.0.109.", + DeprecationWarning) + date = ts + return self.post('origin/visit/add', {'origin': origin, 'date': date}) def origin_visit_update(self, origin, visit_id, status, metadata=None): return self.post('origin/visit/update', {'origin': origin, diff --git a/swh/storage/storage.py b/swh/storage/storage.py --- a/swh/storage/storage.py +++ b/swh/storage/storage.py @@ -9,6 +9,7 @@ import datetime import itertools import json +import warnings import dateutil.parser import psycopg2 @@ -898,12 +899,13 @@ return None @db_transaction() - def origin_visit_add(self, origin, ts, db=None, cur=None): + def origin_visit_add(self, origin, date=None, db=None, cur=None, *, + ts=None): """Add an origin_visit for the origin at ts with status 'ongoing'. Args: origin: Visited Origin id - ts: timestamp of such visit + date: timestamp of such visit Returns: dict: dictionary with keys origin and visit where: @@ -913,12 +915,22 @@ - ts (datetime.DateTime): the visit date """ - if isinstance(ts, str): - ts = dateutil.parser.parse(ts) + if ts is None: + if date is None: + raise TypeError('origin_visit_add expected 2 arguments.') + else: + assert date is None + warnings.warn("argument 'ts' of origin_visit_add was renamed " + "to 'date' in v0.0.109.", + DeprecationWarning) + date = ts + + if isinstance(date, str): + date = dateutil.parser.parse(date) return { 'origin': origin, - 'visit': db.origin_visit_add(origin, ts, cur) + 'visit': db.origin_visit_add(origin, date, cur) } @db_transaction() @@ -967,7 +979,8 @@ origin: The occurrence's origin (identifier). Returns: - The information on that particular (origin, visit) + The information on that particular (origin, visit) or None if + it does not exist """ ori_visit = db.origin_visit_get(origin, visit, cur) diff --git a/swh/storage/tests/test_storage.py b/swh/storage/tests/test_storage.py --- a/swh/storage/tests/test_storage.py +++ b/swh/storage/tests/test_storage.py @@ -1006,7 +1006,7 @@ # when origin_visit1 = self.storage.origin_visit_add( origin_id, - ts=self.date_visit2) + date=self.date_visit2) # then self.assertEqual(origin_visit1['origin'], origin_id) @@ -1031,15 +1031,15 @@ origin_visit1 = self.storage.origin_visit_add( origin_id, - ts=self.date_visit2) + date=self.date_visit2) origin_visit2 = self.storage.origin_visit_add( origin_id, - ts=self.date_visit3) + date=self.date_visit3) origin_visit3 = self.storage.origin_visit_add( origin_id2, - ts=self.date_visit3) + date=self.date_visit3) # when visit1_metadata = { @@ -1054,6 +1054,23 @@ # then actual_origin_visits = list(self.storage.origin_visit_get(origin_id)) + import pprint + pprint.pprint(actual_origin_visits) + pprint.pprint([{ + 'origin': origin_visit2['origin'], + 'date': self.date_visit2, + 'visit': origin_visit1['visit'], + 'status': 'full', + 'metadata': visit1_metadata, + 'snapshot': None, + }, { + 'origin': origin_visit2['origin'], + 'date': self.date_visit3, + 'visit': origin_visit2['visit'], + 'status': 'ongoing', + 'metadata': None, + 'snapshot': None, + }]) self.assertEqual(actual_origin_visits, [{ 'origin': origin_visit2['origin'], 'date': self.date_visit2, @@ -1111,14 +1128,14 @@ origin_visit1 = self.storage.origin_visit_add( origin_id, - ts=self.date_visit2) + date=self.date_visit2) self.storage.snapshot_add(origin_id, origin_visit1['visit'], self.snapshot) # Add some other {origin, visit} entries - self.storage.origin_visit_add(origin_id, ts=self.date_visit3) - self.storage.origin_visit_add(origin_id2, ts=self.date_visit3) + self.storage.origin_visit_add(origin_id, date=self.date_visit3) + self.storage.origin_visit_add(origin_id2, date=self.date_visit3) # when visit1_metadata = {