import re
portal = context.getPortalObject()
portal_catalog = portal.portal_catalog

try:
  data_set = portal.data_set_module.get(data_set_reference)
  if data_set is None or data_set.getReference().endswith("_invalid"):
    return { "status_code": 0, "result": [] }
except Exception as e: # fails because unauthorized access
  context.logEntry("[ERROR] At script getDataStreamList")
  context.logEntry("[ERROR] " + str(e))
  return { "status_code": 1, "error_message": "401 - Unauthorized access. Please check your user credentials and try again." }

data_set = portal.data_set_module.get(data_set_reference)
if data_set is None:
  return []

query_dict = {
  "portal_type": "Data Ingestion Line",
  "resource_portal_type": "Data Product",
  "simulation_state": "Delivered"}

data_stream_list = []

for line in portal_catalog(**query_dict):
  if line.getAggregateDeviceConfiguration() == "data_set_module/" + str(data_set.getId()):
    for item in line.getAggregateSet():
      if "data_stream_module/" in item:
        data_stream = context.restrictedTraverse(item)
        if data_stream.getValidationState == "invalidated" or data_stream.getReference().endswith("_invalid"):
          break
        data_stream_list.append([item, data_stream.getReference()])
        break

return { "status_code": 0, "result": data_stream_list }
