
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import action, api_view
from api.utils import gtfs_realtime_pb2
from project.settings import BASE_DIR
from django.http import JsonResponse
import logging

def status(request):
    nombre_archivo_proto = f'{BASE_DIR}/api/utils/demo.proto'
    with open(nombre_archivo_proto, 'rb') as proto_file:
        feed = gtfs_realtime_pb2.FeedMessage()
        feed.ParseFromString(proto_file.read())

    data = {} #get_object(feed)
    # logging.error(dir(feed))
    # logging.error(feed.header)
    # logging.error(dir(feed.entity))
    logging.error(feed.entity[0])
    # logging.error(dir(feed.entity[0].trip_update))

    return JsonResponse(data, safe=False)


def get_object(obj1, obj2 = {}, level = 1):
    attributes = dir(obj1)
    for attr in attributes:
        if attr[0:1] != '_':
            try:
                for elem in obj1[attr]:
                    logging.error(elem)
            except:
                logging.error(attr)
                pass
    return obj2


def is_subscriptable(obj):
    return hasattr(obj, '__getitem__')

def is_iterable(obj):
    try:
        iter(obj)
        return True
    except TypeError:
        return False
