Skip to content

Commit

Permalink
feat: added redirects and feed contact email to operations (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
cka-y authored Feb 13, 2024
1 parent d6542d9 commit 4b71837
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
14 changes: 12 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ The easiest way to add a GTFS Schedule source is to use the operation `tools.ope
license_url=$OPTIONAL_LICENSE_URL,
name=$OPTIONAL_SOURCE_NAME,
features=[$OPTIONAL_FEATURE_ARRAY],
status=$OPTIONAL_STATUS
status=$OPTIONAL_STATUS,
feed_contact_email=$OPTIONAL_FEED_CONTACT_EMAIL,
redirects=[{
"id": $OPTIONAL_REDIRECT_ID,
"comment": $OPTIONAL_REDIRECT_COMMENT
}],
)
```

Expand Down Expand Up @@ -99,7 +104,12 @@ The default value for every parameter is `None`. Note that once a parameter valu
api_key_parameter_value=$NOT_STORED_API_KEY_PARAMETER_VALUE,
license_url=$OPTIONAL_LICENSE_URL,
features=[$OPTIONAL_FEATURE_ARRAY],
status=$OPTIONAL_STATUS
status=$OPTIONAL_STATUS,
feed_contact_email=$OPTIONAL_FEED_CONTACT_EMAIL,
redirects=[{
"id": $OPTIONAL_REDIRECT_ID,
"comment": $OPTIONAL_REDIRECT_COMMENT
}],
)
```

Expand Down
2 changes: 1 addition & 1 deletion schemas/gtfs_schedule_source_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"type": "object",
"properties": {
"id": {
"type": "string",
"type": "number",
"description": "The target feed id of that redirect."
},
"comment": {
Expand Down
4 changes: 4 additions & 0 deletions tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
API_KEY_PARAMETER_VALUE = "api_key_parameter_value"
NOTE = "note"
ENTITY_TYPE = "entity_type"
FEED_CONTACT_EMAIL = "feed_contact_email"
REDIRECTS = "redirect"
REDIRECT_ID = "id"
REDIRECT_COMMENT = "comment"

# TIME CONSTANTS
SIX_MONTHS_IN_WEEKS = 26
Expand Down
12 changes: 10 additions & 2 deletions tools/operations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
from tools.constants import (
GTFS,
GTFS_RT,
NAME,
PROVIDER,
COUNTRY_CODE,
Expand All @@ -21,6 +19,8 @@
CATALOGS,
ALL,
MDB_SOURCE_ID,
FEED_CONTACT_EMAIL,
REDIRECTS,
)
from tools.representations import GtfsScheduleSourcesCatalog, GtfsRealtimeSourcesCatalog

Expand Down Expand Up @@ -117,6 +117,8 @@ def add_gtfs_schedule_source(
name=None,
status=None,
features=None,
feed_contact_email=None,
redirects=None,
):
"""Add a new GTFS Schedule source to the Mobility Catalogs."""
catalog = GtfsScheduleSourcesCatalog()
Expand All @@ -134,6 +136,8 @@ def add_gtfs_schedule_source(
NAME: name,
STATUS: status,
FEATURES: features,
FEED_CONTACT_EMAIL: feed_contact_email,
REDIRECTS: redirects,
}
catalog.add(**data)
return catalog
Expand All @@ -154,6 +158,8 @@ def update_gtfs_schedule_source(
license_url=None,
status=None,
features=None,
feed_contact_email=None,
redirects=None,
):
"""Update a GTFS Schedule source in the Mobility Catalogs."""
catalog = GtfsScheduleSourcesCatalog()
Expand All @@ -172,6 +178,8 @@ def update_gtfs_schedule_source(
NAME: name,
STATUS: status,
FEATURES: features,
FEED_CONTACT_EMAIL: feed_contact_email,
REDIRECTS: redirects,
}
catalog.update(**data)
return catalog
Expand Down
37 changes: 37 additions & 0 deletions tools/representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
FEATURES,
STATUS,
ACTIVE,
FEED_CONTACT_EMAIL,
REDIRECT_ID,
REDIRECT_COMMENT,
REDIRECTS,
)

PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
Expand Down Expand Up @@ -160,6 +164,16 @@ def get_sources_by_status(self, status):

def add(self, **kwargs):
mdb_source_id = self.identify(self.root)
redirects = kwargs.pop(REDIRECTS, [])
if len(redirects) > 0:
kwargs[REDIRECTS] = [
{
REDIRECT_ID: elem.get(REDIRECT_ID), REDIRECT_COMMENT: elem.get(REDIRECT_COMMENT)
}
for elem in redirects
]
kwargs[REDIRECTS] = list(filter(lambda x: x.get(REDIRECT_ID) != 'None', kwargs[REDIRECTS]))

entity = self.entity_cls.build(mdb_source_id=mdb_source_id, **kwargs)
if isinstance(entity, self.entity_cls):
self.catalog[mdb_source_id] = entity
Expand Down Expand Up @@ -303,6 +317,8 @@ def __init__(self, **kwargs):
self.bbox_extracted_on = bounding_box.pop(EXTRACTED_ON)
urls = kwargs.pop(URLS, {})
self.latest_url = urls.pop(LATEST)
self.feed_contact_email = kwargs.pop(FEED_CONTACT_EMAIL, None)
self.redirects = kwargs.pop(REDIRECTS, [])

def __str__(self):
attributes = {
Expand All @@ -326,6 +342,8 @@ def __str__(self):
LICENSE: self.license_url,
FEATURES: self.features,
STATUS: self.status,
FEED_CONTACT_EMAIL: self.feed_contact_email,
REDIRECTS: self.redirects,
}
return json.dumps(self.schematize(**attributes), ensure_ascii=False)

Expand Down Expand Up @@ -420,6 +438,19 @@ def update(self, **kwargs):
status = kwargs.get(STATUS)
if status is not None:
self.status = status
feed_contact_email = kwargs.get(FEED_CONTACT_EMAIL)
if feed_contact_email is not None:
self.feed_contact_email = feed_contact_email

# Update the redirects
redirects = kwargs.get(REDIRECTS)
if redirects is not None:
self.redirects = [
{
REDIRECT_ID: elem.get(REDIRECT_ID), REDIRECT_COMMENT: elem.get(REDIRECT_COMMENT)
} for elem in redirects
]
self.redirects = list(filter(lambda x: x.get(REDIRECT_ID) is not None, self.redirects))
return self

@classmethod
Expand Down Expand Up @@ -487,6 +518,7 @@ def schematize(cls, **kwargs):
DATA_TYPE: kwargs.pop(DATA_TYPE),
PROVIDER: kwargs.pop(PROVIDER),
NAME: kwargs.pop(NAME, None),
FEED_CONTACT_EMAIL: kwargs.pop(FEED_CONTACT_EMAIL, None),
FEATURES: kwargs.pop(FEATURES, None),
STATUS: kwargs.pop(STATUS, None),
LOCATION: {
Expand All @@ -509,6 +541,7 @@ def schematize(cls, **kwargs):
LATEST: kwargs.pop(LATEST),
LICENSE: kwargs.pop(LICENSE, None),
},
REDIRECTS: kwargs.pop(REDIRECTS, None),
}
if schema[NAME] is None:
del schema[NAME]
Expand All @@ -528,6 +561,10 @@ def schematize(cls, **kwargs):
del schema[FEATURES]
if schema[STATUS] is None:
del schema[STATUS]
if schema[FEED_CONTACT_EMAIL] is None:
del schema[FEED_CONTACT_EMAIL]
if schema[REDIRECTS] is None:
del schema[REDIRECTS]
return schema


Expand Down
18 changes: 17 additions & 1 deletion tools/tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def test_add_gtfs_schedule_source(self, mock_catalog):
test_license_url = "test_license_url"
test_status = "active"
test_features = ["flex"]
feed_contact_email = "test contact email"
redirects = [
{"id": "test_id", "comment": "test_url"},
{"id": "test_id", "comment": "test_url"},
{"wrong_key": "test_value"}
]
under_test = add_gtfs_schedule_source(
provider=test_provider,
name=test_name,
Expand All @@ -113,6 +119,8 @@ def test_add_gtfs_schedule_source(self, mock_catalog):
license_url=test_license_url,
status=test_status,
features=test_features,
feed_contact_email=feed_contact_email,
redirects=redirects,
)
self.assertEqual(under_test, mock_catalog())
self.assertEqual(mock_catalog.call_count, 2)
Expand All @@ -122,7 +130,7 @@ def test_add_gtfs_schedule_source(self, mock_catalog):
def test_update_gtfs_schedule_source(self, mock_catalog):
test_mdb_source_id = "test_mdb_source_id"
test_provider = "test_provider"
test_name = "test_name"
test_name = "test_name changed"
test_country_code = "test_country_code"
test_subdivision_name = "test_subdivision_name"
test_municipality = "test_municipality"
Expand All @@ -134,6 +142,12 @@ def test_update_gtfs_schedule_source(self, mock_catalog):
test_license_url = "test_license_url"
test_status = "active"
test_features = ["flex"]
feed_contact_email = "test contact email changed"
redirects = [
{"id": "test_id", "comment": "test_url"},
{"id": "test_id changed", "comment": "test_url changed"},
{"wrong_key": "test_value"}
]
under_test = update_gtfs_schedule_source(
mdb_source_id=test_mdb_source_id,
provider=test_provider,
Expand All @@ -149,6 +163,8 @@ def test_update_gtfs_schedule_source(self, mock_catalog):
license_url=test_license_url,
status=test_status,
features=test_features,
feed_contact_email=feed_contact_email,
redirects=redirects,
)
self.assertEqual(under_test, mock_catalog())
self.assertEqual(mock_catalog.call_count, 2)
Expand Down

0 comments on commit 4b71837

Please sign in to comment.