from busbus.entity import BaseEntity
from busbus.util import Config

import errno
import os
import six


class Engine(object):
    """
    The busbus.Engine manages data providers and allows querying entities and
    subscribing to entity events the providers.
    """

    def __init__(self, config=None):
        self.config = Config(config)
        self.providers = {}

        try:
            os.mkdir(self.config['busbus_dir'])
        except OSError as exc:
            if exc.errno != errno.EEXIST:
                raise

    def _register_provider(self, provider):
        self.providers[provider._uuid] = provider


class Agency(BaseEntity):
    __attrs__ = ('id', 'name', 'url', 'timezone', 'lang', 'phone_e164',
                 'phone_human', 'fare_url')


class Stop(BaseEntity):
    __attrs__ = ('id', 'code', 'name', 'description', 'location', 'zone',
                 'url', 'parent', 'timezone', 'accessible')

    @property
    def children(self):
        return self._provider.stops.where(lambda s: s.parent == self)


class Route(BaseEntity):
    __attrs__ = ('id', 'agency', 'short_name', 'name', 'description', 'type',
                 'url', 'color', 'text_color')


class Arrival(BaseEntity):
    __attrs__ = ('route', 'stop', 'time', 'departure_time', 'headsign',
                 'short_name', 'bikes_ok')
    __repr_attrs__ = ('route', 'stop', 'time')


ENTITIES = (Agency, Stop, Route, Arrival)

@@ -30,16 +30,55 @@
 eEntity%0A
+from busbus.queryable import Queryable%0A
 from bus
@@ -657,16 +657,478 @@
 ovider%0A%0A
+    @property%0A    def agencies(self):%0A        return Queryable.chain(*%5Bp.agencies for p in self.providers.values()%5D)%0A%0A    @property%0A    def stops(self):%0A        return Queryable.chain(*%5Bp.stops for p in self.providers.values()%5D)%0A%0A    @property%0A    def routes(self):%0A        return Queryable.chain(*%5Bp.routes for p in self.providers.values()%5D)%0A%0A    @property%0A    def arrivals(self):%0A        return Queryable.chain(*%5Bp.arrivals for p in self.providers.values()%5D)%0A%0A
 %0Aclass A

Add entity properties to busbus.Engine