Example #1
0
def json_hook(dct):
    """Hook method for json.load()."""
    if not dct.get('__type__'):
        return dct
    elif dct['__type__'] == "Simulation":
        return Simulation(dct['options'], dct['trackItems'], dct['routes'],
                          dct['trainTypes'], dct['services'], dct['trains'],
                          dct['messageLogger'])
    elif dct['__type__'] == "SignalItem":
        return signalitem.SignalItem(parameters=dct)
    elif dct['__type__'] == "EndItem":
        return enditem.EndItem(parameters=dct)
    elif dct['__type__'] == "InvisibleLinkItem":
        return invisiblelinkitem.InvisibleLinkItem(parameters=dct)
    elif dct['__type__'] == "LineItem":
        return lineitem.LineItem(parameters=dct)
    elif dct['__type__'] == "Place":
        return placeitem.Place(parameters=dct)
    elif dct['__type__'] == "PlatformItem":
        return platformitem.PlatformItem(parameters=dct)
    elif dct['__type__'] == "PointsItem":
        return pointsitem.PointsItem(parameters=dct)
    elif dct['__type__'] == "TextItem":
        return textitem.TextItem(parameters=dct)
    elif dct['__type__'] == "Route":
        return route.Route(parameters=dct)
    elif dct['__type__'] == "Position":
        return position.Position(parameters=dct)
    elif dct['__type__'] == "TrainType":
        return trains.TrainType(parameters=dct)
    elif dct['__type__'] == "Service":
        return trains.Service(parameters=dct)
    elif dct['__type__'] == "ServiceLine":
        return trains.ServiceLine(parameters=dct)
    elif dct['__type__'] == "Train":
        return trains.Train(parameters=dct)
    elif dct['__type__'] == "MessageLogger":
        return logger.MessageLogger(parameters=dct)
    elif dct['__type__'] == "Message":
        return logger.Message(dct)
    else:
        raise utils.FormatException(
            translate("json_hook", "Unknown __type__ '%s' in JSON file") %
            dct['__type__'])
Example #2
0
 def prepareRoute(self, signalId):
     """Prepares the route starting with the SignalItem given by
     _selectedSignal and ending at signalId. Sets _selectedSignal to signalId
     if it is not set. Preparation means:
     - Check that the route leads to signalId, using the current directions
     of each PointsItem.
     - Set _preparedRoute to this route
     - Highlights the route if valid"""
     if self.context == utils.Context.EDITOR_ROUTES:
         si = self.trackItem(signalId)
         if self._selectedSignal is None or self._selectedSignal == si:
             # First signal selected
             self._selectedSignal = si
         else:
             pos = position.Position(self._selectedSignal,
                                     self._selectedSignal.previousItem, 0)
             directions = {}
             cur = pos.next()
             while not isinstance(cur.trackItem, enditem.EndItem):
                 ti = cur.trackItem
                 if isinstance(ti, pointsitem.PointsItem):
                     directions[ti.tiId] = int(ti.pointsReversed)
                 if ti == si:
                     if ti.isOnPosition(cur):
                         self._preparedRoute = route.Route({
                             "routeNum":
                             self._nextRouteId,
                             "beginSignal":
                             self._selectedSignal.tiId,
                             "endSignal":
                             signalId,
                             "directions":
                             directions,
                             "initialState":
                             0
                         })
                         self._preparedRoute.initialize(self)
                         self.selectedRoute = self._preparedRoute
                         self._nextRouteId += 1
                         self._selectedSignal = None
                         si.unselect()
                         return
                 cur = cur.next()
Example #3
0
 def loadRoutes(self, routes):
     for key, dct in routes.items():
         dct['id'] = key
         rte = route.Route(dct)
         self._routes[key] = rte