UNPKG

timetable

Version:

Using GTFS and other sources to produce beautiful and usable timetables for public transport.

102 lines (80 loc) 3.49 kB
// Generated by CoffeeScript 1.7.1 (function() { var TRM, TYPES, alert, badge, datasources_home, debug, echo, get_datasource_infos, glob, help, info, log, njs_fs, njs_path, options, rainbow, rpr, urge, warn, whisper, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; njs_path = require('path'); njs_fs = require('fs'); TYPES = require('coffeenode-types'); TRM = require('coffeenode-trm'); rpr = TRM.rpr.bind(TRM); badge = 'timetable/datasource-infos'; log = TRM.get_logger('plain', badge); info = TRM.get_logger('info', badge); whisper = TRM.get_logger('whisper', badge); alert = TRM.get_logger('alert', badge); debug = TRM.get_logger('debug', badge); warn = TRM.get_logger('warn', badge); help = TRM.get_logger('help', badge); urge = TRM.get_logger('urge', badge); echo = TRM.echo.bind(TRM); rainbow = TRM.rainbow.bind(TRM); options = require('../options'); datasources_home = options['data']['home']; glob = require('glob'); module.exports = get_datasource_infos = function(home) { var R, collection, collection_name, count, filename, matcher, route, target, virtual_route, _i, _j, _len, _len1, _ref, _ref1; if (home == null) { home = datasources_home; } R = {}; matcher = njs_path.join(home, '**/*.txt'); count = 0; info("collecting data sources from"); info(rpr(matcher)); _ref = glob.sync(matcher); for (_i = 0, _len = _ref.length; _i < _len; _i++) { route = _ref[_i]; filename = njs_path.basename(route, njs_path.extname(route)); if (__indexOf.call(options['data']['gtfs-types'], filename) < 0) { continue; } count += 1; collection_name = njs_path.basename(njs_path.dirname(route)); collection = R[collection_name] != null ? R[collection_name] : R[collection_name] = {}; collection[filename] = route; } matcher = njs_path.join(home, '**/*.txt.-[a-z][a-z][a-z][a-z]'); _ref1 = glob.sync(matcher); for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { route = _ref1[_j]; virtual_route = route.replace(/\.-[a-z][a-z][a-z][a-z]$/, ''); filename = njs_path.basename(virtual_route, njs_path.extname(virtual_route)); if (__indexOf.call(options['data']['gtfs-types'], filename) < 0) { continue; } count += 1; collection_name = njs_path.basename(njs_path.dirname(virtual_route)); collection = R[collection_name] != null ? R[collection_name] : R[collection_name] = {}; target = collection[filename] != null ? collection[filename] : collection[filename] = []; if (!TYPES.isa_list(target)) { warn("found duplicate datasources for GTFS type " + (rpr(filename))); warn("in collection " + collection_name + ":"); warn("(1)", target); warn("(2)", route); help("when splitting large data files (recommended), please make sure"); help("to delete the unsplitted file."); warn("aborting"); process.exit(); } target.push(route); } if (count === 0) { warn("unable to find any datasource files in this location"); help("please install data package with `npm install 'timetable-data'`"); help("see " + (require('../package.json'))['homepage'] + " for details"); warn("aborting"); process.exit(); } return R; }; }).call(this);